Skip to content

Commit 188d703

Browse files
committed
Scale double values to 4 decimal digits in geoNear test
This works around small variations in native math libraries used by the server. JAVA-5097
1 parent 4f5417f commit 188d703

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

driver-core/src/test/functional/com/mongodb/client/model/AggregatesTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.junit.jupiter.params.provider.Arguments;
3030
import org.junit.jupiter.params.provider.MethodSource;
3131

32+
import java.math.RoundingMode;
3233
import java.util.Arrays;
3334
import java.util.Collections;
3435
import java.util.List;
@@ -234,13 +235,13 @@ public void testGeoNear() {
234235
+ " 'coordinates' : [ -73.9928, 40.7193 ]\n"
235236
+ " },\n"
236237
+ " 'dist' : {\n"
237-
+ " 'calculated' : 9.539931676365992,\n"
238+
+ " 'calculated' : 9.5399,\n"
238239
+ " 'location' : {\n"
239240
+ " 'type' : 'Point',\n"
240241
+ " 'coordinates' : [ -73.9928, 40.7193 ]\n"
241242
+ " }\n"
242243
+ " }\n"
243-
+ "}]");
244+
+ "}]", 4, RoundingMode.FLOOR);
244245
}
245246

246247
@Test

driver-core/src/test/functional/com/mongodb/client/model/OperationTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import com.mongodb.lang.Nullable;
2424
import org.bson.BsonArray;
2525
import org.bson.BsonDocument;
26+
import org.bson.BsonDouble;
27+
import org.bson.BsonValue;
2628
import org.bson.Document;
2729
import org.bson.codecs.BsonDocumentCodec;
2830
import org.bson.codecs.DecoderContext;
@@ -31,9 +33,12 @@
3133
import org.junit.jupiter.api.AfterEach;
3234
import org.junit.jupiter.api.BeforeEach;
3335

36+
import java.math.BigDecimal;
37+
import java.math.RoundingMode;
3438
import java.util.ArrayList;
3539
import java.util.Collections;
3640
import java.util.List;
41+
import java.util.Map;
3742
import java.util.stream.Collectors;
3843

3944
import static com.mongodb.ClusterFixture.checkReferenceCountReachesTarget;
@@ -110,6 +115,37 @@ protected void assertResults(final List<Bson> pipeline, final String expectedRes
110115
assertEquals(expectedResults, results);
111116
}
112117

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+
113149
protected List<Object> aggregateWithWindowFields(@Nullable final Object partitionBy,
114150
final WindowOutputField output,
115151
final Bson sortSpecification) {

0 commit comments

Comments
 (0)