forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.geonear.test.js
264 lines (219 loc) · 8.36 KB
/
model.geonear.test.js
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
var start = require('./common')
, assert = require('assert')
, mongoose = start.mongoose
, random = require('../lib/utils').random
, Schema = mongoose.Schema
, DocumentObjectId = mongoose.Types.ObjectId
/**
* Setup
*/
var schema = new Schema({
coordinates : { type: [Number], index: '2dsphere' },
type: String
});
function getModel (db) {
return db.model('GeoNear', schema, 'geonear'+random());
}
describe('model', function(){
var mongo24_or_greater = false;
before(function(done){
start.mongodVersion(function (err, version) {
if (err) throw err;
mongo24_or_greater = 2 < version[0] || (2 == version[0] && 4 <= version[1]);
if (!mongo24_or_greater) console.log('not testing mongodb 2.4 features');
done();
})
})
describe('geoNear', function () {
it('works with legacy coordinate points', function (done) {
if (!mongo24_or_greater) return done();
var db = start();
var Geo = getModel(db);
assert.ok(Geo.geoNear instanceof Function);
Geo.on('index', function(err){
assert.ifError(err);
var geos = [];
geos[0] = new Geo({ coordinates : [10,10], type : "Point"});
geos[1] = new Geo({ coordinates : [15,5], type : "Point"});
geos[2] = new Geo({ coordinates : [20,15], type : "Point"});
geos[3] = new Geo({ coordinates : [1,-1], type : "Point"});
var count = geos.length;
for (var i=0; i < geos.length; i++) {
geos[i].save(function (err) {
assert.ifError(err);
--count || next();
});
}
function next() {
Geo.geoNear([9,9], { spherical : true, maxDistance : .1 }, function (err, results, stats) {
assert.ifError(err);
assert.equal(1, results.length);
assert.equal(results[0].obj.type, 'Point');
assert.equal(results[0].obj.coordinates.length, 2);
assert.equal(results[0].obj.coordinates[0], 10);
assert.equal(results[0].obj.coordinates[1], 10);
assert.equal(results[0].obj.id, geos[0].id);
assert.ok(results[0].obj instanceof Geo);
done();
});
}
});
});
it('works with GeoJSON coordinate points', function (done) {
if (!mongo24_or_greater) return done();
var db = start();
var Geo = getModel(db);
assert.ok(Geo.geoNear instanceof Function);
Geo.on('index', function(err){
assert.ifError(err);
var geos = [];
geos[0] = new Geo({ coordinates : [10,10], type : "Point"});
geos[1] = new Geo({ coordinates : [15,5], type : "Point"});
geos[2] = new Geo({ coordinates : [20,15], type : "Point"});
geos[3] = new Geo({ coordinates : [1,-1], type : "Point"});
var count = geos.length;
for (var i=0; i < geos.length; i++) {
geos[i].save(function () {
--count || next();
});
}
function next() {
var pnt = { type : "Point", coordinates : [9,9] };
Geo.geoNear(pnt, { spherical : true, maxDistance : .1 }, function (err, results, stats) {
assert.ifError(err);
assert.equal(1, results.length);
assert.equal(results[0].obj.type, 'Point');
assert.equal(results[0].obj.coordinates.length, 2);
assert.equal(results[0].obj.coordinates[0], 10);
assert.equal(results[0].obj.coordinates[1], 10);
assert.equal(results[0].obj.id, geos[0].id);
assert.ok(results[0].obj instanceof Geo);
done();
});
}
});
});
it('works with lean', function (done) {
if (!mongo24_or_greater) return done();
var db = start();
var Geo = getModel(db);
assert.ok(Geo.geoNear instanceof Function);
Geo.on('index', function(err){
assert.ifError(err);
var geos = [];
geos[0] = new Geo({ coordinates : [10,10], type : "Point"});
geos[1] = new Geo({ coordinates : [15,5], type : "Point"});
geos[2] = new Geo({ coordinates : [20,15], type : "Point"});
geos[3] = new Geo({ coordinates : [1,-1], type : "Point"});
var count = geos.length;
for (var i=0; i < geos.length; i++) {
geos[i].save(function () {
--count || next();
});
}
function next() {
var pnt = { type : "Point", coordinates : [9,9] };
Geo.geoNear(pnt, { spherical : true, maxDistance : .1, lean : true }, function (err, results, stats) {
assert.ifError(err);
assert.equal(1, results.length);
assert.equal(results[0].obj.type, 'Point');
assert.equal(results[0].obj.coordinates.length, 2);
assert.equal(results[0].obj.coordinates[0], 10);
assert.equal(results[0].obj.coordinates[1], 10);
assert.equal(results[0].obj._id, geos[0].id);
assert.ok(!(results[0].obj instanceof Geo));
done();
});
}
});
});
it('throws the correct error messages', function (done) {
if (!mongo24_or_greater) return done();
var db = start();
var Geo = getModel(db);
Geo.on('index', function(err){
assert.ifError(err);
var g = new Geo({ coordinates : [10,10], type : "place"});
g.save(function() {
var threw = false;
Geo.geoNear("1,2", {}, function (e) {
assert.ok(e);
assert.equal(e.message, "Must pass either a legacy coordinate array or GeoJSON Point to geoNear");
Geo.geoNear([1], {}, function (e) {
assert.ok(e);
assert.equal(e.message, "If using legacy coordinates, must be an array of size 2 for geoNear");
Geo.geoNear({ type : "Square" }, {}, function (e) {
assert.ok(e);
assert.equal(e.message, "Must pass either a legacy coordinate array or GeoJSON Point to geoNear");
Geo.geoNear({ type : "Point", coordinates : "1,2" }, {}, function (e) {
assert.ok(e);
assert.equal(e.message, "Must pass either a legacy coordinate array or GeoJSON Point to geoNear");
done();
});
});
});
});
});
});
});
it('returns a promise (gh-1614)', function(done){
if (!mongo24_or_greater) return done();
var db = start();
var Geo = getModel(db);
var pnt = { type : "Point", coordinates : [9,9] };
var prom = Geo.geoNear(pnt, { spherical : true, maxDistance : .1 }, function (err, results, stats) {
});
assert.ok(prom instanceof mongoose.Promise);
db.close();
done();
})
it('allows not passing a callback (gh-1614)', function (done) {
if (!mongo24_or_greater) return done();
var db = start();
var Geo = getModel(db);
Geo.on('index', function(err) {
assert.ifError(err);
var g = new Geo({ coordinates : [10,10], type : "Point"});
g.save(function (err) {
assert.ifError(err);
var pnt = { type : "Point", coordinates : [9,9] };
var promise;
assert.doesNotThrow(function() {
promise = Geo.geoNear(pnt, { spherical : true, maxDistance : 100000 });
});
function validate(ret, stat) {
assert.equal(1, ret.length);
assert.equal(ret[0].obj.coordinates[0], 10);
assert.equal(ret[0].obj.coordinates[1], 10);
assert.ok(stat);
}
function finish() {
db.close(done);
}
promise.then(validate, assert.ifError).then(finish).end();
});
});
});
it('promise fulfill even when no results returned', function(done){
if (!mongo24_or_greater) return done();
var db = start();
var Geo = getModel(db);
Geo.on('index', function(err) {
assert.ifError(err);
var g = new Geo({ coordinates : [1,1], type : "Point"});
g.save(function (err) {
assert.ifError(err);
var pnt = { type : "Point", coordinates : [90, 45] };
var promise;
assert.doesNotThrow(function() {
promise = Geo.geoNear(pnt, { spherical : true, maxDistance : 0.1 });
});
function finish() {
db.close(done);
}
promise.then(finish).end();
});
});
})
});
});