Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Create new functional test for MongoModel.

  • Loading branch information...
commit a5fbf05e83e1720d9b01ce6f31838951bbe334a8 1 parent b187b49
@llun authored
View
109 model/model.js
@@ -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
4 package.json
@@ -11,5 +11,9 @@
"now": "latest",
"oauth": "latest",
"step": "latest"
+ },
+ "devDependencies": {
+ "node-uuid": "latest",
+ "test_it": "latest"
}
}
View
116 test/FunctionalTestMongoModel.js
@@ -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);
+ });
+
+ }
+});
View
240 test/TestModel.js
@@ -1,240 +0,0 @@
-var TestIt = require('test_it');
-
-var mongodb = require('mongodb');
-var server = new mongodb.Server("127.0.0.1", 27017, {});
-var client = null;
-new mongodb.Db('test', server, {}).open(function (error, connectclient) {
- if (error) throw error;
- client = connectclient;
-});
-
-var Model = require('../model/model.js').Model;
-var taskModel = null;
-
-TestIt('testTaskModel', {
- 'before all': function(test) {
- test.waitFor(function(){
- return client !== null;
- }, begin);
-
- this.before = {
- create: false,
- edit: false,
- list: false,
- count: false,
- remove: false,
- exists: false
- }
-
- this.created = [];
- var created = this.created;
-
- this.fixtures = [{
- id: '1',
- detail: 'testT#1',
- status: 'todo',
- updated: new Date().getTime()
- },{
- id: '2',
- detail: 'testT#2',
- status: 'inprogress',
- updated: new Date().getTime()
- },{
- id: '3',
- detail: 'testT#3',
- status: 'done',
- updated: new Date().getTime()
- }];
- var fixtures = this.fixtures;
- var done = false;
-
- function begin(){
- taskModel = new Model('task', client);
- var collection = new mongodb.Collection(client, 'task_collection');
-
- collection.drop(function(){
- collection.insert(fixtures, {safe:true}, function(err, objects) {
- for(var i in objects){
- created.push(objects[i]);
- }
- done = true;
- });
- });
- }
-
- test.waitFor(function(){
- return done;
- }, function(){
- console.log('data prepare done.');
- });
- },
- 'testList': function(test){
- var before = this.before;
-
- var error;
- var data;
- taskModel.list(0, 50, function (err, docs){
- error = err;
- data = docs;
- before.list = true;
- });
-
- test.waitFor(function(){
- return before.list;
- }, function(){
- test.assert(!error);
- test.assertEqual(3, data.length);
- });
- },
- 'testCount': function(test) {
- var before = this.before;
-
- var error = null;
- var count = null;
-
- test.waitFor(function(){
- return before.list;
- }, begin);
-
- function begin(){
- taskModel.count(function(err, data){
- error = err;
- count = data;
- before.count = true;
- });
- }
-
-
- test.waitFor(function(){
- return before.count;
- }, function(){
- test.assert(!error);
- test.assertEqual(3, count);
- });
- },
- 'testCreate': function(test){
- var before = this.before;
-
- var error;
- var createdTask;
- test.waitFor(function(){
- return before.count;
- }, begin);
-
- function begin(){
- var task = {
- id: '4',
- detail: 'task ja !',
- updated: new Date().getTime()
- }
- taskModel.create(task, function(err, data){
- error = err;
- createdTask = data;
- before.create = true;
- });
- }
-
-
- test.waitFor(function(){
- return before.create;
- }, function(){
- test.assert(!error);
- test.assert(createdTask);
- });
- },
- 'testEdit': function(test){
- var before = this.before;
- var created = this.created;
- var error;
- var result;
-
- test.waitFor(function(){
- return before.create;
- }, begin);
-
- function begin(){
- var task = {
- summary: 'iteration#100',
- updated: new Date().getTime()
- }
-
- taskModel.edit(created[0]._id, task, function(err, data){
- error = err;
- result = data;
- before.edit = true;
- });
- }
- test.waitFor(function(){
- return before.edit;
- }, function(){
- test.assert(!error);
- test.assert(result);
- });
- },
- 'testRemove': function(test){
- var before = this.before;
- var created = this.created;
- var error;
- var result;
-
- test.waitFor(function(){
- return before.create;
- }, begin);
-
- function begin(){
- var iteration = {
- name: 'iteration#100'
- }
-
- taskModel.remove(created[0]._id, function(err, data){
- error = err;
- result = data;
- before.remove = true;
- });
- }
- test.waitFor(function(){
- return before.remove;
- }, function(){
- test.assert(!error);
- test.assert(result);
- });
- },
- 'testExists': function(test){
- var before = this.before;
- var created = this.created;
- var error;
- var result;
-
- var error2;
- var result2;
-
- test.waitFor(function(){
- return before.create;
- }, begin);
-
- function begin(){
- var iteration = {
- name: 'iteration#100'
- }
-
- taskModel.exists(created[1]._id, function(err, data){
- error = err;
- result = data;
- taskModel.exists('4e5c6bf676fec90d04000002', function(err, data){
- error2 = err;
- result2 = data;
- before.exists = true;
- });
- });
- }
- test.waitFor(function(){
- return before.exists;
- }, function(){
- test.assert(!error);
- test.assert(result);
-
- test.assert(!error2);
- test.assert(!result2);
- });
- },
-});
Please sign in to comment.
Something went wrong with that request. Please try again.