mirror of
https://github.com/gofiber/storage.git
synced 2025-10-05 16:48:25 +08:00
♻️ improve test
This commit is contained in:
@@ -106,10 +106,10 @@ func (s *Storage) Get(key string) ([]byte, error) {
|
|||||||
result := MongoStorage{}
|
result := MongoStorage{}
|
||||||
|
|
||||||
if err := res.Err(); err != nil {
|
if err := res.Err(); err != nil {
|
||||||
return []byte{}, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err := res.Decode(&result); err != nil {
|
if err := res.Decode(&result); err != nil {
|
||||||
return []byte{}, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return result.Value, nil
|
return result.Value, nil
|
||||||
|
@@ -46,13 +46,32 @@ func TestMongoStore_Set_Get(t *testing.T) {
|
|||||||
key := "example_key"
|
key := "example_key"
|
||||||
value := []byte("123")
|
value := []byte("123")
|
||||||
|
|
||||||
_ = store.Set(key, value, 0)
|
err := store.Set(key, value, 0)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
getVal, _ := store.Get(key)
|
getVal, getErr := store.Get(key)
|
||||||
|
|
||||||
|
utils.AssertEqual(t, nil, getErr)
|
||||||
utils.AssertEqual(t, value, getVal, "correctly set and get value")
|
utils.AssertEqual(t, value, getVal, "correctly set and get value")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMongoStore_Get_Invalid(t *testing.T) {
|
||||||
|
if uri == "" {
|
||||||
|
t.Skip()
|
||||||
|
}
|
||||||
|
store := New(getConfig())
|
||||||
|
defer func() {
|
||||||
|
_ = store.db.Client().Disconnect(context.TODO())
|
||||||
|
}()
|
||||||
|
|
||||||
|
key := "random_invalid_key"
|
||||||
|
|
||||||
|
getVal, getErr := store.Get(key)
|
||||||
|
|
||||||
|
utils.AssertEqual(t, true, getErr != nil)
|
||||||
|
utils.AssertEqual(t, true, getVal == nil, "get nil if key not found")
|
||||||
|
}
|
||||||
|
|
||||||
func TestMongoStore_Delete(t *testing.T) {
|
func TestMongoStore_Delete(t *testing.T) {
|
||||||
if uri == "" {
|
if uri == "" {
|
||||||
t.Skip()
|
t.Skip()
|
||||||
@@ -65,12 +84,15 @@ func TestMongoStore_Delete(t *testing.T) {
|
|||||||
key := "example_key_2"
|
key := "example_key_2"
|
||||||
value := []byte("123")
|
value := []byte("123")
|
||||||
|
|
||||||
_ = store.Set(key, value, 10)
|
err := store.Set(key, value, 0)
|
||||||
_ = store.Delete(key)
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
getVal, _ := store.Get(key)
|
err = store.Delete(key)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
utils.AssertEqual(t, []byte{}, getVal, "correctly delete value")
|
getVal, getErr := store.Get(key)
|
||||||
|
utils.AssertEqual(t, true, getErr != nil)
|
||||||
|
utils.AssertEqual(t, true, getVal == nil, "correctly delete value")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMongoStore_Clear(t *testing.T) {
|
func TestMongoStore_Clear(t *testing.T) {
|
||||||
@@ -85,12 +107,15 @@ func TestMongoStore_Clear(t *testing.T) {
|
|||||||
key := "example_key_2"
|
key := "example_key_2"
|
||||||
value := []byte("123")
|
value := []byte("123")
|
||||||
|
|
||||||
_ = store.Set(key, value, 10)
|
err := store.Set(key, value, 10)
|
||||||
names, _ := store.db.ListCollectionNames(context.TODO(), bson.D{})
|
names, _ := store.db.ListCollectionNames(context.TODO(), bson.D{})
|
||||||
|
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
utils.AssertEqual(t, true, contains(names, colName), "has collection")
|
utils.AssertEqual(t, true, contains(names, colName), "has collection")
|
||||||
_ = store.Clear()
|
|
||||||
|
cErr := store.Clear()
|
||||||
|
|
||||||
names2, _ := store.db.ListCollectionNames(context.TODO(), bson.D{})
|
names2, _ := store.db.ListCollectionNames(context.TODO(), bson.D{})
|
||||||
|
utils.AssertEqual(t, nil, cErr)
|
||||||
utils.AssertEqual(t, false, contains(names2, colName), "do not have collection")
|
utils.AssertEqual(t, false, contains(names2, colName), "do not have collection")
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user