@@ -71,6 +71,7 @@ public void passToTest() {
71
71
72
72
@ Test
73
73
public void switchTest () {
74
+ assumeTrue (serverVersionAtLeast (4 , 4 )); // isNumber
74
75
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/switch/
75
76
assertExpression ("a" , of (0 ).switchOn (on -> on .is (v -> v .eq (of (0 )), v -> of ("a" ))));
76
77
assertExpression ("a" , of (0 ).switchOn (on -> on .isNumber (v -> of ("a" ))));
@@ -185,13 +186,6 @@ public void switchTestInitial() {
185
186
assertExpression ("A" ,
186
187
of (true ).switchOn (on -> on .isBoolean (v -> of ("A" ))),
187
188
"{'$switch': {'branches': [{'case': {'$eq': [{'$type': [true]}, 'bool']}, 'then': 'A'}]}}" );
188
- assertExpression ("A" ,
189
- of (1 ).switchOn (on -> on .isNumber (v -> of ("A" ))),
190
- "{'$switch': {'branches': [{'case': {'$isNumber': [1]}, 'then': 'A'}]}}" );
191
- assertExpression ("A" ,
192
- of (1 ).switchOn (on -> on .isInteger (v -> of ("A" ))),
193
- "{'$switch': {'branches': [{'case': {'$switch': {'branches': [{'case': {'$isNumber': [1]},"
194
- + "'then': {'$eq': [{'$round': 1}, 1]}}], 'default': false}}, 'then': 'A'}]}}" );
195
189
assertExpression ("A" ,
196
190
of ("x" ).switchOn (on -> on .isString (v -> of ("A" ))),
197
191
"{'$switch': {'branches': [{'case': {'$eq': [{'$type': ['x']}, 'string']}, 'then': 'A'}]}}" );
@@ -215,6 +209,35 @@ public void switchTestInitial() {
215
209
"{'$switch': {'branches': [{'case': {'$eq': [null, null]}, 'then': 'A'}]}}" );
216
210
}
217
211
212
+ @ Test
213
+ public void switchTestInitialVersion44 () {
214
+ assumeTrue (serverVersionAtLeast (4 , 4 ));
215
+ assertExpression ("A" ,
216
+ of (1 ).switchOn (on -> on .isNumber (v -> of ("A" ))),
217
+ "{'$switch': {'branches': [{'case': {'$isNumber': [1]}, 'then': 'A'}]}}" );
218
+ assertExpression ("A" ,
219
+ of (1 ).switchOn (on -> on .isInteger (v -> of ("A" ))),
220
+ "{'$switch': {'branches': [{'case': {'$switch': {'branches': [{'case': {'$isNumber': [1]},"
221
+ + "'then': {'$eq': [{'$round': 1}, 1]}}], 'default': false}}, 'then': 'A'}]}}" );
222
+ }
223
+ @ Test
224
+ public void switchTestPartialVersion44 () {
225
+ assumeTrue (serverVersionAtLeast (4 , 4 ));
226
+ assertExpression ("A" ,
227
+ of (1 ).switchOn (on -> on .isNull (v -> of ("X" )).isNumber (v -> of ("A" ))),
228
+ "{'$switch': {'branches': [{'case': {'$eq': [1, null]}, 'then': 'X'}, "
229
+ + "{'case': {'$isNumber': [1]}, 'then': 'A'}]}}" );
230
+ assertExpression ("A" ,
231
+ of (1 ).switchOn (on -> on .isNull (v -> of ("X" )).isInteger (v -> of ("A" ))),
232
+ "{'$switch': {'branches': [{'case': {'$eq': [1, null]}, 'then': 'X'}, {'case': "
233
+ + "{'$switch': {'branches': [{'case': {'$isNumber': [1]}, "
234
+ + "'then': {'$eq': [{'$round': 1}, 1]}}], 'default': false}}, 'then': 'A'}]}}" );
235
+ assertExpression ("A" ,
236
+ ofNull ().switchOn (on -> on .isNumber (v -> of ("X" )).isNull (v -> of ("A" ))),
237
+ "{'$switch': {'branches': [{'case': {'$isNumber': [null]}, 'then': 'X'}, "
238
+ + "{'case': {'$eq': [null, null]}, 'then': 'A'}]}}" );
239
+ }
240
+
218
241
@ Test
219
242
public void switchTestPartial () {
220
243
assertExpression ("A" ,
@@ -243,15 +266,6 @@ public void switchTestPartial() {
243
266
of (true ).switchOn (on -> on .isNull (v -> of ("X" )).isBoolean (v -> of ("A" ))),
244
267
"{'$switch': {'branches': [{'case': {'$eq': [true, null]}, 'then': 'X'}, "
245
268
+ "{'case': {'$eq': [{'$type': [true]}, 'bool']}, 'then': 'A'}]}}" );
246
- assertExpression ("A" ,
247
- of (1 ).switchOn (on -> on .isNull (v -> of ("X" )).isNumber (v -> of ("A" ))),
248
- "{'$switch': {'branches': [{'case': {'$eq': [1, null]}, 'then': 'X'}, "
249
- + "{'case': {'$isNumber': [1]}, 'then': 'A'}]}}" );
250
- assertExpression ("A" ,
251
- of (1 ).switchOn (on -> on .isNull (v -> of ("X" )).isInteger (v -> of ("A" ))),
252
- "{'$switch': {'branches': [{'case': {'$eq': [1, null]}, 'then': 'X'}, {'case': "
253
- + "{'$switch': {'branches': [{'case': {'$isNumber': [1]}, "
254
- + "'then': {'$eq': [{'$round': 1}, 1]}}], 'default': false}}, 'then': 'A'}]}}" );
255
269
assertExpression ("A" ,
256
270
of ("x" ).switchOn (on -> on .isNull (v -> of ("X" )).isString (v -> of ("A" ))),
257
271
"{'$switch': {'branches': [{'case': {'$eq': ['x', null]}, 'then': 'X'}, "
@@ -274,9 +288,5 @@ public void switchTestPartial() {
274
288
ofMap (Document .parse ("{}" )).switchOn (on -> on .isNull (v -> of ("X" )).isMap (v -> of ("A" ))),
275
289
"{'$switch': {'branches': [{'case': {'$eq': [{'$literal': {}}, null]}, 'then': 'X'}, "
276
290
+ "{'case': {'$eq': [{'$type': [{'$literal': {}}]}, 'object']}, 'then': 'A'}]}}" );
277
- assertExpression ("A" ,
278
- ofNull ().switchOn (on -> on .isNumber (v -> of ("X" )).isNull (v -> of ("A" ))),
279
- "{'$switch': {'branches': [{'case': {'$isNumber': [null]}, 'then': 'X'}, "
280
- + "{'case': {'$eq': [null, null]}, 'then': 'A'}]}}" );
281
291
}
282
292
}
0 commit comments