-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
model.geosearch.test.js
185 lines (154 loc) · 5.54 KB
/
model.geosearch.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
'use strict';
const start = require('./common');
const assert = require('assert');
const random = require('../lib/utils').random;
const mongoose = start.mongoose;
const Schema = mongoose.Schema;
describe('model', function() {
let db, schema;
function getModel(db) {
return db.model('GeoSearch', schema, 'geosearch-' + random());
}
before(function() {
schema = new Schema({
pos: [Number],
complex: {},
type: String
});
schema.index({pos: 'geoHaystack', type: 1}, {bucketSize: 1});
db = start();
});
after(function(done) {
db.close(done);
});
describe('geoSearch', function() {
this.timeout(process.env.TRAVIS ? 8000 : 4500);
it('works', function(done) {
const Geo = getModel(db);
assert.ok(Geo.geoSearch instanceof Function);
Geo.on('index', function(err) {
assert.ifError(err);
const geos = [];
geos[0] = new Geo({pos: [10, 10], type: 'place'});
geos[1] = new Geo({pos: [15, 5], type: 'place'});
geos[2] = new Geo({pos: [20, 15], type: 'house'});
geos[3] = new Geo({pos: [1, -1], type: 'house'});
let count = geos.length;
for (let i = 0; i < geos.length; i++) {
geos[i].save(function(err) {
assert.ifError(err);
--count || next();
});
}
function next() {
Geo.geoSearch({type: 'place'}, {near: [9, 9], maxDistance: 5}, function(err, results) {
assert.ifError(err);
assert.equal(results.length, 1);
assert.equal(results[0].type, 'place');
assert.equal(results[0].pos.length, 2);
assert.equal(results[0].pos[0], 10);
assert.equal(results[0].pos[1], 10);
assert.equal(results[0].id, geos[0].id);
assert.ok(results[0] instanceof Geo);
Geo.geoSearch({type: 'place'}, {near: [40, 40], maxDistance: 5}, function(err, results) {
assert.ifError(err);
assert.equal(results.length, 0);
done();
});
});
}
});
});
it('works with lean', function(done) {
const Geo = getModel(db);
assert.ok(Geo.geoSearch instanceof Function);
Geo.on('index', function(err) {
assert.ifError(err);
const geos = [];
geos[0] = new Geo({pos: [10, 10], type: 'place'});
geos[1] = new Geo({pos: [15, 5], type: 'place'});
geos[2] = new Geo({pos: [20, 15], type: 'house'});
geos[3] = new Geo({pos: [1, -1], type: 'house'});
let count = geos.length;
for (let i = 0; i < geos.length; i++) {
geos[i].save(function(err) {
assert.ifError(err);
--count || next();
});
}
function next() {
Geo.geoSearch({type: 'place'}, {near: [9, 9], maxDistance: 5, lean: true}, function(err, results) {
assert.ifError(err);
assert.equal(results.length, 1);
assert.equal(results[0].type, 'place');
assert.equal(results[0].pos.length, 2);
assert.equal(results[0].pos[0], 10);
assert.equal(results[0].pos[1], 10);
assert.equal(results[0]._id, geos[0].id);
assert.strictEqual(results[0].id, undefined);
assert.ok(!(results[0] instanceof Geo));
done();
});
}
});
});
it('throws the correct error messages', function(done) {
const Geo = getModel(db);
assert.ok(Geo.geoSearch instanceof Function);
Geo.on('index', function(err) {
assert.ifError(err);
const g = new Geo({pos: [10, 10], type: 'place'});
g.save(function() {
Geo.geoSearch([], {}, function(e) {
assert.ok(e);
assert.equal(e.message, 'Must pass conditions to geoSearch');
Geo.geoSearch({type: 'test'}, {}, function(e) {
assert.ok(e);
assert.equal(e.message, 'Must specify the near option in geoSearch');
Geo.geoSearch({type: 'test'}, {near: 'hello'}, function(e) {
assert.ok(e);
assert.equal(e.message, 'near option must be an array [x, y]');
Geo.geoSearch({type: 'test'}, {near: [1, 2]}, function(err) {
assert.ok(err);
assert.ok(/maxDistance needs a number/.test(err));
done();
});
});
});
});
});
});
});
it('returns a promise (gh-1614)', function(done) {
const Geo = getModel(db);
Geo.on('index', function() {
const prom = Geo.geoSearch({type: 'place'}, {near: [9, 9], maxDistance: 5});
assert.ok(prom instanceof mongoose.Promise);
prom.then(() => done(), err => done(err));
});
});
it('allows not passing a callback (gh-1614)', function(done) {
const Geo = getModel(db);
Geo.on('index', function(err) {
assert.ifError(err);
const g = new Geo({pos: [10, 10], type: 'place'});
g.save(function(err) {
assert.ifError(err);
let promise;
assert.doesNotThrow(function() {
promise = Geo.geoSearch({type: 'place'}, {near: [9, 9], maxDistance: 5});
});
function validate(ret) {
assert.equal(ret.length, 1);
assert.equal(ret[0].pos[0], 10);
assert.equal(ret[0].pos[1], 10);
}
function finish() {
done();
}
promise.then(validate, assert.ifError).then(finish);
});
});
});
});
});