Skip to content

Commit

Permalink
Create new functional test for MongoModel.
Browse files Browse the repository at this point in the history
  • Loading branch information
Maythee Anegboonlap committed Nov 29, 2011
1 parent b187b49 commit a5fbf05
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 294 deletions.
109 changes: 55 additions & 54 deletions model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(){};
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@
"now": "latest",
"oauth": "latest",
"step": "latest"
},
"devDependencies": {
"node-uuid": "latest",
"test_it": "latest"
}
}
116 changes: 116 additions & 0 deletions test/FunctionalTestMongoModel.js
Original file line number Diff line number Diff line change
@@ -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);
});

}
});
Loading

0 comments on commit a5fbf05

Please sign in to comment.