forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.geosearch.test.js
185 lines (156 loc) · 5.63 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
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({
pos : [Number],
complex : {},
type: String
});
schema.index({ "pos" : "geoHaystack", type : 1},{ bucketSize : 1});
function getModel (db) {
return db.model('GeoSearch', schema, 'geosearch-'+random());
}
describe('model', function(){
describe('geoSearch', function () {
it('works', function (done) {
var db = start();
var Geo = getModel(db);
assert.ok(Geo.geoSearch instanceof Function);
Geo.on('index', function(err){
assert.ifError(err);
var 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"});
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.geoSearch({ type : "place" }, { near : [9,9], maxDistance : 5 }, function (err, results, stats) {
assert.ifError(err);
assert.equal(1, results.length);
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, stats) {
assert.ifError(err);
assert.equal(0, results.length);
done();
});
});
}
});
});
it('works with lean', function (done) {
var db = start();
var Geo = getModel(db);
assert.ok(Geo.geoSearch instanceof Function);
Geo.on('index', function(err){
assert.ifError(err);
var 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"});
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.geoSearch({ type : "place" }, { near : [9,9], maxDistance : 5, lean : true }, function (err, results, stats) {
assert.ifError(err);
assert.equal(1, results.length);
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) {
var db = start();
var Geo = getModel(db);
assert.ok(Geo.geoSearch instanceof Function);
Geo.on('index', function(err){
assert.ifError(err);
var g = new Geo({ pos : [10,10], type : "place"});
g.save(function() {
var threw = false;
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, res) {
assert.ok(err);
assert.ok(/maxDistance needs a number/.test(err));
done();
});
});
});
});
});
});
});
it('returns a promise (gh-1614)', function(done){
var db = start();
var Geo = getModel(db);
var prom = Geo.geoSearch({ type : "place" }, { near : [9,9], maxDistance : 5 }, function (err, results, stats) {
});
assert.ok(prom instanceof mongoose.Promise);
db.close();
done();
})
it('allows not passing a callback (gh-1614)', function (done) {
var db = start();
var Geo = getModel(db);
Geo.on('index', function(err){
assert.ifError(err);
var g = new Geo({ pos : [10,10], type : "place"});
g.save(function (err) {
assert.ifError(err);
var promise;
assert.doesNotThrow(function() {
promise = Geo.geoSearch({ type : "place" }, { near : [9,9], maxDistance : 5 });
});
function validate(ret, stat) {
assert.equal(1, ret.length);
assert.equal(ret[0].pos[0], 10);
assert.equal(ret[0].pos[1], 10);
assert.ok(stat);
}
function finish() {
db.close(done);
}
promise.then(validate, assert.ifError).then(finish).end();
});
});
});
});
});