|
23 | 23 | import com.mongodb.lang.Nullable;
|
24 | 24 | import org.bson.BsonArray;
|
25 | 25 | import org.bson.BsonDocument;
|
| 26 | +import org.bson.BsonDouble; |
| 27 | +import org.bson.BsonValue; |
26 | 28 | import org.bson.Document;
|
27 | 29 | import org.bson.codecs.BsonDocumentCodec;
|
28 | 30 | import org.bson.codecs.DecoderContext;
|
|
31 | 33 | import org.junit.jupiter.api.AfterEach;
|
32 | 34 | import org.junit.jupiter.api.BeforeEach;
|
33 | 35 |
|
| 36 | +import java.math.BigDecimal; |
| 37 | +import java.math.RoundingMode; |
34 | 38 | import java.util.ArrayList;
|
35 | 39 | import java.util.Collections;
|
36 | 40 | import java.util.List;
|
| 41 | +import java.util.Map; |
37 | 42 | import java.util.stream.Collectors;
|
38 | 43 |
|
39 | 44 | import static com.mongodb.ClusterFixture.checkReferenceCountReachesTarget;
|
@@ -110,6 +115,37 @@ protected void assertResults(final List<Bson> pipeline, final String expectedRes
|
110 | 115 | assertEquals(expectedResults, results);
|
111 | 116 | }
|
112 | 117 |
|
| 118 | + protected void assertResults(final List<Bson> pipeline, final String expectedResultsAsString, |
| 119 | + final int scale, final RoundingMode roundingMode) { |
| 120 | + List<BsonDocument> expectedResults = parseToList(expectedResultsAsString); |
| 121 | + List<BsonDocument> results = getCollectionHelper().aggregate(pipeline); |
| 122 | + assertEquals(adjustScale(expectedResults, scale, roundingMode), adjustScale(results, scale, roundingMode)); |
| 123 | + } |
| 124 | + |
| 125 | + private static List<BsonDocument> adjustScale(final List<BsonDocument> documents, final int scale, final RoundingMode roundingMode) { |
| 126 | + documents.replaceAll(value -> adjustScale(value, scale, roundingMode).asDocument()); |
| 127 | + return documents; |
| 128 | + } |
| 129 | + |
| 130 | + private static BsonValue adjustScale(final BsonValue value, final int scale, final RoundingMode roundingMode) { |
| 131 | + if (value.isDouble()) { |
| 132 | + double scaledDoubleValue = BigDecimal.valueOf(value.asDouble().doubleValue()) |
| 133 | + .setScale(scale, roundingMode) |
| 134 | + .doubleValue(); |
| 135 | + return new BsonDouble(scaledDoubleValue); |
| 136 | + } else if (value.isDocument()) { |
| 137 | + for (Map.Entry<String, BsonValue> entry : value.asDocument().entrySet()) { |
| 138 | + entry.setValue(adjustScale(entry.getValue(), scale, roundingMode)); |
| 139 | + } |
| 140 | + } else if (value.isArray()) { |
| 141 | + BsonArray array = value.asArray(); |
| 142 | + for (int i = 0; i < array.size(); i++) { |
| 143 | + array.set(i, adjustScale(array.get(i), scale, roundingMode)); |
| 144 | + } |
| 145 | + } |
| 146 | + return value; |
| 147 | + } |
| 148 | + |
113 | 149 | protected List<Object> aggregateWithWindowFields(@Nullable final Object partitionBy,
|
114 | 150 | final WindowOutputField output,
|
115 | 151 | final Bson sortSpecification) {
|
|
0 commit comments