forked from christkv/node-mongodb-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeo_search_test.js
145 lines (125 loc) · 4.15 KB
/
geo_search_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
var mongodb = process.env['TEST_NATIVE'] != null ? require('../lib/mongodb').native() : require('../lib/mongodb').pure();
var useSSL = process.env['USE_SSL'] != null ? true : false;
var testCase = require('nodeunit').testCase,
debug = require('util').debug,
inspect = require('util').inspect,
nodeunit = require('nodeunit'),
gleak = require('../dev/tools/gleak'),
Db = mongodb.Db,
Cursor = mongodb.Cursor,
Collection = mongodb.Collection,
ObjectID = mongodb.ObjectID,
Long = mongodb.Long,
Server = mongodb.Server;
var MONGODB = 'integration_tests';
var native_parser = (process.env['TEST_NATIVE'] != null);
var client = null;
/**
* Retrieve the server information for the current
* instance of the db client
*
* @ignore
*/
exports.setUp = function(callback) {
var self = exports;
client = new Db(MONGODB, new Server("127.0.0.1", 27017, {auto_reconnect: true, poolSize: 4, ssl:useSSL}), {native_parser: (process.env['TEST_NATIVE'] != null)});
client.open(function(err, db_p) {
if(numberOfTestsRun == (Object.keys(self).length)) {
// If first test drop the db
client.dropDatabase(function(err, done) {
callback();
});
} else {
return callback();
}
});
}
/**
* Retrieve the server information for the current
* instance of the db client
*
* @ignore
*/
exports.tearDown = function(callback) {
var self = this;
numberOfTestsRun = numberOfTestsRun - 1;
// Close connection
client.close();
callback();
}
/**
* Example of a simple geoNear query across some documents
*
* @_class collection
* @_function geoNear
* @ignore
*/
exports.shouldCorrectlyPerformSimpleGeoNearCommand = function(test) {
var db = new Db('integration_tests', new Server("127.0.0.1", 27017,
{auto_reconnect: false, poolSize: 4, ssl:useSSL}), {native_parser: native_parser});
// Establish connection to db
db.open(function(err, db) {
// Fetch the collection
db.collection("simple_geo_near_command", function(err, collection) {
// Add a location based index
collection.ensureIndex({loc:"2d"}, function(err, result) {
// Save a new location tagged document
collection.insert([{a:1, loc:[50, 30]}, {a:1, loc:[30, 50]}], {safe:true}, function(err, result) {
// Use geoNear command to find document
collection.geoNear(50, 50, {query:{a:1}, num:1}, function(err, docs) {
test.equal(1, docs.results.length);
db.close();
test.done();
});
});
});
});
});
}
/**
* Example of a simple geoHaystackSearch query across some documents
*
* @_class collection
* @_function geoHaystackSearch
* @ignore
*/
exports.shouldCorrectlyPerformSimpleGeoHaystackSearchCommand = function(test) {
var db = new Db('integration_tests', new Server("127.0.0.1", 27017,
{auto_reconnect: false, poolSize: 4, ssl:useSSL}), {native_parser: native_parser});
// Establish connection to db
db.open(function(err, db) {
// Fetch the collection
db.collection("simple_geo_haystack_command", function(err, collection) {
// Add a location based index
collection.ensureIndex({loc: "geoHaystack", type: 1}, {bucketSize: 1}, function(err, result) {
// Save a new location tagged document
collection.insert([{a:1, loc:[50, 30]}, {a:1, loc:[30, 50]}], {safe:true}, function(err, result) {
// Use geoNear command to find document
collection.geoHaystackSearch(50, 50, {search:{a:1}, limit:1, maxDistance:100}, function(err, docs) {
test.equal(1, docs.results.length);
db.close();
test.done();
});
});
});
});
});
}
/**
* Retrieve the server information for the current
* instance of the db client
*
* @ignore
*/
exports.noGlobalsLeaked = function(test) {
var leaks = gleak.detectNew();
test.equal(0, leaks.length, "global var leak detected: " + leaks.join(', '));
test.done();
}
/**
* Retrieve the server information for the current
* instance of the db client
*
* @ignore
*/
var numberOfTestsRun = Object.keys(this).length - 2;