Skip to content

Commit 46e1d6e

Browse files
committed
enhancement(users): finished controller test for users
1 parent c1b8829 commit 46e1d6e

File tree

4 files changed

+167
-46
lines changed

4 files changed

+167
-46
lines changed

TODO.md

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
### TODOs
22
| Filename | line # | TODO
33
|:------|:------:|:------
4-
| controllers/Initialize.js | 18 | Test initialize controller
5-
| controllers/Users.js | 13 | Test buildProjection function
6-
| controllers/Users.js | 45 | Test that search works
7-
| controllers/Users.js | 128 | Test limiting
8-
| controllers/Users.js | 129 | Test that response contains count of total record for the query
9-
| controllers/Users.js | 130 | Test that the last document Id in the return array of documents is in the response
10-
| controllers/Users.js | 131 | Test that sorting works
11-
| controllers/Users.js | 132 | Test that projection works
12-
| controllers/Users.js | 133 | Test that populating works
13-
| controllers/Users.js | 134 | Test that date range works
14-
| controllers/Users.js | 284 | Test users controller
15-
| controllers/Users.js | 285 | Test that any deleted data is backed up
164
| routes/index.js | 214 | Implement API Generator
175
| routes/initialize.js | 10 | Test initialize route
186
| routes/users.js | 43 | Test users route

controllers/Users.js

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ var UsersController = {};
1313
UsersController.buildProjection = function(projections){
1414
debug('starting build...');
1515
var projection = projections.split(','); // Projection should be comma separated. eg. name,location
16-
// ToDo: Test buildProjection function
1716
return q.Promise(function(resolve,reject,notify){
1817
debug('This is a promise...');
1918
var num = projection.length;
@@ -52,7 +51,6 @@ UsersController.find = function(req,res,next){
5251
.catch(function(err){
5352
next(err);
5453
});
55-
// ToDo: Test that search works
5654
}else{
5755
query = req.query;
5856
var projection = query.projection; // Projection should be comma separated. eg. name,location
@@ -104,7 +102,8 @@ UsersController.find = function(req,res,next){
104102
if(populate){
105103
delete query.populate;
106104
}
107-
var total = Users.count(query);
105+
var totalResult = Users.count(query);
106+
var total = Users.count({});
108107
var question = Users.find(query);
109108

110109
if(limit){
@@ -121,24 +120,25 @@ UsersController.find = function(req,res,next){
121120
}
122121

123122
if(projection){
124-
q.all([ourProjection,total])
125-
.spread(function(resp,total){
126-
return [question.select(resp),total];
123+
q.all([ourProjection,total,totalResult])
124+
.spread(function(resp,total,totalResult){
125+
return [question.select(resp),total,totalResult];
127126
})
128-
.spread(function(resp,total){
127+
.spread(function(resp,total,totalResult){
129128
var ourLastId = resp[resp.length - 1]._id;
130129
var extraData = {};
131130
extraData.limit = limit * 1;
132131
extraData.total = total;
132+
extraData.totalResult = totalResult;
133133
extraData.lastId = ourLastId;
134134
res.ok(resp, false, extraData);
135135
})
136136
.catch(function(err){
137137
next(err);
138138
});
139139
}else{
140-
q.all([question,total])
141-
.spread(function(resp,total){
140+
q.all([question,total,totalResult])
141+
.spread(function(resp,total,totalResult){
142142
var ourLastId;
143143
if(resp.length === 0){
144144
ourLastId = null;
@@ -149,18 +149,12 @@ UsersController.find = function(req,res,next){
149149
extraData.limit = limit * 1;
150150
extraData.total = total;
151151
extraData.lastId = ourLastId;
152+
extraData.totalResult = totalResult;
152153
res.ok(resp, false, extraData);
153154
})
154155
.catch(function(err){
155156
next(err);
156157
});
157-
// ToDo: Test limiting
158-
// ToDO: Test that response contains count of total record for the query
159-
// ToDo: Test that the last document Id in the return array of documents is in the response
160-
// ToDo: Test that sorting works
161-
// ToDo: Test that projection works
162-
// ToDo: Test that populating works
163-
// ToDo: Test that date range works
164158
}
165159

166160
}
@@ -244,9 +238,9 @@ UsersController.delete = function(req,res,next){
244238
})
245239
.then(function(resp){
246240
// Delete matches
247-
return Users.deleteMany(query);
241+
return [Users.deleteMany(query), resp];
248242
})
249-
.then(function(resp){
243+
.spread(function(deleted, resp){
250244
res.ok(resp);
251245
})
252246
.catch(function(err){
@@ -274,10 +268,10 @@ UsersController.deleteOne = function(req,res,next){
274268
})
275269
.then(function(resp){
276270
// Delete match
277-
return Users.findByIdAndRemove(id);
271+
return [Users.findByIdAndRemove(id),resp];
278272
})
279-
.then(function(resp){
280-
res.ok(resp);
273+
.spread(function(deleted,resp){
274+
res.ok(resp[0]);
281275
})
282276
.catch(function(err){
283277
next(err);
@@ -309,6 +303,3 @@ UsersController.restore = function(req,res,next){
309303
};
310304

311305
module.exports = UsersController;
312-
313-
// Todo: Test users controller
314-
// ToDo: Test that any deleted data is backed up

services/queue/jobs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ jobs.createSearchTags = function(data, done){
6161

6262
var task;
6363
if(update){
64-
task = models[model].update(data,{ $set: { updatedAt: new Date()}, $addToSet: {tags: {$each: split}} });
64+
task = models[model].update(data,{ $set: { updatedAt: new Date().toISOString() }, $addToSet: {tags: {$each: split}} });
6565
}else{
6666
task = models[model].update(data,{ $set: { tags: split} });
6767
}
@@ -79,7 +79,7 @@ jobs.createSearchTags = function(data, done){
7979
// Backup Data to Trash
8080
// ToDo: Test saveToTrash job
8181
jobs.saveToTrash = function(data, done){
82-
log.info('Saving '+data._id+' to Trash...');
82+
log.info('Saving '+data.data._id+' to Trash...');
8383
models.Trash.create(data)
8484
.then(function(res){
8585
done(false, res);

test/controllers/users.js

Lines changed: 150 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@ var chaiAsPromised = require("chai-as-promised");
77
chai.use(chaiAsPromised);
88
var users = require('../../controllers/Users.js');
99
var workers = require('../../services/queue/workers');
10+
var _ = require('lodash');
11+
var db = require('../../models');
1012

1113

1214
var userId;
1315
var userId2;
16+
var lastId;
17+
var forDelete;
18+
var trashId;
19+
var from = new Date(new Date().setMinutes(new Date().getMinutes() - 3)).toISOString();
1420
describe('Users controller', function(){
1521
it('should create documents', function(done){
1622
var next = function(err){
@@ -106,6 +112,7 @@ describe('Users controller', function(){
106112

107113
data.should.be.an.object; /* jslint ignore:line */
108114
extraData.total.should.be.a.number; /* jslint ignore:line */
115+
extraData.totalResult.should.be.a.number; /* jslint ignore:line */
109116
done();
110117
};
111118
var req = {};
@@ -118,7 +125,7 @@ describe('Users controller', function(){
118125
};
119126
var res = {};
120127
res.ok = function(data, cache, extraData){
121-
128+
lastId = extraData.lastId;
122129
data.should.be.an.object; /* jslint ignore:line */
123130
extraData.lastId.should.be.a.string; /* jslint ignore:line */
124131
done();
@@ -189,7 +196,80 @@ describe('Users controller', function(){
189196
req.query.populate = 'toPop';
190197
users.find(req, res, next);
191198
});
192-
});
199+
200+
it('should load next page for pagination', function(done){
201+
var next = function(err){
202+
done(err);
203+
};
204+
var res = {};
205+
res.ok = function(data){
206+
var next = function(err){
207+
done(err);
208+
};
209+
var res = {};
210+
res.ok = function(data, cache, extraData){
211+
forDelete = _.map(data,function(value){
212+
return value._id.toString();
213+
});
214+
data.should.be.an.object; /* jslint ignore:line */
215+
done();
216+
};
217+
var req = {};
218+
req.query = {_id: {$gt: lastId}};
219+
users.find(req, res, next);
220+
};
221+
var req = {};
222+
req.body = [{
223+
name: 'Femi2',
224+
someOtherStringData: 'this is pizza'
225+
},
226+
{
227+
name: 'Bolu2',
228+
someOtherStringData: 'this is a meat'
229+
},
230+
{
231+
name: 'Bayo2',
232+
someOtherStringData: 'Meta'
233+
}];
234+
users.create(req, res, next);
235+
});
236+
237+
it('should filter by date range', function(done){
238+
var next = function(err){
239+
done(err);
240+
};
241+
var res = {};
242+
res.ok = function(data, cache, extraData){
243+
244+
data.should.be.an.object; /* jslint ignore:line */
245+
data.length.should.be.above(0); /* jslint ignore:line */
246+
done();
247+
};
248+
var req = {};
249+
req.query = {};
250+
req.query.from = from;
251+
252+
req.query.to = new Date().toISOString();
253+
users.find(req, res, next);
254+
});
255+
256+
it('should filter by date range without setting the end date', function(done){
257+
var next = function(err){
258+
done(err);
259+
};
260+
var res = {};
261+
res.ok = function(data, cache, extraData){
262+
263+
data.should.be.an.object; /* jslint ignore:line */
264+
data.length.should.be.above(0); /* jslint ignore:line */
265+
done();
266+
};
267+
var req = {};
268+
req.query = {};
269+
req.query.from = from;
270+
users.find(req, res, next);
271+
});
272+
});
193273

194274
it('should find one document', function(done){
195275
var next = function(err){
@@ -225,7 +305,7 @@ it('should update a document', function(done){
225305
};
226306
var res = {};
227307
res.ok = function(data, cache, extraData){
228-
console.log('hhfhf', data);
308+
229309
data.should.be.an('object'); /* jslint ignore:line */
230310
done();
231311
};
@@ -235,12 +315,74 @@ it('should update a document', function(done){
235315
users.updateOne(req, res, next);
236316
});
237317
describe('Delete', function(){
238-
it('should delete multiple data');
239-
it('should have back up multiple deleted data');
240-
it('should delete one data');
241-
it('should have backed up one data');
318+
it('should delete multiple data', function(done){
319+
var next = function(err){
320+
done(err);
321+
};
322+
var res = {};
323+
res.ok = function(data, cache, extraData){
324+
325+
data.length.should.be.above(0); /* jslint ignore:line */
326+
done();
327+
};
328+
var req = {};
329+
req.query = {_id: {$gt: lastId}};
330+
users.delete(req, res, next);
331+
});
332+
it('should have backed up multiple deleted data', function(done){
333+
setTimeout(function(){
334+
db.Trash.find()
335+
.where('data._id')
336+
.in(forDelete)
337+
.then(function(res){
338+
res.length.should.be.above(0);
339+
done();
340+
})
341+
.catch(function(err){
342+
done(err);
343+
});
344+
},1000);
345+
});
346+
it('should delete one data', function(done){
347+
var next = function(err){
348+
done(err);
349+
};
350+
var res = {};
351+
res.ok = function(data, cache, extraData){
352+
data.should.be.an('object'); /* jslint ignore:line */
353+
done();
354+
};
355+
var req = {};
356+
req.params = {id: lastId};
357+
users.deleteOne(req, res, next);
358+
});
359+
it('should have backed up one data', function(done){
360+
setTimeout(function(){
361+
db.Trash.find({'data._id':lastId.toString()})
362+
.then(function(resp){
363+
trashId = resp[0]._id;
364+
resp.length.should.be.above(0);
365+
done();
366+
})
367+
.catch(function(err){
368+
done(err);
369+
});
370+
},1000);
371+
});
242372
});
243373
describe('Restore', function(){
244-
it('should restore a previously deleted data');
374+
it('should restore a previously deleted data', function(done){
375+
var next = function(err){
376+
done(err);
377+
};
378+
var res = {};
379+
res.ok = function(data, cache, extraData){
380+
data.should.be.an('object'); /* jslint ignore:line */
381+
done();
382+
};
383+
var req = {};
384+
req.params = {id: trashId};
385+
users.restore(req, res, next);
386+
});
245387
});
246388
});

0 commit comments

Comments
 (0)