-
Notifications
You must be signed in to change notification settings - Fork 15
/
database_test.go
109 lines (92 loc) · 2.57 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package lungo
import (
"testing"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/readconcern"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
func TestDatabaseClient(t *testing.T) {
clientTest(t, func(t *testing.T, c IClient) {
assert.Equal(t, c, c.Database("").Client())
})
}
func TestDatabaseCollection(t *testing.T) {
databaseTest(t, func(t *testing.T, d IDatabase) {
assert.NotNil(t, d.Collection("foo"))
})
}
func TestDatabaseCreate(t *testing.T) {
databaseTest(t, func(t *testing.T, d IDatabase) {
assert.NoError(t, d.CreateCollection(nil, "bar"))
})
}
func TestDatabaseDrop(t *testing.T) {
collectionTest(t, func(t *testing.T, c ICollection) {
_, err := c.InsertOne(nil, bson.M{
"foo": "bar",
})
assert.NoError(t, err)
assert.Equal(t, []bson.M{
{
"foo": "bar",
},
}, dumpCollection(c, true))
err = c.Database().Drop(nil)
assert.NoError(t, err)
assert.Equal(t, []bson.M{}, dumpCollection(c, true))
})
}
func TestDatabaseListCollectionsAndNames(t *testing.T) {
databaseTest(t, func(t *testing.T, d IDatabase) {
names, err := d.ListCollectionNames(nil, bson.M{
"name": "coll-names",
})
assert.NoError(t, err)
assert.Equal(t, []string{}, names)
csr, err := d.ListCollections(nil, bson.M{
"name": "coll-names",
})
assert.NoError(t, err)
assert.Equal(t, []bson.M{}, readAll(csr))
_, err = d.Collection("coll-names").InsertOne(nil, bson.M{
"foo": "bar",
})
assert.NoError(t, err)
names, err = d.ListCollectionNames(nil, bson.M{
"name": "coll-names",
})
assert.NoError(t, err)
assert.Equal(t, []string{"coll-names"}, names)
csr, err = d.ListCollections(nil, bson.M{
"name": "coll-names",
})
assert.NoError(t, err)
res := readAll(csr)
assert.Len(t, res, 1)
assert.Equal(t, "coll-names", res[0]["name"])
assert.Equal(t, "collection", res[0]["type"])
assert.Equal(t, bson.M{}, res[0]["options"])
assert.Equal(t, false, res[0]["info"].(bson.M)["readOnly"])
})
}
func TestDatabaseName(t *testing.T) {
databaseTest(t, func(t *testing.T, d IDatabase) {
assert.Equal(t, testDB, d.Name())
})
}
func TestDatabaseReadConcern(t *testing.T) {
databaseTest(t, func(t *testing.T, d IDatabase) {
assert.Equal(t, readconcern.New(), d.ReadConcern())
})
}
func TestDatabaseReadPreference(t *testing.T) {
databaseTest(t, func(t *testing.T, d IDatabase) {
assert.Equal(t, readpref.Primary(), d.ReadPreference())
})
}
func TestDatabaseWriteConcern(t *testing.T) {
databaseTest(t, func(t *testing.T, d IDatabase) {
assert.Nil(t, d.WriteConcern())
})
}