Skip to content

Commit 3153770

Browse files
committed
added unit test for trash model
1 parent fda8287 commit 3153770

File tree

14 files changed

+1159
-195
lines changed

14 files changed

+1159
-195
lines changed

controllers/Users.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,8 @@ UsersController.count = function(query){
4040
return Users.count(query);
4141
};
4242

43+
UsersController.restore = function(query){
44+
return Users.count(query);
45+
};
46+
4347
module.exports = UsersController;

gulpfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ gulp.task('default', function(){
2626
});
2727

2828
gulp.task('test', function() {
29-
gulp.src('./test/*.js', {read: false})
29+
gulp.src('./test/**/*.js', {read: false})
3030
// `gulp-mocha` needs filepaths so you can't have any plugins before it
3131
.pipe(mocha({reporter: 'spec'}));
3232
}

models/Trash.js

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
"use strict";
2+
3+
var db = require('../services/database/mongo');
4+
5+
var collection = 'Trash';
6+
7+
var service = 'Users';
8+
9+
var debug = require('debug')(collection);
10+
11+
var schemaObject = {
12+
data: {
13+
type: db.Schema.Types.Mixed
14+
},
15+
service: {
16+
type: 'String',
17+
default: service
18+
}
19+
};
20+
21+
schemaObject.createdAt = {
22+
type: 'Date',
23+
default: Date.now
24+
};
25+
26+
schemaObject.updatedAt = {
27+
type: 'Date'
28+
// default: Date.now
29+
};
30+
31+
schemaObject.owner = {
32+
type: db.Schema.Types.ObjectId,
33+
ref: 'Users'
34+
};
35+
36+
schemaObject.deletedBy = {
37+
type: db.Schema.Types.ObjectId,
38+
ref: 'Users'
39+
};
40+
41+
schemaObject.client = {
42+
type: db.Schema.Types.ObjectId,
43+
ref: 'Clients'
44+
};
45+
46+
schemaObject.developer = {
47+
type: db.Schema.Types.ObjectId,
48+
ref: 'Users'
49+
};
50+
51+
schemaObject.tags = {
52+
type: [String],
53+
index: 'text'
54+
};
55+
56+
// Let us define our schema
57+
var Schema = db.Schema(schemaObject);
58+
59+
// Index all text for full text search
60+
// MyModel.find({$text: {$search: searchString}})
61+
// .skip(20)
62+
// .limit(10)
63+
// .exec(function(err, docs) { ... });
64+
// Schema.index({'tags': 'text'});
65+
66+
Schema.statics.search = function(string) {
67+
return this.find({$text: {$search: string}}, { score : { $meta: "textScore" } })
68+
.sort({ score : { $meta : 'textScore' } });
69+
};
70+
71+
// assign a function to the "methods" object of our Schema
72+
// Schema.methods.someMethod = function (cb) {
73+
// return this.model(collection).find({}, cb);
74+
// };
75+
76+
// assign a function to the "statics" object of our Schema
77+
// Schema.statics.someStaticFunction = function(query, cb) {
78+
// eg. pagination
79+
// this.find(query, null, { skip: 10, limit: 5 }, cb);
80+
// };
81+
82+
// Adding hooks
83+
84+
Schema.pre('save', function(next) {
85+
// Indexing for search
86+
var ourDoc = this._doc;
87+
var split = [];
88+
for(var n in ourDoc){
89+
if(typeof ourDoc[n] === 'string'){
90+
split.push(ourDoc[n].split(' '));
91+
}
92+
}
93+
this.tags = split;
94+
next();
95+
});
96+
97+
Schema.post('init', function(doc) {
98+
debug('%s has been initialized from the db', doc._id);
99+
});
100+
101+
Schema.post('validate', function(doc) {
102+
debug('%s has been validated (but not saved yet)', doc._id);
103+
});
104+
105+
Schema.post('save', function(doc) {
106+
debug('%s has been saved', doc._id);
107+
});
108+
109+
Schema.post('remove', function(doc) {
110+
debug('%s has been removed', doc._id);
111+
});
112+
113+
Schema.pre('validate', function(next) {
114+
debug('this gets printed first');
115+
next();
116+
});
117+
118+
Schema.post('validate', function() {
119+
debug('this gets printed second');
120+
});
121+
122+
Schema.pre('find', function(next) {
123+
debug(this instanceof db.Query); // true
124+
this.start = Date.now();
125+
next();
126+
});
127+
128+
Schema.post('find', function(result) {
129+
debug(this instanceof db.Query); // true
130+
// prints returned documents
131+
debug('find() returned ' + JSON.stringify(result));
132+
// prints number of milliseconds the query took
133+
debug('find() took ' + (Date.now() - this.start) + ' millis');
134+
});
135+
136+
Schema.pre('update', function(next) {
137+
// Adding updated date
138+
139+
// Indexing for search
140+
var ourDoc = this._update.$set;
141+
var split = [];
142+
for(var n in ourDoc){
143+
if(typeof ourDoc[n] === 'string'){
144+
split.push(ourDoc[n].split(' '));
145+
}
146+
}
147+
148+
if(!split){
149+
this.update(this._conditions,{ $set: { updatedAt: new Date()} });
150+
}else{
151+
this.update(this._conditions,{ $set: { updatedAt: new Date()}, $addToSet: {tags: {$each: split}} });
152+
}
153+
154+
next();
155+
});
156+
157+
var Model = db.model(collection, Schema);
158+
159+
module.exports = Model;

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"aes-js": "^3.0.0",
4040
"body-parser": "^1.17.1",
4141
"bugsnag": "^1.9.1",
42+
"cacheman": "^2.2.1",
4243
"cluster": "^0.7.7",
4344
"cors": "^2.8.3",
4445
"crypto": "0.0.3",
@@ -53,6 +54,7 @@
5354
"hpp": "^0.2.2",
5455
"lodash": "^4.17.4",
5556
"mongoose": "^4.9.0",
57+
"mongoose-delete": "^0.3.4",
5658
"node-rest-client": "^3.1.0",
5759
"q": "^1.4.1",
5860
"randomstring": "^1.1.5",

test/config.js renamed to test/config/config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
var chai = require('chai');
33
chai.should();
44

5-
var config = require('../config');
5+
var config = require('../../config');
66

77
describe('#Config test', function(){
88
it('should be an object', function(done){
99
config.should.be.an('object');
1010
done();
1111
});
12-
});
12+
});

0 commit comments

Comments
 (0)