1 var mongodb = process.env['TEST_NATIVE'] != null ? require('../lib/mongodb').native() : require('../lib/mongodb').pure();
3 var testCase = require('../deps/nodeunit').testCase,
4 debug = require('util').debug
5 inspect = require('util').inspect,
6 nodeunit = require('../deps/nodeunit'),
8 Cursor = mongodb.Cursor,
9 Collection = mongodb.Collection,
10 Server = mongodb.Server;
12 var MONGODB = 'integration_tests';
13 var client = new Db(MONGODB, new Server("127.0.0.1", 27017, {auto_reconnect: true, poolSize: 4}), {native_parser: (process.env['TEST_NATIVE'] != null)});
15 // Define the tests, we want them to run as a nested test so we only clean up the
17 var tests = testCase({
18 setUp: function(callback) {
19 client.open(function(err, db_p) {
20 if(numberOfTestsRun == Object.keys(tests).length) {
21 // If first test drop the db
22 client.dropDatabase(function(err, done) {
31 tearDown: function(callback) {
32 numberOfTestsRun = numberOfTestsRun - 1;
33 // Drop the database and close it
34 if(numberOfTestsRun <= 0) {
35 // client.dropDatabase(function(err, done) {
45 // Test the error reporting functionality
46 shouldCorrectlyRetrieveErrorMessagesFromServer : function(test) {
47 // Just run with one connection in the pool
48 var error_client = new Db(MONGODB, new Server("127.0.0.1", 27017, {auto_reconnect: false, poolSize:1}), {native_parser: (process.env['TEST_NATIVE'] != null)});
49 error_client.bson_deserializer = client.bson_deserializer;
50 error_client.bson_serializer = client.bson_serializer;
51 error_client.pkFactory = client.pkFactory;
54 error_client.open(function(err, error_client) {
55 error_client.resetErrorHistory(function() {
56 error_client.error(function(err, documents) {
57 test.equal(true, documents[0].ok);
58 test.equal(0, documents[0].n);
60 // Force error on server
61 error_client.executeDbCommand({forceerror: 1}, function(err, r) {
62 test.equal(0, r.documents[0].ok);
63 test.ok(r.documents[0].errmsg.length > 0);
64 // Check for previous errors
65 error_client.previousErrors(function(err, documents) {
66 test.equal(true, documents[0].ok);
67 test.equal(1, documents[0].nPrev);
68 test.equal("forced error", documents[0].err);
70 // Check for the last error
71 error_client.error(function(err, documents) {
72 test.equal("forced error", documents[0].err);
73 // Force another error
74 error_client.collection('test_error_collection', function(err, collection) {
75 collection.findOne({name:"Fred"}, function(err, document) {
76 // Check that we have two previous errors
77 error_client.previousErrors(function(err, documents) {
78 test.equal(true, documents[0].ok);
79 test.equal(2, documents[0].nPrev);
80 test.equal("forced error", documents[0].err);
82 error_client.resetErrorHistory(function() {
83 error_client.previousErrors(function(err, documents) {
84 test.equal(true, documents[0].ok);
85 test.equal(-1, documents[0].nPrev);
87 error_client.error(function(err, documents) {
88 test.equal(true, documents[0].ok);
89 test.equal(0, documents[0].n);
94 error_client.error(function(err, documents) {
95 test.ok(err instanceof Error);
96 test.ok('notConnected' === err.message);
113 // Test the last status functionality of the driver
114 shouldCorrectlyExecuteLastStatus : function(test) {
115 // Just run with one connection in the pool
116 var error_client = new Db(MONGODB, new Server("127.0.0.1", 27017, {auto_reconnect: false, poolSize:1,}), {native_parser: (process.env['TEST_NATIVE'] != null)});
117 error_client.bson_deserializer = client.bson_deserializer;
118 error_client.bson_serializer = client.bson_serializer;
119 error_client.pkFactory = client.pkFactory;
122 error_client.open(function(err, client) {
123 client.createCollection('test_last_status', function(err, collection) {
124 test.ok(collection instanceof Collection);
125 test.equal('test_last_status', collection.collectionName);
127 // Get the collection
128 client.collection('test_last_status', function(err, collection) {
129 // Remove all the elements of the collection
130 collection.remove(function(err, result) {
131 // Check update of a document
132 collection.insert({i:1}, function(err, ids) {
133 test.equal(1, ids.length);
134 test.ok(ids[0]._id.toHexString().length == 24);
137 collection.update({i:1}, {"$set":{i:2}}, function(err, result) {
138 // Check for the last message from the server
139 client.lastStatus(function(err, status) {
140 test.equal(true, status.documents[0].ok);
141 test.equal(true, status.documents[0].updatedExisting);
142 // Check for failed update of document
143 collection.update({i:1}, {"$set":{i:500}}, function(err, result) {
144 client.lastStatus(function(err, status) {
145 test.equal(true, status.documents[0].ok);
146 test.equal(false, status.documents[0].updatedExisting);
148 // Check safe update of a document
149 collection.insert({x:1}, function(err, ids) {
150 collection.update({x:1}, {"$set":{x:2}}, {'safe':true}, function(err, document) {
153 collection.update({x:1}, {"$set":{x:2}}, {'safe':true});
155 collection.update({y:1}, {"$set":{y:2}}, {'safe':true}, function(err, result) {
156 test.equal(0, result);
158 // Let's close the db
159 error_client.close();
160 // Let's close the db
175 shouldFailInsertDueToUniqueIndex : function(test) {
176 client.createCollection('test_failing_insert_due_to_unique_index', function(err, r) {
177 client.collection('test_failing_insert_due_to_unique_index', function(err, collection) {
178 collection.ensureIndex([['a', 1 ]], true, function(err, indexName) {
179 collection.insert({a:2}, {safe: true}, function(err, r) {
180 test.ok(err == null);
181 collection.insert({a:2}, {safe: true}, function(err, r) {
182 test.ok(err != null);
191 // Test the error reporting functionality
192 shouldFailInsertDueToUniqueIndexStrict : function(test) {
193 var error_client = new Db(MONGODB, new Server("127.0.0.1", 27017, {auto_reconnect: false}), {native_parser: (process.env['TEST_NATIVE'] != null)});
194 error_client.bson_deserializer = client.bson_deserializer;
195 error_client.bson_serializer = client.bson_serializer;
196 error_client.pkFactory = client.pkFactory;
198 error_client.open(function(err, error_client) {
199 error_client.dropCollection('test_failing_insert_due_to_unique_index_strict', function(err, r) {
200 error_client.createCollection('test_failing_insert_due_to_unique_index_strict', function(err, r) {
201 error_client.collection('test_failing_insert_due_to_unique_index_strict', function(err, collection) {
202 collection.ensureIndex([['a', 1 ]], true, function(err, indexName) {
203 collection.insert({a:2}, {safe:true}, function(err, r) {
204 test.ok(err == null);
205 collection.insert({a:2}, {safe:true}, function(err, r) {
206 test.ok(err != null);
207 error_client.close();
218 'safe mode should pass the disconnected error to the callback': function (test) {
219 var error_client = new Db(MONGODB, new Server("127.0.0.1", 27017, {auto_reconnect: false}), {native_parser: (process.env['TEST_NATIVE'] != null)});
220 error_client.bson_deserializer = client.bson_deserializer;
221 error_client.bson_serializer = client.bson_serializer;
222 error_client.pkFactory = client.pkFactory;
224 var name = 'test_safe_mode_when_disconnected';
225 error_client.open(function(err, error_client) {
226 test.ok(err == null);
227 error_client.resetErrorHistory(function() {
228 error_client.dropCollection(name, function() {
229 error_client.createCollection(name, function(err, collection) {
230 test.ok(err == null);
231 collection.insert({ inserted: true }, { safe: true }, function (err) {
232 test.ok(err == null);
233 error_client.close();
235 collection.insert({ works: true }, { safe: true }, function (err) {
236 test.ok(err instanceof Error);
237 test.ok('notConnected' === err.message);
239 collection.update({ inserted: true }, { inserted: true, x: 1 }, { safe: true }, function (err) {
240 test.ok(err instanceof Error);
241 test.ok('notConnected' === err.message);
243 collection.remove({ inserted: true }, { safe: true }, function (err) {
244 test.ok(err instanceof Error);
245 test.ok('notConnected' === err.message);
247 collection.findOne({ works: true }, function (err) {
248 test.ok(err instanceof Error);
249 test.ok('notConnected' === err.message);
264 // Stupid freaking workaround due to there being no way to run setup once for each suite
265 var numberOfTestsRun = Object.keys(tests).length;
267 module.exports = tests;