Skip to content

Commit

Permalink
enhancement(users): finished controller test for users
Browse files Browse the repository at this point in the history
  • Loading branch information
iolufemi committed Aug 26, 2017
1 parent c1b8829 commit 46e1d6e
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 46 deletions.
12 changes: 0 additions & 12 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
39 changes: 15 additions & 24 deletions controllers/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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){
Expand All @@ -121,24 +120,25 @@ 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);
})
.catch(function(err){
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;
Expand All @@ -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
}

}
Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions services/queue/jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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} });
}
Expand All @@ -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);
Expand Down
158 changes: 150 additions & 8 deletions test/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -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 = {};
Expand All @@ -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();
Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -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();
};
Expand All @@ -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);
});
});
});

0 comments on commit 46e1d6e

Please sign in to comment.