Skip to content

Commit

Permalink
Splitting out integration tests into nodeunit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
christkv committed May 20, 2011
1 parent e19e308 commit a878ecf
Show file tree
Hide file tree
Showing 10 changed files with 1,157 additions and 984 deletions.
980 changes: 2 additions & 978 deletions integration/integration_tests.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/mongodb/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ Collection.prototype.mapReduce = function mapReduce (map, reduce, options, callb
processtime: result.documents[0].timeMillis
, counts: result.documents[0].counts
};

callback(err, collection, stats);
});
});
Expand Down
173 changes: 171 additions & 2 deletions test/collection_operation_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ var testCase = require('nodeunit').testCase,
inspect = require('sys').inspect,
nodeunit = require('nodeunit'),
Db = require('../lib/mongodb').Db,
Cursor = require('../lib/mongodb').Cursor,
Collection = require('../lib/mongodb').Collection,
Server = require('../lib/mongodb').Server;

var client = new Db('integration_tests', new Server("127.0.0.1", 27017, {auto_reconnect: false}));
var MONGODB = 'integration_tests';
var client = new Db(MONGODB, new Server("127.0.0.1", 27017, {auto_reconnect: false}));

// Define the tests, we want them to run as a nested test so we only clean up the
// db connection once
Expand Down Expand Up @@ -91,7 +94,173 @@ var tests = testCase({
});
});
});
}
},

// Test dropping of collections
shouldCorrectlyDropCollection : function(test) {
client.createCollection('test_drop_collection2', function(err, r) {
client.dropCollection('test_drop_collection', function(err, r) {
test.ok(err instanceof Error);
test.equal("ns not found", err.message);
var found = false;
// Ensure we don't have the collection in the set of names
client.collectionNames(function(err, replies) {
replies.forEach(function(err, document) {
if(document.name == "test_drop_collection") {
found = true;
return;
}
});
// If we have an instance of the index throw and error
if(found) throw new Error("should not fail");
// Let's close the db
test.done();
});
});
});
},

// Test dropping using the collection drop command
shouldCorrectlyDropCollectionWithDropFunction : function(test) {
client.createCollection('test_other_drop', function(err, r) {
client.collection('test_other_drop', function(err, collection) {
collection.drop(function(err, reply) {
// Ensure we don't have the collection in the set of names
client.collectionNames(function(err, replies) {
var found = false;
replies.forEach(function(document) {
if(document.name == "test_other_drop") {
found = true;
return;
}
});
// If we have an instance of the index throw and error
if(found) throw new Error("should not fail");
// Let's close the db
test.done();
});
});
});
});
},

shouldCorrectlyRetriveCollectionNames : function(test) {
client.createCollection('test_collection_names', function(err, r) {
client.collectionNames(function(err, documents) {
var found = false;
var found2 = false;
documents.forEach(function(document) {
if(document.name == MONGODB + '.test_collection_names') found = true;
});
test.ok(found);
// Insert a document in an non-existing collection should create the collection
client.collection('test_collection_names2', function(err, collection) {
collection.insert({a:1}, {safe:true}, function(err, r) {
client.collectionNames(function(err, documents) {
documents.forEach(function(document) {
if(document.name == MONGODB + '.test_collection_names2') found = true;
if(document.name == MONGODB + '.test_collection_names') found2 = true;
});

test.ok(found);
test.ok(found2);
// Let's close the db
test.done();
});
})
});
});
});
},

shouldCorrectlyRetrieveCollectionInfo : function(test) {
client.createCollection('test_collections_info', function(err, r) {
client.collectionsInfo(function(err, cursor) {
test.ok((cursor instanceof Cursor));
// Fetch all the collection info
cursor.toArray(function(err, documents) {
test.ok(documents.length > 1);

var found = false;
documents.forEach(function(document) {
if(document.name == MONGODB + '.test_collections_info') found = true;
});
test.ok(found);
// Let's close the db
test.done();
});
});
});
},

shouldCorrectlyRetriveCollectionOptions : function(test) {
client.createCollection('test_collection_options', {'capped':true, 'size':1024}, function(err, collection) {
test.ok(collection instanceof Collection);
test.equal('test_collection_options', collection.collectionName);
// Let's fetch the collection options
collection.options(function(err, options) {
test.equal(true, options.capped);
test.equal(1024, options.size);
test.equal("test_collection_options", options.create);
// Let's close the db
test.done();
});
});
},

shouldEnsureStrictAccessCollection : function(test) {
var error_client = new Db(MONGODB, new Server("127.0.0.1", 27017, {auto_reconnect: false}), {strict:true});
error_client.bson_deserializer = client.bson_deserializer;
error_client.bson_serializer = client.bson_serializer;
error_client.pkFactory = client.pkFactory;

test.equal(true, error_client.strict);
error_client.open(function(err, error_client) {
error_client.collection('does-not-exist', function(err, collection) {
test.ok(err instanceof Error);
test.equal("Collection does-not-exist does not exist. Currently in strict mode.", err.message);
});

error_client.createCollection('test_strict_access_collection', function(err, collection) {
error_client.collection('test_strict_access_collection', function(err, collection) {
test.ok(collection instanceof Collection);
// Let's close the db
error_client.close();
test.done();
});
});
});
},

shouldPerformStrictCreateCollection : function(test) {
var error_client = new Db(MONGODB, new Server("127.0.0.1", 27017, {auto_reconnect: false}), {strict:true});
error_client.bson_deserializer = client.bson_deserializer;
error_client.bson_serializer = client.bson_serializer;
error_client.pkFactory = client.pkFactory;
test.equal(true, error_client.strict);

error_client.open(function(err, error_client) {
error_client.createCollection('test_strict_create_collection', function(err, collection) {
test.ok(collection instanceof Collection);

// Creating an existing collection should fail
error_client.createCollection('test_strict_create_collection', function(err, collection) {
test.ok(err instanceof Error);
test.equal("Collection test_strict_create_collection already exists. Currently in strict mode.", err.message);

// Switch out of strict mode and try to re-create collection
error_client.strict = false;
error_client.createCollection('test_strict_create_collection', function(err, collection) {
test.ok(collection instanceof Collection);

// Let's close the db
error_client.close();
test.done();
});
});
});
});
},
})

// Stupid freaking workaround due to there being no way to run setup once for each suite
Expand Down
104 changes: 104 additions & 0 deletions test/cursor_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
var testCase = require('nodeunit').testCase,
debug = require('sys').debug
inspect = require('sys').inspect,
nodeunit = require('nodeunit'),
Db = require('../lib/mongodb').Db,
Cursor = require('../lib/mongodb').Cursor,
Collection = require('../lib/mongodb').Collection,
Server = require('../lib/mongodb').Server;

var MONGODB = 'integration_tests';
var client = new Db(MONGODB, new Server("127.0.0.1", 27017, {auto_reconnect: false}));

// Define the tests, we want them to run as a nested test so we only clean up the
// db connection once
var tests = testCase({
setUp: function(callback) {
client.open(function(err, db_p) {
// Save reference to db
client = db_p;
// Start tests
callback();
});
},

tearDown: function(callback) {
numberOfTestsRun = numberOfTestsRun - 1;
// Drop the database and close it
if(numberOfTestsRun <= 0) {
client.dropDatabase(function(err, done) {
client.close();
callback();
});
} else {
client.close();
callback();
}
},

shouldCorrectlyExecuteToArray : function(test) {
// Create a non-unique index and test inserts
client.createCollection('test_array', function(err, collection) {
collection.insert({'b':[1, 2, 3]}, function(err, ids) {
collection.find(function(err, cursor) {
cursor.toArray(function(err, documents) {
test.deepEqual([1, 2, 3], documents[0].b);
// Let's close the db
test.done();
});
}, {});
});
});
},

shouldCorrectlyExecuteToArrayAndFailOnFurtherCursorAccess : function(test) {
client.createCollection('test_to_a', function(err, collection) {
test.ok(collection instanceof Collection);
collection.insert({'a':1}, function(err, ids) {
collection.find({}, function(err, cursor) {
cursor.toArray(function(err, items) {
// Should fail if called again (cursor should be closed)
cursor.toArray(function(err, items) {
test.ok(err instanceof Error);
test.equal("Cursor is closed", err.message);

// Should fail if called again (cursor should be closed)
cursor.each(function(err, item) {
test.ok(err instanceof Error);
test.equal("Cursor is closed", err.message);
// Let's close the db
test.done();
});
});
});
});
});
});
},

shouldCorrectlyFailToArrayDueToFinishedEachOperation : function(test) {
client.createCollection('test_to_a_after_each', function(err, collection) {
test.ok(collection instanceof Collection);
collection.insert({'a':1}, function(err, ids) {
collection.find(function(err, cursor) {
cursor.each(function(err, item) {
if(item == null) {
cursor.toArray(function(err, items) {
test.ok(err instanceof Error);
test.equal("Cursor is closed", err.message);

// Let's close the db
test.done();
});
};
});
});
});
});
},
})

// Stupid freaking workaround due to there being no way to run setup once for each suite
var numberOfTestsRun = Object.keys(tests).length;
// Assign out tests
module.exports = tests;
49 changes: 48 additions & 1 deletion test/db_connect_test.js → test/db_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,54 @@ var tests = testCase({
error_client_pair.on("error", function(err) {});
error_client_pair.on("close", closeListener);
error_client_pair.open(function(err, error_client_pair) {});
}
},

shouldCorrectlyExecuteEvalFunctions : function(test) {
client.eval('function (x) {return x;}', [3], function(err, result) {
test.equal(3, result);
});

client.eval('function (x) {db.test_eval.save({y:x});}', [5], function(err, result) {
test.equal(null, result)
// Locate the entry
client.collection('test_eval', function(err, collection) {
collection.findOne(function(err, item) {
test.equal(5, item.y);
});
});
});

client.eval('function (x, y) {return x + y;}', [2, 3], function(err, result) {
test.equal(5, result);
});

client.eval('function () {return 5;}', function(err, result) {
test.equal(5, result);
});

client.eval('2 + 3;', function(err, result) {
test.equal(5, result);
});

client.eval(new client.bson_serializer.Code("2 + 3;"), function(err, result) {
test.equal(5, result);
});

client.eval(new client.bson_serializer.Code("return i;", {'i':2}), function(err, result) {
test.equal(2, result);
});

client.eval(new client.bson_serializer.Code("i + 3;", {'i':2}), function(err, result) {
test.equal(5, result);
});

client.eval("5 ++ 5;", function(err, result) {
test.ok(err instanceof Error);
test.ok(err.message != null);
// Let's close the db
test.done();
});
},
})

// Stupid freaking workaround due to there being no way to run setup once for each suite
Expand Down
Loading

0 comments on commit a878ecf

Please sign in to comment.