5
5
import asyncio
6
6
import binascii
7
7
import datetime
8
+ import math
8
9
import re
9
10
import sys
10
11
from string import ascii_letters
13
14
import pytest_asyncio
14
15
import valkey
15
16
from tests .conftest import (
17
+ assert_geo_is_close ,
16
18
assert_resp_response ,
17
19
assert_resp_response_in ,
20
+ assert_resp_response_isclose ,
18
21
is_resp2_connection ,
19
22
skip_if_server_version_gte ,
20
23
skip_if_server_version_lt ,
@@ -2459,7 +2462,7 @@ async def test_geopos(self, r: valkey.Valkey):
2459
2462
2460
2463
await r .geoadd ("barcelona" , values )
2461
2464
# valkey uses 52 bits precision, hereby small errors may be introduced.
2462
- assert_resp_response (
2465
+ assert_resp_response_isclose (
2463
2466
r ,
2464
2467
await r .geopos ("barcelona" , "place1" , "place2" ),
2465
2468
[
@@ -2519,7 +2522,30 @@ async def test_georadius_units(self, r: valkey.Valkey):
2519
2522
2520
2523
@skip_unless_arch_bits (64 )
2521
2524
@skip_if_server_version_lt ("3.2.0" )
2522
- async def test_georadius_with (self , r : valkey .Valkey ):
2525
+ @pytest .mark .parametrize (
2526
+ "georadius_kwargs, expected_georadius_result" ,
2527
+ [
2528
+ (
2529
+ {"withdist" : True , "withcoord" : True , "withhash" : True },
2530
+ [b"place1" , 0.0881 , 3471609698139488 ],
2531
+ ),
2532
+ (
2533
+ {"withdist" : True , "withcoord" : True },
2534
+ [b"place1" , 0.0881 ],
2535
+ ),
2536
+ (
2537
+ {"withhash" : True , "withcoord" : True },
2538
+ [b"place1" , 3471609698139488 ],
2539
+ ),
2540
+ (
2541
+ {"withdist" : True , "withhash" : True },
2542
+ [b"place1" , 0.0881 , 3471609698139488 ],
2543
+ ),
2544
+ ],
2545
+ )
2546
+ async def test_georadius_with (
2547
+ self , r : valkey .Valkey , georadius_kwargs , expected_georadius_result
2548
+ ):
2523
2549
values = (2.1909389952632 , 41.433791470673 , "place1" ) + (
2524
2550
2.1873744593677 ,
2525
2551
41.406342043777 ,
@@ -2530,33 +2556,23 @@ async def test_georadius_with(self, r: valkey.Valkey):
2530
2556
2531
2557
# test a bunch of combinations to test the parse response
2532
2558
# function.
2533
- assert await r .georadius (
2559
+ georadius_result = await r .georadius (
2534
2560
"barcelona" ,
2535
2561
2.191 ,
2536
2562
41.433 ,
2537
2563
1 ,
2538
2564
unit = "km" ,
2539
- withdist = True ,
2540
- withcoord = True ,
2541
- withhash = True ,
2542
- ) == [
2543
- [
2544
- b"place1" ,
2545
- 0.0881 ,
2546
- 3471609698139488 ,
2547
- (2.19093829393386841 , 41.43379028184083523 ),
2548
- ]
2549
- ]
2550
-
2551
- assert await r .georadius (
2552
- "barcelona" , 2.191 , 41.433 , 1 , unit = "km" , withdist = True , withcoord = True
2553
- ) == [[b"place1" , 0.0881 , (2.19093829393386841 , 41.43379028184083523 )]]
2565
+ ** georadius_kwargs ,
2566
+ )
2554
2567
2555
- assert await r .georadius (
2556
- "barcelona" , 2.191 , 41.433 , 1 , unit = "km" , withhash = True , withcoord = True
2557
- ) == [
2558
- [b"place1" , 3471609698139488 , (2.19093829393386841 , 41.43379028184083523 )]
2559
- ]
2568
+ assert len (georadius_result ) == 1
2569
+ if "withcoord" in georadius_kwargs :
2570
+ assert_geo_is_close (
2571
+ georadius_result [0 ][- 1 ], (2.19093829393386841 , 41.43379028184083523 )
2572
+ )
2573
+ assert georadius_result [0 ][:- 1 ] == expected_georadius_result
2574
+ else :
2575
+ assert georadius_result == [expected_georadius_result ]
2560
2576
2561
2577
# test no values.
2562
2578
assert (
@@ -2566,9 +2582,7 @@ async def test_georadius_with(self, r: valkey.Valkey):
2566
2582
1 ,
2567
2583
1 ,
2568
2584
unit = "km" ,
2569
- withdist = True ,
2570
- withcoord = True ,
2571
- withhash = True ,
2585
+ ** georadius_kwargs ,
2572
2586
)
2573
2587
== []
2574
2588
)
@@ -2632,7 +2646,8 @@ async def test_georadius_store_dist(self, r: valkey.Valkey):
2632
2646
"barcelona" , 2.191 , 41.433 , 1000 , store_dist = "places_barcelona"
2633
2647
)
2634
2648
# instead of save the geo score, the distance is saved.
2635
- assert await r .zscore ("places_barcelona" , "place1" ) == 88.05060698409301
2649
+ z_score = await r .zscore ("places_barcelona" , "place1" )
2650
+ assert math .isclose (z_score , 88.05060698409301 )
2636
2651
2637
2652
@skip_unless_arch_bits (64 )
2638
2653
@skip_if_server_version_lt ("3.2.0" )
@@ -2650,21 +2665,22 @@ async def test_georadiusmember(self, r: valkey.Valkey):
2650
2665
]
2651
2666
assert await r .georadiusbymember ("barcelona" , "place1" , 10 ) == [b"place1" ]
2652
2667
2653
- assert await r .georadiusbymember (
2668
+ radius_place2 , radius_place1 = await r .georadiusbymember (
2654
2669
"barcelona" , "place1" , 4000 , withdist = True , withcoord = True , withhash = True
2655
- ) == [
2656
- [
2657
- b"\x80 place2" ,
2658
- 3067.4157 ,
2659
- 3471609625421029 ,
2660
- (2.187376320362091 , 41.40634178640635 ),
2661
- ],
2662
- [
2663
- b"place1" ,
2664
- 0.0 ,
2665
- 3471609698139488 ,
2666
- (2.1909382939338684 , 41.433790281840835 ),
2667
- ],
2670
+ )
2671
+
2672
+ assert_geo_is_close (radius_place2 [- 1 ], (2.187376320362091 , 41.40634178640635 ))
2673
+ assert radius_place2 [:- 1 ] == [
2674
+ b"\x80 place2" ,
2675
+ 3067.4157 ,
2676
+ 3471609625421029 ,
2677
+ ]
2678
+
2679
+ assert_geo_is_close (radius_place1 [- 1 ], (2.1909382939338684 , 41.433790281840835 ))
2680
+ assert radius_place1 [:- 1 ] == [
2681
+ b"place1" ,
2682
+ 0.0 ,
2683
+ 3471609698139488 ,
2668
2684
]
2669
2685
2670
2686
@skip_if_server_version_lt ("5.0.0" )
0 commit comments