summaryrefslogtreecommitdiff
path: root/jstests/core/geo_mindistance_boundaries.js
diff options
context:
space:
mode:
authorRandolph Tan <randolph@10gen.com>2014-01-14 14:09:42 -0500
committerRandolph Tan <randolph@10gen.com>2014-02-28 16:26:33 -0500
commit5595b945603b0712c537787e31e6da661c424fee (patch)
tree90945ee3fe4931032f3af2d397bb755fbf5d30ef /jstests/core/geo_mindistance_boundaries.js
parentcd62080dcb036e83f8fca6d68d9bcab67bf7a21c (diff)
downloadmongo-5595b945603b0712c537787e31e6da661c424fee.tar.gz
SERVER-12127 migrate js tests to jscore suite when not related to writes
Moved test jstest/[a-i].js -> jstests/core/ and made changes to comply with write command api
Diffstat (limited to 'jstests/core/geo_mindistance_boundaries.js')
-rw-r--r--jstests/core/geo_mindistance_boundaries.js124
1 files changed, 124 insertions, 0 deletions
diff --git a/jstests/core/geo_mindistance_boundaries.js b/jstests/core/geo_mindistance_boundaries.js
new file mode 100644
index 00000000000..80e933827b6
--- /dev/null
+++ b/jstests/core/geo_mindistance_boundaries.js
@@ -0,0 +1,124 @@
+/* Test boundary conditions for $minDistance option for $near and $nearSphere
+ * queries. SERVER-9395.
+*/
+var t = db.geo_mindistance_boundaries;
+t.drop();
+t.insert({loc: [1, 0]}); // 1 degree of longitude from origin.
+
+/* $minDistance is supported for 2dsphere index only, not 2d or geoHaystack. */
+t.ensureIndex({loc: "2dsphere"});
+
+//
+// Useful constants.
+//
+
+var km = 1000,
+ earthRadiusMeters = 6378.1 * km,
+ geoJSONPoint = {type: 'Point', coordinates: [0, 0]},
+ // One degree of longitude at the equator, about 111 km.
+ degreeInMeters = 2 * Math.PI * earthRadiusMeters / 360,
+ metersEpsilon = Number.MIN_VALUE;
+
+/* Grow epsilon's exponent until epsilon exceeds the margin of error for the
+ * representation of degreeInMeters. The server uses 64-bit math, too, so we'll
+ * find the smallest epsilon the server can detect.
+*/
+while (degreeInMeters + metersEpsilon == degreeInMeters) { metersEpsilon *= 2; }
+
+//
+// Test boundary conditions for $near and GeoJSON, in meters.
+//
+
+
+// minDistance must be within the args to $near, not on the side.
+assert.throws(function() { t.find({loc:{$near:{$geometry: geoJSONPoint},
+ $minDistance:0.1}}).itcount();});
+
+assert.eq(
+ 1, t.find({loc: {
+ $near: {$geometry: geoJSONPoint,
+ $minDistance: degreeInMeters
+ }}}).itcount(),
+ "Expected to find (0, 1) within $minDistance 1 degree from origin"
+);
+
+assert.eq(
+ 1, t.find({loc: {
+ $near: {$geometry: geoJSONPoint,
+ $minDistance: degreeInMeters - metersEpsilon
+ }}}).itcount(),
+ "Expected to find (0, 1) within $minDistance (1 degree - epsilon) from origin"
+);
+
+assert.eq(
+ 0, t.find({loc: {
+ $near: {$geometry: geoJSONPoint,
+ $minDistance: degreeInMeters + metersEpsilon
+ }}}).itcount(),
+ "Expected *not* to find (0, 1) within $minDistance (1 degree + epsilon) from origin"
+);
+
+//
+// Test boundary conditions for $nearSphere and GeoJSON, in meters.
+//
+
+assert.eq(
+ 1, t.find({loc: {
+ $nearSphere: {$geometry: geoJSONPoint,
+ $minDistance: degreeInMeters
+ }}}).itcount(),
+ "Expected to find (0, 1) within $minDistance 1 degree from origin"
+);
+
+assert.eq(
+ 1, t.find({loc: {
+ $nearSphere: geoJSONPoint,
+ $minDistance: degreeInMeters - metersEpsilon
+ }}).itcount(),
+ "Expected to find (0, 1) within $minDistance (1 degree - epsilon) from origin"
+);
+
+assert.eq(
+ 0, t.find({loc: {
+ $nearSphere: geoJSONPoint,
+ $minDistance: degreeInMeters + metersEpsilon
+ }}).itcount(),
+ "Expected *not* to find (0, 1) within $minDistance (1 degree + epsilon) from origin"
+);
+
+//
+// Test boundary conditions for $nearSphere and a legacy point, in radians.
+//
+// $minDistance with legacy point requires $nearSphere; $near not
+// supported.
+//
+
+var legacyPoint = [0, 0],
+ degreeInRadians = 2 * Math.PI / 360,
+ radiansEpsilon = Number.MIN_VALUE;
+
+while (1 + radiansEpsilon == 1) { radiansEpsilon *= 2; }
+
+assert.eq(
+ 1, t.find({loc: {
+ $nearSphere: legacyPoint,
+ $minDistance: degreeInRadians
+ }}).itcount(),
+ "Expected to find (0, 1) within $minDistance 1 degree from origin"
+);
+
+assert.eq(
+ 1, t.find({loc: {
+ $nearSphere: legacyPoint,
+ $minDistance: degreeInRadians - radiansEpsilon
+ }}).itcount(),
+ "Expected to find (0, 1) within $minDistance (1 degree - epsilon) from origin"
+);
+
+assert.eq(
+ 0, t.find({loc: {
+ $nearSphere: legacyPoint,
+ $minDistance: degreeInRadians + radiansEpsilon
+ }}).itcount(),
+ "Expected *not* to find (0, 1) within $minDistance (1 degree + epsilon) from origin"
+);