summaryrefslogtreecommitdiff
path: root/jstests/core/geo_borders.js
blob: f110d58ec00f36557302c255591d0a783397aaaa (plain)
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
// Cannot implicitly shard accessed collections because of use of $near query instead of geoNear
// command.
// @tags: [assumes_unsharded_collection]

t = db.borders;
t.drop();

epsilon = 0.0001;

// For these tests, *required* that step ends exactly on max
min = -1;
max = 1;
step = 1;
numItems = 0;

for (var x = min; x <= max; x += step) {
    for (var y = min; y <= max; y += step) {
        t.insert({loc: {x: x, y: y}});
        numItems++;
    }
}

overallMin = -1;
overallMax = 1;

// Create a point index slightly smaller than the points we have
var res =
    t.ensureIndex({loc: "2d"}, {max: overallMax - epsilon / 2, min: overallMin + epsilon / 2});
assert.commandFailed(res);

// Create a point index only slightly bigger than the points we have
res = t.ensureIndex({loc: "2d"}, {max: overallMax + epsilon, min: overallMin - epsilon});
assert.commandWorked(res);

// ************
// Box Tests
// ************

// If the bounds are bigger than the box itself, just clip at the borders
assert.eq(numItems, t.find({
                         loc: {
                             $within: {
                                 $box: [
                                     [overallMin - 2 * epsilon, overallMin - 2 * epsilon],
                                     [overallMax + 2 * epsilon, overallMax + 2 * epsilon]
                                 ]
                             }
                         }
                     }).count());

// Check this works also for bounds where only a single dimension is off-bounds
assert.eq(numItems - 5, t.find({
                             loc: {
                                 $within: {
                                     $box: [
                                         [overallMin - 2 * epsilon, overallMin - 0.5 * epsilon],
                                         [overallMax - epsilon, overallMax - epsilon]
                                     ]
                                 }
                             }
                         }).count());

// Make sure we can get at least close to the bounds of the index
assert.eq(numItems, t.find({
                         loc: {
                             $within: {
                                 $box: [
                                     [overallMin - epsilon / 2, overallMin - epsilon / 2],
                                     [overallMax + epsilon / 2, overallMax + epsilon / 2]
                                 ]
                             }
                         }
                     }).count());

// Make sure we can get at least close to the bounds of the index
assert.eq(numItems, t.find({
                         loc: {
                             $within: {
                                 $box: [
                                     [overallMax + epsilon / 2, overallMax + epsilon / 2],
                                     [overallMin - epsilon / 2, overallMin - epsilon / 2]
                                 ]
                             }
                         }
                     }).count());

// Check that swapping min/max has good behavior
assert.eq(numItems, t.find({
                         loc: {
                             $within: {
                                 $box: [
                                     [overallMax + epsilon / 2, overallMax + epsilon / 2],
                                     [overallMin - epsilon / 2, overallMin - epsilon / 2]
                                 ]
                             }
                         }
                     }).count());

assert.eq(numItems, t.find({
                         loc: {
                             $within: {
                                 $box: [
                                     [overallMax + epsilon / 2, overallMin - epsilon / 2],
                                     [overallMin - epsilon / 2, overallMax + epsilon / 2]
                                 ]
                             }
                         }
                     }).count());

// **************
// Circle tests
// **************

center = (overallMax + overallMin) / 2;
center = [center, center];
radius = overallMax;

offCenter = [center[0] + radius, center[1] + radius];
onBounds = [offCenter[0] + epsilon, offCenter[1] + epsilon];
offBounds = [onBounds[0] + epsilon, onBounds[1] + epsilon];
onBoundsNeg = [-onBounds[0], -onBounds[1]];

// Make sure we can get all points when radius is exactly at full bounds
assert.lt(0, t.find({loc: {$within: {$center: [center, radius + epsilon]}}}).count());

// Make sure we can get points when radius is over full bounds
assert.lt(0, t.find({loc: {$within: {$center: [center, radius + 2 * epsilon]}}}).count());

// Make sure we can get points when radius is over full bounds, off-centered
assert.lt(0, t.find({loc: {$within: {$center: [offCenter, radius + 2 * epsilon]}}}).count());

// Make sure we get correct corner point when center is in bounds
// (x bounds wrap, so could get other corner)
cornerPt = t.findOne({loc: {$within: {$center: [offCenter, step / 2]}}});
assert.eq(cornerPt.loc.y, overallMax);

// Make sure we get correct corner point when center is on bounds
// NOTE: Only valid points on MIN bounds
cornerPt = t.findOne(
    {loc: {$within: {$center: [onBoundsNeg, Math.sqrt(2 * epsilon * epsilon) + (step / 2)]}}});
assert.eq(cornerPt.loc.y, overallMin);

// Make sure we can't get corner point when center is over bounds
// TODO: SERVER-5800 clean up wrapping rules for different CRS queries - not sure this is an error
/*
assert.throws(function(){
    t.findOne( { loc : { $within : { $center : [ offBounds, Math.sqrt( 8 * epsilon * epsilon ) + (
step / 2 ) ] } } } );
});
*/

// Make sure we can't get corner point when center is on max bounds
// Broken - see SERVER-13581
// assert.throws(function(){
//    t.findOne( { loc : { $within : { $center : [ onBounds, Math.sqrt( 8 * epsilon * epsilon ) + (
//    step / 2 ) ] } } } );
//});

// ***********
// Near tests
// ***********

// Make sure we can get all nearby points to point in range
assert.eq(overallMax, t.find({loc: {$near: offCenter}}).next().loc.y);

// Make sure we can get all nearby points to point on boundary
assert.eq(overallMin, t.find({loc: {$near: onBoundsNeg}}).next().loc.y);

// Make sure we can't get all nearby points to point over boundary
// TODO: SERVER-9986 clean up wrapping rules for different CRS queries - not sure this is an error
/*
assert.throws(function(){
    t.findOne( { loc : { $near : offBounds } } );
});
*/

// Make sure we can't get all nearby points to point on max boundary
// Broken - see SERVER-13581
// assert.throws(function(){
//    t.findOne( { loc : { $near : onBoundsNeg } } );
//});

// Make sure we can get all nearby points within one step (4 points in top
// corner)
assert.eq(4, t.find({loc: {$near: offCenter, $maxDistance: step * 1.9}}).count());

// **************
// Command Tests
// **************
// Make sure we can get all nearby points to point in range
assert.eq(overallMax, db.runCommand({geoNear: "borders", near: offCenter}).results[0].obj.loc.y);

// Make sure we can get all nearby points to point on boundary
assert.eq(overallMin, db.runCommand({geoNear: "borders", near: onBoundsNeg}).results[0].obj.loc.y);

// Make sure we can't get all nearby points to point over boundary
// TODO: SERVER-9986 clean up wrapping rules for different CRS queries - not sure this is an error
/*
assert.commandFailed( db.runCommand( { geoNear : "borders", near : offBounds } ));
*/

// Make sure we can't get all nearby points to point on max boundary
assert.commandWorked(db.runCommand({geoNear: "borders", near: onBounds}));

// Make sure we can get all nearby points within one step (4 points in top
// corner)
assert.eq(
    4,
    db.runCommand({geoNear: "borders", near: offCenter, maxDistance: step * 1.5}).results.length);