Skip to content

Commit

Permalink
First Commit for Mongo DB Driver
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Kvalheim committed Jan 7, 2010
0 parents commit f304d17
Show file tree
Hide file tree
Showing 78 changed files with 20,480 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

NODE = node

test:
@$(NODE) spec/spec.node.js

integrate_test:
@$(NODE) integration/integration_tests.js

.PHONY: test
132 changes: 132 additions & 0 deletions dummy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
require.paths.unshift("lib");
require("mongodb/connection");
require("mongodb/db");

sys = require("sys");

require("mongodb/bson/bson")
require("mongodb/bson/collections")
require("mongodb/bson/binary_parser")
require("goog/math/integer")
require("goog/math/long")

sys = require("sys");

// var a = {b:23, a:12, d:14, c:45};
// var b = {2:32, 1:12};
// var c = [{2:32}, {1:12}];
//
// for(var i in a) {
// sys.puts("=== " + i);
// }
//
// for(var i in b) {
// sys.puts("=== " + i);
// }
//
// for(var i in c) {
// sys.puts("===" + sys.inspect(c[i]));
// }
//
// var a = new BSON().serialize(new OrderedHash().add('a', 1));
// new BinaryParser().pprint(a);
// var b = new BSON().deserialize(b);

// List off all the nodes
// var nodes = [{host: "127.0.0.1", port: 27017}, {host: "127.0.0.1", port: 27017}];
var nodes = [{host: "127.0.0.1", port: 27017}];
// Create a db object
var db = new Db('test', nodes, {});
db.addListener("connect", function() {
// We can now use the db to access stuff
// this.collections_info(null, function(reply) {
// sys.puts("db.collections_info callback");
// for(var index in reply[0].documents) {
// var document = reply[0].documents[index];
// sys.puts("name: " + document.name);
// }
// });
//
// this.collection_names(null, function(reply) {
// sys.puts("db.collection_names callback");
// for(var index in reply[0].documents) {
// var document = reply[0].documents[index];
// sys.puts("name: " + document.name);
// }
// });

// setInterval(function() {
// db.collection_names(null, function(reply) {
// sys.puts("db.collection_names callback 2");
// for(var index in reply[0].documents) {
// var document = reply[0].documents[index];
// // sys.puts("name: " + document.name);
// }
// });
// }, 1);

// for(var i = 0; i < 10000; i++) {
// this.collection_names(null, function(reply) {
// sys.puts("db.collection_names callback 2");
// for(var index in reply[0].documents) {
// var document = reply[0].documents[index];
// sys.puts("name: " + document.name);
// }
// });
// }

// db.collection_names(null, function(reply) {
// sys.puts("db.collection_names callback 2");
// for(var index in reply[0].documents) {
// var document = reply[0].documents[index];
// sys.puts("name: " + document.name);
// }
// });


// this.authenticate("admin", "admin", function(reply){
// sys.puts("authentication request");
// sys.puts(sys.inspect(reply[0].is_error()));
// sys.puts(sys.inspect(reply[0].error_message()));
// // Logout
// // db.logout(function(reply) {
// // sys.puts("executed logout command");
// // // sys.puts(sys.inspect(reply));
// // });
// db.lastError(function(reply) {
// sys.puts("got last error");
// sys.puts(sys.inspect(reply));
// });
// });

// db.createCollection("crazy", function(reply) {
// sys.puts("================== collection create function executed");
// // db.dropCollection("crazy", function(reply) {
// // sys.puts("================== collection drop function executed");
// // });
// });

// db.renameCollection("crazy", "crazy2", function(reply) {
// sys.puts("================== collection rename function executed");
// });

// db.dropCollection("crazy2", function(reply) {
// sys.puts("================== collection drop function executed");
// });
});
// Open the app
db.open();
// Let's call a list of all collection names in the specified db
// db.collections_info(null, function(reply) {
// sys.puts("db.collections_info callback");
// });


// Close the database
// db.close();

// var connection = new Connection('127.0.0.1', 27017);
// sys.puts("Connection created");
// connection.addListener("connect", function() {
// sys.puts("Tcp Connection connected");
// });
199 changes: 199 additions & 0 deletions integration/integration_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
require.paths.unshift("./lib");

GLOBAL.DEBUG = true;

sys = require("sys");
test = require("mjsunit");
require("mongodb/db");

/*******************************************************************************************************
Integration Tests
*******************************************************************************************************/

// Test the creation of a collection on the mongo db
function test_collection_methods() {
client.createCollection('integration_test_collection', function(replies) {
// Verify that all the result are correct coming back (should contain the value ok)
test.assertEquals(1, replies[0].documents[0].ok);
// Let's check that the collection was created correctly
client.collection_names(null, function(replies) {
test.assertEquals("integration_tests_.integration_test_collection", replies[0].documents[0].name);
// Rename the collection and check that it's gone
client.renameCollection("integration_test_collection", "integration_test_collection2", function(replies) {
test.assertEquals(1, replies[0].documents[0].ok);
// Drop the collection and check that it's gone
client.dropCollection("integration_test_collection2", function(replies) {
test.assertEquals(1, replies[0].documents[0].ok);
finished_tests.push({test_collection_methods:'ok'});
})
});
});
})
}

// Test the authentication method for the user
function test_authentication() {
var user_name = 'spongebob';
var password = 'password';
var user_password = MD5.hex_md5(user_name + ":mongo:" + password);

client.authenticate('admin', 'admin', function(replies) {
test.assertEquals(0, replies[0].documents[0].ok);
test.assertEquals("auth fails", replies[0].documents[0].errmsg);
// Fetch a user collection
var user_collection = client.collection('system.users');
// Insert a user document
var user_doc = new OrderedHash().add('user', user_name).add('pwd', user_password);
// Insert the user into the system users collections
user_collection.insert(user_doc, function(replies) {
test.assertTrue(replies[0].documents[0]['_id'].toHexString().length == 24);
// Ensure authentication works correctly
client.authenticate(user_name, password, function(replies) {
test.assertEquals(1, replies[0].documents[0].ok);
finished_tests.push({test_authentication:'ok'});
});
});
});
}

// Test the access to collections
function test_collections() {
// Create two collections
var spiderman_collection = client.collection('test.spiderman');
var mario_collection = client.collection('test.mario');
// Insert test documents (creates collections)
spiderman_collection.insert(new OrderedHash().add("foo", 5));
mario_collection.insert(new OrderedHash().add("bar", 0));
// Assert collections
client.collections(function(collections) {
test.assertTrue(collections.length >= 2);
test.assertTrue(locate_collection_by_name("test.spiderman", collections) != null);
test.assertTrue(locate_collection_by_name("test.mario", collections) != null);
test.assertTrue(locate_collection_by_name("does_not_exist", collections) == null);
finished_tests.push({test_collections:'ok'});
});
}

// Test the generation of the object ids
function test_object_id_generation() {
var collection = client.collection('test_object_id_generation.data');
var number_of_tests_done = 0;

// Insert test documents (creates collections and test fetch by query)
collection.insert(new OrderedHash().add("name", "Fred").add("age", 42), function(ids) {
test.assertEquals(1, ids.length);
test.assertEquals(1, ids[0].documents.length);
test.assertTrue(ids[0].documents[0]['_id'].toHexString().length == 24);
// Locate the first document inserted
collection.findOne(new OrderedHash().add("name", "Fred"), function(records) {
test.assertEquals(1, records.length);
test.assertEquals(1, records[0].documents.length);
test.assertEquals(ids[0].documents[0]['_id'].toHexString(), records[0].documents[0]['_id'].toHexString());
number_of_tests_done++;
});
});

// Insert another test document and collect using ObjectId
collection.insert(new OrderedHash().add("name", "Pat").add("age", 21), function(ids) {
test.assertEquals(1, ids.length);
test.assertEquals(1, ids[0].documents.length);
test.assertTrue(ids[0].documents[0]['_id'].toHexString().length == 24);
// Locate the first document inserted
collection.findOne(ids[0].documents[0]['_id'], function(records) {
test.assertEquals(1, records.length);
test.assertEquals(1, records[0].documents.length);
test.assertEquals(ids[0].documents[0]['_id'].toHexString(), records[0].documents[0]['_id'].toHexString());
number_of_tests_done++;
});
});

// Manually created id
var objectId = new ObjectID(null);

// Insert a manually created document with generated oid
collection.insert(new OrderedHash().add("_id", objectId.id).add("name", "Donald").add("age", 95), function(ids) {
test.assertEquals(1, ids.length);
test.assertEquals(1, ids[0].documents.length);
test.assertTrue(ids[0].documents[0]['_id'].toHexString().length == 24);
test.assertEquals(objectId.toHexString(), ids[0].documents[0]['_id'].toHexString());
// Locate the first document inserted
collection.findOne(ids[0].documents[0]['_id'], function(records) {
test.assertEquals(1, records.length);
test.assertEquals(1, records[0].documents.length);
test.assertEquals(ids[0].documents[0]['_id'].toHexString(), records[0].documents[0]['_id'].toHexString());
test.assertEquals(objectId.toHexString(), records[0].documents[0]['_id'].toHexString());
number_of_tests_done++;
});
});

var intervalId = setInterval(function() {
if(number_of_tests_done == 3) {
clearInterval(intervalId);
finished_tests.push({test_object_id_generation:'ok'});
}
}, 100);
}

/*******************************************************************************************************
Setup For Running Tests
*******************************************************************************************************/
// Set up the client connection
var client = new Db('integration_tests_', [{host: "127.0.0.1", port: 27017}], {});
client.addListener("connect", function() {
// Do cleanup of the db
client.dropDatabase(function() {
// Run all the tests
run_all_tests();
// Start the timer that checks that all the tests have finished or failed
ensure_tests_finished();
});
});
client.open();

function ensure_tests_finished() {
var intervalId = setInterval(function() {
if(finished_tests.length >= client_tests.length) {
// Print out the result
sys.puts("= Results =========================================================");
// Stop interval timer and close db connection
clearInterval(intervalId);
client.close();
// Print all the statuses
finished_tests.forEach(function(t) {
for(var i in t) {
sys.puts(i + " = " + sys.inspect(t[i]));
}
});
}
}, 100);
};

// All the client tests
var client_tests = [test_collection_methods, test_authentication, test_collections, test_object_id_generation];
// var client_tests = [test_object_id_generation];
var finished_tests = [];
// Run all the tests
function run_all_tests() {
sys.puts("= Executing tests =====================================================");
// Run all the tests
client_tests.forEach(function (t) {
var function_name = t.name;
sys.puts("executing test: [" + function_name + "]");
try {
t();
} catch(error) {
sys.puts(sys.inspect(error));
finished_tests.push({function_name:error});
}
});
}

/**
Helper Utilities for the testing
**/
function locate_collection_by_name(collectionName, collections) {
for(var index in collections) {
var collection = collections[index];
if(collection.collectionName == collectionName) return collection;
}
}
Loading

0 comments on commit f304d17

Please sign in to comment.