-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
minquery_test.go
159 lines (132 loc) · 3.49 KB
/
minquery_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
package minquery
import (
"encoding/hex"
"log"
"testing"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"github.com/icza/mighty"
)
var sess *mgo.Session
func init() {
var err error
sess, err = mgo.Dial("mongodb://localhost/minquery")
if err != nil {
panic(err)
}
// Check required min version (3.2)
bi, err := sess.BuildInfo()
if err != nil {
panic(err)
}
if !bi.VersionAtLeast(3, 2) {
log.Panicf("This test requires at least MongoDB 3.2, got only %v", bi.Version)
}
c := sess.DB("").C("users")
if err := c.EnsureIndex(mgo.Index{Key: []string{"name", "_id"}}); err != nil {
panic(err)
}
if err := c.EnsureIndex(mgo.Index{Key: []string{"name", "-_id"}}); err != nil {
panic(err)
}
if _, err := c.RemoveAll(nil); err != nil {
panic(err)
}
}
type User struct {
ID bson.ObjectId `bson:"_id"`
Name string `bson:"name"`
Country string `bson:"country"`
}
func TestMinQuery(t *testing.T) {
eq, neq, deq := mighty.Eq(t), mighty.Neq(t), mighty.Deq(t)
_, _ = eq, neq
c := sess.DB("").C("users")
// Insert test documents:
users := []*User{
{Name: "Aaron", Country: "UK"},
{Name: "Alice", Country: "US"},
{Name: "Alice", Country: "US"},
{Name: "Chloe", Country: "US"},
{Name: "Dakota", Country: "US"},
{Name: "Ed", Country: "US"},
{Name: "Fae", Country: "US"},
{Name: "Glan", Country: "US"},
}
cursorFields := []string{"name", "_id"}
for _, u := range users {
u.ID = bson.NewObjectId()
eq(nil, c.Insert(u))
}
mq := NewWithHint(
sess.DB(""),
"users",
bson.M{"country": "US"},
map[string]int{"name": 1, "_id": 1},
).
Sort("name", "_id").Limit(3)
var result []*User
cursor, err := mq.All(&result, cursorFields...)
eq(nil, err)
deq(users[1:4], result)
cursor, err = mq.Cursor(cursor).All(&result, cursorFields...)
eq(nil, err)
deq(users[4:7], result)
cursor, err = mq.Cursor(cursor).All(&result, cursorFields...)
eq(nil, err)
deq(users[7:], result)
_, err = mq.Cursor(cursor).All(&result, cursorFields...)
eq(nil, err)
eq(0, len(result))
var parres []bson.M
_, err = mq.Sort("+name", "-_id", "").Select(bson.M{"name": 1, "_id": 1}).
Cursor("").Limit(2).All(&parres, cursorFields...)
eq(nil, err)
deq([]bson.M{
{"name": "Alice", "_id": users[2].ID},
{"name": "Alice", "_id": users[1].ID},
}, parres)
_, err = mq.CursorCodec(testCodec{}).All(&parres, cursorFields...)
eq(nil, err)
deq([]bson.M{
{"name": "Alice", "_id": users[2].ID},
{"name": "Alice", "_id": users[1].ID},
}, parres)
// Test cursor error:
_, err = mq.Cursor("(INVALID)").All(&parres, cursorFields...)
neq(nil, err)
mq.Cursor("")
// Test db.Run() failure
mq.Select("invalid-select-doc")
_, err = mq.All(&result, cursorFields...)
neq(nil, err)
mq.Select(nil)
// Test cursor creation error
_, err = mq.CursorCodec(testCodec{testError: true}).
All(&parres, cursorFields...)
neq(nil, err)
// Test first batch unmarshal error:
mq.(*minQuery).testError = true
_, err = mq.CursorCodec(testCodec{testError: true}).
All(&parres, cursorFields...)
neq(nil, err)
mq.(*minQuery).testError = false
}
type testCodec struct {
testError bool
}
func (tc testCodec) CreateCursor(cursorData bson.D) (string, error) {
if tc.testError {
return "", errTestValue
}
data, err := bson.Marshal(cursorData)
return hex.EncodeToString(data), err
}
func (testCodec) ParseCursor(c string) (cursorData bson.D, err error) {
var data []byte
if data, err = hex.DecodeString(c); err != nil {
return
}
err = bson.Unmarshal(data, &cursorData)
return
}