This repository has been archived by the owner on Dec 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
query_test.go
214 lines (202 loc) · 6.06 KB
/
query_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package t38c
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestChannels(t *testing.T) {
channels := &Channels{}
geofence := &Geofence{}
tests := []struct {
Cmd cmd
Expected string
}{
{
Cmd: channels.SetChan("foo", geofence.Roam("cat", "dog", "*", 50, false).Actions(Enter, Exit)).toCmd(),
Expected: "SETCHAN foo NEARBY cat FENCE DETECT enter,exit ROAM dog * 50",
},
{
Cmd: channels.SetChan("foo", geofence.Roam("cat", "dog", "*", 50, true).Actions(Enter, Exit)).toCmd(),
Expected: "SETCHAN foo NEARBY cat FENCE NODWELL DETECT enter,exit ROAM dog * 50",
},
}
for _, test := range tests {
assert.Equal(t, test.Expected, test.Cmd.String())
}
}
func TestHooks(t *testing.T) {
hooks := &Hooks{}
geofence := &Geofence{}
tests := []struct {
Cmd cmd
Expected string
}{
{
Cmd: hooks.SetHook("foo", "localhost:1337", geofence.Roam("cat", "dog", "*", 50, false).Actions(Enter, Exit)).toCmd(),
Expected: "SETHOOK foo localhost:1337 NEARBY cat FENCE DETECT enter,exit ROAM dog * 50",
},
{
Cmd: hooks.SetHook("foo", "localhost:1337", geofence.Roam("cat", "dog", "*", 50, true).Actions(Enter, Exit)).toCmd(),
Expected: "SETHOOK foo localhost:1337 NEARBY cat FENCE NODWELL DETECT enter,exit ROAM dog * 50",
},
}
for _, test := range tests {
assert.Equal(t, test.Expected, test.Cmd.String())
}
}
func TestSearch(t *testing.T) {
search := &Search{}
tests := []struct {
Cmd cmd
Expected string
}{
{
Cmd: search.Nearby("fleet", 10, 20, 30).
Where("speed", 10, 20).
Wherein("speed", 10, 20, 30).
Match("abc*").
Cursor(10).
Format(FormatIDs).
Limit(5).toCmd(),
Expected: "NEARBY fleet WHERE speed 10 20 WHEREIN speed 3 10 20 30 MATCH abc* CURSOR 10 LIMIT 5 IDS POINT 10 20 30",
},
{
Cmd: search.Intersects("fleet").
Tile(10, 20, 30).
Match("abc*").toCmd(),
Expected: "INTERSECTS fleet MATCH abc* TILE 10 20 30",
},
{
Cmd: search.Within("foo").Get("baz", "objID").Distance().WhereEval("bar").toCmd(),
Expected: "WITHIN foo WHEREEVAL bar 0 DISTANCE GET baz objID",
},
{
Cmd: search.Search("foo").Asc().Cursor(5).Limit(5).Match("bar*").FormatIDs().toCmd(),
Expected: "SEARCH foo MATCH bar* ASC CURSOR 5 LIMIT 5 IDS",
},
{
Cmd: search.Search("foo").Desc().Where("bar", 0, 20).FormatCount().toCmd(),
Expected: "SEARCH foo WHERE bar 0 20 DESC COUNT",
},
{
Cmd: search.Scan("foo").Cursor(5).Limit(5).Asc().Match("bar").Wherein("baz", 10, 12, 13).NoFields().toCmd(),
Expected: "SCAN foo WHEREIN baz 3 10 12 13 MATCH bar ASC NOFIELDS CURSOR 5 LIMIT 5",
},
{
Cmd: search.Scan("foo").Where("field", 0, 20).Limit(5).Desc().Match("bar").Format(FormatPoints).toCmd(),
Expected: "SCAN foo WHERE field 0 20 MATCH bar DESC LIMIT 5 POINTS",
},
}
for _, test := range tests {
assert.Equal(t, test.Expected, test.Cmd.String())
}
}
func TestGeofence(t *testing.T) {
geofence := &Geofence{}
tests := []struct {
Cmd cmd
Expected string
}{
{
Cmd: geofence.Nearby("fleet", 10, 20, 30).
Actions(Enter, Exit, Cross).
Clip().
Commands(Set, Del).
Cursor(5).
Format(FormatHashes(5)).toCmd(),
Expected: "NEARBY fleet CLIP CURSOR 5 FENCE DETECT enter,exit,cross COMMANDS set,del HASHES 5 POINT 10 20 30",
},
{
Cmd: geofence.Roam("agent", "target", "*", 100, false).
Distance().
Wherein("price", 20, 30).
WhereEval("foo", "arg1", "arg2").toCmd(),
Expected: "NEARBY agent WHEREIN price 2 20 30 WHEREEVAL foo 2 arg1 arg2 DISTANCE FENCE ROAM target * 100",
},
{
Cmd: geofence.Roam("agent", "target", "*", 100, true).
Distance().
Wherein("price", 20, 30).
WhereEval("foo", "arg1", "arg2").toCmd(),
Expected: "NEARBY agent WHEREIN price 2 20 30 WHEREEVAL foo 2 arg1 arg2 DISTANCE FENCE NODWELL ROAM target * 100",
},
{
Cmd: geofence.Within("foo").
Bounds(10, 20, 30, 40).
WhereEvalSHA("sha-hash", "arg1", "arg2").
NoFields().
Limit(1).toCmd(),
Expected: "WITHIN foo WHEREEVALSHA sha-hash 2 arg1 arg2 NOFIELDS LIMIT 1 FENCE BOUNDS 10 20 30 40",
},
{
Cmd: geofence.Intersects("foo").Circle(10, 20, 30).Sparse(5).Where("param", 0, 100).Match("*").toCmd(),
Expected: "INTERSECTS foo WHERE param 0 100 MATCH * SPARSE 5 FENCE CIRCLE 10 20 30",
},
}
for _, test := range tests {
assert.Equal(t, test.Expected, test.Cmd.String())
}
}
func TestKeys(t *testing.T) {
keys := &Keys{}
tests := []struct {
Cmd cmd
Expected string
}{
{
Cmd: keys.Set("agent", "49").
PointZ(0, 0, -20).
Field("age", 55).
IfNotExists().
Expiration(60 * 60 * 24 * 365).toCmd(),
Expected: "SET agent 49 NX EX 31536000 FIELD age 55 POINT 0 0 -20",
},
{
Cmd: keys.Set("agent", "47").
PointZ(0, 0, -20).
Field("age", 55).
IfExists().Field("foo", 10).toCmd(),
Expected: "SET agent 47 XX FIELD age 55 FIELD foo 10 POINT 0 0 -20",
},
{
Cmd: keys.FSet("agent", "47").
Field("cash", 100500).
IfExists().toCmd(),
Expected: "FSET agent 47 XX cash 100500",
},
{
Cmd: keys.FSet("agent", "47").
Field("id", 123456789012345680).
IfExists().toCmd(),
Expected: "FSET agent 47 XX id 123456789012345680",
},
{
Cmd: keys.FSet("agent", "47").
Field("id", -123456789012345680).
IfExists().toCmd(),
Expected: "FSET agent 47 XX id -123456789012345680",
},
{
Cmd: keys.FSet("agent", "47").
Field("small", 0.00000000012345678901234568).
IfExists().toCmd(),
Expected: "FSET agent 47 XX small 0.00000000012345678901234568",
},
{
Cmd: keys.FSet("agent", "47").
Field("small", -0.00000000012345678901234568).
IfExists().toCmd(),
Expected: "FSET agent 47 XX small -0.00000000012345678901234568",
},
{
Cmd: keys.JSet("foo", "bar", "some.field", "some-value").Raw().toCmd(),
Expected: "JSET foo bar some.field some-value RAW",
},
{
Cmd: keys.JSet("foo", "bar", "some.field", "some-value").Str().toCmd(),
Expected: "JSET foo bar some.field some-value STR",
},
}
for _, test := range tests {
assert.Equal(t, test.Expected, test.Cmd.String())
}
}