diff --git a/TODO.md b/TODO.md index eec7a703..57e9683c 100644 --- a/TODO.md +++ b/TODO.md @@ -1,18 +1,6 @@ ### TODOs | Filename | line # | TODO |:------|:------:|:------ -| controllers/Initialize.js | 18 | Test initialize controller -| controllers/Users.js | 13 | Test buildProjection function -| controllers/Users.js | 45 | Test that search works -| controllers/Users.js | 128 | Test limiting -| controllers/Users.js | 129 | Test that response contains count of total record for the query -| controllers/Users.js | 130 | Test that the last document Id in the return array of documents is in the response -| controllers/Users.js | 131 | Test that sorting works -| controllers/Users.js | 132 | Test that projection works -| controllers/Users.js | 133 | Test that populating works -| controllers/Users.js | 134 | Test that date range works -| controllers/Users.js | 284 | Test users controller -| controllers/Users.js | 285 | Test that any deleted data is backed up | routes/index.js | 214 | Implement API Generator | routes/initialize.js | 10 | Test initialize route | routes/users.js | 43 | Test users route diff --git a/controllers/Users.js b/controllers/Users.js index 52e50901..f1c77c71 100644 --- a/controllers/Users.js +++ b/controllers/Users.js @@ -13,7 +13,6 @@ var UsersController = {}; UsersController.buildProjection = function(projections){ debug('starting build...'); var projection = projections.split(','); // Projection should be comma separated. eg. name,location - // ToDo: Test buildProjection function return q.Promise(function(resolve,reject,notify){ debug('This is a promise...'); var num = projection.length; @@ -52,7 +51,6 @@ UsersController.find = function(req,res,next){ .catch(function(err){ next(err); }); - // ToDo: Test that search works }else{ query = req.query; var projection = query.projection; // Projection should be comma separated. eg. name,location @@ -104,7 +102,8 @@ UsersController.find = function(req,res,next){ if(populate){ delete query.populate; } - var total = Users.count(query); + var totalResult = Users.count(query); + var total = Users.count({}); var question = Users.find(query); if(limit){ @@ -121,15 +120,16 @@ UsersController.find = function(req,res,next){ } if(projection){ - q.all([ourProjection,total]) - .spread(function(resp,total){ - return [question.select(resp),total]; + q.all([ourProjection,total,totalResult]) + .spread(function(resp,total,totalResult){ + return [question.select(resp),total,totalResult]; }) - .spread(function(resp,total){ + .spread(function(resp,total,totalResult){ var ourLastId = resp[resp.length - 1]._id; var extraData = {}; extraData.limit = limit * 1; extraData.total = total; + extraData.totalResult = totalResult; extraData.lastId = ourLastId; res.ok(resp, false, extraData); }) @@ -137,8 +137,8 @@ UsersController.find = function(req,res,next){ next(err); }); }else{ - q.all([question,total]) - .spread(function(resp,total){ + q.all([question,total,totalResult]) + .spread(function(resp,total,totalResult){ var ourLastId; if(resp.length === 0){ ourLastId = null; @@ -149,18 +149,12 @@ UsersController.find = function(req,res,next){ extraData.limit = limit * 1; extraData.total = total; extraData.lastId = ourLastId; + extraData.totalResult = totalResult; res.ok(resp, false, extraData); }) .catch(function(err){ next(err); }); - // ToDo: Test limiting - // ToDO: Test that response contains count of total record for the query - // ToDo: Test that the last document Id in the return array of documents is in the response - // ToDo: Test that sorting works - // ToDo: Test that projection works - // ToDo: Test that populating works - // ToDo: Test that date range works } } @@ -244,9 +238,9 @@ UsersController.delete = function(req,res,next){ }) .then(function(resp){ // Delete matches - return Users.deleteMany(query); + return [Users.deleteMany(query), resp]; }) - .then(function(resp){ + .spread(function(deleted, resp){ res.ok(resp); }) .catch(function(err){ @@ -274,10 +268,10 @@ UsersController.deleteOne = function(req,res,next){ }) .then(function(resp){ // Delete match - return Users.findByIdAndRemove(id); + return [Users.findByIdAndRemove(id),resp]; }) - .then(function(resp){ - res.ok(resp); + .spread(function(deleted,resp){ + res.ok(resp[0]); }) .catch(function(err){ next(err); @@ -309,6 +303,3 @@ UsersController.restore = function(req,res,next){ }; module.exports = UsersController; - -// Todo: Test users controller -// ToDo: Test that any deleted data is backed up diff --git a/services/queue/jobs.js b/services/queue/jobs.js index f24e47aa..cc4b8417 100644 --- a/services/queue/jobs.js +++ b/services/queue/jobs.js @@ -61,7 +61,7 @@ jobs.createSearchTags = function(data, done){ var task; if(update){ - task = models[model].update(data,{ $set: { updatedAt: new Date()}, $addToSet: {tags: {$each: split}} }); + task = models[model].update(data,{ $set: { updatedAt: new Date().toISOString() }, $addToSet: {tags: {$each: split}} }); }else{ task = models[model].update(data,{ $set: { tags: split} }); } @@ -79,7 +79,7 @@ jobs.createSearchTags = function(data, done){ // Backup Data to Trash // ToDo: Test saveToTrash job jobs.saveToTrash = function(data, done){ - log.info('Saving '+data._id+' to Trash...'); + log.info('Saving '+data.data._id+' to Trash...'); models.Trash.create(data) .then(function(res){ done(false, res); diff --git a/test/controllers/users.js b/test/controllers/users.js index e65dca96..04e60b53 100644 --- a/test/controllers/users.js +++ b/test/controllers/users.js @@ -7,10 +7,16 @@ var chaiAsPromised = require("chai-as-promised"); chai.use(chaiAsPromised); var users = require('../../controllers/Users.js'); var workers = require('../../services/queue/workers'); +var _ = require('lodash'); +var db = require('../../models'); var userId; var userId2; +var lastId; +var forDelete; +var trashId; +var from = new Date(new Date().setMinutes(new Date().getMinutes() - 3)).toISOString(); describe('Users controller', function(){ it('should create documents', function(done){ var next = function(err){ @@ -106,6 +112,7 @@ describe('Users controller', function(){ data.should.be.an.object; /* jslint ignore:line */ extraData.total.should.be.a.number; /* jslint ignore:line */ + extraData.totalResult.should.be.a.number; /* jslint ignore:line */ done(); }; var req = {}; @@ -118,7 +125,7 @@ describe('Users controller', function(){ }; var res = {}; res.ok = function(data, cache, extraData){ - + lastId = extraData.lastId; data.should.be.an.object; /* jslint ignore:line */ extraData.lastId.should.be.a.string; /* jslint ignore:line */ done(); @@ -189,7 +196,80 @@ describe('Users controller', function(){ req.query.populate = 'toPop'; users.find(req, res, next); }); - }); + + it('should load next page for pagination', function(done){ + var next = function(err){ + done(err); + }; + var res = {}; + res.ok = function(data){ + var next = function(err){ + done(err); + }; + var res = {}; + res.ok = function(data, cache, extraData){ + forDelete = _.map(data,function(value){ + return value._id.toString(); + }); + data.should.be.an.object; /* jslint ignore:line */ + done(); + }; + var req = {}; + req.query = {_id: {$gt: lastId}}; + users.find(req, res, next); + }; + var req = {}; + req.body = [{ + name: 'Femi2', + someOtherStringData: 'this is pizza' + }, + { + name: 'Bolu2', + someOtherStringData: 'this is a meat' + }, + { + name: 'Bayo2', + someOtherStringData: 'Meta' + }]; + users.create(req, res, next); + }); + +it('should filter by date range', function(done){ + var next = function(err){ + done(err); + }; + var res = {}; + res.ok = function(data, cache, extraData){ + + data.should.be.an.object; /* jslint ignore:line */ + data.length.should.be.above(0); /* jslint ignore:line */ + done(); + }; + var req = {}; + req.query = {}; + req.query.from = from; + + req.query.to = new Date().toISOString(); + users.find(req, res, next); +}); + +it('should filter by date range without setting the end date', function(done){ + var next = function(err){ + done(err); + }; + var res = {}; + res.ok = function(data, cache, extraData){ + + data.should.be.an.object; /* jslint ignore:line */ + data.length.should.be.above(0); /* jslint ignore:line */ + done(); + }; + var req = {}; + req.query = {}; + req.query.from = from; + users.find(req, res, next); +}); +}); it('should find one document', function(done){ var next = function(err){ @@ -225,7 +305,7 @@ it('should update a document', function(done){ }; var res = {}; res.ok = function(data, cache, extraData){ - console.log('hhfhf', data); + data.should.be.an('object'); /* jslint ignore:line */ done(); }; @@ -235,12 +315,74 @@ it('should update a document', function(done){ users.updateOne(req, res, next); }); describe('Delete', function(){ - it('should delete multiple data'); - it('should have back up multiple deleted data'); - it('should delete one data'); - it('should have backed up one data'); + it('should delete multiple data', function(done){ + var next = function(err){ + done(err); + }; + var res = {}; + res.ok = function(data, cache, extraData){ + + data.length.should.be.above(0); /* jslint ignore:line */ + done(); + }; + var req = {}; + req.query = {_id: {$gt: lastId}}; + users.delete(req, res, next); + }); + it('should have backed up multiple deleted data', function(done){ + setTimeout(function(){ + db.Trash.find() + .where('data._id') + .in(forDelete) + .then(function(res){ + res.length.should.be.above(0); + done(); + }) + .catch(function(err){ + done(err); + }); + },1000); + }); + it('should delete one data', function(done){ + var next = function(err){ + done(err); + }; + var res = {}; + res.ok = function(data, cache, extraData){ + data.should.be.an('object'); /* jslint ignore:line */ + done(); + }; + var req = {}; + req.params = {id: lastId}; + users.deleteOne(req, res, next); + }); + it('should have backed up one data', function(done){ + setTimeout(function(){ + db.Trash.find({'data._id':lastId.toString()}) + .then(function(resp){ + trashId = resp[0]._id; + resp.length.should.be.above(0); + done(); + }) + .catch(function(err){ + done(err); + }); + },1000); + }); }); describe('Restore', function(){ - it('should restore a previously deleted data'); + it('should restore a previously deleted data', function(done){ + var next = function(err){ + done(err); + }; + var res = {}; + res.ok = function(data, cache, extraData){ + data.should.be.an('object'); /* jslint ignore:line */ + done(); + }; + var req = {}; + req.params = {id: trashId}; + users.restore(req, res, next); + }); }); });