|
| 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 | +} |
0 commit comments