Skip to content

Commit 8d74790

Browse files
committed
Merge branch 'filtersFunctionalTests' into 3.0.x
2 parents 4b7892c + 499f891 commit 8d74790

File tree

4 files changed

+450
-3
lines changed

4 files changed

+450
-3
lines changed
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
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.BsonType
22+
import org.bson.Document
23+
import org.bson.conversions.Bson
24+
import spock.lang.IgnoreIf
25+
26+
import java.util.regex.Pattern
27+
28+
import static com.mongodb.ClusterFixture.serverVersionAtLeast
29+
import static com.mongodb.client.model.Filters.all
30+
import static com.mongodb.client.model.Filters.and
31+
import static com.mongodb.client.model.Filters.elemMatch
32+
import static com.mongodb.client.model.Filters.eq
33+
import static com.mongodb.client.model.Filters.exists
34+
import static com.mongodb.client.model.Filters.gt
35+
import static com.mongodb.client.model.Filters.gte
36+
import static com.mongodb.client.model.Filters.lt
37+
import static com.mongodb.client.model.Filters.lte
38+
import static com.mongodb.client.model.Filters.mod
39+
import static com.mongodb.client.model.Filters.ne
40+
import static com.mongodb.client.model.Filters.nin
41+
import static com.mongodb.client.model.Filters.nor
42+
import static com.mongodb.client.model.Filters.not
43+
import static com.mongodb.client.model.Filters.or
44+
import static com.mongodb.client.model.Filters.regex
45+
import static com.mongodb.client.model.Filters.size
46+
import static com.mongodb.client.model.Filters.text
47+
import static com.mongodb.client.model.Filters.type
48+
import static com.mongodb.client.model.Filters.where
49+
50+
class FiltersFunctionalSpecification extends OperationFunctionalSpecification {
51+
def a = new Document('_id', 1).append('x', 1)
52+
.append('y', 'a')
53+
.append('a', [1, 2, 3])
54+
.append('a1', [new Document('c', 1).append('d', 2), new Document('c', 2).append('d', 3)])
55+
56+
def b = new Document('_id', 2).append('x', 2)
57+
.append('y', 'b')
58+
.append('a', [3, 4, 5, 6])
59+
.append('a1', [new Document('c', 2).append('d', 3), new Document('c', 3).append('d', 4)])
60+
61+
def c = new Document('_id', 3).append('x', 3)
62+
.append('y', 'c')
63+
.append('z', true)
64+
65+
def setup() {
66+
getCollectionHelper().insertDocuments(a, b, c)
67+
getCollectionHelper().createIndex(new Document('y', 'text'))
68+
}
69+
70+
def 'find'(Bson filter) {
71+
getCollectionHelper().find(filter, new Document('_id', 1)) // sort by _id
72+
}
73+
74+
def 'eq'() {
75+
expect:
76+
find(eq('x', 1)) == [a]
77+
}
78+
79+
def '$ne'() {
80+
expect:
81+
find(ne('x', 1)) == [b, c]
82+
}
83+
84+
@IgnoreIf({ !serverVersionAtLeast([2, 6, 0]) })
85+
def '$not'() {
86+
expect:
87+
find(not(eq('x', 1))) == [b, c]
88+
find(not(gt('x', 1))) == [a]
89+
find(not(regex('y', 'a.*'))) == [b, c]
90+
91+
when:
92+
find(not(and(eq('x', 1), eq('x', 1)))) == [b, c]
93+
94+
then:
95+
thrown MongoQueryException
96+
}
97+
98+
def '$nor'() {
99+
expect:
100+
find(nor(eq('x', 1))) == [b, c]
101+
find(nor(eq('x', 1), eq('x', 2))) == [c]
102+
find(nor(and(eq('x', 1), eq('y', 'b')))) == [a, b, c]
103+
}
104+
105+
def '$gt'() {
106+
expect:
107+
find(gt('x', 1)) == [b, c]
108+
}
109+
110+
def '$lt'() {
111+
expect:
112+
find(lt('x', 3)) == [a, b]
113+
}
114+
115+
def '$gte'() {
116+
expect:
117+
find(gte('x', 2)) == [b, c]
118+
}
119+
120+
def '$lte'() {
121+
expect:
122+
find(lte('x', 2)) == [a, b]
123+
}
124+
125+
def '$exists'() {
126+
expect:
127+
find(exists('z')) == [c]
128+
find(exists('z', false)) == [a, b]
129+
}
130+
131+
def '$or'() {
132+
expect:
133+
find(or([eq('x', 1)])) == [a]
134+
find(or([eq('x', 1), eq('y', 'b')])) == [a, b]
135+
}
136+
137+
def 'and'() {
138+
expect:
139+
find(and([eq('x', 1)])) == [a]
140+
find(and([eq('x', 1), eq('y', 'a')])) == [a]
141+
}
142+
143+
def 'and should duplicate clashing keys'() {
144+
expect:
145+
find(and([eq('x', 1), eq('x', 1)])) == [a]
146+
}
147+
148+
def 'and should flatten multiple operators for the same key'() {
149+
expect:
150+
find(and([gte('x', 1), lte('x', 2)])) == [a, b]
151+
}
152+
153+
def 'and should flatten nested'() {
154+
expect:
155+
find(and([and([eq('x', 3), eq('y', 'c')]), eq('z', true)])) == [c]
156+
find(and([and([eq('x', 3), eq('x', 3)]), eq('z', true)])) == [c]
157+
find(and([gt('x', 1), gt('y', 'a')])) == [b, c]
158+
find(and([lt('x', 4), lt('x', 3)])) == [a, b]
159+
}
160+
161+
def 'should render $all'() {
162+
expect:
163+
find(all('a', [1, 2])) == [a]
164+
}
165+
166+
def 'should render $elemMatch'() {
167+
expect:
168+
find(elemMatch('a', new Document('$gte', 2).append('$lte', 2))) == [a]
169+
find(elemMatch('a1', and(eq('c', 1), gte('d', 2)))) == [a]
170+
find(elemMatch('a1', and(eq('c', 2), eq('d', 3)))) == [a, b]
171+
}
172+
173+
def 'should render $in'() {
174+
expect:
175+
find(Filters.in('a', [0, 1, 2])) == [a]
176+
}
177+
178+
def 'should render $nin'() {
179+
expect:
180+
find(nin('a', [1, 2])) == [b, c]
181+
}
182+
183+
def 'should render $mod'() {
184+
expect:
185+
find(mod('x', 2, 0)) == [b]
186+
}
187+
188+
def 'should render $size'() {
189+
expect:
190+
find(size('a', 4)) == [b]
191+
}
192+
193+
def 'should render $type'() {
194+
expect:
195+
find(type('x', BsonType.INT32)) == [a, b, c]
196+
find(type('x', BsonType.ARRAY)) == []
197+
}
198+
199+
@IgnoreIf({ !serverVersionAtLeast([2, 6, 0]) })
200+
def 'should render $text'() {
201+
expect:
202+
find(text('I love MongoDB')) == []
203+
find(text('I love MongoDB', 'English')) == []
204+
}
205+
206+
def 'should render $regex'() {
207+
expect:
208+
find(regex('y', 'a.*')) == [a]
209+
find(regex('y', 'a.*', 'si')) == [a]
210+
find(regex('y', Pattern.compile('a.*'))) == [a]
211+
}
212+
213+
def 'should render $where'() {
214+
expect:
215+
find(where('Array.isArray(this.a)')) == [a, b]
216+
}
217+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
import spock.lang.IgnoreIf
24+
25+
import static com.mongodb.ClusterFixture.serverVersionAtLeast
26+
import static com.mongodb.client.model.Filters.and
27+
import static com.mongodb.client.model.Filters.eq
28+
import static com.mongodb.client.model.Projections.elemMatch
29+
import static com.mongodb.client.model.Projections.exclude
30+
import static com.mongodb.client.model.Projections.excludeId
31+
import static com.mongodb.client.model.Projections.fields
32+
import static com.mongodb.client.model.Projections.include
33+
import static com.mongodb.client.model.Projections.metaTextScore
34+
import static com.mongodb.client.model.Projections.slice
35+
36+
class ProjectionFunctionalSpecification extends OperationFunctionalSpecification {
37+
def a = new Document('_id', 1).append('x', 1).append('y', [new Document('a', 1).append('b', 2),
38+
new Document('a', 2).append('b', 3),
39+
new Document('a', 3).append('b', 4)])
40+
def aYSlice1 = new Document('_id', 1).append('x', 1).append('y', [new Document('a', 1).append('b', 2)])
41+
def aYSlice12 = new Document('_id', 1).append('x', 1).append('y', [new Document('a', 2).append('b', 3),
42+
new Document('a', 3).append('b', 4)])
43+
def aNoY = new Document('_id', 1).append('x', 1)
44+
def aId = new Document('_id', 1)
45+
def aNoId = new Document().append('x', 1).append('y', [new Document('a', 1).append('b', 2),
46+
new Document('a', 2).append('b', 3),
47+
new Document('a', 3).append('b', 4)])
48+
def aWithScore = new Document('_id', 1).append('x', 1).append('y', [new Document('a', 1).append('b', 2),
49+
new Document('a', 2).append('b', 3),
50+
new Document('a', 3).append('b', 4)])
51+
.append('score', 0.0)
52+
53+
def setup() {
54+
getCollectionHelper().insertDocuments(a)
55+
getCollectionHelper().createIndex(new Document('y', 'text'))
56+
}
57+
58+
def 'find'(Bson projection) {
59+
getCollectionHelper().find(null, null, projection)
60+
}
61+
62+
def 'find'(Bson filter, Bson projection) {
63+
getCollectionHelper().find(filter, null, projection)
64+
}
65+
66+
def 'include'() {
67+
expect:
68+
find(include('x')) == [aNoY]
69+
find(include('x', 'y')) == [a]
70+
find(include(['x', 'y', 'x'])) == [a]
71+
}
72+
73+
def 'exclude'() {
74+
expect:
75+
find(exclude('y')) == [aNoY]
76+
find(exclude('x', 'y')) == [aId]
77+
find(exclude(['x', 'y', 'x'])) == [aId]
78+
}
79+
80+
def 'excludeId'() {
81+
expect:
82+
find(excludeId()) == [aNoId]
83+
}
84+
85+
def 'firstElem'() {
86+
expect:
87+
find(new Document('y', new Document('$elemMatch', new Document('a', 1).append('b', 2))),
88+
fields(include('x'), elemMatch('y'))) == [aYSlice1]
89+
}
90+
91+
def 'elemMatch'() {
92+
expect:
93+
find(fields(include('x'), elemMatch('y', and(eq('a', 1), eq('b', 2))))) == [aYSlice1]
94+
}
95+
96+
def 'slice'() {
97+
expect:
98+
find(slice('y', 1)) == [aYSlice1]
99+
find(slice('y', 1, 2)) == [aYSlice12]
100+
}
101+
102+
@IgnoreIf({ !serverVersionAtLeast([2, 6, 0]) })
103+
def 'metaTextScore'() {
104+
expect:
105+
find(metaTextScore('score')) == [aWithScore]
106+
}
107+
108+
def 'combine fields'() {
109+
expect:
110+
find(fields(include('x', 'y'), exclude('_id'))) == [aNoId]
111+
}
112+
113+
def 'combine fields illegally'() {
114+
when:
115+
find(fields(include('x', 'y'), exclude('y'))) == [aNoY]
116+
117+
then:
118+
thrown(MongoQueryException)
119+
}
120+
}

0 commit comments

Comments
 (0)