Skip to content

Scale double values to 4 decimal digits in geoNear test #1197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.math.RoundingMode;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -234,13 +235,13 @@ public void testGeoNear() {
+ " 'coordinates' : [ -73.9928, 40.7193 ]\n"
+ " },\n"
+ " 'dist' : {\n"
+ " 'calculated' : 9.539931676365992,\n"
+ " 'calculated' : 9.5399,\n"
+ " 'location' : {\n"
+ " 'type' : 'Point',\n"
+ " 'coordinates' : [ -73.9928, 40.7193 ]\n"
+ " }\n"
+ " }\n"
+ "}]");
+ "}]", 4, RoundingMode.FLOOR);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.mongodb.lang.Nullable;
import org.bson.BsonArray;
import org.bson.BsonDocument;
import org.bson.BsonDouble;
import org.bson.BsonValue;
import org.bson.Document;
import org.bson.codecs.BsonDocumentCodec;
import org.bson.codecs.DecoderContext;
Expand All @@ -31,9 +33,12 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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

protected void assertResults(final List<Bson> pipeline, final String expectedResultsAsString,
final int scale, final RoundingMode roundingMode) {
List<BsonDocument> expectedResults = parseToList(expectedResultsAsString);
List<BsonDocument> results = getCollectionHelper().aggregate(pipeline);
assertEquals(adjustScale(expectedResults, scale, roundingMode), adjustScale(results, scale, roundingMode));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Another option would have been to perform recursive equality, with an epsilon on floating points. But I don't think we'll have an issue in these tests since the decimal places aren't close to being 0 (which would make the flooring flaky).

}

private static List<BsonDocument> adjustScale(final List<BsonDocument> documents, final int scale, final RoundingMode roundingMode) {
documents.replaceAll(value -> adjustScale(value, scale, roundingMode).asDocument());
return documents;
}

private static BsonValue adjustScale(final BsonValue value, final int scale, final RoundingMode roundingMode) {
if (value.isDouble()) {
double scaledDoubleValue = BigDecimal.valueOf(value.asDouble().doubleValue())
.setScale(scale, roundingMode)
.doubleValue();
return new BsonDouble(scaledDoubleValue);
} else if (value.isDocument()) {
for (Map.Entry<String, BsonValue> entry : value.asDocument().entrySet()) {
entry.setValue(adjustScale(entry.getValue(), scale, roundingMode));
}
} else if (value.isArray()) {
BsonArray array = value.asArray();
for (int i = 0; i < array.size(); i++) {
array.set(i, adjustScale(array.get(i), scale, roundingMode));
}
}
return value;
}

protected List<Object> aggregateWithWindowFields(@Nullable final Object partitionBy,
final WindowOutputField output,
final Bson sortSpecification) {
Expand Down