-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
165 lines (157 loc) · 2.83 KB
/
util_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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package mongo
import (
"reflect"
"testing"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
)
type user struct {
ID bson.ObjectId `bson:"_id,omitempty"`
Name string `bson:"name"`
}
type userSelector struct {
ID *bson.ObjectId `bson:"_id"`
IDs *[]bson.ObjectId `bson:"_id"`
Name *string `bson:"name"`
}
func (us *userSelector) Database() string {
return "test"
}
func (us *userSelector) Collection() string {
return "users"
}
func Test_structToBsonM(t *testing.T) {
type args struct {
s interface{}
}
id := bson.NewObjectId()
ids := []bson.ObjectId{bson.NewObjectId()}
name := "hello"
tests := []struct {
name string
args args
want bson.M
wantErr bool
}{
// TODO: Add test cases.
{
name: "nil",
args: args{
nil,
},
want: bson.M{},
wantErr: true,
},
{
name: "struct",
args: args{
user{},
},
want: bson.M{},
wantErr: true,
},
{
name: "empty",
args: args{
&userSelector{},
},
want: bson.M{},
wantErr: false,
},
{
name: "name",
args: args{
&userSelector{Name: &name},
},
want: bson.M{"name": name},
wantErr: false,
},
{
name: "id",
args: args{
&userSelector{ID: &id},
},
want: bson.M{"_id": id},
wantErr: false,
},
{
name: "ids",
args: args{
&userSelector{IDs: &ids},
},
want: bson.M{"_id": bson.M{"$in": ids}},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := structToBsonM(tt.args.s)
if (err != nil) != tt.wantErr {
t.Errorf("structToBsonM() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("structToBsonM() = %v, want %v", got, tt.want)
}
})
}
}
func Test_getCollection(t *testing.T) {
sess, err := mgo.Dial("127.0.0.1")
if err != nil {
t.Skip("Cannot connect to mongodb.")
return
}
defer sess.Close()
type args struct {
sess *mgo.Session
query interface{}
}
tests := []struct {
name string
args args
wantC *mgo.Collection
wantErr bool
}{
// TODO: Add test cases.
{
name: "nil",
args: args{
sess: sess,
query: nil,
},
wantC: nil,
wantErr: true,
},
{
name: "struct",
args: args{
sess: sess,
query: user{},
},
wantC: nil,
wantErr: true,
},
{
name: "struct pointer",
args: args{
sess: sess,
query: &user{},
},
wantC: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotC, err := getCollection(tt.args.sess, tt.args.query)
if (err != nil) != tt.wantErr {
t.Errorf("getCollection() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotC, tt.wantC) {
t.Errorf("getCollection() = %v, want %v", gotC, tt.wantC)
}
})
}
}