Permalink
Browse files

Create new functional test for MongoModel.

  • Loading branch information...
1 parent b187b49 commit a5fbf05e83e1720d9b01ce6f31838951bbe334a8 Maythee Anegboonlap committed Nov 29, 2011
Showing with 175 additions and 294 deletions.
  1. +55 −54 model/model.js
  2. +4 −0 package.json
  3. +116 −0 test/FunctionalTestMongoModel.js
  4. +0 −240 test/TestModel.js
View
@@ -9,6 +9,61 @@ var Model = function(name, client){
this.collection = new mongodb.Collection(client, name + '_collection');
}
+Model.prototype.create = function(object, callback){
+ callback = callback || function(){};
+
+ if(!object){
+ callback('invalid args');
+ return;
+ }
+
+ object._id = object.id;
+
+ var collection = this.collection;
+ collection.insert(object, {safe:true},
+ function(err, objects) {
+ if (err){
+ _log.error ('Create error');
+ _log.error (err.message);
+ console.trace();
+
+ callback(err.message);
+ }else{
+ callback(null, objects[0] || null);
+ }
+ });
+}
+
+Model.prototype.edit = function(id, object, callback){
+ callback = callback || function(){};
+
+ if(!id || !object){
+ callback('invalid args - id:'+id+', model:'+object);
+ return;
+ }
+
+ // Don't change object._id property
+ if (object._id) {
+ delete object._id;
+ }
+
+ var client = this.client;
+
+ var collection = this.collection;
+ collection.update({ '_id': id}, {'$set': object}, {safe:true},
+ function(err, objects) {
+ if (err){
+ _log.error ('Edit error');
+ _log.error (err.message);
+ _log.error (object);
+
+ callback(err.message);
+ }else{
+ callback(null, objects);
+ }
+ });
+}
+
Model.prototype.list = function(offset, limit, callback){
callback = callback || function(){};
offset = offset || 0;
@@ -71,60 +126,6 @@ Model.prototype.get = function (id, callback) {
});
}
-Model.prototype.create = function(object, callback){
- callback = callback || function(){};
-
- if(!object){
- callback('invalid args');
- return;
- }
-
- object._id = object.id;
-
- var collection = this.collection;
- collection.insert(object, {safe:true},
- function(err, objects) {
- if (err){
- _log.error ('Create error');
- _log.error (err.message);
- console.trace();
-
- callback(err.message);
- }else{
- callback(null, objects[0] || null);
- }
- });
-}
-
-Model.prototype.edit = function(id, object, callback){
- callback = callback || function(){};
-
- if(!id || !object){
- callback('invalid args - id:'+id+', model:'+object);
- return;
- }
-
- // Don't change object._id property
- if (object._id) {
- delete object._id;
- }
-
- var client = this.client;
-
- var collection = this.collection;
- collection.update({ '_id': id}, {'$set': object}, {safe:true},
- function(err, objects) {
- if (err){
- _log.error ('Edit error');
- _log.error (err.message);
- _log.error (object);
-
- callback(err.message);
- }else{
- callback(null, objects);
- }
- });
-}
Model.prototype.remove = function(id, callback){
callback = callback || function(){};
View
@@ -11,5 +11,9 @@
"now": "latest",
"oauth": "latest",
"step": "latest"
+ },
+ "devDependencies": {
+ "node-uuid": "latest",
+ "test_it": "latest"
}
}
@@ -0,0 +1,116 @@
+var log4js = require('log4js'),
+ mongodb = require('mongodb'),
+ step = require('step'),
+ testit = require('test_it'),
+ uuid = require('node-uuid');
+
+var Model = require('../model/model.js').Model;
+var logger = log4js.getLogger('test');
+
+var local = {};
+
+var require = {
+ begin: false,
+ create: false,
+ edit: false
+}
+
+testit('TestMongoModel', {
+ 'before all': function (test) {
+
+ step(
+ function begin () {
+ var server = new mongodb.Server("127.0.0.1", 27017, {});
+ new mongodb.Db('test', server, {}).open(this);
+ },
+ function gotConnection (error, client) {
+ if (error) throw error;
+
+ local.client = client;
+ local.collections = new mongodb.Collection(client, 'item_collection');
+
+ var fixtures = [{
+ name: 'Project1',
+ children: [1, 2, 3]
+ }, {
+ name: 'Project2',
+ children: [3, 4]
+ }];
+
+ local.collections.insertAll(fixtures, this);
+ },
+ function insertedFixtures (error, docs) {
+ if (error) throw error;
+
+ logger.info ('begin test');
+ require.begin = true;
+ local.model = Model.get('item', local.client);
+
+ });
+
+ },
+
+ 'after all': function (test) {
+ step(
+ function begin () {
+ var collections = local.collections;
+ collections.drop(this);
+ },
+ function dropedCollections (error) {
+ if (error) throw error;
+
+ logger.info ('end test');
+ });
+ },
+
+ 'test create with uuid': function (test) {
+ var collections;
+ var model;
+
+ var done = false;
+ var output = {};
+
+ step(
+ function begin () {
+ test.waitFor(
+ function () {
+ return require.begin;
+ }, this)
+ },
+ function logic () {
+ collections = local.collections;
+ model = local.model;
+
+ model.create({
+ id: uuid.v4(),
+ name: 'Project 3',
+ children: []
+ }, this);
+ },
+ function get (error) {
+ collections.find().toArray(this);
+ },
+ function found (error, docs) {
+ if (!error) {
+ output.count = docs.length;
+ output.target = docs[docs.length - 1];
+ } else {
+ logger.error (error);
+ }
+
+ done = true;
+ });
+
+ test.waitFor(
+ function (time) {
+ return done;
+ },
+ function () {
+ require.create = true;
+
+ test.assert(3, output.count);
+ test.assertEqual(output.target.id, output.target._id);
+ });
+
+ }
+});
Oops, something went wrong.

0 comments on commit a5fbf05

Please sign in to comment.