-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect_test.go
62 lines (57 loc) · 2.12 KB
/
connect_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package easymongo_test
import (
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/tophergopher/easymongo"
)
func TestConnect(t *testing.T) {
setup(t)
t.Cleanup(func() { teardown(t) })
t.Run("TestPing", func(t *testing.T) {
is := assert.New(t)
is.NoError(conn.Ping(), "Could not ping test instance")
})
t.Run("ConnectWithOptions", func(t *testing.T) {
is := assert.New(t)
// TODO: Rest of flags
log := logrus.New()
tmpConn, err := easymongo.ConnectWith(conn.MongoURI()).Flags(
easymongo.DefaultAnywhere).Debug().Logger(log).Connect()
is.NoError(err, "Issue connecting to the test instance using options")
is.NoError(tmpConn.Ping(), "Could not ping the test instance")
})
t.Run("Connect", func(t *testing.T) {
is := assert.New(t)
defer func() {
if trace := recover(); trace != nil {
t.Errorf("Connect panicked: %v", trace)
t.Fail()
}
}()
tmpConn := easymongo.Connect(conn.MongoURI())
is.NoError(tmpConn.Ping(), "Could not ping the test instance")
})
t.Run("GetCurrentConnection", func(t *testing.T) {
is := assert.New(t)
globalConn := easymongo.GetCurrentConnection()
is.NotNil(conn)
is.Equal(conn.MongoURI(), globalConn.MongoURI(), "The global connection doesn't match the current connection")
})
t.Run("ConnectUsingMongoClient", func(t *testing.T) {
is := assert.New(t)
// TODO: Actually construct a client rather than using a recycled one
tmpConn := easymongo.ConnectWith(conn.MongoURI()).FromMongoDriverClient(conn.MongoDriverClient())
is.NoError(tmpConn.Ping(), "Could not ping the test instance")
})
t.Run("Connect to a DB with different flags", func(t *testing.T) {
is := assert.New(t)
db := conn.DatabaseByConnectionType("my_db", easymongo.DefaultPrimary)
is.NotNil(db, "The database shouldn't be empty")
db = conn.DatabaseByConnectionType("my_db",
easymongo.ReadConcernLinearizable|easymongo.ReadPreferencePrimaryPreferred|easymongo.WriteConcernW3)
is.NotNil(db, "The database shouldn't be empty")
db = conn.DatabaseByConnectionType("my_db", easymongo.DefaultSecondary)
is.NotNil(db, "The database shouldn't be empty")
})
}