Skip to content

Commit

Permalink
add MakeTestConnectionWithColl
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Aug 31, 2021
1 parent 1bc2d88 commit 8d66ac2
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@ import (
// MakeTestConnection connects to MONGO_TEST url or "mongo" host (in no env) and returns new connection.
// collection name randomized on each call
func MakeTestConnection(t *testing.T) (mg *driver.Client, coll *driver.Collection, teardown func()) {
collName := fmt.Sprintf("test_%d", time.Now().Nanosecond())
return MakeTestConnectionWithColl(t, collName)
}

// MakeTestConnectionWithColl connects to MONGO_TEST url or "mongo" host (in no env) and returns new connection.
// collection name passed in as cname param
func MakeTestConnectionWithColl(t *testing.T, cname string) (mg *driver.Client, coll *driver.Collection, teardown func()) {
mongoURL := getMongoURL(t)
log.Print("[DEBUG] connect to mongo test instance")
opts := options.ClientOptions{}
opts.SetAppName("test")
opts.SetConnectTimeout(time.Second)
mg, err := driver.Connect(context.Background(), options.Client().ApplyURI(mongoURL))
require.NoError(t, err, "failed to make mongo client")
collName := fmt.Sprintf("test_%d", time.Now().Nanosecond())
coll = mg.Database("test").Collection(collName)
coll = mg.Database("test").Collection(cname)
teardown = func() {
require.NoError(t, coll.Drop(context.Background()))
assert.NoError(t, mg.Disconnect(context.Background()))
Expand Down

0 comments on commit 8d66ac2

Please sign in to comment.