-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
138 lines (127 loc) · 4.42 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
var async = require('async');
var assert = require('assert');
var mongoose = require('mongoose');
var mongoose_deleted = require('./');
var _ = require('lodash');
var user, cars;
function models() {
user = new mongoose.Schema({ name: String });
mongoose_deleted(user);
user = mongoose.model('user', user);
car = new mongoose.Schema({ name: String });
mongoose_deleted(car, { select : true, toJSON : true });
car = mongoose.model('car', car);
}
function test() {
var name = "John Q Public";
async.waterfall([
function(cb) {
user.remove({}, function(err) { cb(err); });
},
function(cb) {
car.remove({}, function(err) { cb(err); });
},
function(cb) {
var user1 = new user({ name : name });
user1.save(function(err, doc) {
assert.equal(doc.deleted, false, 'deleted should exist');
assert.equal(doc.toJSON().deleted, null, 'deleted JSON should exist');
cb(err);
});
},
function(cb) {
user.findOne({ name : name }, function(err, doc) {
assert.equal(err, null);
assert.equal(doc.name, name);
assert.equal(doc.deleted, null, 'deleted should not exist');
cb(null, doc);
});
},
function(doc, cb) {
user.findOne({ name : name }, { name : 1, deleted : 1 }, function(err, doc) {
assert.equal(err, null);
assert.equal(doc.name, name);
assert.equal(doc.deleted, false, 'deleted should exist');
assert.equal(doc.toJSON().deleted, null, 'deleted JSON should not exist');
assert.equal(doc.toJSON({ deleted : true }).deleted, false, 'deleted JSON should exist');
cb(null, doc);
});
},
function(doc, cb) {
doc.delete(function(err) { cb(err); });
},
function(cb) {
user.findOne({ name: name }, function(err, doc) {
assert.equal(err, null);
assert.equal(doc, null);
cb();
});
},
function(cb) {
user.findOne({ name: name, deleted: true }, function(err, doc) {
assert.equal(err, null);
assert.equal(doc.name, name, 'doc should exist');
cb();
});
},
function(cb) {
user.findOne({ name: name, $or : [
{ deleted : { $exists: true } },
{ deleted : { $exists: false } },
] }, function(err, doc) {
assert.equal(err, null);
assert.equal(doc.name, name, 'doc should exist');
cb();
});
},
function(cb) {
user.count(function(err, number) {
assert.equal(number, 0, 'users.count() should equal 0');
cb();
});
},
function(cb) {
user.count({ deleted : true },function(err, number) {
assert.equal(number, 1, 'deleted users should be 1');
cb();
});
},
function(cb) {
var car1 = new car({ name : "Jetta" });
car1.save(function(err, doc) { cb(err); });
},
function(cb) {
car.findOne({ name : "Jetta" }, function(err, doc) {
assert.equal(doc.deleted, false, 'deleted should exist');
assert.equal(doc.toJSON().deleted, false, 'deleted JSON should exist');
assert.equal(doc.toJSON({ deleted : false }).deleted, null, 'deleted JSON should not exist');
cb(null, doc);
});
},
function(doc, cb) {
doc.delete(function(err, doc) { return cb(err, doc); });
},
function(doc, cb) {
assert.equal(doc.deleted, true, 'deleted should be true');
doc.restore(function(err, doc) { cb(err, doc); });
},
function(doc, cb) {
assert.equal(doc.deleted, false, 'deleted should be false');
cb();
}
], function(err) {
assert.equal(err, null);
disco();
});
}
// connect and test
mongoose.connect('mongodb://localhost/md-test', function() {
models();
test();
});
function disco() {
if (!/node-dev$/.test(process.env._)) {
mongoose.disconnect();
process.exit(0);
}
}