Skip to content

Commit bb73a97

Browse files
jyeminrozza
authored andcommitted
Added functional test specifications for Filters, Projections, and Sorts
1 parent 7bac500 commit bb73a97

File tree

4 files changed

+437
-3
lines changed

4 files changed

+437
-3
lines changed
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
/*
2+
* Copyright 2015 MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONObjectITIONS OF ANY KINObject, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb.client.model
18+
19+
import com.mongodb.OperationFunctionalSpecification
20+
import org.bson.BsonType
21+
import org.bson.Document
22+
import org.bson.conversions.Bson
23+
24+
import java.util.regex.Pattern
25+
26+
import static com.mongodb.client.model.Filters.all
27+
import static com.mongodb.client.model.Filters.and
28+
import static com.mongodb.client.model.Filters.elemMatch
29+
import static com.mongodb.client.model.Filters.eq
30+
import static com.mongodb.client.model.Filters.exists
31+
import static com.mongodb.client.model.Filters.gt
32+
import static com.mongodb.client.model.Filters.gte
33+
import static com.mongodb.client.model.Filters.lt
34+
import static com.mongodb.client.model.Filters.lte
35+
import static com.mongodb.client.model.Filters.mod
36+
import static com.mongodb.client.model.Filters.ne
37+
import static com.mongodb.client.model.Filters.nin
38+
import static com.mongodb.client.model.Filters.nor
39+
import static com.mongodb.client.model.Filters.not
40+
import static com.mongodb.client.model.Filters.or
41+
import static com.mongodb.client.model.Filters.regex
42+
import static com.mongodb.client.model.Filters.size
43+
import static com.mongodb.client.model.Filters.text
44+
import static com.mongodb.client.model.Filters.type
45+
import static com.mongodb.client.model.Filters.where
46+
47+
class FiltersFunctionalSpecification extends OperationFunctionalSpecification {
48+
def a = new Document('_id', 1).append('x', 1)
49+
.append('y', 'a')
50+
.append('a', [1, 2, 3])
51+
.append('a1', [new Document('c', 1).append('d', 2), new Document('c', 2).append('d', 3)])
52+
53+
def b = new Document('_id', 2).append('x', 2)
54+
.append('y', 'b')
55+
.append('a', [3, 4, 5, 6])
56+
.append('a1', [new Document('c', 2).append('d', 3), new Document('c', 3).append('d', 4)])
57+
58+
def c = new Document('_id', 3).append('x', 3)
59+
.append('y', 'c')
60+
.append('z', true)
61+
62+
def setup() {
63+
getCollectionHelper().insertDocuments(a, b, c)
64+
getCollectionHelper().createIndex(new Document('y', 'text'))
65+
}
66+
67+
def 'find'(Bson filter) {
68+
getCollectionHelper().find(filter, new Document('_id', 1)) // sort by _id
69+
}
70+
71+
def 'eq'() {
72+
expect:
73+
find(eq('x', 1)) == [a]
74+
}
75+
76+
def '$ne'() {
77+
expect:
78+
find(ne('x', 1)) == [b, c]
79+
}
80+
81+
def '$not'() {
82+
expect:
83+
find(not(eq('x', 1))) == [b, c]
84+
find(not(gt('x', 1))) == [a]
85+
find(not(regex('y', 'a.*'))) == [b, c]
86+
find(not(and(eq('x', 1), eq('x', 1)))) == [b, c]
87+
find(not(and(Filters.in('a', 1, 2), eq('x', 1)))) == [b, c]
88+
find(not(and(eq('x', 1), eq('y', 'a')))) == [b, c]
89+
// find(not(and(eq('x', 1), eq('y', 'b')))) == [a, b, c] // should pass, but see JAVA-1740
90+
}
91+
92+
def '$nor'() {
93+
expect:
94+
find(nor(eq('x', 1))) == [b, c]
95+
find(nor(eq('x', 1), eq('x', 2))) == [c]
96+
find(nor(and(eq('x', 1), eq('y', 'b')))) == [a, b, c]
97+
}
98+
99+
def '$gt'() {
100+
expect:
101+
find(gt('x', 1)) == [b, c]
102+
}
103+
104+
def '$lt'() {
105+
expect:
106+
find(lt('x', 3)) == [a, b]
107+
}
108+
109+
def '$gte'() {
110+
expect:
111+
find(gte('x', 2)) == [b, c]
112+
}
113+
114+
def '$lte'() {
115+
expect:
116+
find(lte('x', 2)) == [a, b]
117+
}
118+
119+
def '$exists'() {
120+
expect:
121+
find(exists('z')) == [c]
122+
find(exists('z', false)) == [a, b]
123+
}
124+
125+
def '$or'() {
126+
expect:
127+
find(or([eq('x', 1)])) == [a]
128+
find(or([eq('x', 1), eq('y', 'b')])) == [a, b]
129+
}
130+
131+
def 'and'() {
132+
expect:
133+
find(and([eq('x', 1)])) == [a]
134+
find(and([eq('x', 1), eq('y', 'a')])) == [a]
135+
}
136+
137+
def 'and should duplicate clashing keys'() {
138+
expect:
139+
find(and([eq('x', 1), eq('x', 1)])) == [a]
140+
}
141+
142+
def 'and should flatten multiple operators for the same key'() {
143+
expect:
144+
find(and([gte('x', 1), lte('x', 2)])) == [a, b]
145+
}
146+
147+
def 'and should flatten nested'() {
148+
expect:
149+
find(and([and([eq('x', 3), eq('y', 'c')]), eq('z', true)])) == [c]
150+
find(and([and([eq('x', 3), eq('x', 3)]), eq('z', true)])) == [c]
151+
find(and([gt('x', 1), gt('y', 'a')])) == [b, c]
152+
find(and([lt('x', 4), lt('x', 3)])) == [a, b]
153+
}
154+
155+
def 'should render $all'() {
156+
expect:
157+
find(all('a', [1, 2])) == [a]
158+
}
159+
160+
def 'should render $elemMatch'() {
161+
expect:
162+
find(elemMatch('a', new Document('$gte', 2).append('$lte', 2))) == [a]
163+
find(elemMatch('a1', and(eq('c', 1), gte('d', 2)))) == [a]
164+
find(elemMatch('a1', and(eq('c', 2), eq('d', 3)))) == [a, b]
165+
}
166+
167+
def 'should render $in'() {
168+
expect:
169+
find(Filters.in('a', [0, 1, 2])) == [a]
170+
}
171+
172+
def 'should render $nin'() {
173+
expect:
174+
find(nin('a', [1, 2])) == [b, c]
175+
}
176+
177+
def 'should render $mod'() {
178+
expect:
179+
find(mod('x', 2, 0)) == [b]
180+
}
181+
182+
def 'should render $size'() {
183+
expect:
184+
find(size('a', 4)) == [b]
185+
}
186+
187+
def 'should render $type'() {
188+
expect:
189+
find(type('x', BsonType.INT32)) == [a, b, c]
190+
find(type('x', BsonType.ARRAY)) == []
191+
}
192+
193+
def 'should render $text'() {
194+
expect:
195+
find(text('I love MongoDB')) == []
196+
find(text('I love MongoDB', 'English')) == []
197+
}
198+
199+
def 'should render $regex'() {
200+
expect:
201+
find(regex('y', 'a.*')) == [a]
202+
find(regex('y', 'a.*', 'si')) == [a]
203+
find(regex('y', Pattern.compile('a.*'))) == [a]
204+
}
205+
206+
def 'should render $where'() {
207+
expect:
208+
find(where('Array.isArray(this.a)')) == [a, b]
209+
}
210+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright 2015 MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONObjectITIONS OF ANY KINObject, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb.client.model
18+
19+
import com.mongodb.MongoQueryException
20+
import com.mongodb.OperationFunctionalSpecification
21+
import org.bson.Document
22+
import org.bson.conversions.Bson
23+
24+
import static com.mongodb.client.model.Filters.and
25+
import static com.mongodb.client.model.Filters.eq
26+
import static com.mongodb.client.model.Projections.elemMatch
27+
import static com.mongodb.client.model.Projections.exclude
28+
import static com.mongodb.client.model.Projections.excludeId
29+
import static com.mongodb.client.model.Projections.fields
30+
import static com.mongodb.client.model.Projections.include
31+
import static com.mongodb.client.model.Projections.metaTextScore
32+
import static com.mongodb.client.model.Projections.slice
33+
34+
class ProjectionFunctionalSpecification extends OperationFunctionalSpecification {
35+
def a = new Document('_id', 1).append('x', 1).append('y', [new Document('a', 1).append('b', 2),
36+
new Document('a', 2).append('b', 3),
37+
new Document('a', 3).append('b', 4)])
38+
def aYSlice1 = new Document('_id', 1).append('x', 1).append('y', [new Document('a', 1).append('b', 2)])
39+
def aYSlice12 = new Document('_id', 1).append('x', 1).append('y', [new Document('a', 2).append('b', 3),
40+
new Document('a', 3).append('b', 4)])
41+
def aNoY = new Document('_id', 1).append('x', 1)
42+
def aId = new Document('_id', 1)
43+
def aNoId = new Document().append('x', 1).append('y', [new Document('a', 1).append('b', 2),
44+
new Document('a', 2).append('b', 3),
45+
new Document('a', 3).append('b', 4)])
46+
def aWithScore = new Document('_id', 1).append('x', 1).append('y', [new Document('a', 1).append('b', 2),
47+
new Document('a', 2).append('b', 3),
48+
new Document('a', 3).append('b', 4)])
49+
.append('score', 0.0)
50+
51+
def setup() {
52+
getCollectionHelper().insertDocuments(a)
53+
getCollectionHelper().createIndex(new Document('y', 'text'))
54+
}
55+
56+
def 'find'(Bson projection) {
57+
getCollectionHelper().find(new Document(), new Document('_id', 1), projection)
58+
}
59+
60+
def 'find'(Bson filter, Bson projection) {
61+
getCollectionHelper().find(filter, new Document('_id', 1), projection)
62+
}
63+
64+
def 'include'() {
65+
expect:
66+
find(include('x')) == [aNoY]
67+
find(include('x', 'y')) == [a]
68+
find(include(['x', 'y', 'x'])) == [a]
69+
}
70+
71+
def 'exclude'() {
72+
expect:
73+
find(exclude('y')) == [aNoY]
74+
find(exclude('x', 'y')) == [aId]
75+
find(exclude(['x', 'y', 'x'])) == [aId]
76+
}
77+
78+
def 'excludeId'() {
79+
expect:
80+
find(excludeId()) == [aNoId]
81+
}
82+
83+
def 'firstElem'() {
84+
expect:
85+
find(new Document('y', new Document('$elemMatch', new Document('a', 1).append('b', 2))),
86+
fields(include('x'), elemMatch('y'))) == [aYSlice1]
87+
}
88+
89+
def 'elemMatch'() {
90+
expect:
91+
find(fields(include('x'), elemMatch('y', and(eq('a', 1), eq('b', 2))))) == [aYSlice1]
92+
}
93+
94+
def 'slice'() {
95+
expect:
96+
find(slice('y', 1)) == [aYSlice1]
97+
find(slice('y', 1, 2)) == [aYSlice12]
98+
}
99+
100+
def 'metaTextScore'() {
101+
expect:
102+
find(metaTextScore('score')) == [aWithScore]
103+
}
104+
105+
def 'combine fields'() {
106+
expect:
107+
find(fields(include('x', 'y'), exclude('_id'))) == [aNoId]
108+
}
109+
110+
def 'combine fields illegally'() {
111+
when:
112+
find(fields(include('x', 'y'), exclude('y'))) == [aNoY]
113+
114+
then:
115+
thrown(MongoQueryException)
116+
}
117+
}

0 commit comments

Comments
 (0)