1
+ 'use strict' ;
1
2
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 ;
7
7
8
8
/**
9
9
* Setup
@@ -23,19 +23,28 @@ function metersToRadians(m) {
23
23
}
24
24
25
25
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 ;
31
33
32
34
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 ) ;
37
40
} ) ;
38
41
42
+ var count = 0 ;
43
+ function getModel ( db ) {
44
+ ++ count ;
45
+ return db . model ( 'GeoNear' + count , schema , 'geonear' + count ) ;
46
+ }
47
+
39
48
var mongo24_or_greater = false ;
40
49
before ( function ( done ) {
41
50
start . mongodVersion ( function ( err , version ) {
@@ -45,16 +54,14 @@ describe('model', function() {
45
54
done ( ) ;
46
55
} ) ;
47
56
} ) ;
57
+
48
58
describe ( 'geoNear' , function ( ) {
49
59
it ( 'works with legacy coordinate points' , function ( done ) {
50
60
if ( ! mongo24_or_greater ) return done ( ) ;
51
- var db = start ( ) ;
52
61
var Geo = getModel ( db ) ;
53
62
assert . ok ( Geo . geoNear instanceof Function ) ;
54
63
55
- Geo . on ( 'index' , function ( err ) {
56
- assert . ifError ( err ) ;
57
-
64
+ Geo . init ( ) . then ( function ( ) {
58
65
var geos = [ ] ;
59
66
geos [ 0 ] = new Geo ( {
60
67
coordinates : testLocations . MONGODB_NYC_OFFICE ,
@@ -93,21 +100,18 @@ describe('model', function() {
93
100
assert . equal ( results [ 0 ] . obj . coordinates [ 1 ] , testLocations . MONGODB_NYC_OFFICE [ 1 ] ) ;
94
101
assert . equal ( results [ 0 ] . obj . id , geos [ 0 ] . id ) ;
95
102
assert . ok ( results [ 0 ] . obj instanceof Geo ) ;
96
- db . close ( done ) ;
103
+ done ( ) ;
97
104
} ) ;
98
105
}
99
106
} ) ;
100
107
} ) ;
101
108
102
109
it ( 'works with GeoJSON coordinate points' , function ( done ) {
103
110
if ( ! mongo24_or_greater ) return done ( ) ;
104
- var db = start ( ) ;
105
111
var Geo = getModel ( db ) ;
106
112
assert . ok ( Geo . geoNear instanceof Function ) ;
107
113
108
- Geo . on ( 'index' , function ( err ) {
109
- assert . ifError ( err ) ;
110
-
114
+ Geo . init ( ) . then ( function ( ) {
111
115
var geos = [ ] ;
112
116
geos [ 0 ] = new Geo ( {
113
117
coordinates : testLocations . MONGODB_NYC_OFFICE ,
@@ -146,21 +150,18 @@ describe('model', function() {
146
150
assert . equal ( results [ 0 ] . obj . coordinates [ 1 ] , testLocations . MONGODB_NYC_OFFICE [ 1 ] ) ;
147
151
assert . equal ( results [ 0 ] . obj . id , geos [ 0 ] . id ) ;
148
152
assert . ok ( results [ 0 ] . obj instanceof Geo ) ;
149
- db . close ( done ) ;
153
+ done ( ) ;
150
154
} ) ;
151
155
}
152
- } ) ;
156
+ } ) . catch ( done ) ;
153
157
} ) ;
154
158
155
159
it ( 'works with lean' , function ( done ) {
156
160
if ( ! mongo24_or_greater ) return done ( ) ;
157
- var db = start ( ) ;
158
161
var Geo = getModel ( db ) ;
159
162
assert . ok ( Geo . geoNear instanceof Function ) ;
160
163
161
- Geo . on ( 'index' , function ( err ) {
162
- assert . ifError ( err ) ;
163
-
164
+ Geo . init ( ) . then ( function ( ) {
164
165
var geos = [ ] ;
165
166
geos [ 0 ] = new Geo ( {
166
167
coordinates : testLocations . MONGODB_NYC_OFFICE ,
@@ -199,7 +200,7 @@ describe('model', function() {
199
200
assert . equal ( results [ 0 ] . obj . coordinates [ 1 ] , testLocations . MONGODB_NYC_OFFICE [ 1 ] ) ;
200
201
assert . equal ( results [ 0 ] . obj . _id , geos [ 0 ] . id ) ;
201
202
assert . ok ( ! ( results [ 0 ] . obj instanceof Geo ) ) ;
202
- db . close ( done ) ;
203
+ done ( ) ;
203
204
} ) ;
204
205
}
205
206
} ) ;
@@ -208,12 +209,9 @@ describe('model', function() {
208
209
it ( 'throws the correct error messages' , function ( done ) {
209
210
if ( ! mongo24_or_greater ) return done ( ) ;
210
211
211
- var db = start ( ) ;
212
212
var Geo = getModel ( db ) ;
213
213
214
- Geo . on ( 'index' , function ( err ) {
215
- assert . ifError ( err ) ;
216
-
214
+ Geo . init ( ) . then ( function ( ) {
217
215
var g = new Geo ( { coordinates : [ 10 , 10 ] , type : 'place' } ) ;
218
216
g . save ( function ( ) {
219
217
Geo . geoNear ( '1,2' , { } , function ( e ) {
@@ -232,33 +230,30 @@ describe('model', function() {
232
230
assert . ok ( e ) ;
233
231
assert . equal ( e . message , 'Must pass either a legacy coordinate array or GeoJSON Point to geoNear' ) ;
234
232
235
- db . close ( done ) ;
233
+ done ( ) ;
236
234
} ) ;
237
235
} ) ;
238
236
} ) ;
239
237
} ) ;
240
238
} ) ;
241
239
} ) ;
242
240
} ) ;
241
+
243
242
it ( 'returns a promise (gh-1614)' , function ( done ) {
244
243
if ( ! mongo24_or_greater ) return done ( ) ;
245
- var db = start ( ) ;
246
244
var Geo = getModel ( db ) ;
247
245
248
246
var pnt = { type : 'Point' , coordinates : testLocations . PORT_AUTHORITY_STATION } ;
249
247
// using GeoJSON point
250
248
var prom = Geo . geoNear ( pnt , { spherical : true , maxDistance : 300 } , function ( ) { } ) ;
251
249
assert . ok ( prom instanceof mongoose . Promise ) ;
252
- db . close ( ) ;
253
250
done ( ) ;
254
251
} ) ;
255
252
256
253
it ( 'allows not passing a callback (gh-1614)' , function ( done ) {
257
254
if ( ! mongo24_or_greater ) return done ( ) ;
258
- var db = start ( ) ;
259
255
var Geo = getModel ( db ) ;
260
- Geo . on ( 'index' , function ( err ) {
261
- assert . ifError ( err ) ;
256
+ Geo . init ( ) . then ( function ( ) {
262
257
var g = new Geo ( { coordinates : testLocations . MONGODB_NYC_OFFICE , type : 'Point' } ) ;
263
258
g . save ( function ( err ) {
264
259
assert . ifError ( err ) ;
@@ -277,19 +272,18 @@ describe('model', function() {
277
272
}
278
273
279
274
function finish ( ) {
280
- db . close ( done ) ;
275
+ done ( ) ;
281
276
}
282
277
283
278
promise . then ( validate , assert . ifError ) . then ( finish ) . end ( ) ;
284
279
} ) ;
285
280
} ) ;
286
281
} ) ;
282
+
287
283
it ( 'promise fulfill even when no results returned' , function ( done ) {
288
284
if ( ! mongo24_or_greater ) return done ( ) ;
289
- var db = start ( ) ;
290
285
var Geo = getModel ( db ) ;
291
- Geo . on ( 'index' , function ( err ) {
292
- assert . ifError ( err ) ;
286
+ Geo . init ( ) . then ( function ( ) {
293
287
var g = new Geo ( { coordinates : [ 1 , 1 ] , type : 'Point' } ) ;
294
288
g . save ( function ( err ) {
295
289
assert . ifError ( err ) ;
@@ -301,12 +295,30 @@ describe('model', function() {
301
295
} ) ;
302
296
303
297
function finish ( ) {
304
- db . close ( done ) ;
298
+ done ( ) ;
305
299
}
306
300
307
301
promise . then ( finish ) . end ( ) ;
308
302
} ) ;
309
303
} ) ;
310
304
} ) ;
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
+ } ) ;
311
323
} ) ;
312
324
} ) ;
0 commit comments