-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_test.go
80 lines (77 loc) · 2.53 KB
/
database_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
package easymongo_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/tophergopher/easymongo"
)
func TestGetDatabase(t *testing.T) {
setup(t)
var err error
coll := createBatmanArchive(t)
dbName := coll.GetDatabase().Name()
t.Run("GetDatabase", func(t *testing.T) {
is := assert.New(t)
db := conn.Database(dbName)
is.NotNil(db)
})
t.Run("DatabaseNames", func(t *testing.T) {
is := assert.New(t)
dbNames := conn.DatabaseNames()
is.NoError(err, "Couldn't find the database names")
is.Contains(dbNames, dbName, "There should only be one database present")
})
t.Run("ListDatabases", func(t *testing.T) {
is := assert.New(t)
dbs := conn.ListDatabases()
is.NoError(err, "Couldn't list the databases")
found := false
for _, db := range dbs {
if db.Name() == dbName {
found = true
}
}
is.True(found, "Could not find the expected database among the database list")
})
t.Run("db.CollectionNames", func(t *testing.T) {
is := assert.New(t)
collNames := conn.Database(dbName).CollectionNames()
is.Len(collNames, 1, "There should only be one collection present")
})
t.Run("db.ListCollections", func(t *testing.T) {
is := assert.New(t)
colls, err := conn.Database(dbName).ListCollections()
is.NoError(err)
is.Len(colls, 1, "There should only be one collection present")
if len(colls) != 1 || err != nil {
t.FailNow()
}
is.NotNil(colls[0])
if colls[0] == nil {
t.FailNow()
}
// Now just try a standard Count() operation on that collection object
is.Equal("enemies", colls[0].Name(), "The collection name on the test appears to be unset. This appears to be an improperly initialized Collection object.")
})
t.Run("db.ListCollections", func(t *testing.T) {
is := assert.New(t)
colls, err := conn.Database(dbName).ListCollections()
is.NoError(err, "Couldn't list the collections")
is.Len(colls, 1, "There should only be one collection present")
})
t.Run("easymongo.GetDatabase global", func(t *testing.T) {
is := assert.New(t)
colls, err := easymongo.GetDatabase(dbName).ListCollections()
is.NoError(err, "Couldn't list the collections")
is.Len(colls, 1, "There should only be one collection present")
})
t.Run("db.Drop()", func(t *testing.T) {
is := assert.New(t)
dbNamesBefore := conn.DatabaseNames()
db := conn.Database(dbName)
err := db.Drop()
is.NoError(err, "Could not drop the database")
dbNamesAfter := conn.DatabaseNames()
dbLengthDiff := len(dbNamesBefore) - len(dbNamesAfter)
is.Equal(1, dbLengthDiff, "A database should have been dropped")
})
}