diff options
author | Emiliano Ciavatta | 2020-04-07 18:46:36 +0000 |
---|---|---|
committer | Emiliano Ciavatta | 2020-04-07 18:46:56 +0000 |
commit | 468690c60ee2e57ed2ccb4375e9ada5d2fed9473 (patch) | |
tree | 138b9f4c69731abef66e789b0e044417824f5c49 /storage_test.go | |
parent | 590405d948530aecdf7399833c3d0b8585f5601b (diff) |
Before storage refactor
Diffstat (limited to 'storage_test.go')
-rw-r--r-- | storage_test.go | 131 |
1 files changed, 130 insertions, 1 deletions
diff --git a/storage_test.go b/storage_test.go index b46b60a..40440a4 100644 --- a/storage_test.go +++ b/storage_test.go @@ -1,10 +1,27 @@ package main import ( + "context" + "errors" + "github.com/stretchr/testify/assert" + "go.mongodb.org/mongo-driver/bson/primitive" "testing" + "time" ) -const testCollection = "characters" +type a struct { + Id primitive.ObjectID `bson:"_id,omitempty"` + A string `bson:"a,omitempty"` + B int `bson:"b,omitempty"` + C time.Time `bson:"c,omitempty"` + D map[string]b `bson:"d"` + E []b `bson:"e,omitempty"` +} + +type b struct { + A string `bson:"a,omitempty"` + B int `bson:"b,omitempty"` +} func testInsert(t *testing.T) { // insert a document in an invalid connection @@ -88,3 +105,115 @@ func TestBasicOperations(t *testing.T) { t.Run("testInsert", testInsert) t.Run("testFindOne", testFindOne) } + +func TestInsertManyFindDocuments(t *testing.T) { + testTime := time.Now() + oid1, err := primitive.ObjectIDFromHex("ffffffffffffffffffffffff") + assert.Nil(t, err) + + docs := []interface{}{ + a{ + A: "test0", + B: 0, + C: testTime, + D: map[string]b{ + "first": {A: "0", B: 0}, + "second": {A: "1", B: 1}, + }, + E: []b{ + {A: "0", B: 0}, {A: "1", B: 0}, + }, + }, + a{ + Id: oid1, + A: "test1", + B: 1, + C: testTime, + D: map[string]b{}, + E: []b{}, + }, + a{}, + } + + ids, err := storage.InsertMany(testContext, testInsertManyFindCollection, docs) + assert.Nil(t, err) + assert.Len(t, ids, 3) + assert.Equal(t, ids[1], oid1) + + var results []a + err = storage.Find(testContext, testInsertManyFindCollection, NoFilters, &results) + assert.Nil(t, err) + assert.Len(t, results, 3) + doc0, doc1, doc2 := docs[0].(a), docs[1].(a), docs[2].(a) + assert.Equal(t, ids[0], results[0].Id) + assert.Equal(t, doc1.Id, results[1].Id) + assert.Equal(t, ids[2], results[2].Id) + assert.Equal(t, doc0.A, results[0].A) + assert.Equal(t, doc1.A, results[1].A) + assert.Equal(t, doc2.A, results[2].A) + assert.Equal(t, doc0.B, results[0].B) + assert.Equal(t, doc1.B, results[1].B) + assert.Equal(t, doc2.B, results[2].B) + assert.Equal(t, doc0.C.Unix(), results[0].C.Unix()) + assert.Equal(t, doc1.C.Unix(), results[1].C.Unix()) + assert.Equal(t, doc2.C.Unix(), results[2].C.Unix()) + assert.Equal(t, doc0.D, results[0].D) + assert.Equal(t, doc1.D, results[1].D) + assert.Equal(t, doc2.D, results[2].D) + assert.Equal(t, doc0.E, results[0].E) + assert.Nil(t, results[1].E) + assert.Nil(t, results[2].E) +} + +type testStorage struct { + insertFunc func(ctx context.Context, collectionName string, document interface{}) (interface{}, error) + insertManyFunc func(ctx context.Context, collectionName string, document []interface{}) ([]interface{}, error) + updateOne func(ctx context.Context, collectionName string, filter interface{}, update interface {}, upsert bool) (interface{}, error) + updateMany func(ctx context.Context, collectionName string, filter interface{}, update interface {}, upsert bool) (interface{}, error) + findOne func(ctx context.Context, collectionName string, filter interface{}) (UnorderedDocument, error) + find func(ctx context.Context, collectionName string, filter interface{}, results interface{}) error +} + +func (ts testStorage) InsertOne(ctx context.Context, collectionName string, document interface{}) (interface{}, error) { + if ts.insertFunc != nil { + return ts.insertFunc(ctx, collectionName, document) + } + return nil, errors.New("not implemented") +} + +func (ts testStorage) InsertMany(ctx context.Context, collectionName string, document []interface{}) ([]interface{}, error) { + if ts.insertFunc != nil { + return ts.insertManyFunc(ctx, collectionName, document) + } + return nil, errors.New("not implemented") +} + +func (ts testStorage) UpdateOne(ctx context.Context, collectionName string, filter interface{}, update interface {}, + upsert bool) (interface{}, error) { + if ts.updateOne != nil { + return ts.updateOne(ctx, collectionName, filter, update, upsert) + } + return nil, errors.New("not implemented") +} + +func (ts testStorage) UpdateMany(ctx context.Context, collectionName string, filter interface{}, update interface {}, + upsert bool) (interface{}, error) { + if ts.updateOne != nil { + return ts.updateMany(ctx, collectionName, filter, update, upsert) + } + return nil, errors.New("not implemented") +} + +func (ts testStorage) FindOne(ctx context.Context, collectionName string, filter interface{}) (UnorderedDocument, error) { + if ts.findOne != nil { + return ts.findOne(ctx, collectionName, filter) + } + return nil, errors.New("not implemented") +} + +func (ts testStorage) Find(ctx context.Context, collectionName string, filter interface{}, results interface{}) error { + if ts.find != nil { + return ts.find(ctx, collectionName, filter, results) + } + return errors.New("not implemented") +} |