|
| 1 | +/* |
| 2 | + * Copyright <2021> Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 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 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + * |
| 15 | + */ |
| 16 | + |
| 17 | +package software.amazon.documentdb.jdbc.query; |
| 18 | + |
| 19 | +import org.bson.BsonDocument; |
| 20 | +import org.junit.jupiter.api.Assertions; |
| 21 | +import org.junit.jupiter.api.DisplayName; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | +import java.util.stream.Collectors; |
| 26 | + |
| 27 | +public class DocumentDbMqlQueryContextTest { |
| 28 | + |
| 29 | + @Test |
| 30 | + @DisplayName("Tests that aggregate operations are correctly converted from BSON document to string in getter.") |
| 31 | + void testGetAggregateOperationsAsStrings() { |
| 32 | + final List<String> stages = new ArrayList<>(); |
| 33 | + // Generic stage |
| 34 | + stages.add( |
| 35 | + "{\"$unwind\": {" |
| 36 | + + "\"path\": \"$array\", " |
| 37 | + + "\"includeArrayIndex\": \"array_index_lvl_0\", " |
| 38 | + + "\"preserveNullAndEmptyArrays\": true}}"); |
| 39 | + // Stage with 3-valued logic (many null checks) |
| 40 | + stages.add( |
| 41 | + "{\"$project\": {" |
| 42 | + + "\"booleanField\": " |
| 43 | + + "{\"$cond\": [{\"$and\": [{\"$gt\": [\"$array.field\", null]}, " |
| 44 | + + "{\"$gt\": [\"$array.field2\", null]}]}, " |
| 45 | + + "{\"$eq\": [\"$array.field\", \"$array.field2\"]}, null]}}}"); |
| 46 | + // Stage with different Bson types |
| 47 | + stages.add( |
| 48 | + "{\"$project\": {" |
| 49 | + + "\"literalNull\": {\"$literal\": null}, " |
| 50 | + + "\"literalTimestamp\": {\"$date\": {\"$numberLong\": \"1505938660000\"}}, " |
| 51 | + + "\"literalInt\": {\"$literal\": {\"$numberInt\": \"-2147483648\"}}, " |
| 52 | + + "\"literalDecimal\": {\"$literal\": {\"$numberDouble\": \"123.45\"}}, " |
| 53 | + + "\"literalVarchar\": {\"$literal\": \"Hello! 你好!\"}, " |
| 54 | + + "\"literalBinary\": {\"$binary\": {\"base64\": \"RfCr\", \"subType\": \"00\"}}}}"); |
| 55 | + // Stage with a lot of nesting and aggregate operators |
| 56 | + stages.add( |
| 57 | + "{\"$project\": {\"EXPR$0\": {\"$substrCP\": [\"$array.field\", " |
| 58 | + + "{\"$subtract\": [\"$array.field2\", {\"$numberInt\": \"1\"}]}, " |
| 59 | + + "{\"$subtract\": [\"$array.field1\", \"$array.field2\"]}]}, " |
| 60 | + + "\"_id\": {\"$numberInt\": \"0\"}}}"); |
| 61 | + |
| 62 | + final DocumentDbMqlQueryContext context = |
| 63 | + DocumentDbMqlQueryContext.builder() |
| 64 | + .aggregateOperations( |
| 65 | + stages.stream().map(BsonDocument::parse).collect(Collectors.toList())) |
| 66 | + .build(); |
| 67 | + Assertions.assertEquals(stages.size(), context.getAggregateOperationsAsStrings().size()); |
| 68 | + for (int i = 0; i < stages.size(); i++) { |
| 69 | + Assertions.assertEquals(stages.get(i), context.getAggregateOperationsAsStrings().get(i)); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments