-
Notifications
You must be signed in to change notification settings - Fork 40
/
resultSet_test.go
149 lines (124 loc) · 3.29 KB
/
resultSet_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
package bongo
import (
. "github.com/smartystreets/goconvey/convey"
"github.com/globalsign/mgo/bson"
"testing"
)
func TestResultSet(t *testing.T) {
conn := getConnection()
collection := conn.Collection("tests")
defer conn.Session.Close()
Convey("Basic find/pagination", t, func() {
// Create 10 things
for i := 0; i < 10; i++ {
doc := &noHookDocument{}
collection.Save(doc)
}
Convey("should let you iterate through all results without paginating", func() {
rset := collection.Find(nil)
defer rset.Free()
count := 0
doc := &noHookDocument{}
for rset.Next(doc) {
count++
}
So(count, ShouldEqual, 10)
})
Convey("should let you paginate and get pagination info", func() {
rset := collection.Find(nil)
defer rset.Free()
info, err := rset.Paginate(3, 1)
So(err, ShouldEqual, nil)
So(info.TotalRecords, ShouldEqual, 10)
So(info.TotalPages, ShouldEqual, 4)
So(info.Current, ShouldEqual, 1)
So(info.PerPage, ShouldEqual, 3)
So(info.RecordsOnPage, ShouldEqual, 3)
rset2 := collection.Find(nil)
defer rset2.Free()
info, err = rset2.Paginate(3, 4)
So(err, ShouldEqual, nil)
So(info.TotalRecords, ShouldEqual, 10)
So(info.TotalPages, ShouldEqual, 4)
So(info.Current, ShouldEqual, 4)
So(info.PerPage, ShouldEqual, 3)
So(info.RecordsOnPage, ShouldEqual, 1)
})
Reset(func() {
conn.Session.DB("bongotest").DropDatabase()
})
})
Convey("Find/pagination w/ query", t, func() {
// Create 10 things
for i := 0; i < 5; i++ {
doc := &noHookDocument{}
doc.Name = "foo"
collection.Save(doc)
}
for i := 0; i < 5; i++ {
doc := &noHookDocument{}
doc.Name = "bar"
collection.Save(doc)
}
Convey("should let you iterate through all filtered results without paginating", func() {
rset := collection.Find(bson.M{
"name": "foo",
})
defer rset.Free()
count := 0
doc := &noHookDocument{}
for rset.Next(doc) {
count++
}
So(count, ShouldEqual, 5)
})
Convey("should let you paginate and get pagination info on filtered query", func() {
rset := collection.Find(bson.M{
"name": "foo",
})
defer rset.Free()
info, err := rset.Paginate(3, 1)
So(err, ShouldEqual, nil)
So(info.TotalRecords, ShouldEqual, 5)
So(info.TotalPages, ShouldEqual, 2)
So(info.Current, ShouldEqual, 1)
So(info.PerPage, ShouldEqual, 3)
So(info.RecordsOnPage, ShouldEqual, 3)
rset2 := collection.Find(bson.M{
"name": "foo",
})
defer rset2.Free()
info, err = rset2.Paginate(3, 2)
So(err, ShouldEqual, nil)
So(info.TotalRecords, ShouldEqual, 5)
So(info.TotalPages, ShouldEqual, 2)
So(info.Current, ShouldEqual, 2)
So(info.PerPage, ShouldEqual, 3)
So(info.RecordsOnPage, ShouldEqual, 2)
})
Reset(func() {
conn.Session.DB("bongotest").DropDatabase()
})
})
Convey("hooks", t, func() {
// Create 10 things
for i := 0; i < 10; i++ {
doc := &hookedDocument{}
collection.Save(doc)
}
Convey("should let you iterate through all results without paginating", func() {
rset := collection.Find(nil)
defer rset.Free()
count := 0
doc := &hookedDocument{}
for rset.Next(doc) {
So(doc.RanAfterFind, ShouldEqual, true)
count++
}
So(count, ShouldEqual, 10)
})
Reset(func() {
conn.Session.DB("bongotest").DropDatabase()
})
})
}