summaryrefslogtreecommitdiff
path: root/jstests/core/geo_validate.js
blob: 3a945b6eae8ce4b3322b0947875579b47ce13d53 (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
/**
 * Test to make sure that invalid geo options are caught.
 *
 * +Inf is explicitly disallowed as a valid value for $min/maxDistance on 5.0, but not on
 * previous versions.
 * @tags: [
 * ]
 */
(function() {
let coll = db.geo_validate;
coll.drop();

assert.commandWorked(coll.createIndex({geo: "2dsphere"}));

//
//
// Make sure we can't do a $within search with an invalid circular region
assert.throws(function() {
    coll.findOne({geo: {$within: {$center: [[0, 0], -1]}}});
});
assert.throws(function() {
    coll.findOne({geo: {$within: {$centerSphere: [[0, 0], -1]}}});
});
assert.throws(function() {
    coll.findOne({geo: {$within: {$center: [[0, 0], NaN]}}});
});
assert.throws(function() {
    coll.findOne({geo: {$within: {$centerSphere: [[0, 0], NaN]}}});
});
assert.throws(function() {
    coll.findOne({geo: {$within: {$center: [[0, 0], -Infinity]}}});
});
assert.throws(function() {
    coll.findOne({geo: {$within: {$centerSphere: [[0, 0], -Infinity]}}});
});

//
//
// Make sure we can't do geo search with invalid point coordinates.
assert.throws(function() {
    coll.findOne({geo: {$within: {$center: [[NaN, 0], 1]}}});
});
assert.throws(function() {
    coll.findOne({geo: {$within: {$centerSphere: [[NaN, 0], 1]}}});
});
assert.throws(function() {
    coll.findOne({geo: {$within: {$center: [[Infinity, 0], 1]}}});
});
assert.throws(function() {
    coll.findOne({geo: {$within: {$centerSphere: [[-Infinity, 0], 1]}}});
});

//
//
// Make sure we can do a $within search with a zero-radius circular region
assert.commandWorked(coll.insert({geo: [0, 0]}));
assert.neq(null, coll.findOne({geo: {$within: {$center: [[0, 0], 0]}}}));
assert.neq(null, coll.findOne({geo: {$within: {$centerSphere: [[0, 0], 0]}}}));
assert.neq(null, coll.findOne({geo: {$within: {$center: [[0, 0], Infinity]}}}));
assert.neq(null, coll.findOne({geo: {$within: {$centerSphere: [[0, 0], Infinity]}}}));

//
//
// Make sure we can't do a $near search with an invalid circular region
assert.throws(function() {
    coll.findOne({geo: {$geoNear: [0, 0, -1]}});
});
assert.throws(function() {
    coll.findOne({geo: {$geoNear: [0, 0], $maxDistance: -1}});
});
assert.throws(function() {
    coll.findOne({geo: {$geoNear: [0, 0, NaN]}});
});
assert.throws(function() {
    coll.findOne({geo: {$geoNear: [0, 0], $maxDistance: NaN}});
});
assert.throws(function() {
    coll.findOne({geo: {$geoNear: [0, 0, -Infinity]}});
});
assert.throws(function() {
    coll.findOne({geo: {$geoNear: [0, 0], $maxDistance: -Infinity}});
});

//
// SERVER-56928 Make sure we can't do a $near search with invalid min/max distance.
assert.throws(function() {
    coll.findOne(
        {geo: {$near: {$geometry: {type: 'Point', coordinates: [0, 0]}, $minDistance: NaN}}});
});

assert.throws(function() {
    coll.findOne(
        {geo: {$near: {$geometry: {type: 'Point', coordinates: [0, 0]}, $maxDistance: NaN}}});
});

const invalidValues = [NaN, -NaN, Infinity, -Infinity];
for (const val of invalidValues) {
    assert.throws(function() {
        coll.findOne({
            geo: {
                $near: {
                    $geometry: {type: 'Point', coordinates: [0, 0]},
                    $minDistance: val,
                    $maxDistance: val,
                }
            }
        });
    }, [], "$near failed with min/maxDistance values of " + val);
}

//
// SERVER-17241 Polygon has no loop
assert.writeError(coll.insert({geo: {type: 'Polygon', coordinates: []}}));

//
// SERVER-17486 Loop has less then 3 vertices.
assert.writeError(coll.insert({geo: {type: 'Polygon', coordinates: [[]]}}));
assert.writeError(coll.insert({geo: {type: 'Polygon', coordinates: [[[0, 0]]]}}));
assert.writeError(
    coll.insert({geo: {type: 'Polygon', coordinates: [[[0, 0], [0, 0], [0, 0], [0, 0]]]}}));
})();