forked from HDT3213/godis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeo_test.go
103 lines (96 loc) · 2.94 KB
/
geo_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package godis
import (
"fmt"
"github.com/hdt3213/godis/lib/utils"
"github.com/hdt3213/godis/redis/reply"
"github.com/hdt3213/godis/redis/reply/asserts"
"strconv"
"testing"
)
func TestGeoHash(t *testing.T) {
execFlushDB(testDB, utils.ToCmdLine())
key := utils.RandString(10)
pos := utils.RandString(10)
result := execGeoAdd(testDB, utils.ToCmdLine(key, "13.361389", "38.115556", pos))
asserts.AssertIntReply(t, result, 1)
result = execGeoHash(testDB, utils.ToCmdLine(key, pos))
asserts.AssertMultiBulkReply(t, result, []string{"sqc8b49rnys00"})
}
func TestGeoRadius(t *testing.T) {
execFlushDB(testDB, utils.ToCmdLine())
key := utils.RandString(10)
pos1 := utils.RandString(10)
pos2 := utils.RandString(10)
execGeoAdd(testDB, utils.ToCmdLine(key,
"13.361389", "38.115556", pos1,
"15.087269", "37.502669", pos2,
))
result := execGeoRadius(testDB, utils.ToCmdLine(key, "15", "37", "200", "km"))
asserts.AssertMultiBulkReplySize(t, result, 2)
}
func TestGeoRadiusByMember(t *testing.T) {
execFlushDB(testDB, utils.ToCmdLine())
key := utils.RandString(10)
pos1 := utils.RandString(10)
pos2 := utils.RandString(10)
pivot := utils.RandString(10)
execGeoAdd(testDB, utils.ToCmdLine(key,
"13.361389", "38.115556", pos1,
"17.087269", "38.502669", pos2,
"13.583333", "37.316667", pivot,
))
result := execGeoRadiusByMember(testDB, utils.ToCmdLine(key, pivot, "100", "km"))
asserts.AssertMultiBulkReplySize(t, result, 2)
}
func TestGeoPos(t *testing.T) {
execFlushDB(testDB, utils.ToCmdLine())
key := utils.RandString(10)
pos1 := utils.RandString(10)
pos2 := utils.RandString(10)
execGeoAdd(testDB, utils.ToCmdLine(key,
"13.361389", "38.115556", pos1,
))
result := execGeoPos(testDB, utils.ToCmdLine(key, pos1, pos2))
expected := "*2\r\n*2\r\n$18\r\n13.361386698670685\r\n$17\r\n38.11555536696687\r\n*0\r\n"
if string(result.ToBytes()) != expected {
t.Error("test failed")
}
}
func TestGeoDist(t *testing.T) {
execFlushDB(testDB, utils.ToCmdLine())
key := utils.RandString(10)
pos1 := utils.RandString(10)
pos2 := utils.RandString(10)
execGeoAdd(testDB, utils.ToCmdLine(key,
"13.361389", "38.115556", pos1,
"15.087269", "37.502669", pos2,
))
result := execGeoDist(testDB, utils.ToCmdLine(key, pos1, pos2, "km"))
bulkReply, ok := result.(*reply.BulkReply)
if !ok {
t.Error(fmt.Sprintf("expected bulk reply, actually %s", result.ToBytes()))
return
}
dist, err := strconv.ParseFloat(string(bulkReply.Arg), 10)
if err != nil {
t.Error(err)
return
}
if dist < 166.274 || dist > 166.275 {
t.Errorf("expected 166.274, actual: %f", dist)
}
result = execGeoDist(testDB, utils.ToCmdLine(key, pos1, pos2, "m"))
bulkReply, ok = result.(*reply.BulkReply)
if !ok {
t.Error(fmt.Sprintf("expected bulk reply, actually %s", result.ToBytes()))
return
}
dist, err = strconv.ParseFloat(string(bulkReply.Arg), 10)
if err != nil {
t.Error(err)
return
}
if dist < 166274 || dist > 166275 {
t.Errorf("expected 166274, actual: %f", dist)
}
}