-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery_test.go
183 lines (161 loc) · 5.16 KB
/
query_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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package cloudant_test
import (
. "github.com/obieq/go-cloudant"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Query", func() {
var (
query *Query
sortingQuery *Query
operator map[string]interface{}
results []CloudantAutomobile
)
BeforeEach(func() {
// create test query data (will only be performed once per test run)
createTestQueryDataWithIndices()
query = NewQuery()
operator = make(map[string]interface{})
results = []CloudantAutomobile{}
})
Describe("Querying", func() {
Context("With a non-indexed field", func() {
It("should return a 400 error", func() {
query.Selector["non-existent-field-name"] = 6
err := testDb.Query(query, &results)
Ω(err).To(HaveOccurred())
Ω(err.(*CloudantError).StatusCode).Should(Equal(400))
Ω(err.(*CloudantError).Code).Should(Equal("no_usable_index"))
})
})
Context("With one indexed field", func() {
It("should find zero documents", func() {
query.Selector["Model"] = "does-not-exist"
err := testDb.Query(query, &results)
Ω(err).NotTo(HaveOccurred())
Ω(len(results)).Should(Equal(0))
})
It("should find one document", func() {
query.Selector["Model"] = "Diablo"
err := testDb.Query(query, &results)
Ω(err).NotTo(HaveOccurred())
Ω(len(results)).Should(Equal(1))
})
It("should find two documents", func() {
query.Selector["Model"] = "Gallardo"
err := testDb.Query(query, &results)
Ω(err).NotTo(HaveOccurred())
Ω(len(results)).Should(Equal(2))
})
It("should specify explicit operators", func() {
operator["$eq"] = "Gallardo"
query.Selector["Model"] = operator
err := testDb.Query(query, &results)
Ω(err).NotTo(HaveOccurred())
Ω(len(results)).Should(Equal(2))
})
It("should limit", func() {
operator["$gt"] = "1111"
query.Selector["Model"] = operator
query.Limit = 6
err := testDb.Query(query, &results)
Ω(err).NotTo(HaveOccurred())
Ω(len(results)).Should(Equal(6))
})
It("should skip", func() {
operator["$gt"] = "1111"
query.Selector["Model"] = operator
query.Skip = 6
err := testDb.Query(query, &results)
Ω(err).NotTo(HaveOccurred())
Ω(len(results)).Should(Equal(2))
})
})
Context("With one indexed field and one non-indexed field", func() {
It("should find zero documents", func() {
query.Selector["Model"] = "458"
query.Selector["Trim"] = "sport"
err := testDb.Query(query, &results)
Ω(err).NotTo(HaveOccurred())
Ω(len(results)).Should(Equal(0))
})
It("should find one document", func() {
query.Selector["Model"] = "550 Maranello"
query.Selector["Trim"] = "sport"
err := testDb.Query(query, &results)
Ω(err).NotTo(HaveOccurred())
Ω(len(results)).Should(Equal(1))
})
It("should find two documents", func() {
query.Selector["Model"] = "Gallardo"
query.Selector["Trim"] = "basic"
err := testDb.Query(query, &results)
Ω(err).NotTo(HaveOccurred())
Ω(len(results)).Should(Equal(2))
})
})
Context("With two indexed fields", func() {
It("should find zero documents", func() {
query.Selector["Year"] = 2000
query.Selector["Make"] = "Packard"
err := testDb.Query(query, &results)
Ω(err).NotTo(HaveOccurred())
Ω(len(results)).Should(Equal(0))
})
It("should find one document", func() {
query.Selector["Year"] = 2000
query.Selector["Make"] = "Lamborghini"
err := testDb.Query(query, &results)
Ω(err).NotTo(HaveOccurred())
Ω(len(results)).Should(Equal(1))
})
It("should find two documents", func() {
query.Selector["Year"] = 2010
query.Selector["Make"] = "Ferrari"
err := testDb.Query(query, &results)
Ω(err).NotTo(HaveOccurred())
Ω(len(results)).Should(Equal(2))
})
})
})
Describe("Sorting", func() {
BeforeEach(func() {
sortingQuery = NewQuery()
operator2 := make(map[string]interface{})
operator["$gt"] = 1800
operator2["$gt"] = "AA"
sortingQuery.Selector["Year"] = operator
sortingQuery.Selector["Make"] = operator2
})
It("should sort one field ascending", func() {
sortingQuery.Sort("Year", true)
err := testDb.Query(sortingQuery, &results)
Ω(err).NotTo(HaveOccurred())
Ω(len(results)).Should(Equal(8))
Ω(results[0].Year).Should(Equal(2000))
Ω(results[7].Year).Should(Equal(2011))
})
It("should sort one field descending", func() {
sortingQuery.Sort("Year", false)
err := testDb.Query(sortingQuery, &results)
Ω(err).NotTo(HaveOccurred())
Ω(len(results)).Should(Equal(8))
Ω(results[0].Year).Should(Equal(2011))
Ω(results[7].Year).Should(Equal(2000))
})
})
Describe("Error Handling", func() {
It("should return an error if selector fails json.Marshal", func() {
query.Selector = GenerateInvalidJson()
err := testDb.Query(query, &results)
Ω(err).To(HaveOccurred())
Ω(err.Error()).Should(Equal("json: unsupported type: map[int]interface {}"))
})
It("should return an error if the http request fails", func() {
db := errClientRequest.GetDatabase("non-existent-db-name")
query.Selector["Model"] = "Diablo"
err := db.Query(query, &results)
Ω(err).To(HaveOccurred())
})
})
})