From 3611c2ca28f746083828f48bc9ee8af7cf21185f Mon Sep 17 00:00:00 2001 From: Christian Kvalheim Date: Mon, 11 Mar 2013 13:04:54 +0100 Subject: [PATCH] Moved more tests --- new_tests/functional/connection_tests.js | 159 ++++ new_tests/functional/contexts/index.js | 12 + new_tests/functional/contexts/other.js | 11 + new_tests/functional/contexts/shared.js | 2 + new_tests/functional/index_tests.js | 803 ++++++++++++++++++++ new_tests/runner.js | 16 +- test/{connect_test.js => __connect_test.js} | 0 test/{index_test.js => __index_test.js} | 0 8 files changed, 1001 insertions(+), 2 deletions(-) create mode 100644 new_tests/functional/contexts/index.js create mode 100644 new_tests/functional/contexts/other.js create mode 100644 new_tests/functional/contexts/shared.js create mode 100644 new_tests/functional/index_tests.js rename test/{connect_test.js => __connect_test.js} (100%) rename test/{index_test.js => __index_test.js} (100%) diff --git a/new_tests/functional/connection_tests.js b/new_tests/functional/connection_tests.js index ce6bbb3031..9d7e9fe589 100644 --- a/new_tests/functional/connection_tests.js +++ b/new_tests/functional/connection_tests.js @@ -59,3 +59,162 @@ exports['Should fail to connect using non-domain socket with undefined port'] = test.ok(/port must be specified/.test(error)); test.done(); } + +/** + * @ignore + */ +function connectionTester(test, testName, callback) { + return function(err, db) { + test.equal(err, null); + db.collection(testName, function(err, collection) { + test.equal(err, null); + var doc = {foo:123}; + collection.insert({foo:123}, {w:1}, function(err, docs) { + test.equal(err, null); + db.dropDatabase(function(err, done) { + db.close(); + test.equal(err, null); + test.ok(done); + if(callback) return callback(db); + test.done(); + }); + }); + }); + }; +}; + +/** + * @ignore + */ +exports.testConnectNoOptions = function(configuration, test) { + var connect = configuration.getMongoPackage().connect; + + connect(configuration.url(), connectionTester(test, 'testConnectNoOptions', function(db) { + test.done(); + })); +}; + +/** + * @ignore + */ +exports.testConnectDbOptions = function(configuration, test) { + var connect = configuration.getMongoPackage().connect; + + connect(configuration.url(), + { db: {native_parser: (process.env['TEST_NATIVE'] != null)} }, + connectionTester(test, 'testConnectDbOptions', function(db) { + test.equal(process.env['TEST_NATIVE'] != null, db.native_parser); + test.done(); + })); +}; + +/** + * @ignore + */ +exports.testConnectServerOptions = function(configuration, test) { + var connect = configuration.getMongoPackage().connect; + + connect(configuration.url(), + { server: {auto_reconnect: true, poolSize: 4} }, + connectionTester(test, 'testConnectServerOptions', function(db) { + test.equal(4, db.serverConfig.poolSize); + test.equal(true, db.serverConfig.autoReconnect); + test.done(); + })); +}; + +/** + * @ignore + */ +exports.testConnectAllOptions = function(configuration, test) { + var connect = configuration.getMongoPackage().connect; + + connect(configuration.url(), + { server: {auto_reconnect: true, poolSize: 4}, + db: {native_parser: (process.env['TEST_NATIVE'] != null)} }, + connectionTester(test, 'testConnectAllOptions', function(db) { + test.equal(process.env['TEST_NATIVE'] != null, db.native_parser); + test.equal(4, db.serverConfig.poolSize); + test.equal(true, db.serverConfig.autoReconnect); + test.done(); + })); +}; + +/** + * @ignore + */ +exports.testConnectGoodAuth = function(configuration, test) { + var connect = configuration.getMongoPackage().connect; + var user = 'testConnectGoodAuth', password = 'password'; + // First add a user. + connect(configuration.url(), function(err, db) { + test.equal(err, null); + db.addUser(user, password, function(err, result) { + test.equal(err, null); + db.close(); + restOfTest(); + }); + }); + + function restOfTest() { + connect(configuration.url(user, password), connectionTester(test, 'testConnectGoodAuth', function(db) { + test.equal(false, db.safe); + test.done(); + })); + } +}; + +/** + * @ignore + */ +exports.testConnectBadAuth = function(configuration, test) { + var connect = configuration.getMongoPackage().connect; + + connect(configuration.url('slithy', 'toves'), function(err, db) { + test.ok(err); + test.equal(null, db); + test.done(); + }); +}; + +/** + * @ignore + */ +exports.testConnectThrowsNoCallbackProvided = function(configuration, test) { + var connect = configuration.getMongoPackage().connect; + + test.throws(function() { + var db = connect(configuration.url()); + }); + test.done(); +}; + +/** + * @ignore + */ +exports.testConnectBadUrl = function(configuration, test) { + test.throws(function() { + connect('mangodb://localhost:27017/test?safe=false', function(err, db) { + test.ok(false, 'Bad URL!'); + }); + }); + test.done(); +}; + +/** + * Example of a simple url connection string, with no acknowledgement of writes. + * + * @_class db + * @_function Db.connect + */ +exports.shouldCorrectlyDoSimpleCountExamples = function(configuration, test) { + var Db = configuration.getMongoPackage().Db; + + // Connect to the server + Db.connect(configuration.url(), function(err, db) { + test.equal(null, err); + + db.close(); + test.done(); + }); +} \ No newline at end of file diff --git a/new_tests/functional/contexts/index.js b/new_tests/functional/contexts/index.js new file mode 100644 index 0000000000..f7970f846e --- /dev/null +++ b/new_tests/functional/contexts/index.js @@ -0,0 +1,12 @@ + +var vm = require('vm') +var fs = require('fs') +var path = require('path') + +var shared = require('./shared'); +var ind = fs.readFileSync(__dirname + '/other.js', 'utf8'); +var filename = path.resolve(__dirname + '/other.js'); +var script = vm.createScript(ind, filename); +script.runInNewContext({ require: require, __filename: filename }); + +module.exports = exports = shared; diff --git a/new_tests/functional/contexts/other.js b/new_tests/functional/contexts/other.js new file mode 100644 index 0000000000..ddf99362db --- /dev/null +++ b/new_tests/functional/contexts/other.js @@ -0,0 +1,11 @@ + +var shared = require('./shared'); +shared.object = { name: 1, age: -1 }; +shared.array = ['name'] +shared.string = 'woot' +shared.string2 = String('woot') +shared.number = 3 +shared.number2 = Number(3) +shared.bool = true +shared.bool2 = Boolean(true) +shared.date = new Date; diff --git a/new_tests/functional/contexts/shared.js b/new_tests/functional/contexts/shared.js new file mode 100644 index 0000000000..305f57e30e --- /dev/null +++ b/new_tests/functional/contexts/shared.js @@ -0,0 +1,2 @@ + +module.exports = exports = {}; diff --git a/new_tests/functional/index_tests.js b/new_tests/functional/index_tests.js new file mode 100644 index 0000000000..0aec3f7970 --- /dev/null +++ b/new_tests/functional/index_tests.js @@ -0,0 +1,803 @@ +/** + * A simple createIndex using a simple single field index + * + * @_class collection + * @_function createIndex + */ +exports.shouldCreateASimpleIndexOnASingleField = function(configuration, test) { + var db = configuration.newDbInstance({w:0}, {poolSize:1, auto_reconnect:true}); + + // Establish connection to db + db.open(function(err, db) { + + // Create a collection we want to drop later + db.createCollection('simple_index_test', function(err, collection) { + test.equal(null, err); + + // Insert a bunch of documents for the index + collection.insert([{a:1}, {a:2}, {a:3}, {a:4}], {w:1}, function(err, result) { + test.equal(null, err); + + // Create an index on the a field + collection.createIndex('a', {w:1}, function(err, indexName) { + test.equal("a_1", indexName); + + // Peform a query, with explain to show we hit the query + collection.find({a:2}, {explain:true}).toArray(function(err, explanation) { + test.deepEqual([[2, 2]], explanation[0].indexBounds.a); + + db.close(); + test.done(); + }); + }); + }); + }); + }); +} + +/** + * A more complex createIndex using a compound unique index in the background and dropping duplicated documents + * + * @_class collection + * @_function createIndex + */ +exports.shouldCreateComplexIndexOnTwoFields = function(configuration, test) { + var db = configuration.newDbInstance({w:0}, {poolSize:1, auto_reconnect:true}); + + // Establish connection to db + db.open(function(err, db) { + + // Create a collection we want to drop later + db.createCollection('more_complex_index_test', function(err, collection) { + test.equal(null, err); + + // Insert a bunch of documents for the index + collection.insert([{a:1, b:1}, {a:1, b:1} + , {a:2, b:2}, {a:3, b:3}, {a:4, b:4}], {w:1}, function(err, result) { + test.equal(null, err); + + // Create an index on the a field + collection.createIndex({a:1, b:1} + , {unique:true, background:true, dropDups:true, w:1}, function(err, indexName) { + + // Show that duplicate records got dropped + collection.find({}).toArray(function(err, items) { + test.equal(null, err); + test.equal(4, items.length); + + // Peform a query, with explain to show we hit the query + collection.find({a:2}, {explain:true}).toArray(function(err, explanation) { + test.equal(null, err); + test.ok(explanation[0].indexBounds.a != null); + test.ok(explanation[0].indexBounds.b != null); + + db.close(); + test.done(); + }); + }) + }); + }); + }); + }); +} + +/** + * A more complex ensureIndex using a compound unique index in the background and dropping duplicated documents. + * + * @_class collection + * @_function ensureIndex + */ +exports.shouldCreateComplexEnsureIndex = function(configuration, test) { + var db = configuration.newDbInstance({w:0}, {poolSize:1, auto_reconnect:true}); + + // Establish connection to db + db.open(function(err, db) { + + // Create a collection we want to drop later + db.createCollection('more_complex_ensure_index_test', function(err, collection) { + test.equal(null, err); + + // Insert a bunch of documents for the index + collection.insert([{a:1, b:1}, {a:1, b:1} + , {a:2, b:2}, {a:3, b:3}, {a:4, b:4}], {w:1}, function(err, result) { + test.equal(null, err); + + // Create an index on the a field + collection.ensureIndex({a:1, b:1} + , {unique:true, background:true, dropDups:true, w:1}, function(err, indexName) { + + // Show that duplicate records got dropped + collection.find({}).toArray(function(err, items) { + test.equal(null, err); + test.equal(4, items.length); + + // Peform a query, with explain to show we hit the query + collection.find({a:2}, {explain:true}).toArray(function(err, explanation) { + test.equal(null, err); + test.ok(explanation[0].indexBounds.a != null); + test.ok(explanation[0].indexBounds.b != null); + + db.close(); + test.done(); + }); + }) + }); + }); + }); + }); +} + +/** + * An examples showing the information returned by indexInformation + * + * @_class collection + * @_function indexInformation + */ +exports.shouldCorrectlyShowTheResultsFromIndexInformation = function(configuration, test) { + var db = configuration.newDbInstance({w:0}, {poolSize:1, auto_reconnect:true}); + + // Establish connection to db + db.open(function(err, db) { + + // Create a collection we want to drop later + db.createCollection('more_index_information_test', function(err, collection) { + test.equal(null, err); + + // Insert a bunch of documents for the index + collection.insert([{a:1, b:1}, {a:1, b:1} + , {a:2, b:2}, {a:3, b:3}, {a:4, b:4}], {w:1}, function(err, result) { + test.equal(null, err); + + // Create an index on the a field + collection.ensureIndex({a:1, b:1} + , {unique:true, background:true, dropDups:true, w:1}, function(err, indexName) { + + // Fetch basic indexInformation for collection + collection.indexInformation(function(err, indexInformation) { + test.deepEqual([ [ '_id', 1 ] ], indexInformation._id_); + test.deepEqual([ [ 'a', 1 ], [ 'b', 1 ] ], indexInformation.a_1_b_1); + + // Fetch full index information + collection.indexInformation({full:true}, function(err, indexInformation) { + test.deepEqual({ _id: 1 }, indexInformation[0].key); + test.deepEqual({ a: 1, b: 1 }, indexInformation[1].key); + + db.close(); + test.done(); + }); + }); + }); + }); + }); + }); +} + +/** + * An examples showing the creation and dropping of an index + * + * @_class collection + * @_function dropIndex + */ +exports.shouldCorrectlyCreateAndDropIndex = function(configuration, test) { + var db = configuration.newDbInstance({w:0}, {poolSize:1, auto_reconnect:true}); + + // Establish connection to db + db.open(function(err, db) { + + // Create a collection we want to drop later + db.createCollection('create_and_drop_an_index', function(err, collection) { + test.equal(null, err); + + // Insert a bunch of documents for the index + collection.insert([{a:1, b:1}, {a:1, b:1} + , {a:2, b:2}, {a:3, b:3}, {a:4, b:4}], {w:1}, function(err, result) { + test.equal(null, err); + + // Create an index on the a field + collection.ensureIndex({a:1, b:1} + , {unique:true, background:true, dropDups:true, w:1}, function(err, indexName) { + + // Drop the index + collection.dropIndex("a_1_b_1", function(err, result) { + test.equal(null, err); + + // Verify that the index is gone + collection.indexInformation(function(err, indexInformation) { + test.deepEqual([ [ '_id', 1 ] ], indexInformation._id_); + test.equal(null, indexInformation.a_1_b_1); + + db.close(); + test.done(); + }); + }); + }); + }); + }); + }); +} + +/** + * An examples showing the creation and dropping of an index + * + * @_class collection + * @_function dropIndexes + */ +exports.shouldCorrectlyCreateAndDropAllIndex = function(configuration, test) { + var db = configuration.newDbInstance({w:0}, {poolSize:1, auto_reconnect:true}); + + // Establish connection to db + db.open(function(err, db) { + + // Create a collection we want to drop later + db.createCollection('shouldCorrectlyCreateAndDropAllIndex', function(err, collection) { + test.equal(null, err); + + // Insert a bunch of documents for the index + collection.insert([{a:1, b:1}, {a:1, b:1} + , {a:2, b:2}, {a:3, b:3}, {a:4, b:4, c:4}], {w:1}, function(err, result) { + test.equal(null, err); + + // Create an index on the a field + collection.ensureIndex({a:1, b:1} + , {unique:true, background:true, dropDups:true, w:1}, function(err, indexName) { + + // Create an additional index + collection.ensureIndex({c:1} + , {unique:true, background:true, dropDups:true, w:1}, function(err, indexName) { + + // Drop the index + collection.dropAllIndexes(function(err, result) { + test.equal(null, err); + + // Verify that the index is gone + collection.indexInformation(function(err, indexInformation) { + test.deepEqual([ [ '_id', 1 ] ], indexInformation._id_); + test.equal(null, indexInformation.a_1_b_1); + test.equal(null, indexInformation.c_1); + + db.close(); + test.done(); + }); + }); + }); + }); + }); + }); + }); +} + +/** + * An example showing how to force a reindex of a collection. + * + * @_class collection + * @_function reIndex + */ +exports.shouldCorrectlyForceReindexOnCollection = function(configuration, test) { + var db = configuration.newDbInstance({w:0}, {poolSize:1, auto_reconnect:true}); + + // Establish connection to db + db.open(function(err, db) { + + // Create a collection we want to drop later + db.createCollection('shouldCorrectlyForceReindexOnCollection', function(err, collection) { + test.equal(null, err); + + // Insert a bunch of documents for the index + collection.insert([{a:1, b:1}, {a:1, b:1} + , {a:2, b:2}, {a:3, b:3}, {a:4, b:4, c:4}], {w:1}, function(err, result) { + test.equal(null, err); + + // Create an index on the a field + collection.ensureIndex({a:1, b:1} + , {unique:true, background:true, dropDups:true, w:1}, function(err, indexName) { + + // Force a reindex of the collection + collection.reIndex(function(err, result) { + test.equal(null, err); + test.equal(true, result); + + // Verify that the index is gone + collection.indexInformation(function(err, indexInformation) { + test.deepEqual([ [ '_id', 1 ] ], indexInformation._id_); + test.deepEqual([ [ 'a', 1 ], [ 'b', 1 ] ], indexInformation.a_1_b_1); + + db.close(); + test.done(); + }); + }); + }); + }); + }); + }); +} + +/** + * @ignore + */ +exports.shouldCorrectlyExtractIndexInformation = function(configuration, test) { + var client = configuration.db(); + + client.createCollection('test_index_information', function(err, collection) { + collection.insert({a:1}, {w:1}, function(err, ids) { + // Create an index on the collection + client.createIndex(collection.collectionName, 'a', {w:1}, function(err, indexName) { + test.equal("a_1", indexName); + // Let's fetch the index information + client.indexInformation(collection.collectionName, function(err, collectionInfo) { + test.ok(collectionInfo['_id_'] != null); + test.equal('_id', collectionInfo['_id_'][0][0]); + test.ok(collectionInfo['a_1'] != null); + test.deepEqual([["a", 1]], collectionInfo['a_1']); + + client.indexInformation(function(err, collectionInfo2) { + var count1 = 0, count2 = 0; + // Get count of indexes + for(var i in collectionInfo) { count1 += 1;} + for(var i in collectionInfo2) { count2 += 1;} + + // Tests + test.ok(count2 >= count1); + test.ok(collectionInfo2['_id_'] != null); + test.equal('_id', collectionInfo2['_id_'][0][0]); + test.ok(collectionInfo2['a_1'] != null); + test.deepEqual([["a", 1]], collectionInfo2['a_1']); + test.ok((collectionInfo[indexName] != null)); + test.deepEqual([["a", 1]], collectionInfo[indexName]); + + // Let's close the db + test.done(); + }); + }); + }); + }) + }); +} + +/** + * @ignore + */ +exports.shouldCorrectlyHandleMultipleColumnIndexes = function(configuration, test) { + var client = configuration.db(); + + client.createCollection('test_multiple_index_cols', function(err, collection) { + collection.insert({a:1}, function(err, ids) { + // Create an index on the collection + client.createIndex(collection.collectionName, [['a', -1], ['b', 1], ['c', -1]], {w:1}, function(err, indexName) { + test.equal("a_-1_b_1_c_-1", indexName); + // Let's fetch the index information + client.indexInformation(collection.collectionName, function(err, collectionInfo) { + var count1 = 0; + // Get count of indexes + for(var i in collectionInfo) { count1 += 1;} + + // Test + test.equal(2, count1); + test.ok(collectionInfo[indexName] != null); + test.deepEqual([['a', -1], ['b', 1], ['c', -1]], collectionInfo[indexName]); + + // Let's close the db + test.done(); + }); + }); + }); + }); +} + +/** + * @ignore + */ +exports.shouldCorrectlyHandleUniqueIndex = function(configuration, test) { + var client = configuration.db(); + // Create a non-unique index and test inserts + client.createCollection('test_unique_index', function(err, collection) { + client.createIndex(collection.collectionName, 'hello', {w:1}, function(err, indexName) { + // Insert some docs + collection.insert([{'hello':'world'}, {'hello':'mike'}, {'hello':'world'}], {w:1}, function(err, errors) { + // Assert that we have no erros + client.error(function(err, errors) { + test.equal(1, errors.length); + test.equal(null, errors[0].err); + + // Create a unique index and test that insert fails + client.createCollection('test_unique_index2', function(err, collection) { + client.createIndex(collection.collectionName, 'hello', {unique:true, w:1}, function(err, indexName) { + // Insert some docs + collection.insert([{'hello':'world'}, {'hello':'mike'}, {'hello':'world'}], {w:1}, function(err, ids) { + test.ok(err != null); + test.equal(11000, err.code); + test.done(); + }); + }); + }); + }); + }); + }); + }); +} + +/** + * @ignore + */ +exports.shouldCorrectlyCreateSubfieldIndex = function(configuration, test) { + var client = configuration.db(); + // Create a non-unique index and test inserts + client.createCollection('test_index_on_subfield', function(err, collection) { + collection.insert([{'hello': {'a':4, 'b':5}}, {'hello': {'a':7, 'b':2}}, {'hello': {'a':4, 'b':10}}], {w:1}, function(err, ids) { + // Assert that we have no erros + client.error(function(err, errors) { + test.equal(1, errors.length); + test.ok(errors[0].err == null); + + // Create a unique subfield index and test that insert fails + client.createCollection('test_index_on_subfield2', function(err, collection) { + client.createIndex(collection.collectionName, 'hello.a', {w:1, unique:true}, function(err, indexName) { + collection.insert([{'hello': {'a':4, 'b':5}}, {'hello': {'a':7, 'b':2}}, {'hello': {'a':4, 'b':10}}], {w:1}, function(err, ids) { + // Assert that we have erros + test.ok(err != null); + test.done(); + }); + }); + }); + }); + }); + }); +} + +/** + * @ignore + */ +exports.shouldCorrectlyDropIndexes = function(configuration, test) { + var client = configuration.db(); + + client.createCollection('test_drop_indexes', function(err, collection) { + collection.insert({a:1}, {w:1}, function(err, ids) { + // Create an index on the collection + client.createIndex(collection.collectionName, 'a', {w:1}, function(err, indexName) { + test.equal("a_1", indexName); + // Drop all the indexes + collection.dropAllIndexes(function(err, result) { + test.equal(true, result); + + collection.indexInformation(function(err, result) { + test.ok(result['a_1'] == null); + test.done(); + }) + }) + }); + }) + }); +} + +/** + * @ignore + */ +exports.shouldThrowErrorOnAttemptingSafeCreateIndexWithNoCallback = function(configuration, test) { + var client = configuration.db(); + + client.createCollection('shouldThrowErrorOnAttemptingSafeUpdateWithNoCallback', function(err, collection) { + + try { + // insert a doc + collection.createIndex({a:1}, {w:1}); + test.ok(false); + } catch(err) {} + + test.done(); + }); +} + +/** + * @ignore + */ +exports.shouldThrowErrorOnAttemptingSafeEnsureIndexWithNoCallback = function(configuration, test) { + var client = configuration.db(); + + client.createCollection('shouldThrowErrorOnAttemptingSafeUpdateWithNoCallback', function(err, collection) { + + try { + // insert a doc + collection.ensureIndex({a:1}, {w:1}); + test.ok(false); + } catch(err) {} + + test.done(); + }); +} + + +/** + * @ignore + */ +exports.shouldCorrectlyHandleDistinctIndexes = function(configuration, test) { + var client = configuration.db(); + + client.createCollection('test_distinct_queries', function(err, collection) { + collection.insert([{'a':0, 'b':{'c':'a'}}, + {'a':1, 'b':{'c':'b'}}, + {'a':1, 'b':{'c':'c'}}, + {'a':2, 'b':{'c':'a'}}, {'a':3}, {'a':3}], {w:1}, function(err, ids) { + collection.distinct('a', function(err, docs) { + test.deepEqual([0, 1, 2, 3], docs.sort()); + }); + + collection.distinct('b.c', function(err, docs) { + test.deepEqual(['a', 'b', 'c'], docs.sort()); + test.done(); + }); + }) + }); +} + +/** + * @ignore + */ +exports.shouldCorrectlyExecuteEnsureIndex = function(configuration, test) { + var client = configuration.db(); + + client.createCollection('test_ensure_index', function(err, collection) { + // Create an index on the collection + client.ensureIndex(collection.collectionName, 'a', {w:1}, function(err, indexName) { + test.equal("a_1", indexName); + // Let's fetch the index information + client.indexInformation(collection.collectionName, function(err, collectionInfo) { + test.ok(collectionInfo['_id_'] != null); + test.equal('_id', collectionInfo['_id_'][0][0]); + test.ok(collectionInfo['a_1'] != null); + test.deepEqual([["a", 1]], collectionInfo['a_1']); + + client.ensureIndex(collection.collectionName, 'a', {w:1}, function(err, indexName) { + test.equal("a_1", indexName); + // Let's fetch the index information + client.indexInformation(collection.collectionName, function(err, collectionInfo) { + test.ok(collectionInfo['_id_'] != null); + test.equal('_id', collectionInfo['_id_'][0][0]); + test.ok(collectionInfo['a_1'] != null); + test.deepEqual([["a", 1]], collectionInfo['a_1']); + // Let's close the db + test.done(); + }); + }); + }); + }); + }) +} + +/** + * @ignore + */ +exports.shouldCorrectlyCreateAndUseSparseIndex = function(configuration, test) { + var client = configuration.db(); + + client.createCollection('create_and_use_sparse_index_test', function(err, r) { + client.collection('create_and_use_sparse_index_test', function(err, collection) { + + collection.ensureIndex({title:1}, {sparse:true, w:1}, function(err, indexName) { + collection.insert([{name:"Jim"}, {name:"Sarah", title:"Princess"}], {w:1}, function(err, result) { + collection.find({title:{$ne:null}}).sort({title:1}).toArray(function(err, items) { + test.equal(1, items.length); + test.equal("Sarah", items[0].name); + + // Fetch the info for the indexes + collection.indexInformation({full:true}, function(err, indexInfo) { + test.equal(null, err); + test.equal(2, indexInfo.length); + test.done(); + }) + }) + }); + }) + }) + }) +} + +/** + * @ignore + */ +exports["Should correctly execute insert with keepGoing option on mongod >= 1.9.1"] = function(configuration, test) { + var client = configuration.db(); + + client.admin().serverInfo(function(err, result){ + if(parseInt((result.version.replace(/\./g, ''))) >= 191) { + client.createCollection('shouldCorrectlyExecuteKeepGoingWithMongodb191OrHigher', function(err, collection) { + collection.ensureIndex({title:1}, {unique:true, w:1}, function(err, indexName) { + collection.insert([{name:"Jim"}, {name:"Sarah", title:"Princess"}], {w:1}, function(err, result) { + // Force keep going flag, ignoring unique index issue + collection.insert([{name:"Jim"}, {name:"Sarah", title:"Princess"}, {name:'Gump', title:"Gump"}], {w:1, keepGoing:true}, function(err, result) { + collection.count(function(err, count) { + test.equal(3, count); + test.done(); + }) + }); + }); + }); + }); + } else { + test.done(); + } + }); +} + +/** + * @ignore + */ +exports.shouldCorrectlyHandleGeospatialIndexes = function(configuration, test) { + var client = configuration.db(); + + client.admin().serverInfo(function(err, result){ + if(parseInt((result.version.replace(/\./g, ''))) >= 191) { + client.createCollection('geospatial_index_test', function(err, r) { + client.collection('geospatial_index_test', function(err, collection) { + collection.ensureIndex({loc:'2d'}, {w:1}, function(err, indexName) { + collection.insert({'loc': [-100,100]}, {w:1}, function(err, result) { + test.equal(err,null); + collection.insert({'loc': [200,200]}, {w:1}, function(err, result) { + err = err ? err : {}; + test.ok(err.err.indexOf("point not in interval of") != -1); + test.ok(err.err.indexOf("-180") != -1); + test.ok(err.err.indexOf("180") != -1); + test.done(); + }); + }); + }); + }); + }); + } else { + test.done(); + } + }); +} + +/** + * @ignore + */ +exports.shouldCorrectlyHandleGeospatialIndexesAlteredRange = function(configuration, test) { + var client = configuration.db(); + + client.admin().serverInfo(function(err, result){ + if(parseInt((result.version.replace(/\./g, ''))) >= 191) { + client.createCollection('geospatial_index_altered_test', function(err, r) { + client.collection('geospatial_index_altered_test', function(err, collection) { + collection.ensureIndex({loc:'2d'},{min:0,max:1024, w:1}, function(err, indexName) { + collection.insert({'loc': [100,100]}, {w:1}, function(err, result) { + test.equal(err,null); + collection.insert({'loc': [200,200]}, {w:1}, function(err, result) { + test.equal(err,null); + collection.insert({'loc': [-200,-200]}, {w:1}, function(err, result) { + err = err ? err : {}; + test.ok(err.err.indexOf("point not in interval of") != -1); + test.ok(err.err.indexOf("0") != -1); + test.ok(err.err.indexOf("1024") != -1); + test.done(); + }); + }); + }); + }); + }); + }); + } else { + test.done(); + } + }); +} + +/** + * @ignore + */ +exports.shouldThrowDuplicateKeyErrorWhenCreatingIndex = function(configuration, test) { + var client = configuration.db(); + + client.createCollection('shouldThrowDuplicateKeyErrorWhenCreatingIndex', function(err, collection) { + collection.insert([{a:1}, {a:1}], {w:1}, function(err, result) { + test.equal(null, err); + + collection.ensureIndex({a:1}, {unique:true, w:1}, function(err, indexName) { + test.ok(err != null); + test.done(); + }); + }) + }); +} + +/** + * @ignore + */ +exports.shouldThrowDuplicateKeyErrorWhenDriverInStrictMode = function(configuration, test) { + var db = configuration.newDbInstance({w:0}, {poolSize:1, auto_reconnect:true}); + + // Establish connection to db + db.open(function(err, db) { + db.createCollection('shouldThrowDuplicateKeyErrorWhenDriverInStrictMode', function(err, collection) { + collection.insert([{a:1}, {a:1}], {w:1}, function(err, result) { + test.equal(null, err); + + collection.ensureIndex({a:1}, {unique:true, w:1}, function(err, indexName) { + test.ok(err != null); + db.close(); + test.done(); + }); + }) + }); + }); +} + +/** + * @ignore + */ +exports.shouldCorrectlyUseMinMaxForSettingRangeInEnsureIndex = function(configuration, test) { + var client = configuration.db(); + + // Establish connection to db + client.createCollection('shouldCorrectlyUseMinMaxForSettingRangeInEnsureIndex', function(err, collection) { + test.equal(null, err); + + collection.ensureIndex({loc:'2d'}, {min:200, max:1400, w:1}, function(err, indexName) { + test.equal(null, err); + + collection.insert({loc:[600, 600]}, {w:1}, function(err, result) { + test.equal(null, err); + test.done(); + }); + }); + }); +} + +/** + * @ignore + */ +exports['Should correctly create an index with overriden name'] = function(configuration, test) { + var client = configuration.db(); + // Establish connection to db + client.createCollection('shouldCorrectlyCreateAnIndexWithOverridenName', function(err, collection) { + test.equal(null, err); + + collection.ensureIndex("name", {name: "myfunky_name"}, function(err, indexName) { + test.equal(null, err); + + // Fetch full index information + collection.indexInformation({full:false}, function(err, indexInformation) { + test.ok(indexInformation['myfunky_name'] != null); + test.done(); + }); + }); + }); +} + +exports['should handle index declarations using objects from other contexts'] = function(configuration, test) { + var client = configuration.db(); + var shared = require('./contexts'); + + client.collection('indexcontext').ensureIndex(shared.object, { safe: true, background: true }, function (err) { + test.equal(null, err); + client.collection('indexcontext').ensureIndex(shared.array, { safe: true, background: true }, function (err) { + test.equal(null, err); + test.done(); + }) + }) +} + +exports['should correctly return error message when applying unique index to dupicate documents'] = function(configuration, test) { + var client = configuration.db(); + var collection = client.collection("should_throw_error_due_to_duplicates"); + collection.insert([{a:1}, {a:1}, {a:1}], {w:1}, function(err, result) { + test.equal(null, err); + + collection.ensureIndex({a:1}, {w:1, unique:true}, function(err, result) { + test.ok(err != null); + test.done(); + }); + }); +} + +exports['should correctly drop index with no callback'] = function(configuration, test) { + var client = configuration.db(); + var collection = client.collection("should_correctly_drop_index"); + collection.insert([{a:1}], {w:1}, function(err, result) { + test.equal(null, err); + + collection.ensureIndex({a:1}, {w:1}, function(err, result) { + collection.dropIndex("a_1") + + test.done(); + }); + }); +} \ No newline at end of file diff --git a/new_tests/runner.js b/new_tests/runner.js index f0045d8a56..59fcbd2067 100644 --- a/new_tests/runner.js +++ b/new_tests/runner.js @@ -19,6 +19,7 @@ var configurations = Configuration // Single server configuration .add('single_server', function() { + var self = this; var serverManager = new ServerManager(); var db = new Db('integration_tests', new Server("127.0.0.1", 27017, {auto_reconnect: false, poolSize: 4}), {w:0, native_parser: false}); @@ -69,6 +70,16 @@ var configurations = Configuration return db; } + this.url = function(user, password) { + if(user) { + return 'mongodb://' + user + ':' + password + '@localhost:27017/' + self.db_name + '?safe=false'; + } + + return 'mongodb://localhost:27017/' + self.db_name + '?safe=false'; + } + + this.creatUrl = + // Used in tests this.db_name = "integration_tests"; }) @@ -180,7 +191,8 @@ var functional_tests_runner = Runner // Third parameter is the list of files to execute .add("functional_tests", [ - '/new_tests/functional/cursor_tests.js' + // '/new_tests/functional/index_tests.js' + // , '/new_tests/functional/cursor_tests.js' // , '/new_tests/functional/find_tests.js' // , '/new_tests/functional/insert_tests.js' // , '/new_tests/functional/admin_mode_tests.js' @@ -191,7 +203,7 @@ var functional_tests_runner = Runner // , '/new_tests/functional/uri_tests.js' // , '/new_tests/functional/url_parser_tests.js' // , '/new_tests/functional/objectid_tests.js' - // , '/new_tests/functional/connection_tests.js' + '/new_tests/functional/connection_tests.js' // , '/new_tests/functional/collection_tests.js' // , '/new_tests/functional/db_tests.js' ] diff --git a/test/connect_test.js b/test/__connect_test.js similarity index 100% rename from test/connect_test.js rename to test/__connect_test.js diff --git a/test/index_test.js b/test/__index_test.js similarity index 100% rename from test/index_test.js rename to test/__index_test.js