/
mongo_client_test.js
174 lines (153 loc) · 4.82 KB
/
mongo_client_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
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 native_parser = (process.env['TEST_NATIVE'] != null);
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,
Server = mongodb.Server,
MongoClient = mongodb.MongoClient;
var MONGODB = 'integration_tests';
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}), {w:0, 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();
}
/**
* A basic example using the MongoClient to connect using a Server instance, similar to existing Db version
*
* @_class mongoclient
* @_function open
*/
exports['Should correctly connect using MongoClient to a single server'] = function(test) {
// Set up the connection to the local db
var mongoclient = new MongoClient(new Server("localhost", 27017, {native_parser: true}));
// Open the connection to the server
mongoclient.open(function(err, mongoclient) {
// Get the first db and do an update document on it
var db = mongoclient.db("integration_tests");
db.collection('mongoclient_test').update({a:1}, {b:1}, {upsert:true}, function(err, result) {
test.equal(null, err);
test.equal(1, result);
// Get another db and do an update document on it
var db2 = mongoclient.db("integration_tests2");
db2.collection('mongoclient_test').update({a:1}, {b:1}, {upsert:true}, function(err, result) {
test.equal(null, err);
test.equal(1, result);
// Close the connection
mongoclient.close();
test.done();
});
});
});
}
/**
* Example of a simple url connection string for a single server connection
*
* @_class mongoclient
* @_function MongoClient.connect
*/
exports['Should correctly connect using MongoClient to a single server using connect'] = function(test) {
// Connect using the connection string
MongoClient.connect("mongodb://localhost:27017/integration_tests", {native_parser:true}, function(err, db) {
test.equal(null, err);
db.collection('mongoclient_test').update({a:1}, {b:1}, {upsert:true}, function(err, result) {
test.equal(null, err);
test.equal(1, result);
db.close();
test.done();
});
});
}
/**
* @ignore
*/
exports['Should correctly connect using MongoClient to a single server using connect with optional server setting'] = function(test) {
// Connect using the connection string
MongoClient.connect("mongodb://localhost:27017/integration_tests", {
db: {
native_parser: false
},
server: {
socketOptions: {
connectTimeoutMS: 500
}
}
}, function(err, db) {
test.equal(null, err);
test.equal(500, db.serverConfig.socketOptions.connectTimeoutMS);
test.equal(false, db.native_parser);
db.collection('mongoclient_test').update({a:1}, {b:1}, {upsert:true}, function(err, result) {
test.equal(null, err);
test.equal(1, result);
db.close();
test.done();
});
});
}
/**
* @ignore
*/
exports['Should correctly allow for w:0 overriding on the connect url'] = function(test) {
// Connect using the connection string
MongoClient.connect("mongodb://localhost:27017/integration_tests?w=0", function(err, db) {
test.equal(null, err);
db.collection('mongoclient_test').update({a:1}, {b:1}, {upsert:true}, function(err, result) {
test.equal(null, err);
test.equal(null, result);
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;