-
Notifications
You must be signed in to change notification settings - Fork 40
/
main_test.go
87 lines (69 loc) · 2.21 KB
/
main_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package bongo
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
// For test usage
func getConnection() *Connection {
conf := &Config{
ConnectionString: "localhost",
Database: "bongotest",
}
conn, err := Connect(conf)
conn.Context.Set("foo", "bar")
if err != nil {
panic(err)
}
return conn
}
func TestFailSSLConnec(t *testing.T) {
Convey("should fail to connect to a database because of unsupported ssl flag", t, func() {
conf := &Config{
ConnectionString: "mongodb://localhost?ssl=true",
Database: "bongotest",
}
_, err := Connect(conf)
So(err.Error(), ShouldEqual, "cannot parse given URI mongodb://localhost?ssl=true due to error: unsupported connection URL option: ssl=true")
})
}
func TestConnect(t *testing.T) {
Convey("should be able to connect to a database using a config", t, func() {
conf := &Config{
ConnectionString: "localhost",
Database: "bongotest",
}
conn, err := Connect(conf)
defer conn.Session.Close()
So(err, ShouldEqual, nil)
conn.Context.Set("foo", "bar")
value := conn.Context.Get("foo")
So(value, ShouldEqual, "bar")
err = conn.Session.Ping()
So(err, ShouldEqual, nil)
})
}
func TestRetrieveCollection(t *testing.T) {
Convey("should be able to retrieve a collection instance from a connection", t, func() {
conn := getConnection()
defer conn.Session.Close()
col := conn.Collection("tests");
So(col.Name, ShouldEqual, "tests")
So(col.Connection, ShouldEqual, conn)
So(col.Context.Get("foo"), ShouldEqual, "bar")
So(conn.Config.Database, ShouldEqual, col.Database)
})
Convey("should be able to retrieve a collection instance from a connection with different databases", t, func() {
conn := getConnection()
defer conn.Session.Close()
col1 := conn.CollectionFromDatabase("tests", "test1");
So(col1.Name, ShouldEqual, "tests")
So(col1.Connection, ShouldEqual, conn)
So(col1.Database, ShouldEqual, "test1")
col2 := conn.CollectionFromDatabase("tests", "test2");
So(col2.Name, ShouldEqual, "tests")
So(col2.Connection, ShouldEqual, conn)
So(col2.Database, ShouldEqual, "test2")
So(col2.Connection, ShouldEqual, col1.Connection)
So(col1.Database, ShouldNotEqual, col2.Database)
})
}