Skip to content

Commit

Permalink
Merge pull request #13 from jensteichert/f/disconnect
Browse files Browse the repository at this point in the history
F/disconnect
  • Loading branch information
jensteichert authored Jun 12, 2023
2 parents bcb05fe + 3a185f6 commit 9cb2cc1
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ func(t *Todo) BeforeUpdate() error {
### ToDo
- [x] CRUD
- [x] Hooks
- [ ] Pagination
- [x] Disconnect
- [ ] Context
- [ ] Aggregations
- [ ] Transactions

Expand Down
65 changes: 63 additions & 2 deletions collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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()
}
6 changes: 6 additions & 0 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions indexes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -30,5 +29,5 @@ func TestCollection_CreateIndex(t *testing.T) {
indexCursor2.All(DefaultContext(), &indxs)

// new index
assert.Equal(t, len(indxs), indexCountBefore + 1)
}
assert.Equal(t, len(indxs), indexCountBefore+1)
}

0 comments on commit 9cb2cc1

Please sign in to comment.