-
Notifications
You must be signed in to change notification settings - Fork 16
/
spatial_queries_test.go
207 lines (165 loc) · 5.94 KB
/
spatial_queries_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
package tests
import (
"testing"
ravendb "github.com/ravendb/ravendb-go-client"
"github.com/stretchr/testify/assert"
)
func NewSpatialQueriesInMemoryTestIdx() *ravendb.IndexCreationTask {
res := ravendb.NewIndexCreationTask("SpatialQueriesInMemoryTestIdx")
res.Map = "docs.Listings.Select(listingItem => new {\n" +
" classCodes = listingItem.classCodes,\n" +
" latitude = listingItem.latitude,\n" +
" longitude = listingItem.longitude,\n" +
" coordinates = this.CreateSpatialField(((double ? )((double)(listingItem.latitude))), ((double ? )((double)(listingItem.longitude))))\n" +
"})"
return res
}
func spatialQueriesCanRunSpatialQueriesInMemory(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
err = NewSpatialQueriesInMemoryTestIdx().Execute(store, nil, "")
assert.NoError(t, err)
}
type Listing struct {
ClassCodes string `json:"classCodes"`
Latitude int64 `json:"latitude"`
Longitude int64 `json:"longitude"`
}
func (l *Listing) getClassCodes() string {
return l.ClassCodes
}
func (l *Listing) setClassCodes(classCodes string) {
l.ClassCodes = classCodes
}
func (l *Listing) getLatitude() int64 {
return l.Latitude
}
func (l *Listing) setLatitude(latitude int64) {
l.Latitude = latitude
}
func (l *Listing) getLongitude() int64 {
return l.Longitude
}
func (l *Listing) setLongitude(longitude int64) {
l.Longitude = longitude
}
func spatialQueriesCanSuccessfullyDoSpatialQueryOfNearbyLocations(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
areaOneDocOne := NewDummyGeoDoc(55.6880508001, 13.5717346673)
areaOneDocTwo := NewDummyGeoDoc(55.6821978456, 13.6076183965)
areaOneDocThree := NewDummyGeoDoc(55.673251569, 13.5946697607)
// This item is 12 miles (approx 19 km) from the closest in areaOne
closeButOutsideAreaOne := NewDummyGeoDoc(55.8634157297, 13.5497731987)
// This item is about 3900 miles from areaOne
newYork := NewDummyGeoDoc(40.7137578228, -74.0126901936)
{
session := openSessionMust(t, store)
err = session.Store(areaOneDocOne)
assert.NoError(t, err)
err = session.Store(areaOneDocTwo)
assert.NoError(t, err)
err = session.Store(areaOneDocThree)
assert.NoError(t, err)
err = session.Store(closeButOutsideAreaOne)
assert.NoError(t, err)
err = session.Store(newYork)
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
indexDefinition := ravendb.NewIndexDefinition()
indexDefinition.Name = "FindByLatLng"
indexDefinition.Maps = []string{"from doc in docs select new { coordinates = CreateSpatialField(doc.latitude, doc.longitude) }"}
op := ravendb.NewPutIndexesOperation(indexDefinition)
err = store.Maintenance().Send(op)
assert.NoError(t, err)
// Wait until the index is built
var notUsed []*DummyGeoDoc
q := session.QueryIndex("FindByLatLng")
q = q.WaitForNonStaleResults(0)
err = q.GetResults(¬Used)
assert.NoError(t, err)
lat := float64(55.6836422426)
lng := float64(13.5871808352) // in the middle of AreaOne
radius := float64(5.0)
var nearbyDocs []*DummyGeoDoc
q = session.QueryIndex("FindByLatLng")
q = q.WithinRadiusOf("coordinates", radius, lat, lng)
q = q.WaitForNonStaleResults(0)
err = q.GetResults(&nearbyDocs)
assert.NoError(t, err)
assert.Equal(t, len(nearbyDocs), 3)
session.Close()
}
}
func spatialQueriesCanSuccessfullyQueryByMiles(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
myHouse := NewDummyGeoDoc(44.757767, -93.355322)
// The gym is about 7.32 miles (11.79 kilometers) from my house.
gym := NewDummyGeoDoc(44.682861, -93.25)
{
session := openSessionMust(t, store)
err = session.Store(myHouse)
assert.NoError(t, err)
err = session.Store(gym)
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
indexDefinition := ravendb.NewIndexDefinition()
indexDefinition.Name = "FindByLatLng"
indexDefinition.Maps = []string{"from doc in docs select new { coordinates = CreateSpatialField(doc.latitude, doc.longitude) }"}
op := ravendb.NewPutIndexesOperation(indexDefinition)
err = store.Maintenance().Send(op)
assert.NoError(t, err)
// Wait until the index is built
var notUsed []*DummyGeoDoc
q := session.QueryIndex("FindByLatLng")
q = q.WaitForNonStaleResults(0)
err = q.GetResults(¬Used)
assert.NoError(t, err)
radius := float64(8)
// Find within 8 miles.
// We should find both my house and the gym.
var matchesWithinMiles []*DummyGeoDoc
q = session.QueryIndex("FindByLatLng")
q = q.WithinRadiusOfWithUnits("coordinates", radius, myHouse.Latitude, myHouse.Longitude, ravendb.SpatialUnitsMiles)
q = q.WaitForNonStaleResults(0)
err = q.GetResults(&matchesWithinMiles)
assert.NoError(t, err)
assert.Equal(t, len(matchesWithinMiles), 2)
// Find within 8 kilometers.
// We should find only my house, since the gym is ~11 kilometers out.
var matchesWithinKilometers []*DummyGeoDoc
q = session.QueryIndex("FindByLatLng")
q = q.WithinRadiusOfWithUnits("coordinates", radius, myHouse.Latitude, myHouse.Longitude, ravendb.SpatialUnitsKilometers)
q = q.WaitForNonStaleResults(0)
err = q.GetResults(&matchesWithinKilometers)
assert.NoError(t, err)
assert.Equal(t, len(matchesWithinKilometers), 1)
session.Close()
}
}
type DummyGeoDoc struct {
ID string
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
}
func NewDummyGeoDoc(latitude float64, longitude float64) *DummyGeoDoc {
return &DummyGeoDoc{
Latitude: latitude,
Longitude: longitude,
}
}
func TestSpatialQueries(t *testing.T) {
driver := createTestDriver(t)
destroy := func() { destroyDriver(t, driver) }
defer recoverTest(t, destroy)
// matches order of Java tests
spatialQueriesCanRunSpatialQueriesInMemory(t, driver)
spatialQueriesCanSuccessfullyQueryByMiles(t, driver)
spatialQueriesCanSuccessfullyDoSpatialQueryOfNearbyLocations(t, driver)
}