diff --git a/src/test/java/redis/clients/jedis/ClusterPipeliningTest.java b/src/test/java/redis/clients/jedis/ClusterPipeliningTest.java index 2edcb67456..8b28e7b436 100644 --- a/src/test/java/redis/clients/jedis/ClusterPipeliningTest.java +++ b/src/test/java/redis/clients/jedis/ClusterPipeliningTest.java @@ -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.*; @@ -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; @@ -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 values = new ArrayList<>(); - values.add(new GeoCoordinate(2.19093829393386841, 41.43379028184083523)); - values.add(new GeoCoordinate(2.18737632036209106, 41.40634178640635099)); List hashValues = new ArrayList<>(); hashValues.add("sp3e9yg3kd0"); @@ -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); @@ -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")); diff --git a/src/test/java/redis/clients/jedis/commands/jedis/GeoCommandsTest.java b/src/test/java/redis/clients/jedis/commands/jedis/GeoCommandsTest.java index be00a4d665..2b9bb98d2a 100644 --- a/src/test/java/redis/clients/jedis/commands/jedis/GeoCommandsTest.java +++ b/src/test/java/redis/clients/jedis/commands/jedis/GeoCommandsTest.java @@ -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; @@ -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 diff --git a/src/test/java/redis/clients/jedis/commands/unified/GeoCommandsTestBase.java b/src/test/java/redis/clients/jedis/commands/unified/GeoCommandsTestBase.java index e05a5f99cf..14647a880d 100644 --- a/src/test/java/redis/clients/jedis/commands/unified/GeoCommandsTestBase.java +++ b/src/test/java/redis/clients/jedis/commands/unified/GeoCommandsTestBase.java @@ -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; @@ -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 diff --git a/src/test/java/redis/clients/jedis/commands/unified/pipeline/GeoPipelineCommandsTest.java b/src/test/java/redis/clients/jedis/commands/unified/pipeline/GeoPipelineCommandsTest.java index d8b7443a8f..143bd08c2c 100644 --- a/src/test/java/redis/clients/jedis/commands/unified/pipeline/GeoPipelineCommandsTest.java +++ b/src/test/java/redis/clients/jedis/commands/unified/pipeline/GeoPipelineCommandsTest.java @@ -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; @@ -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; @@ -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 )); } @@ -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)); @@ -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)); } @@ -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)); } @@ -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)); } @@ -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)); } @@ -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)); } @@ -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)); } @@ -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)); } @@ -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)); @@ -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)); } diff --git a/src/test/java/redis/clients/jedis/util/GeoCoordinateMatcher.java b/src/test/java/redis/clients/jedis/util/GeoCoordinateMatcher.java index 5b21d3d427..caf82d99c5 100644 --- a/src/test/java/redis/clients/jedis/util/GeoCoordinateMatcher.java +++ b/src/test/java/redis/clients/jedis/util/GeoCoordinateMatcher.java @@ -6,29 +6,53 @@ public class GeoCoordinateMatcher extends TypeSafeMatcher { - 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); } } diff --git a/src/test/java/redis/clients/jedis/util/GeoRadiusResponseMatcher.java b/src/test/java/redis/clients/jedis/util/GeoRadiusResponseMatcher.java new file mode 100644 index 0000000000..4ebe9ad67f --- /dev/null +++ b/src/test/java/redis/clients/jedis/util/GeoRadiusResponseMatcher.java @@ -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 { + private final GeoRadiusResponse expected; + private final double coordinateTolerance; + + public static Matcher isEqualToGeoRadiusResponse(GeoRadiusResponse expected) { + return new GeoRadiusResponseMatcher(expected, GeoCoordinateMatcher.DEFAULT_TOLERANCE); + } + + public static Matcher 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); + } + +} \ No newline at end of file