diff --git a/README.md b/README.md index a4abad0..5fd6d57 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,8 @@ func(t *Todo) BeforeUpdate() error { ### ToDo - [x] CRUD - [x] Hooks -- [ ] Pagination +- [x] Disconnect +- [ ] Context - [ ] Aggregations - [ ] Transactions diff --git a/collection_test.go b/collection_test.go index 24eadee..ecea53b 100644 --- a/collection_test.go +++ b/collection_test.go @@ -30,6 +30,8 @@ func TestCollection_FindById(t *testing.T) { assert.Nil(t, err) assert.NotNil(t, result) assert.Equal(t, doc.ID, result.ID) + + mockDb.Disconnect() } func TestCollection_FindOne(t *testing.T) { @@ -48,6 +50,8 @@ func TestCollection_FindOne(t *testing.T) { assert.Nil(t, err) assert.NotNil(t, result) assert.Equal(t, doc.ID, result.ID) + + mockDb.Disconnect() } func TestCollection_Find(t *testing.T) { @@ -69,6 +73,8 @@ func TestCollection_Find(t *testing.T) { assert.NotNil(t, result) assert.Equal(t, len(result), 1) assert.Equal(t, doc.ID, result[0].ID) + + mockDb.Disconnect() } func TestCollection_Find_Empty(t *testing.T) { @@ -90,16 +96,69 @@ func TestCollection_Find_Empty(t *testing.T) { assert.NotNil(t, result) assert.Equal(t, len(result), 0) assert.Equal(t, result, []*testdoc{}) + + mockDb.Disconnect() } -// TODO +/*func TestCollection_Find_WithError(t *testing.T) { + rand.Seed(time.Now().UnixNano()) + collection := GetCollection[*testdoc](&mockDb, "testdocs") + + result, err := collection.Find(bson.M{"title": "NonExisting"}) + + assert.NotNil(t, err) + assert.Nil(t, result) +}*/ + func TestCollection_UpdateOne(t *testing.T) { + rand.Seed(time.Now().UnixNano()) + mockDb.Connect("mongodb://localhost:27017/colt?readPreference=primary&directConnection=true&ssl=false", "colt") + + collection := GetCollection[*testdoc](&mockDb, "testdocs") + + random := fmt.Sprint(rand.Int()) + doc := testdoc{Title: random} + + collection.Insert(&doc) + + fmt.Println(doc.ID) + doc.Title = doc.Title + " updated" + err := collection.UpdateOne(bson.M{"title": random}, &doc) + totalWithNewTitle, err := collection.CountDocuments(bson.M{"title": random + " updated"}) + + assert.Nil(t, err) + assert.NotNil(t, totalWithNewTitle) + assert.Equal(t, totalWithNewTitle, int64(1)) + + mockDb.Disconnect() } -// TODO func TestCollection_UpdateMany(t *testing.T) { + rand.Seed(time.Now().UnixNano()) + mockDb.Connect("mongodb://localhost:27017/colt?readPreference=primary&directConnection=true&ssl=false", "colt") + + collection := GetCollection[*testdoc](&mockDb, "testdocs") + + random := fmt.Sprint(rand.Int()) + doc := testdoc{Title: random} + doc2 := testdoc{Title: random} + collection.Insert(&doc) + collection.Insert(&doc2) + + err := collection.UpdateMany(bson.M{"title": doc.Title}, bson.M{"$set": bson.M{"title": doc.Title + " updated"}}) + + fmt.Println(err) + time.Sleep(200 * time.Millisecond) + + totalWithNewTitle, err := collection.CountDocuments(bson.M{"title": doc.Title + " updated"}) + + assert.Nil(t, err) + assert.NotNil(t, totalWithNewTitle) + assert.Equal(t, totalWithNewTitle, int64(2)) + + mockDb.Disconnect() } // TODO @@ -130,4 +189,6 @@ func TestCollection_CountDocuments(t *testing.T) { assert.Nil(t, err) assert.NotNil(t, resultEmpty) assert.Equal(t, resultEmpty, int64(0)) + + mockDb.Disconnect() } diff --git a/database.go b/database.go index c41015e..6707d46 100644 --- a/database.go +++ b/database.go @@ -41,6 +41,12 @@ func (db *Database) Connect(connectionString string, dbName string) error { return err } +func (db *Database) Disconnect() error { + err := db.client.Disconnect(DefaultContext()); + db.db = nil + return err +} + func DefaultContext() context.Context { ctx, _ := context.WithTimeout(context.Background(), 5*time.Second) return ctx diff --git a/indexes_test.go b/indexes_test.go index 8a35f16..3c30a9a 100644 --- a/indexes_test.go +++ b/indexes_test.go @@ -9,7 +9,6 @@ import ( "time" ) - func TestCollection_CreateIndex(t *testing.T) { rand.Seed(time.Now().UnixNano()) mockDb.Connect("mongodb://localhost:27017/colt?readPreference=primary&directConnection=true&ssl=false", "colt") @@ -30,5 +29,5 @@ func TestCollection_CreateIndex(t *testing.T) { indexCursor2.All(DefaultContext(), &indxs) // new index - assert.Equal(t, len(indxs), indexCountBefore + 1) -} \ No newline at end of file + assert.Equal(t, len(indxs), indexCountBefore+1) +}