Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f4bf4e3

Browse files
Geo* unit tests
1 parent 4f49bf7 commit f4bf4e3

File tree

2 files changed

+146
-3
lines changed

2 files changed

+146
-3
lines changed

redis_commands.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2802,6 +2802,10 @@ static void get_georadius_opts(HashTable *ht, int *withcoord, int *withdist,
28022802
*withdist = 1;
28032803
} else if (!strcasecmp(optstr, "withhash")) {
28042804
*withhash = 1;
2805+
} else if (!strcasecmp(optstr, "asc")) {
2806+
*sort = SORT_ASC;
2807+
} else if (!strcasecmp(optstr, "desc")) {
2808+
*sort = SORT_DESC;
28052809
}
28062810
} else if (!strcasecmp(optkey, "count") && Z_TYPE_PP(optval) == IS_LONG) {
28072811
*count = Z_LVAL_PP(optval);

tests/RedisTest.php

Lines changed: 142 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php defined('PHPREDIS_TESTRUN') or die("Use TestRedis.php to run tests!\n");
1+
<?php defined('PHPREDIS_TESTRUN') or die("Use TestRedis.php to run tests!\n");
22

33
require_once(dirname($_SERVER['PHP_SELF'])."/TestSuite.php");
44

@@ -7,6 +7,15 @@ class Redis_Test extends TestSuite
77
const PORT = 6379;
88
const AUTH = NULL; //replace with a string to use Redis authentication
99

10+
/* City lat/long */
11+
protected $cities = Array(
12+
'Chico' => Array(-121.837478, 39.728494),
13+
'Sacramento' => Array(-121.494400, 38.581572),
14+
'Gridley' => Array(-121.693583, 39.363777),
15+
'Marysville' => Array(-121.591355, 39.145725),
16+
'Cupertino' => Array(-122.032182, 37.322998)
17+
);
18+
1019
/**
1120
* @var Redis
1221
*/
@@ -2414,7 +2423,7 @@ public function testMultiExec() {
24142423

24152424
public function testFailedTransactions() {
24162425
$this->redis->set('x', 42);
2417-
2426+
24182427
// failed transaction
24192428
$this->redis->watch('x');
24202429

@@ -2539,7 +2548,7 @@ protected function sequence($mode) {
25392548
->ttl('key')
25402549
->expireAt('key', '0000')
25412550
->exec();
2542-
2551+
25432552
$this->assertTrue(is_array($ret));
25442553
$i = 0;
25452554
$ttl = $ret[$i++];
@@ -4756,6 +4765,136 @@ public function testPFCommands() {
47564765
}
47574766
}
47584767

4768+
//
4769+
// GEO* command tests
4770+
//
4771+
4772+
protected function addCities($key) {
4773+
$this->redis->del($key);
4774+
foreach ($this->cities as $city => $longlat) {
4775+
$this->redis->geoadd($key, $longlat[0], $longlat[1], $city);
4776+
}
4777+
}
4778+
4779+
/* GEOADD */
4780+
public function testGeoAdd() {
4781+
$this->redis->del('geokey');
4782+
4783+
/* Add them one at a time */
4784+
foreach ($this->cities as $city => $longlat) {
4785+
$this->assertEquals($this->redis->geoadd('geokey', $longlat[0], $longlat[1], $city), 1);
4786+
}
4787+
4788+
/* Add them again, all at once */
4789+
$args = ['geokey'];
4790+
foreach ($this->cities as $city => $longlat) {
4791+
$args = array_merge($args, Array($longlat[0], $longlat[1], $city));
4792+
}
4793+
4794+
/* They all exist, should be nothing added */
4795+
$this->assertEquals(call_user_func_array(Array($this->redis, 'geoadd'), $args), 0);
4796+
}
4797+
4798+
/* GEORADIUS */
4799+
public function genericGeoRadiusTest($cmd) {
4800+
/* Chico */
4801+
$city = 'Chico';
4802+
$lng = -121.837478;
4803+
$lat = 39.728494;
4804+
4805+
$this->addCities('gk');
4806+
4807+
/* Pre tested with redis-cli. We're just verifying proper delivery of distance and unit */
4808+
if ($cmd == 'georadius') {
4809+
$this->assertEquals($this->redis->georadius('gk', $lng, $lat, 10, 'mi'), Array('Chico'));
4810+
$this->assertEquals($this->redis->georadius('gk', $lng, $lat, 30, 'mi'), Array('Gridley','Chico'));
4811+
$this->assertEquals($this->redis->georadius('gk', $lng, $lat, 50, 'km'), Array('Gridley','Chico'));
4812+
$this->assertEquals($this->redis->georadius('gk', $lng, $lat, 50000, 'm'), Array('Gridley','Chico'));
4813+
$this->assertEquals($this->redis->georadius('gk', $lng, $lat, 150000, 'ft'), Array('Gridley', 'Chico'));
4814+
$args = Array('georadius', 'gk', $lng, $lat, 500, 'mi');
4815+
} else {
4816+
$this->assertEquals($this->redis->georadiusbymember('gk', $city, 10, 'mi'), Array('Chico'));
4817+
$this->assertEquals($this->redis->georadiusbymember('gk', $city, 30, 'mi'), Array('Gridley','Chico'));
4818+
$this->assertEquals($this->redis->georadiusbymember('gk', $city, 50, 'km'), Array('Gridley','Chico'));
4819+
$this->assertEquals($this->redis->georadiusbymember('gk', $city, 50000, 'm'), Array('Gridley','Chico'));
4820+
$this->assertEquals($this->redis->georadiusbymember('gk', $city, 150000, 'ft'), Array('Gridley', 'Chico'));
4821+
$args = Array('georadiusbymember', 'gk', $city, 500, 'mi');
4822+
}
4823+
4824+
/* Options */
4825+
$opts = Array('WITHCOORD', 'WITHDIST', 'WITHHASH');
4826+
$sortopts = Array('', 'ASC', 'DESC');
4827+
4828+
for ($i = 0; $i < count($opts); $i++) {
4829+
$subopts = array_slice($opts, 0, $i);
4830+
shuffle($subopts);
4831+
4832+
$subargs = $args;
4833+
foreach ($subopts as $opt) {
4834+
$subargs[] = $opt;
4835+
}
4836+
4837+
for ($c = 0; $c < 3; $c++) {
4838+
/* Add a count if we're past first iteration */
4839+
if ($c > 0) {
4840+
$subopts['count'] = $c;
4841+
$subargs[] = 'count';
4842+
$subargs[] = $c;
4843+
}
4844+
4845+
/* Adding optional sort */
4846+
foreach ($sortopts as $sortopt) {
4847+
$realargs = $subargs;
4848+
$realopts = $subopts;
4849+
if ($sortopt) {
4850+
$realargs[] = $sortopt;
4851+
$realopts[] = $sortopt;
4852+
}
4853+
4854+
$ret1 = call_user_func_array(Array($this->redis, 'rawcommand'), $realargs);
4855+
if ($cmd == 'georadius') {
4856+
$ret2 = $this->redis->$cmd('gk', $lng, $lat, 500, 'mi', $realopts);
4857+
} else {
4858+
$ret2 = $this->redis->$cmd('gk', $city, 500, 'mi', $realopts);
4859+
}
4860+
$this->assertEquals($ret1, $ret2);
4861+
}
4862+
}
4863+
}
4864+
}
4865+
4866+
public function testGeoRadius() {
4867+
$this->genericGeoRadiusTest('georadius');
4868+
}
4869+
4870+
public function testGeoRadiusByMember() {
4871+
$this->genericGeoRadiusTest('georadiusbymember');
4872+
}
4873+
4874+
public function testGeoPos() {
4875+
$this->addCities('gk');
4876+
$this->assertEquals($this->redis->geopos('gk', 'Chico', 'Sacramento'), $this->redis->rawCommand('geopos', 'gk', 'Chico', 'Sacramento'));
4877+
$this->assertEquals($this->redis->geopos('gk', 'Cupertino'), $this->redis->rawCommand('geopos', 'gk', 'Cupertino'));
4878+
}
4879+
4880+
public function testGeoHash() {
4881+
$this->addCities('gk');
4882+
$this->assertEquals($this->redis->geohash('gk', 'Chico', 'Sacramento'), $this->redis->rawCommand('geohash', 'gk', 'Chico', 'Sacramento'));
4883+
$this->assertEquals($this->redis->geohash('gk', 'Chico'), $this->redis->rawCommand('geohash', 'gk', 'Chico'));
4884+
}
4885+
4886+
public function testGeoDist() {
4887+
$this->addCities('gk');
4888+
4889+
$r1 = $this->redis->geodist('gk', 'Chico', 'Cupertino');
4890+
$r2 = $this->redis->rawCommand('geodist', 'gk', 'Chico', 'Cupertino');
4891+
$this->assertEquals(round($r1, 8), round($r2, 8));
4892+
4893+
$r1 = $this->redis->geodist('gk', 'Chico', 'Cupertino', 'km');
4894+
$r2 = $this->redis->rawCommand('geodist', 'gk', 'Chico', 'Cupertino', 'km');
4895+
$this->assertEquals(round($r1, 8), round($r2, 8));
4896+
}
4897+
47594898
/* Test a 'raw' command */
47604899
public function testRawCommand() {
47614900
$this->redis->set('mykey','some-value');

0 commit comments

Comments
 (0)