Skip to content

Commit 14eb80a

Browse files
committed
test(model): repro #5765
1 parent e8a29bb commit 14eb80a

File tree

1 file changed

+57
-45
lines changed

1 file changed

+57
-45
lines changed

test/model.geonear.test.js

Lines changed: 57 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
'use strict';
12

2-
var start = require('./common'),
3-
assert = require('power-assert'),
4-
mongoose = start.mongoose,
5-
random = require('../lib/utils').random,
6-
Schema = mongoose.Schema;
3+
var start = require('./common');
4+
var assert = require('power-assert');
5+
var mongoose = start.mongoose;
6+
var Schema = mongoose.Schema;
77

88
/**
99
* Setup
@@ -23,19 +23,28 @@ function metersToRadians(m) {
2323
}
2424

2525
describe('model', function() {
26-
var schema;
27-
28-
function getModel(db) {
29-
return db.model('GeoNear', schema, 'geonear' + random());
30-
}
26+
var schema = new Schema({
27+
coordinates: {type: [Number]},
28+
type: String,
29+
priority: Number
30+
});
31+
schema.index({ coordinates: '2dsphere' }, { background: false });
32+
var db;
3133

3234
before(function() {
33-
schema = new Schema({
34-
coordinates: {type: [Number], index: '2dsphere'},
35-
type: String
36-
});
35+
db = start();
36+
});
37+
38+
after(function(done) {
39+
db.close(done);
3740
});
3841

42+
var count = 0;
43+
function getModel(db) {
44+
++count;
45+
return db.model('GeoNear' + count, schema, 'geonear' + count);
46+
}
47+
3948
var mongo24_or_greater = false;
4049
before(function(done) {
4150
start.mongodVersion(function(err, version) {
@@ -45,16 +54,14 @@ describe('model', function() {
4554
done();
4655
});
4756
});
57+
4858
describe('geoNear', function() {
4959
it('works with legacy coordinate points', function(done) {
5060
if (!mongo24_or_greater) return done();
51-
var db = start();
5261
var Geo = getModel(db);
5362
assert.ok(Geo.geoNear instanceof Function);
5463

55-
Geo.on('index', function(err) {
56-
assert.ifError(err);
57-
64+
Geo.init().then(function() {
5865
var geos = [];
5966
geos[0] = new Geo({
6067
coordinates: testLocations.MONGODB_NYC_OFFICE,
@@ -93,21 +100,18 @@ describe('model', function() {
93100
assert.equal(results[0].obj.coordinates[1], testLocations.MONGODB_NYC_OFFICE[1]);
94101
assert.equal(results[0].obj.id, geos[0].id);
95102
assert.ok(results[0].obj instanceof Geo);
96-
db.close(done);
103+
done();
97104
});
98105
}
99106
});
100107
});
101108

102109
it('works with GeoJSON coordinate points', function(done) {
103110
if (!mongo24_or_greater) return done();
104-
var db = start();
105111
var Geo = getModel(db);
106112
assert.ok(Geo.geoNear instanceof Function);
107113

108-
Geo.on('index', function(err) {
109-
assert.ifError(err);
110-
114+
Geo.init().then(function() {
111115
var geos = [];
112116
geos[0] = new Geo({
113117
coordinates: testLocations.MONGODB_NYC_OFFICE,
@@ -146,21 +150,18 @@ describe('model', function() {
146150
assert.equal(results[0].obj.coordinates[1], testLocations.MONGODB_NYC_OFFICE[1]);
147151
assert.equal(results[0].obj.id, geos[0].id);
148152
assert.ok(results[0].obj instanceof Geo);
149-
db.close(done);
153+
done();
150154
});
151155
}
152-
});
156+
}).catch(done);
153157
});
154158

155159
it('works with lean', function(done) {
156160
if (!mongo24_or_greater) return done();
157-
var db = start();
158161
var Geo = getModel(db);
159162
assert.ok(Geo.geoNear instanceof Function);
160163

161-
Geo.on('index', function(err) {
162-
assert.ifError(err);
163-
164+
Geo.init().then(function() {
164165
var geos = [];
165166
geos[0] = new Geo({
166167
coordinates: testLocations.MONGODB_NYC_OFFICE,
@@ -199,7 +200,7 @@ describe('model', function() {
199200
assert.equal(results[0].obj.coordinates[1], testLocations.MONGODB_NYC_OFFICE[1]);
200201
assert.equal(results[0].obj._id, geos[0].id);
201202
assert.ok(!(results[0].obj instanceof Geo));
202-
db.close(done);
203+
done();
203204
});
204205
}
205206
});
@@ -208,12 +209,9 @@ describe('model', function() {
208209
it('throws the correct error messages', function(done) {
209210
if (!mongo24_or_greater) return done();
210211

211-
var db = start();
212212
var Geo = getModel(db);
213213

214-
Geo.on('index', function(err) {
215-
assert.ifError(err);
216-
214+
Geo.init().then(function() {
217215
var g = new Geo({coordinates: [10, 10], type: 'place'});
218216
g.save(function() {
219217
Geo.geoNear('1,2', {}, function(e) {
@@ -232,33 +230,30 @@ describe('model', function() {
232230
assert.ok(e);
233231
assert.equal(e.message, 'Must pass either a legacy coordinate array or GeoJSON Point to geoNear');
234232

235-
db.close(done);
233+
done();
236234
});
237235
});
238236
});
239237
});
240238
});
241239
});
242240
});
241+
243242
it('returns a promise (gh-1614)', function(done) {
244243
if (!mongo24_or_greater) return done();
245-
var db = start();
246244
var Geo = getModel(db);
247245

248246
var pnt = {type: 'Point', coordinates: testLocations.PORT_AUTHORITY_STATION};
249247
// using GeoJSON point
250248
var prom = Geo.geoNear(pnt, {spherical: true, maxDistance: 300}, function() {});
251249
assert.ok(prom instanceof mongoose.Promise);
252-
db.close();
253250
done();
254251
});
255252

256253
it('allows not passing a callback (gh-1614)', function(done) {
257254
if (!mongo24_or_greater) return done();
258-
var db = start();
259255
var Geo = getModel(db);
260-
Geo.on('index', function(err) {
261-
assert.ifError(err);
256+
Geo.init().then(function() {
262257
var g = new Geo({coordinates: testLocations.MONGODB_NYC_OFFICE, type: 'Point'});
263258
g.save(function(err) {
264259
assert.ifError(err);
@@ -277,19 +272,18 @@ describe('model', function() {
277272
}
278273

279274
function finish() {
280-
db.close(done);
275+
done();
281276
}
282277

283278
promise.then(validate, assert.ifError).then(finish).end();
284279
});
285280
});
286281
});
282+
287283
it('promise fulfill even when no results returned', function(done) {
288284
if (!mongo24_or_greater) return done();
289-
var db = start();
290285
var Geo = getModel(db);
291-
Geo.on('index', function(err) {
292-
assert.ifError(err);
286+
Geo.init().then(function() {
293287
var g = new Geo({coordinates: [1, 1], type: 'Point'});
294288
g.save(function(err) {
295289
assert.ifError(err);
@@ -301,12 +295,30 @@ describe('model', function() {
301295
});
302296

303297
function finish() {
304-
db.close(done);
298+
done();
305299
}
306300

307301
promise.then(finish).end();
308302
});
309303
});
310304
});
305+
306+
it('casts (gh-5765)', function() {
307+
if (!mongo24_or_greater) return done();
308+
var Geo = getModel(db);
309+
Geo.init().then(function() {
310+
var g = new Geo({coordinates: [1, 1], type: 'Point', priority: 1});
311+
g.save(function(error) {
312+
assert.ifError(error);
313+
var opts = { maxDistance: 1000, query: { priority: '1' } };
314+
Geo.geoNear([1, 1], opts, function(error, res) {
315+
assert.ifError(error);
316+
assert.equal(res.length, 1);
317+
assert.equal(res[0].priority, 1);
318+
done();
319+
});
320+
});
321+
});
322+
});
311323
});
312324
});

0 commit comments

Comments
 (0)