Skip to content

Commit

Permalink
Fix GeoXXX tests
Browse files Browse the repository at this point in the history
java.lang.AssertionError:
Expected :[(2.1909382939338684,41.433790281840835), (2.187376320362091,41.40634178640635)]
Actual   :[(2.1909382939338684,41.43379028184083), (2.187376320362091,41.40634178640635)]

Compare with tolerance to avoid error on returned precision on mac
  • Loading branch information
ggivo committed Nov 10, 2024
1 parent 2059849 commit 68c372a
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 43 deletions.
31 changes: 20 additions & 11 deletions src/test/java/redis/clients/jedis/ClusterPipeliningTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package redis.clients.jedis;

import static org.hamcrest.Matchers.contains;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.*;
import static redis.clients.jedis.Protocol.CLUSTER_HASHSLOTS;
import static redis.clients.jedis.util.GeoRadiusResponseMatcher.isEqualToGeoRadiusResponse;

import java.util.*;

Expand All @@ -21,6 +24,7 @@
import redis.clients.jedis.resps.StreamEntry;
import redis.clients.jedis.resps.Tuple;
import redis.clients.jedis.util.AssertUtil;
import redis.clients.jedis.util.GeoCoordinateMatcher;
import redis.clients.jedis.util.JedisClusterTestUtil;
import redis.clients.jedis.util.SafeEncoder;

Expand Down Expand Up @@ -693,9 +697,6 @@ public void clusterPipelineGeo() {
hm.put("place1", new GeoCoordinate(2.1909389952632, 41.433791470673));
hm.put("place2", new GeoCoordinate(2.1873744593677, 41.406342043777));

List<GeoCoordinate> values = new ArrayList<>();
values.add(new GeoCoordinate(2.19093829393386841, 41.43379028184083523));
values.add(new GeoCoordinate(2.18737632036209106, 41.40634178640635099));

List<String> hashValues = new ArrayList<>();
hashValues.add("sp3e9yg3kd0");
Expand All @@ -706,11 +707,6 @@ public void clusterPipelineGeo() {
GeoRadiusParam params2 = new GeoRadiusParam().count(1, true);
GeoRadiusStoreParam storeParams = new GeoRadiusStoreParam().store("radius{#}");

GeoRadiusResponse expectedResponse = new GeoRadiusResponse("place1".getBytes());
expectedResponse.setCoordinate(new GeoCoordinate(2.19093829393386841, 41.43379028184083523));
expectedResponse.setDistance(0.0881);
expectedResponse.setRawScore(3471609698139488L);

ClusterConnectionProvider provider = new ClusterConnectionProvider(nodes, DEFAULT_CLIENT_CONFIG);
ClusterPipeline p = new ClusterPipeline(provider);

Expand Down Expand Up @@ -738,11 +734,24 @@ public void clusterPipelineGeo() {
assertEquals(Double.valueOf(3067.4157), r2.get());
assertEquals(Double.valueOf(3.0674), r3.get());
assertEquals(hashValues, r4.get());
assertEquals(values, r5.get());
assertThat(r5.get(), contains(
GeoCoordinateMatcher.isEqualWithTolerance(2.19093829393386841, 41.43379028184083523),
GeoCoordinateMatcher.isEqualWithTolerance(2.18737632036209106, 41.40634178640635099))
);
assertTrue(r6.get().size() == 1 && r6.get().get(0).getMemberByString().equals("place1"));
assertTrue(r7.get().size() == 1 && r7.get().get(0).getMemberByString().equals("place1"));
assertEquals(expectedResponse, r8.get().get(0));
assertEquals(expectedResponse, r9.get().get(0));


GeoRadiusResponse expectedResponse = new GeoRadiusResponse("place1".getBytes());
expectedResponse.setCoordinate(new GeoCoordinate(2.19093829393386841, 41.43379028184083523));
expectedResponse.setDistance(0.0881);
expectedResponse.setRawScore(3471609698139488L);

assertThat(r8.get().get(0),isEqualToGeoRadiusResponse(expectedResponse));
assertThat(r9.get().get(0),isEqualToGeoRadiusResponse(expectedResponse));



assertEquals(Long.valueOf(1), r10.get());
assertTrue(r11.get().size() == 1 && r11.get().contains("place1"));
assertTrue(r12.get().size() == 2 && r12.get().get(0).getMemberByString().equals("place2"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.Assert.*;
import static redis.clients.jedis.util.AssertUtil.assertByteArrayListEquals;
import static redis.clients.jedis.util.GeoCoordinateMatcher.isEqualWithTolerance;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -532,7 +533,7 @@ public void geosearch() {
assertEquals(1, members.size());
assertEquals("place1", members.get(0).getMemberByString());
assertEquals(0.0881, members.get(0).getDistance(), 10);
assertEquals(new GeoCoordinate(2.19093829393386841, 41.43379028184083523), members.get(0).getCoordinate());
assertThat(members.get(0).getCoordinate(), isEqualWithTolerance( 2.19093829393386841, 41.43379028184083523));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.Assert.*;
import static redis.clients.jedis.util.AssertUtil.assertByteArrayListEquals;
import static redis.clients.jedis.util.GeoCoordinateMatcher.isEqualWithTolerance;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -529,7 +530,7 @@ public void geosearch() {
assertEquals(1, members.size());
assertEquals("place1", members.get(0).getMemberByString());
assertEquals(0.0881, members.get(0).getDistance(), 10);
assertEquals(new GeoCoordinate(2.19093829393386841, 41.43379028184083523), members.get(0).getCoordinate());
assertThat(members.get(0).getCoordinate(), isEqualWithTolerance(2.19093829393386841, 41.43379028184083523));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals;
import static redis.clients.jedis.util.GeoCoordinateMatcher.atCoordinates;
import static redis.clients.jedis.util.GeoCoordinateMatcher.isEqualWithTolerance;

import java.util.HashMap;
import java.util.List;
Expand All @@ -23,7 +22,6 @@
import redis.clients.jedis.RedisProtocol;
import redis.clients.jedis.Response;
import redis.clients.jedis.args.GeoUnit;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.params.GeoAddParams;
import redis.clients.jedis.params.GeoRadiusParam;
import redis.clients.jedis.params.GeoRadiusStoreParam;
Expand Down Expand Up @@ -181,14 +179,14 @@ public void geopos() {
pipe.sync();

assertThat(coordinates.get(), contains(
atCoordinates(3.0, 4.0),
atCoordinates(2.0, 3.0),
isEqualWithTolerance(3.0, 4.0),
isEqualWithTolerance(2.0, 3.0),
null
));

assertThat(bcoordinates.get(), contains(
atCoordinates(3.0, 4.0),
atCoordinates(2.0, 3.0),
isEqualWithTolerance(3.0, 4.0),
isEqualWithTolerance(2.0, 3.0),
null
));
}
Expand Down Expand Up @@ -257,7 +255,7 @@ public void georadius() {
assertThat(members4.get().stream().map(GeoRadiusResponse::getDistance).collect(Collectors.toList()),
contains(closeTo(56.4413, EPSILON)));
assertThat(members4.get().stream().map(GeoRadiusResponse::getCoordinate).collect(Collectors.toList()),
contains(atCoordinates(15.087269, 37.502669)));
contains(isEqualWithTolerance(15.087269, 37.502669)));
assertThat(members4.get().stream().map(GeoRadiusResponse::getRawScore).collect(Collectors.toList()),
contains(3479447370796909L));

Expand Down Expand Up @@ -356,7 +354,7 @@ public void georadiusReadonly() {
assertThat(members4.get().stream().map(GeoRadiusResponse::getDistance).collect(Collectors.toList()),
contains(closeTo(56.4413, EPSILON)));
assertThat(members4.get().stream().map(GeoRadiusResponse::getCoordinate).collect(Collectors.toList()),
contains(atCoordinates(15.087269, 37.502669)));
contains(isEqualWithTolerance(15.087269, 37.502669)));
assertThat(members4.get().stream().map(GeoRadiusResponse::getRawScore).collect(Collectors.toList()),
contains(0L));
}
Expand Down Expand Up @@ -417,7 +415,7 @@ public void georadiusBinary() {
assertThat(members4.get().stream().map(GeoRadiusResponse::getDistance).collect(Collectors.toList()),
contains(closeTo(56.4413, EPSILON)));
assertThat(members4.get().stream().map(GeoRadiusResponse::getCoordinate).collect(Collectors.toList()),
contains(atCoordinates(15.087269, 37.502669)));
contains(isEqualWithTolerance(15.087269, 37.502669)));
assertThat(members4.get().stream().map(GeoRadiusResponse::getRawScore).collect(Collectors.toList()),
contains(0L));
}
Expand Down Expand Up @@ -498,7 +496,7 @@ public void georadiusReadonlyBinary() {
assertThat(members4.get().stream().map(GeoRadiusResponse::getDistance).collect(Collectors.toList()),
contains(closeTo(56.4413, EPSILON)));
assertThat(members4.get().stream().map(GeoRadiusResponse::getCoordinate).collect(Collectors.toList()),
contains(atCoordinates(15.087269, 37.502669)));
contains(isEqualWithTolerance(15.087269, 37.502669)));
assertThat(members4.get().stream().map(GeoRadiusResponse::getRawScore).collect(Collectors.toList()),
contains(0L));
}
Expand Down Expand Up @@ -543,7 +541,7 @@ public void georadiusByMember() {
assertThat(members3.get().stream().map(GeoRadiusResponse::getDistance).collect(Collectors.toList()),
contains(closeTo(0.0, EPSILON)));
assertThat(members3.get().stream().map(GeoRadiusResponse::getCoordinate).collect(Collectors.toList()),
contains(atCoordinates(13.583333, 37.316667)));
contains(isEqualWithTolerance(13.583333, 37.316667)));
assertThat(members3.get().stream().map(GeoRadiusResponse::getRawScore).collect(Collectors.toList()),
contains(0L));
}
Expand Down Expand Up @@ -606,7 +604,7 @@ public void georadiusByMemberReadonly() {
assertThat(members3.get().stream().map(GeoRadiusResponse::getDistance).collect(Collectors.toList()),
contains(closeTo(0.0, EPSILON)));
assertThat(members3.get().stream().map(GeoRadiusResponse::getCoordinate).collect(Collectors.toList()),
contains(atCoordinates(13.583333, 37.316667)));
contains(isEqualWithTolerance(13.583333, 37.316667)));
assertThat(members3.get().stream().map(GeoRadiusResponse::getRawScore).collect(Collectors.toList()),
contains(0L));
}
Expand Down Expand Up @@ -650,7 +648,7 @@ public void georadiusByMemberBinary() {
assertThat(members3.get().stream().map(GeoRadiusResponse::getDistance).collect(Collectors.toList()),
contains(closeTo(0.0, EPSILON)));
assertThat(members3.get().stream().map(GeoRadiusResponse::getCoordinate).collect(Collectors.toList()),
contains(atCoordinates(13.583333, 37.316667)));
contains(isEqualWithTolerance(13.583333, 37.316667)));
assertThat(members3.get().stream().map(GeoRadiusResponse::getRawScore).collect(Collectors.toList()),
contains(0L));
}
Expand Down Expand Up @@ -712,7 +710,7 @@ public void georadiusByMemberReadonlyBinary() {
assertThat(members3.get().stream().map(GeoRadiusResponse::getDistance).collect(Collectors.toList()),
contains(closeTo(0.0, EPSILON)));
assertThat(members3.get().stream().map(GeoRadiusResponse::getCoordinate).collect(Collectors.toList()),
contains(atCoordinates(13.583333, 37.316667)));
contains(isEqualWithTolerance(13.583333, 37.316667)));
assertThat(members3.get().stream().map(GeoRadiusResponse::getRawScore).collect(Collectors.toList()),
contains(0L));
}
Expand Down Expand Up @@ -775,7 +773,7 @@ public void geosearch() {
assertThat(members4.get().stream().map(GeoRadiusResponse::getDistance).collect(Collectors.toList()),
contains(closeTo(0.0, EPSILON), closeTo(3.0674, EPSILON)));
assertThat(members4.get().stream().map(GeoRadiusResponse::getCoordinate).collect(Collectors.toList()),
contains(atCoordinates(2.1909389952632d, 41.433791470673d), atCoordinates(2.1873744593677d, 41.406342043777d)));
contains(isEqualWithTolerance(2.1909389952632d, 41.433791470673d), isEqualWithTolerance(2.1873744593677d, 41.406342043777d)));
assertThat(members4.get().stream().map(GeoRadiusResponse::getRawScore).collect(Collectors.toList()),
contains(3471609698139488L, 3471609625421029L));

Expand All @@ -793,7 +791,7 @@ public void geosearch() {
assertThat(members8.get().stream().map(GeoRadiusResponse::getDistance).collect(Collectors.toList()),
contains(closeTo(0.0881, EPSILON)));
assertThat(members8.get().stream().map(GeoRadiusResponse::getCoordinate).collect(Collectors.toList()),
contains(atCoordinates(2.1909389952632d, 41.433791470673d)));
contains(isEqualWithTolerance(2.1909389952632d, 41.433791470673d)));
assertThat(members8.get().stream().map(GeoRadiusResponse::getRawScore).collect(Collectors.toList()),
contains(0L));
}
Expand Down
50 changes: 37 additions & 13 deletions src/test/java/redis/clients/jedis/util/GeoCoordinateMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,53 @@

public class GeoCoordinateMatcher extends TypeSafeMatcher<GeoCoordinate> {

public static GeoCoordinateMatcher atCoordinates(double longitude, double latitude) {
return new GeoCoordinateMatcher(longitude, latitude);
public static GeoCoordinateMatcher isEqualWithTolerance(GeoCoordinate expected, double tolerance) {
return new GeoCoordinateMatcher(expected, tolerance);
}

private static final double EPSILON = 1e-5;
public static GeoCoordinateMatcher isEqualWithTolerance(GeoCoordinate expected) {
return new GeoCoordinateMatcher(expected, DEFAULT_TOLERANCE);
}

public static GeoCoordinateMatcher isEqualWithTolerance(double longitude, double latitude, double tolerance) {
return new GeoCoordinateMatcher(new GeoCoordinate(longitude, latitude), tolerance);
}

public static GeoCoordinateMatcher isEqualWithTolerance(double longitude, double latitude) {
return new GeoCoordinateMatcher(new GeoCoordinate(longitude,latitude), latitude);
}

public static final double DEFAULT_TOLERANCE = 1e-14;

private final double longitude;
private final double latitude;
private final double tolerance;
private final GeoCoordinate expected;

public GeoCoordinateMatcher(double longitude, double latitude) {
this.longitude = longitude;
this.latitude = latitude;

public GeoCoordinateMatcher(GeoCoordinate expected, double tolerance) {
this.expected = expected;
this.tolerance = tolerance;
}

public GeoCoordinateMatcher(GeoCoordinate expected) {
this(expected, DEFAULT_TOLERANCE);
}

@Override
protected boolean matchesSafely(GeoCoordinate item) {
return item != null &&
Math.abs(longitude - item.getLongitude()) < EPSILON &&
Math.abs(latitude - item.getLatitude()) < EPSILON;
protected boolean matchesSafely(GeoCoordinate actual) {
return Math.abs(actual.getLatitude() - expected.getLatitude()) < tolerance &&
Math.abs(actual.getLongitude() - expected.getLongitude()) < tolerance;
}

@Override
public void describeTo(Description description) {
description.appendText("matches " + longitude + " longitude " + latitude + " latitude with precision " + EPSILON);
description.appendText("a GeoCoordinate within ")
.appendValue(tolerance)
.appendText(" of ")
.appendValue(expected);
}

@Override
protected void describeMismatchSafely(GeoCoordinate actual, Description mismatchDescription) {
mismatchDescription.appendText("was ").appendValue(actual);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package redis.clients.jedis.util;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import redis.clients.jedis.GeoCoordinate;
import redis.clients.jedis.resps.GeoRadiusResponse;

public class GeoRadiusResponseMatcher extends TypeSafeMatcher<GeoRadiusResponse> {
private final GeoRadiusResponse expected;
private final double coordinateTolerance;

public static Matcher<GeoRadiusResponse> isEqualToGeoRadiusResponse(GeoRadiusResponse expected) {
return new GeoRadiusResponseMatcher(expected, GeoCoordinateMatcher.DEFAULT_TOLERANCE);
}

public static Matcher<GeoRadiusResponse> isEqualToGeoRadiusResponse(GeoRadiusResponse expected, double tolerance) {
return new GeoRadiusResponseMatcher(expected, tolerance);
}

public GeoRadiusResponseMatcher(GeoRadiusResponse expected, double coordinateTolerance) {
this.expected = expected;
this.coordinateTolerance = coordinateTolerance;
}

@Override
protected boolean matchesSafely(GeoRadiusResponse actual) {
// Check if coordinates match within the tolerance
GeoCoordinate expectedCoord = expected.getCoordinate();
GeoCoordinate actualCoord = actual.getCoordinate();
if (!GeoCoordinateMatcher.isEqualWithTolerance(expectedCoord, coordinateTolerance).matches(actualCoord)) {
return false;
}

// Check if distance and rawScore match exactly
if (Double.compare(expected.getDistance(), actual.getDistance()) != 0) {
return false;
}
return expected.getRawScore() == actual.getRawScore();
}

@Override
public void describeTo(Description description) {
description.appendText("a GeoRadiusResponse with coordinate ")
.appendValue(expected.getCoordinate())
.appendText(", distance ")
.appendValue(expected.getDistance())
.appendText(", and rawScore ")
.appendValue(expected.getRawScore());
}

@Override
protected void describeMismatchSafely(GeoRadiusResponse actual, Description mismatchDescription) {
mismatchDescription.appendText("was ")
.appendValue(actual);
}

}

0 comments on commit 68c372a

Please sign in to comment.