-
Notifications
You must be signed in to change notification settings - Fork 1
/
geocollection_test.go
295 lines (283 loc) · 9.29 KB
/
geocollection_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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright 2023 SpotHero
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package geocollection
import (
"testing"
"github.com/golang/geo/s2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type testCell = struct {
cellID s2.CellID
lat, lon float64
}
// cell IDs were generated using the Sidewalk Labs s2 demo
// https://s2.sidewalklabs.com/regioncoverer/
var (
// downtown Chicago
cell1 = testCell{
cellID: s2.CellIDFromToken("880e2cbc904a0c29"),
lat: 41.87963549397698,
lon: -87.63028184499035,
}
// midtown Manhattan
cell2 = testCell{
cellID: s2.CellIDFromToken("89c25900437"),
lat: 40.75306726395187,
lon: -73.98119781456353,
}
)
type testItem struct {
contents string
key int
lat float64
lon float64
}
func TestCollection_Set(t *testing.T) {
type cellContains struct {
item testItem
cellID s2.CellID
}
tests := []struct {
name string
items []testItem
expectedCellIDContains []cellContains
}{
{
name: "Should set an item",
items: []testItem{{contents: "0", lat: cell1.lat, lon: cell1.lon}},
expectedCellIDContains: []cellContains{{cellID: cell1.cellID, item: testItem{contents: "0", lat: cell1.lat, lon: cell1.lon}}},
}, {
name: "Should set multiple items",
items: []testItem{
{contents: "0", lat: cell1.lat, lon: cell1.lon},
{key: 1, contents: "1", lat: cell2.lat, lon: cell2.lon},
},
expectedCellIDContains: []cellContains{
{cellID: cell1.cellID, item: testItem{contents: "0", lat: cell1.lat, lon: cell1.lon}},
{cellID: cell2.cellID, item: testItem{key: 1, contents: "1", lat: cell2.lat, lon: cell2.lon}}},
}, {
name: "Should replace an item's coordinates",
items: []testItem{
{contents: "0", lat: cell1.lat, lon: cell1.lon},
{contents: "0", lat: cell2.lat, lon: cell2.lon},
},
expectedCellIDContains: []cellContains{{cellID: cell2.cellID, item: testItem{contents: "0", lat: cell2.lat, lon: cell2.lon}}},
}, {
name: "Should replace an item's contents only",
items: []testItem{
{contents: "0", lat: cell1.lat, lon: cell1.lon},
{contents: "1", lat: cell1.lat, lon: cell1.lon},
},
expectedCellIDContains: []cellContains{{cellID: cell1.cellID, item: testItem{contents: "1", lat: cell1.lat, lon: cell1.lon}}},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cl := NewCollection()
for _, item := range test.items {
cl.Set(item.key, item.contents, item.lat, item.lon)
}
assert.Len(t, cl.keys, len(test.expectedCellIDContains))
// assert that the location's cell has been cached at every cell level (31 of them)
assert.Len(t, cl.cells, 31)
for _, expectedContains := range test.expectedCellIDContains {
expectedCellID := expectedContains.cellID
assert.Contains(t, cl.keys, expectedContains.item.key)
require.Contains(t, cl.cells[expectedCellID.Level()][expectedCellID.Pos()], expectedContains.item.key)
assert.Contains(t, cl.cells[expectedCellID.Level()], expectedCellID.Pos())
require.Contains(t, cl.items, expectedContains.item.key)
assert.Equal(
t,
cl.items[expectedContains.item.key],
collectionContents{
contents: expectedContains.item.contents,
latitude: expectedContains.item.lat,
longitude: expectedContains.item.lon,
},
)
}
})
}
}
func TestCollection_Delete(t *testing.T) {
cell := cell1
item := testItem{key: 0, lat: cell.lat, lon: cell.lon}
tests := []struct {
name string
expectedRemainingKeys []int
deleteKey int
}{
{
name: "Should delete an item",
deleteKey: item.key,
expectedRemainingKeys: []int{},
}, {
name: "Deleting an item that does not exist should be ok",
deleteKey: item.key + 1,
expectedRemainingKeys: []int{item.key},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cl := NewCollection()
cl.Set(item.key, item.contents, item.lat, item.lon)
cl.Delete(test.deleteKey)
assert.NotContains(t, cl.keys, test.deleteKey)
for level := maxCellLevel; level >= 0; level-- {
assert.NotContains(t, cl.cells[level][cell.cellID.Pos()], test.deleteKey)
for _, remainingID := range test.expectedRemainingKeys {
assert.Contains(t, cl.cells[level][cell.cellID.Parent(level).Pos()], remainingID)
}
}
for _, remainingID := range test.expectedRemainingKeys {
assert.Contains(t, cl.keys, remainingID)
}
})
}
}
func TestCollection_ItemsWithinDistance(t *testing.T) {
item1 := testItem{key: 0, contents: "1", lat: cell1.lat, lon: cell1.lon}
item2 := testItem{key: 1, contents: "2", lat: cell2.lat, lon: cell2.lon}
tests := []struct {
name string
expectedContents []string
searchLat float64
searchLon float64
distanceMeters float64
useFastAlgorithm bool
}{
{
name: "Search should return relevant results",
searchLat: cell1.lat - 0.001,
searchLon: cell1.lon - 0.001,
distanceMeters: 1000,
useFastAlgorithm: false,
expectedContents: []string{item1.contents},
},
{
name: "Search should return relevant with the fast cover algorithm",
searchLat: cell1.lat - 0.001,
searchLon: cell1.lon - 0.001,
distanceMeters: 1000,
useFastAlgorithm: true,
expectedContents: []string{item1.contents},
}, {
name: "Search should return multiple relevant results",
searchLat: cell1.lat,
searchLon: cell1.lon,
distanceMeters: 4000000,
useFastAlgorithm: false,
expectedContents: []string{item1.contents, item2.contents},
}, {
name: "Search should return no results when no items are close by",
searchLat: 0,
searchLon: 0,
distanceMeters: 1,
useFastAlgorithm: false,
expectedContents: []string{},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cl := NewCollection()
cl.Set(item1.key, item1.contents, item1.lat, item1.lon)
cl.Set(item2.key, item2.contents, item2.lat, item2.lon)
results, _ := cl.ItemsWithinDistance(
test.searchLat, test.searchLon, test.distanceMeters, SearchCoveringParameters{
MaxLevel: 5, MinLevel: 5, LevelMod: 1, MaxCells: 5, UseFastCovering: test.useFastAlgorithm})
assert.Len(t, results, len(test.expectedContents))
for _, content := range test.expectedContents {
assert.Contains(t, results, content)
}
})
}
}
func TestCollection_ItemByKey(t *testing.T) {
c := NewCollection()
c.items[1] = collectionContents{contents: "1"}
tests := []struct {
key interface{}
expectedContents interface{}
name string
}{
{name: "Item is retrieved from collection by its key", key: 1, expectedContents: "1"},
{name: "No key exists returns nil", key: 2},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.expectedContents, c.ItemByKey(test.key))
})
}
}
func TestCollection_GetItems(t *testing.T) {
c := NewCollection()
// using the same contents value because map to slice isn't ordered always.
item1 := testItem{key: 0, contents: "1", lat: cell1.lat, lon: cell1.lon}
item2 := testItem{key: 1, contents: "1", lat: cell2.lat, lon: cell2.lon}
c.Set(item1.key, item1.contents, item1.lat, item1.lon)
c.Set(item2.key, item2.contents, item2.lat, item2.lon)
tests := []struct {
name string
expectedContents []string
startIndex int
pageSize int
}{
{
name: "All items are retrieved from collection",
expectedContents: []string{"1", "1"},
startIndex: 0,
pageSize: 10,
},
{
name: "All items are retrieved from startIndex",
expectedContents: []string{"1"},
startIndex: 1,
pageSize: 10,
},
{
name: "startIndex greater than length of the array",
expectedContents: []string{},
startIndex: 2,
pageSize: 10,
},
{
name: "pageIndex is less than the length",
expectedContents: []string{"1"},
startIndex: 0,
pageSize: 1,
},
{
name: "pageIndex and startIndex is less than the length",
expectedContents: []string{"1"},
startIndex: 1,
pageSize: 1,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
results := c.GetItems(test.pageSize, test.startIndex)
assert.Len(t, results, len(test.expectedContents))
for _, content := range test.expectedContents {
assert.Contains(t, results, content)
}
})
}
}
func TestEarthDistanceMeters(t *testing.T) {
// pick 2 points off a map that are roughly 105 meters of each other
p1 := NewPointFromLatLng(41.883170, -87.632278)
p2 := NewPointFromLatLng(41.883178, -87.630916)
assert.InDelta(t, 105, EarthDistanceMeters(p1, p2), 10)
}