|
| 1 | +/* |
| 2 | + * Copyright 2008-present 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 CONDITIONS OF ANY KIND, 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.expressions; |
| 18 | + |
| 19 | +import org.bson.BsonDocument; |
| 20 | +import org.bson.BsonString; |
| 21 | +import org.bson.BsonValue; |
| 22 | +import org.bson.codecs.BsonValueCodecProvider; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import java.time.Instant; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.Date; |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +import static com.mongodb.client.model.expressions.Expressions.of; |
| 31 | +import static com.mongodb.client.model.expressions.Expressions.ofBooleanArray; |
| 32 | +import static com.mongodb.client.model.expressions.Expressions.ofDocument; |
| 33 | +import static com.mongodb.client.model.expressions.Expressions.ofIntegerArray; |
| 34 | +import static com.mongodb.client.model.expressions.Expressions.ofNull; |
| 35 | +import static org.bson.codecs.configuration.CodecRegistries.fromProviders; |
| 36 | +import static org.junit.jupiter.api.Assertions.fail; |
| 37 | + |
| 38 | +@SuppressWarnings({"ConstantConditions"}) |
| 39 | +class ComparisonExpressionsFunctionalTest extends AbstractExpressionsFunctionalTest { |
| 40 | + // https://www.mongodb.com/docs/manual/reference/operator/aggregation/#comparison-expression-operators |
| 41 | + // (Complete as of 6.0) |
| 42 | + // Comparison expressions are part of the the generic Expression class. |
| 43 | + |
| 44 | + static <R extends Expression> R ofRem() { |
| 45 | + // $$REMOVE is intentionally not exposed to users |
| 46 | + return new MqlExpression<>((cr) -> new BsonString("$$REMOVE")).assertImplementsAllExpressions(); |
| 47 | + } |
| 48 | + |
| 49 | + // https://www.mongodb.com/docs/manual/reference/bson-type-comparison-order/#std-label-bson-types-comparison-order |
| 50 | + private final List<Expression> sampleValues = Arrays.asList( |
| 51 | + ofRem(), |
| 52 | + ofNull(), |
| 53 | + of(0), |
| 54 | + of(1), |
| 55 | + of(""), |
| 56 | + of("str"), |
| 57 | + ofDocument(BsonDocument.parse("{}")), |
| 58 | + ofDocument(BsonDocument.parse("{a: 1}")), |
| 59 | + ofDocument(BsonDocument.parse("{a: 2}")), |
| 60 | + ofDocument(BsonDocument.parse("{a: 2, b: 1}")), |
| 61 | + ofDocument(BsonDocument.parse("{b: 1, a: 2}")), |
| 62 | + ofDocument(BsonDocument.parse("{'':''}")), |
| 63 | + ofIntegerArray(0), |
| 64 | + ofIntegerArray(1), |
| 65 | + ofBooleanArray(true), |
| 66 | + of(false), |
| 67 | + of(true), |
| 68 | + of(Instant.now()) |
| 69 | + ); |
| 70 | + |
| 71 | + @Test |
| 72 | + public void eqTest() { |
| 73 | + // https://www.mongodb.com/docs/manual/reference/operator/aggregation/eq/ |
| 74 | + assertExpression( |
| 75 | + 1 == 2, |
| 76 | + of(1).eq(of(2)), |
| 77 | + "{'$eq': [1, 2]}"); |
| 78 | + assertExpression( |
| 79 | + false, |
| 80 | + ofDocument(BsonDocument.parse("{}")).eq(ofIntegerArray()), |
| 81 | + "{'$eq': [{'$literal': {}}, []]}"); |
| 82 | + |
| 83 | + // numbers are equal, even though of different types |
| 84 | + assertExpression( |
| 85 | + 1 == 1.0, |
| 86 | + of(1).eq(of(1.0)), |
| 87 | + "{'$eq': [1, 1.0]}"); |
| 88 | + assertExpression( |
| 89 | + 1 == 1L, |
| 90 | + of(1).eq(of(1L)), |
| 91 | + "{'$eq': [1, { '$numberLong': '1' }]}"); |
| 92 | + |
| 93 | + // ensure that no two samples are equal to each other |
| 94 | + for (int i = 0; i < sampleValues.size(); i++) { |
| 95 | + for (int j = 0; j < sampleValues.size(); j++) { |
| 96 | + if (i == j) { |
| 97 | + continue; |
| 98 | + } |
| 99 | + Expression first = sampleValues.get(i); |
| 100 | + Expression second = sampleValues.get(j); |
| 101 | + BsonValue evaluate = evaluate(first.eq(second)); |
| 102 | + if (evaluate.asBoolean().getValue()) { |
| 103 | + BsonValue v1 = ((MqlExpression<?>) first).toBsonValue(fromProviders(new BsonValueCodecProvider())); |
| 104 | + BsonValue v2 = ((MqlExpression<?>) second).toBsonValue(fromProviders(new BsonValueCodecProvider())); |
| 105 | + fail(i + "," + j + " --" + v1 + " and " + v2 + " should not equal"); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void neTest() { |
| 113 | + // https://www.mongodb.com/docs/manual/reference/operator/aggregation/ne/ |
| 114 | + assertExpression( |
| 115 | + 1 != 2, |
| 116 | + of(1).ne(of(2)), |
| 117 | + "{'$ne': [1, 2]}"); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void ltTest() { |
| 122 | + // https://www.mongodb.com/docs/manual/reference/operator/aggregation/lt/ |
| 123 | + assertExpression( |
| 124 | + -1 < 1, |
| 125 | + of(-1).lt(of(1)), |
| 126 | + "{'$lt': [-1, 1]}"); |
| 127 | + assertExpression( |
| 128 | + 0 < 0, |
| 129 | + of(0).lt(of(0)), |
| 130 | + "{'$lt': [0, 0]}"); |
| 131 | + |
| 132 | + assertExpression( |
| 133 | + true, |
| 134 | + ofNull().lt(of(0)), |
| 135 | + "{'$lt': [null, 0]}"); |
| 136 | + |
| 137 | + for (int i = 0; i < sampleValues.size() - 1; i++) { |
| 138 | + for (int j = i + 1; j < sampleValues.size(); j++) { |
| 139 | + Expression first = sampleValues.get(i); |
| 140 | + Expression second = sampleValues.get(j); |
| 141 | + BsonValue evaluate = evaluate(first.lt(second)); |
| 142 | + if (!evaluate.asBoolean().getValue()) { |
| 143 | + BsonValue v1 = ((MqlExpression<?>) first).toBsonValue(fromProviders(new BsonValueCodecProvider())); |
| 144 | + BsonValue v2 = ((MqlExpression<?>) second).toBsonValue(fromProviders(new BsonValueCodecProvider())); |
| 145 | + fail(i + "," + j + " --" + v1 + " < " + v2 + " should be true"); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void lteTest() { |
| 153 | + // https://www.mongodb.com/docs/manual/reference/operator/aggregation/lte/ |
| 154 | + assertExpression( |
| 155 | + -1 <= 1, |
| 156 | + of(-1).lte(of(1)), |
| 157 | + "{'$lte': [-1, 1]}"); |
| 158 | + assertExpression( |
| 159 | + 0 <= 0, |
| 160 | + of(0).lte(of(0)), |
| 161 | + "{'$lte': [0, 0]}"); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void gtTest() { |
| 166 | + // https://www.mongodb.com/docs/manual/reference/operator/aggregation/gt/ |
| 167 | + assertExpression( |
| 168 | + -1 > 1, |
| 169 | + of(-1).gt(of(1)), |
| 170 | + "{'$gt': [-1, 1]}"); |
| 171 | + assertExpression( |
| 172 | + 0 > 0, |
| 173 | + of(0).gt(of(0)), |
| 174 | + "{'$gt': [0, 0]}"); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + public void gteTest() { |
| 179 | + // https://www.mongodb.com/docs/manual/reference/operator/aggregation/gte/ |
| 180 | + assertExpression( |
| 181 | + -1 >= 1, |
| 182 | + of(-1).gte(of(1)), |
| 183 | + "{'$gte': [-1, 1]}"); |
| 184 | + assertExpression( |
| 185 | + 0 >= 0, |
| 186 | + of(0).gte(of(0)), |
| 187 | + "{'$gte': [0, 0]}"); |
| 188 | + } |
| 189 | + |
| 190 | +} |
0 commit comments