Skip to content

Commit 68c372a

Browse files
committed
Fix GeoXXX tests
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
1 parent 2059849 commit 68c372a

File tree

6 files changed

+134
-43
lines changed

6 files changed

+134
-43
lines changed

src/test/java/redis/clients/jedis/ClusterPipeliningTest.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package redis.clients.jedis;
22

3+
import static org.hamcrest.Matchers.contains;
4+
import static org.hamcrest.MatcherAssert.assertThat;
35
import static org.junit.Assert.*;
46
import static redis.clients.jedis.Protocol.CLUSTER_HASHSLOTS;
7+
import static redis.clients.jedis.util.GeoRadiusResponseMatcher.isEqualToGeoRadiusResponse;
58

69
import java.util.*;
710

@@ -21,6 +24,7 @@
2124
import redis.clients.jedis.resps.StreamEntry;
2225
import redis.clients.jedis.resps.Tuple;
2326
import redis.clients.jedis.util.AssertUtil;
27+
import redis.clients.jedis.util.GeoCoordinateMatcher;
2428
import redis.clients.jedis.util.JedisClusterTestUtil;
2529
import redis.clients.jedis.util.SafeEncoder;
2630

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

696-
List<GeoCoordinate> values = new ArrayList<>();
697-
values.add(new GeoCoordinate(2.19093829393386841, 41.43379028184083523));
698-
values.add(new GeoCoordinate(2.18737632036209106, 41.40634178640635099));
699700

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

709-
GeoRadiusResponse expectedResponse = new GeoRadiusResponse("place1".getBytes());
710-
expectedResponse.setCoordinate(new GeoCoordinate(2.19093829393386841, 41.43379028184083523));
711-
expectedResponse.setDistance(0.0881);
712-
expectedResponse.setRawScore(3471609698139488L);
713-
714710
ClusterConnectionProvider provider = new ClusterConnectionProvider(nodes, DEFAULT_CLIENT_CONFIG);
715711
ClusterPipeline p = new ClusterPipeline(provider);
716712

@@ -738,11 +734,24 @@ public void clusterPipelineGeo() {
738734
assertEquals(Double.valueOf(3067.4157), r2.get());
739735
assertEquals(Double.valueOf(3.0674), r3.get());
740736
assertEquals(hashValues, r4.get());
741-
assertEquals(values, r5.get());
737+
assertThat(r5.get(), contains(
738+
GeoCoordinateMatcher.isEqualWithTolerance(2.19093829393386841, 41.43379028184083523),
739+
GeoCoordinateMatcher.isEqualWithTolerance(2.18737632036209106, 41.40634178640635099))
740+
);
742741
assertTrue(r6.get().size() == 1 && r6.get().get(0).getMemberByString().equals("place1"));
743742
assertTrue(r7.get().size() == 1 && r7.get().get(0).getMemberByString().equals("place1"));
744-
assertEquals(expectedResponse, r8.get().get(0));
745-
assertEquals(expectedResponse, r9.get().get(0));
743+
744+
745+
GeoRadiusResponse expectedResponse = new GeoRadiusResponse("place1".getBytes());
746+
expectedResponse.setCoordinate(new GeoCoordinate(2.19093829393386841, 41.43379028184083523));
747+
expectedResponse.setDistance(0.0881);
748+
expectedResponse.setRawScore(3471609698139488L);
749+
750+
assertThat(r8.get().get(0),isEqualToGeoRadiusResponse(expectedResponse));
751+
assertThat(r9.get().get(0),isEqualToGeoRadiusResponse(expectedResponse));
752+
753+
754+
746755
assertEquals(Long.valueOf(1), r10.get());
747756
assertTrue(r11.get().size() == 1 && r11.get().contains("place1"));
748757
assertTrue(r12.get().size() == 2 && r12.get().get(0).getMemberByString().equals("place2"));

src/test/java/redis/clients/jedis/commands/jedis/GeoCommandsTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.Assert.*;
44
import static redis.clients.jedis.util.AssertUtil.assertByteArrayListEquals;
5+
import static redis.clients.jedis.util.GeoCoordinateMatcher.isEqualWithTolerance;
56

67
import java.util.ArrayList;
78
import java.util.HashMap;
@@ -532,7 +533,7 @@ public void geosearch() {
532533
assertEquals(1, members.size());
533534
assertEquals("place1", members.get(0).getMemberByString());
534535
assertEquals(0.0881, members.get(0).getDistance(), 10);
535-
assertEquals(new GeoCoordinate(2.19093829393386841, 41.43379028184083523), members.get(0).getCoordinate());
536+
assertThat(members.get(0).getCoordinate(), isEqualWithTolerance( 2.19093829393386841, 41.43379028184083523));
536537
}
537538

538539
@Test

src/test/java/redis/clients/jedis/commands/unified/GeoCommandsTestBase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.Assert.*;
44
import static redis.clients.jedis.util.AssertUtil.assertByteArrayListEquals;
5+
import static redis.clients.jedis.util.GeoCoordinateMatcher.isEqualWithTolerance;
56

67
import java.util.ArrayList;
78
import java.util.HashMap;
@@ -529,7 +530,7 @@ public void geosearch() {
529530
assertEquals(1, members.size());
530531
assertEquals("place1", members.get(0).getMemberByString());
531532
assertEquals(0.0881, members.get(0).getDistance(), 10);
532-
assertEquals(new GeoCoordinate(2.19093829393386841, 41.43379028184083523), members.get(0).getCoordinate());
533+
assertThat(members.get(0).getCoordinate(), isEqualWithTolerance(2.19093829393386841, 41.43379028184083523));
533534
}
534535

535536
@Test

src/test/java/redis/clients/jedis/commands/unified/pipeline/GeoPipelineCommandsTest.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
import static org.hamcrest.Matchers.contains;
77
import static org.hamcrest.Matchers.containsInAnyOrder;
88
import static org.hamcrest.Matchers.equalTo;
9-
import static org.hamcrest.Matchers.instanceOf;
109
import static org.hamcrest.Matchers.nullValue;
1110
import static org.junit.Assert.assertEquals;
12-
import static redis.clients.jedis.util.GeoCoordinateMatcher.atCoordinates;
11+
import static redis.clients.jedis.util.GeoCoordinateMatcher.isEqualWithTolerance;
1312

1413
import java.util.HashMap;
1514
import java.util.List;
@@ -23,7 +22,6 @@
2322
import redis.clients.jedis.RedisProtocol;
2423
import redis.clients.jedis.Response;
2524
import redis.clients.jedis.args.GeoUnit;
26-
import redis.clients.jedis.exceptions.JedisDataException;
2725
import redis.clients.jedis.params.GeoAddParams;
2826
import redis.clients.jedis.params.GeoRadiusParam;
2927
import redis.clients.jedis.params.GeoRadiusStoreParam;
@@ -181,14 +179,14 @@ public void geopos() {
181179
pipe.sync();
182180

183181
assertThat(coordinates.get(), contains(
184-
atCoordinates(3.0, 4.0),
185-
atCoordinates(2.0, 3.0),
182+
isEqualWithTolerance(3.0, 4.0),
183+
isEqualWithTolerance(2.0, 3.0),
186184
null
187185
));
188186

189187
assertThat(bcoordinates.get(), contains(
190-
atCoordinates(3.0, 4.0),
191-
atCoordinates(2.0, 3.0),
188+
isEqualWithTolerance(3.0, 4.0),
189+
isEqualWithTolerance(2.0, 3.0),
192190
null
193191
));
194192
}
@@ -257,7 +255,7 @@ public void georadius() {
257255
assertThat(members4.get().stream().map(GeoRadiusResponse::getDistance).collect(Collectors.toList()),
258256
contains(closeTo(56.4413, EPSILON)));
259257
assertThat(members4.get().stream().map(GeoRadiusResponse::getCoordinate).collect(Collectors.toList()),
260-
contains(atCoordinates(15.087269, 37.502669)));
258+
contains(isEqualWithTolerance(15.087269, 37.502669)));
261259
assertThat(members4.get().stream().map(GeoRadiusResponse::getRawScore).collect(Collectors.toList()),
262260
contains(3479447370796909L));
263261

@@ -356,7 +354,7 @@ public void georadiusReadonly() {
356354
assertThat(members4.get().stream().map(GeoRadiusResponse::getDistance).collect(Collectors.toList()),
357355
contains(closeTo(56.4413, EPSILON)));
358356
assertThat(members4.get().stream().map(GeoRadiusResponse::getCoordinate).collect(Collectors.toList()),
359-
contains(atCoordinates(15.087269, 37.502669)));
357+
contains(isEqualWithTolerance(15.087269, 37.502669)));
360358
assertThat(members4.get().stream().map(GeoRadiusResponse::getRawScore).collect(Collectors.toList()),
361359
contains(0L));
362360
}
@@ -417,7 +415,7 @@ public void georadiusBinary() {
417415
assertThat(members4.get().stream().map(GeoRadiusResponse::getDistance).collect(Collectors.toList()),
418416
contains(closeTo(56.4413, EPSILON)));
419417
assertThat(members4.get().stream().map(GeoRadiusResponse::getCoordinate).collect(Collectors.toList()),
420-
contains(atCoordinates(15.087269, 37.502669)));
418+
contains(isEqualWithTolerance(15.087269, 37.502669)));
421419
assertThat(members4.get().stream().map(GeoRadiusResponse::getRawScore).collect(Collectors.toList()),
422420
contains(0L));
423421
}
@@ -498,7 +496,7 @@ public void georadiusReadonlyBinary() {
498496
assertThat(members4.get().stream().map(GeoRadiusResponse::getDistance).collect(Collectors.toList()),
499497
contains(closeTo(56.4413, EPSILON)));
500498
assertThat(members4.get().stream().map(GeoRadiusResponse::getCoordinate).collect(Collectors.toList()),
501-
contains(atCoordinates(15.087269, 37.502669)));
499+
contains(isEqualWithTolerance(15.087269, 37.502669)));
502500
assertThat(members4.get().stream().map(GeoRadiusResponse::getRawScore).collect(Collectors.toList()),
503501
contains(0L));
504502
}
@@ -543,7 +541,7 @@ public void georadiusByMember() {
543541
assertThat(members3.get().stream().map(GeoRadiusResponse::getDistance).collect(Collectors.toList()),
544542
contains(closeTo(0.0, EPSILON)));
545543
assertThat(members3.get().stream().map(GeoRadiusResponse::getCoordinate).collect(Collectors.toList()),
546-
contains(atCoordinates(13.583333, 37.316667)));
544+
contains(isEqualWithTolerance(13.583333, 37.316667)));
547545
assertThat(members3.get().stream().map(GeoRadiusResponse::getRawScore).collect(Collectors.toList()),
548546
contains(0L));
549547
}
@@ -606,7 +604,7 @@ public void georadiusByMemberReadonly() {
606604
assertThat(members3.get().stream().map(GeoRadiusResponse::getDistance).collect(Collectors.toList()),
607605
contains(closeTo(0.0, EPSILON)));
608606
assertThat(members3.get().stream().map(GeoRadiusResponse::getCoordinate).collect(Collectors.toList()),
609-
contains(atCoordinates(13.583333, 37.316667)));
607+
contains(isEqualWithTolerance(13.583333, 37.316667)));
610608
assertThat(members3.get().stream().map(GeoRadiusResponse::getRawScore).collect(Collectors.toList()),
611609
contains(0L));
612610
}
@@ -650,7 +648,7 @@ public void georadiusByMemberBinary() {
650648
assertThat(members3.get().stream().map(GeoRadiusResponse::getDistance).collect(Collectors.toList()),
651649
contains(closeTo(0.0, EPSILON)));
652650
assertThat(members3.get().stream().map(GeoRadiusResponse::getCoordinate).collect(Collectors.toList()),
653-
contains(atCoordinates(13.583333, 37.316667)));
651+
contains(isEqualWithTolerance(13.583333, 37.316667)));
654652
assertThat(members3.get().stream().map(GeoRadiusResponse::getRawScore).collect(Collectors.toList()),
655653
contains(0L));
656654
}
@@ -712,7 +710,7 @@ public void georadiusByMemberReadonlyBinary() {
712710
assertThat(members3.get().stream().map(GeoRadiusResponse::getDistance).collect(Collectors.toList()),
713711
contains(closeTo(0.0, EPSILON)));
714712
assertThat(members3.get().stream().map(GeoRadiusResponse::getCoordinate).collect(Collectors.toList()),
715-
contains(atCoordinates(13.583333, 37.316667)));
713+
contains(isEqualWithTolerance(13.583333, 37.316667)));
716714
assertThat(members3.get().stream().map(GeoRadiusResponse::getRawScore).collect(Collectors.toList()),
717715
contains(0L));
718716
}
@@ -775,7 +773,7 @@ public void geosearch() {
775773
assertThat(members4.get().stream().map(GeoRadiusResponse::getDistance).collect(Collectors.toList()),
776774
contains(closeTo(0.0, EPSILON), closeTo(3.0674, EPSILON)));
777775
assertThat(members4.get().stream().map(GeoRadiusResponse::getCoordinate).collect(Collectors.toList()),
778-
contains(atCoordinates(2.1909389952632d, 41.433791470673d), atCoordinates(2.1873744593677d, 41.406342043777d)));
776+
contains(isEqualWithTolerance(2.1909389952632d, 41.433791470673d), isEqualWithTolerance(2.1873744593677d, 41.406342043777d)));
779777
assertThat(members4.get().stream().map(GeoRadiusResponse::getRawScore).collect(Collectors.toList()),
780778
contains(3471609698139488L, 3471609625421029L));
781779

@@ -793,7 +791,7 @@ public void geosearch() {
793791
assertThat(members8.get().stream().map(GeoRadiusResponse::getDistance).collect(Collectors.toList()),
794792
contains(closeTo(0.0881, EPSILON)));
795793
assertThat(members8.get().stream().map(GeoRadiusResponse::getCoordinate).collect(Collectors.toList()),
796-
contains(atCoordinates(2.1909389952632d, 41.433791470673d)));
794+
contains(isEqualWithTolerance(2.1909389952632d, 41.433791470673d)));
797795
assertThat(members8.get().stream().map(GeoRadiusResponse::getRawScore).collect(Collectors.toList()),
798796
contains(0L));
799797
}

src/test/java/redis/clients/jedis/util/GeoCoordinateMatcher.java

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,53 @@
66

77
public class GeoCoordinateMatcher extends TypeSafeMatcher<GeoCoordinate> {
88

9-
public static GeoCoordinateMatcher atCoordinates(double longitude, double latitude) {
10-
return new GeoCoordinateMatcher(longitude, latitude);
9+
public static GeoCoordinateMatcher isEqualWithTolerance(GeoCoordinate expected, double tolerance) {
10+
return new GeoCoordinateMatcher(expected, tolerance);
1111
}
1212

13-
private static final double EPSILON = 1e-5;
13+
public static GeoCoordinateMatcher isEqualWithTolerance(GeoCoordinate expected) {
14+
return new GeoCoordinateMatcher(expected, DEFAULT_TOLERANCE);
15+
}
16+
17+
public static GeoCoordinateMatcher isEqualWithTolerance(double longitude, double latitude, double tolerance) {
18+
return new GeoCoordinateMatcher(new GeoCoordinate(longitude, latitude), tolerance);
19+
}
20+
21+
public static GeoCoordinateMatcher isEqualWithTolerance(double longitude, double latitude) {
22+
return new GeoCoordinateMatcher(new GeoCoordinate(longitude,latitude), latitude);
23+
}
24+
25+
public static final double DEFAULT_TOLERANCE = 1e-14;
1426

15-
private final double longitude;
16-
private final double latitude;
27+
private final double tolerance;
28+
private final GeoCoordinate expected;
1729

18-
public GeoCoordinateMatcher(double longitude, double latitude) {
19-
this.longitude = longitude;
20-
this.latitude = latitude;
30+
31+
public GeoCoordinateMatcher(GeoCoordinate expected, double tolerance) {
32+
this.expected = expected;
33+
this.tolerance = tolerance;
34+
}
35+
36+
public GeoCoordinateMatcher(GeoCoordinate expected) {
37+
this(expected, DEFAULT_TOLERANCE);
2138
}
2239

2340
@Override
24-
protected boolean matchesSafely(GeoCoordinate item) {
25-
return item != null &&
26-
Math.abs(longitude - item.getLongitude()) < EPSILON &&
27-
Math.abs(latitude - item.getLatitude()) < EPSILON;
41+
protected boolean matchesSafely(GeoCoordinate actual) {
42+
return Math.abs(actual.getLatitude() - expected.getLatitude()) < tolerance &&
43+
Math.abs(actual.getLongitude() - expected.getLongitude()) < tolerance;
2844
}
2945

3046
@Override
3147
public void describeTo(Description description) {
32-
description.appendText("matches " + longitude + " longitude " + latitude + " latitude with precision " + EPSILON);
48+
description.appendText("a GeoCoordinate within ")
49+
.appendValue(tolerance)
50+
.appendText(" of ")
51+
.appendValue(expected);
52+
}
53+
54+
@Override
55+
protected void describeMismatchSafely(GeoCoordinate actual, Description mismatchDescription) {
56+
mismatchDescription.appendText("was ").appendValue(actual);
3357
}
3458
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package redis.clients.jedis.util;
2+
3+
import org.hamcrest.Description;
4+
import org.hamcrest.Matcher;
5+
import org.hamcrest.TypeSafeMatcher;
6+
import redis.clients.jedis.GeoCoordinate;
7+
import redis.clients.jedis.resps.GeoRadiusResponse;
8+
9+
public class GeoRadiusResponseMatcher extends TypeSafeMatcher<GeoRadiusResponse> {
10+
private final GeoRadiusResponse expected;
11+
private final double coordinateTolerance;
12+
13+
public static Matcher<GeoRadiusResponse> isEqualToGeoRadiusResponse(GeoRadiusResponse expected) {
14+
return new GeoRadiusResponseMatcher(expected, GeoCoordinateMatcher.DEFAULT_TOLERANCE);
15+
}
16+
17+
public static Matcher<GeoRadiusResponse> isEqualToGeoRadiusResponse(GeoRadiusResponse expected, double tolerance) {
18+
return new GeoRadiusResponseMatcher(expected, tolerance);
19+
}
20+
21+
public GeoRadiusResponseMatcher(GeoRadiusResponse expected, double coordinateTolerance) {
22+
this.expected = expected;
23+
this.coordinateTolerance = coordinateTolerance;
24+
}
25+
26+
@Override
27+
protected boolean matchesSafely(GeoRadiusResponse actual) {
28+
// Check if coordinates match within the tolerance
29+
GeoCoordinate expectedCoord = expected.getCoordinate();
30+
GeoCoordinate actualCoord = actual.getCoordinate();
31+
if (!GeoCoordinateMatcher.isEqualWithTolerance(expectedCoord, coordinateTolerance).matches(actualCoord)) {
32+
return false;
33+
}
34+
35+
// Check if distance and rawScore match exactly
36+
if (Double.compare(expected.getDistance(), actual.getDistance()) != 0) {
37+
return false;
38+
}
39+
return expected.getRawScore() == actual.getRawScore();
40+
}
41+
42+
@Override
43+
public void describeTo(Description description) {
44+
description.appendText("a GeoRadiusResponse with coordinate ")
45+
.appendValue(expected.getCoordinate())
46+
.appendText(", distance ")
47+
.appendValue(expected.getDistance())
48+
.appendText(", and rawScore ")
49+
.appendValue(expected.getRawScore());
50+
}
51+
52+
@Override
53+
protected void describeMismatchSafely(GeoRadiusResponse actual, Description mismatchDescription) {
54+
mismatchDescription.appendText("was ")
55+
.appendValue(actual);
56+
}
57+
58+
}

0 commit comments

Comments
 (0)