Skip to content

Commit

Permalink
add API and Test with mocha
Browse files Browse the repository at this point in the history
  • Loading branch information
i5ting committed Jun 26, 2015
1 parent 3a08912 commit 10d5f3b
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var watch = require('gulp-watch');
var path = 'test/**/*.js';

gulp.task('watch', function() {
gulp.watch(['test/**/*.js', '../lib/*.js'], ['mocha']);
gulp.watch(['test/**/*.js', 'lib/*.js'], ['mocha']);
});

var mocha = require('gulp-mocha');
Expand Down
19 changes: 8 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,18 @@ Meeting.delete({"username":"sss","password":"password"},function(err, user){
Test status

- [x] create
- [x] delete
- [x] deleteAll
- [ ] deleteById
- [x] delete = remove
- [x] deleteAll = removeAll
- [x] deleteById = removeById
- [x] getById
- [x] getAll = all
- [x] all = find({})
- [ ] query = getByQuery = find
- [x] one = fineOne
- [ ] update
- [ ] updateById
- [x] all = getAll = find({})
- [x] query = getByQuery = find
- [x] one = fineOne
- [x] update
- [x] updateById = updateOne
- [ ] page(todo)




more see [api doc](api.md)

## Contributing
Expand Down
43 changes: 42 additions & 1 deletion api.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,46 @@ equal

## updateById

Meeting.updateById(new_user._id, {
username: 'updated_user'
}, function(err, result){
console.dir(result)

assert.equal(result.ok, 1);
assert.equal(result.nModified, 1);
assert.equal(result.n, 1);
done();
});

## update

way 1: 3 params

## update
Meeting.update({'_id': new_user._id}, {
username: 'updated_user2'
}, function(err2, result){
console.dir(err2)
console.dir(result)

assert.equal(result.ok, 1);
assert.equal(result.nModified, 1);
assert.equal(result.n, 1);
done();
});
way 2: 4 params

Meeting.update({'_id': new_user._id}, {
username: 'updated_user3'
}, {multi: false}, function(err2, result){
console.dir(err2)
console.dir(result)

assert.equal(result.ok, 1);
assert.equal(result.nModified, 1);
assert.equal(result.n, 1);
done();
});
84 changes: 59 additions & 25 deletions lib/dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@
*/

function MongooseDao (Model){
/*if(typeof Model === 'undefined' || Model == null)
throw new Error('Model can not be null.');*/
if(!Model){
throw new Error(Model + " is not valid, please check if it is a mongoose model!");
}
this.model = Model;
}

// create
MongooseDao.prototype.create = function(doc, cb) {
this.model.create(doc, cb);
};


// read
MongooseDao.prototype.getById = function(id, cb) {
this.model.findOne({_id:id}, cb);
};
Expand All @@ -36,45 +39,76 @@ MongooseDao.prototype.countByQuery = function(query, callback) {
});
};

MongooseDao.prototype.query = MongooseDao.prototype.getByQuery = MongooseDao.prototype.getAll = function(query, cb) {
MongooseDao.prototype.query = MongooseDao.prototype.find = MongooseDao.prototype.getByQuery = function(query, cb) {
this.model.find(query, cb);
};

MongooseDao.prototype.all = function(cb) {
MongooseDao.prototype.all = MongooseDao.prototype.getAll = function(cb) {
this.model.find({}, cb);
};

MongooseDao.prototype.one = function(query, cb) {
this.model.findOne(query, cb);
};

MongooseDao.prototype.delete = function(query, cb){
// update
MongooseDao.prototype.updateById = MongooseDao.prototype.updateOne = function(id, update, cb) {
// console.log('MongooseDao.prototype.updateById' + update);
this.update({_id:id}, update , {}, cb);
};

// way1: conditions, update , cb
// way2: conditions, update ,options, cb
MongooseDao.prototype.update = function() {
var conditions, update ,options, cb;

var _options;
if (arguments.length == 3) {
_options = {}
conditions = arguments[0];
update = arguments[1];
cb = arguments[2];
}else if(arguments.length == 4) {
conditions = arguments[0];
update = arguments[1];
_options = arguments[2];
cb = arguments[3];
}else{
throw new Error("MongooseDao.prototype.update param is not valid!")
}


var opt = { multi: true };
_extend(opt, _options);

this.model.update(conditions, update, opt, cb);
};

// delete
MongooseDao.prototype.delete = MongooseDao.prototype.remove = function(query, cb){
this.model.remove(query, cb);
};

MongooseDao.prototype.deleteAll = function(cb){
MongooseDao.prototype.deleteAll = MongooseDao.prototype.removeAll = function(cb){
this.delete({}, cb);
};


MongooseDao.prototype.deleteById = function(id, callback) {
console.log('MongooseDao.prototype.deleteById');
this.delete({_id:id}, callback);
MongooseDao.prototype.deleteById = MongooseDao.prototype.removeById = function(id, cb) {
// console.log('MongooseDao.prototype.deleteById');
this.delete({_id: id}, cb);
};

MongooseDao.prototype.updateById = function(id, update, callback) {
console.log('MongooseDao.prototype.updateById');
this.update({_id:id}, update , {}, callback);
};

MongooseDao.prototype.update = function(conditions, update ,options, callback) {
this.model.update(conditions, update, options, function (error) {
if(error) {
return callback(error);
}
function _extend(des, src) {
if (!des) {
des = {};
}
if (src) {
for (var i in src) {
des[i] = src[i];
}
}

return des;
}

return callback(null);
});
};

module.exports = MongooseDao;
105 changes: 103 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var Meeting = require('./lib/Meeting');
// console.log(user);
// });

var fixture_id;
describe('MongooseDao', function(){
before(function(done) {
// runs before all tests in this block
Expand All @@ -23,6 +24,7 @@ describe('MongooseDao', function(){

Meeting.create({"username":"fixture-user","password":"password"},function(err, user){
// console.log(user);
fixture_id = user._id;
done();
});
});
Expand All @@ -40,7 +42,7 @@ describe('MongooseDao', function(){
// runs after each test in this block
})

describe('#create()', function(){
describe('#MongooseDao()', function(){
it('should return ok when record create', function(done){
Meeting.create({"username":"sss","password":"password"},function(err, user){
assert.equal(user.username, 'sss');
Expand All @@ -55,13 +57,26 @@ describe('MongooseDao', function(){
});
})

it('should return ok when record deleteById', function(done){
Meeting.deleteById(fixture_id, function(err){
assert.equal(err, null);
done();
});
})

it('should return ok when record removeById', function(done){
Meeting.removeById(fixture_id, function(err){
assert.equal(err, null);
done();
});
})

it('should return ok when record getById', function(done){
Meeting.one({"username":"sss","password":"password"},function(err, user){
if(err){
console.dir(err);
}
// console.dir(user._id)

Meeting.getById(user._id, function(err, new_user){
assert.equal(new_user.username, 'sss');
done();
Expand All @@ -82,5 +97,91 @@ describe('MongooseDao', function(){
});
})

it('should return ok when record all', function(done){
Meeting.all(function(err, users){
if(err){
console.dir(err);
}

// console.dir(users);
assert.equal(users.length > 0, true);
done();
});
})

it('should return ok when record updateById', function(done){
Meeting.one({"username":"sss","password":"password"},function(err, user){
if(err){
console.dir(err);
}

Meeting.getById(user._id, function(err, new_user){
Meeting.updateById(new_user._id, {
username: 'updated_user'
}, function(err2, result){
// console.dir(result)

assert.equal(result.ok, 1);
assert.equal(result.nModified, 1);
assert.equal(result.n, 1);

done();
});

});
});
})


it('should return ok when record update', function(done){
Meeting.one({"password":"password"}, function(err, user){
if(err){
console.dir(err);
done(err);
}
Meeting.getById(user._id, function(err, new_user){
// update with 3 params
Meeting.update({'_id': new_user._id}, {
username: 'updated_user2'
}, function(err2, result){
// console.dir(err2)
// console.dir(result)

assert.equal(result.ok, 1);
assert.equal(result.nModified, 1);
assert.equal(result.n, 1);

done();
});

});
});
})

it('should return ok when record update', function(done){
Meeting.one({"password":"password"}, function(err, user){
if(err){
console.dir(err);
done(err);
}
Meeting.getById(user._id, function(err, new_user){
// update with 4 params
Meeting.update({'_id': new_user._id}, {
username: 'updated_user3'
}, {multi: false}, function(err2, result){
// console.dir(err2)
// console.dir(result)

assert.equal(result.ok, 1);
assert.equal(result.nModified, 1);
assert.equal(result.n, 1);

done();
});

});
});
})

})
})

0 comments on commit 10d5f3b

Please sign in to comment.