-
Notifications
You must be signed in to change notification settings - Fork 1
/
mongo_test.go
85 lines (75 loc) · 1.96 KB
/
mongo_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
package main
import (
"reflect"
"testing"
"gopkg.in/mgo.v2/bson"
. "github.com/smartystreets/goconvey/convey"
)
func TestMongo(t *testing.T) {
Convey("Test MongoDB Find Users", t, func() {
setDefaultValues(reflect.ValueOf(&dcfg).Elem())
dcfg.DB.Name = "tunaccount_test"
initMongo()
m := getMongo()
defer m.Close()
m.UserColl().Insert(
&User{
UID: m.getNextSeq("uid"),
GID: m.getNextSeq("gid"),
Name: "张三",
Email: "[email protected]",
Phone: "+86-010-1234-5678",
Username: "zhangsan",
Password: generateSSHA("123456"),
LoginShell: "/bin/bash",
IsActive: true,
},
&User{
UID: m.getNextSeq("uid"),
GID: m.getNextSeq("gid"),
Name: "李四",
Email: "[email protected]",
Phone: "+86-010-1234-5678",
Username: "lisi",
Password: generateSSHA("233333"),
LoginShell: "/bin/bash",
IsActive: false,
Tags: []string{"testing"},
},
&User{
UID: m.getNextSeq("uid"),
GID: m.getNextSeq("gid"),
Name: "王尼玛",
Email: "[email protected]",
Phone: "+86-010-1234-5678",
Username: "wangnima",
Password: generateSSHA("233333"),
LoginShell: "/bin/bash",
IsActive: true,
Tags: []string{"testing"},
},
)
Convey("When query all", func() {
m := getMongo()
defer m.Close()
var allUsers []User
m.UserColl().Find(bson.M{}).All(&allUsers)
So(len(allUsers), ShouldEqual, 3)
})
Convey("When query with filters", func() {
m := getMongo()
defer m.Close()
activeUsers := m.FindUsers(bson.M{}, "")
So(len(activeUsers), ShouldEqual, 2)
testUsers := m.FindUsers(bson.M{}, "testing")
So(len(testUsers), ShouldEqual, 1)
invalidUsers := m.FindUsers(bson.M{}, "ooxx")
So(len(invalidUsers), ShouldEqual, 0)
})
Reset(func() {
m := getMongo()
defer m.Close()
m.session.DB(m.dbname).DropDatabase()
})
})
}