/
features.js
502 lines (429 loc) · 16 KB
/
features.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
"use strict";
var sys = require('sys');
exports.checkModules = function (t) {
var redis, nohm, Class, Conduct;
t.expect(4);
redis = require('redis-client');
t.ok(typeof redis.Client === 'function', 'redis-client should be available -- forgot to do "git submodule update --init"?');
nohm = require('nohm');
t.ok(typeof nohm.Model === 'function', 'nohm should be available -- something is fishy here.');
Class = require('class');
t.ok(typeof Class.Class === 'function', 'Class should be available -- forgot to do "git submodule update --init"?');
Conduct = require('conductor');
t.ok(typeof Conduct === 'function', 'Conductor should be available -- forgot to do "git submodule update --init"?');
t.done();
};
var prefix = 'nohm';
process.argv.forEach(function (val, index) {
if (val === '--nohm-prefix') {
prefix = process.argv[index + 1];
}
});
// real tests start in 3.. 2.. 1.. NOW!
var redis = require('redis-client').createClient();
var nohm = require('nohm');
var Conduct = require('conductor');
var UserMockup = nohm.Model.extend({
constructor: function () {
this.modelName = 'UserMockup';
this.properties = {
name: {
type: 'string',
value: 'test',
unique: true,
validations: [
'notEmpty'
]
},
visits: {
type: 'integer',
index: true
},
email: {
type: 'string',
unique: true,
value: 'email@email.de',
validations: [
'email'
]
},
country: {
type: 'string',
value: 'Tibet',
index: true,
validations: [
'notEmpty'
]
},
json: {
type: 'json',
value: {}
}
};
nohm.Model.call(this);
}
});
exports.redisClean = function (t) {
t.expect(1);
redis.keys(prefix + ':*:*Mockup:*', function (err, value) {
t.ok(value === null, 'The redis database seems to contain fragments from previous nohm testruns. Use the redis command "KEYS nohm:*:*Mockup:*" to see what keys could be the cause.');
t.done();
});
};
exports.propertyGetter = function (t) {
var user = new UserMockup(),
exceptionThrown;
t.expect(7);
t.equals(typeof(user.p), 'function', 'Property getter short p is not available.');
t.equals(typeof(user.prop), 'function', 'Property getter short prop is not available.');
t.equals(typeof(user.property), 'function', 'Property getter is not available.');
t.equals(user.p('email'), 'email@email.de', 'Property getter did not return the correct value for email.');
t.equals(user.p('name'), 'test', 'Property getter did not return the correct value for name.');
console.log('Note: there should be an error message in the next line. (intended behaviour)');
t.ok(!user.p('hurgelwurz'), 'Accessing an undefined property did not return false');
t.same(user.p('json'), {}, 'Property getter did not return the correct value for json.');
t.done();
};
exports.propertySetter = function (t) {
var user = new UserMockup(),
result,
controlUser = new UserMockup();
t.expect(7);
t.ok(user.p('email', 'asdasd'), 'Setting a property without validation did not return `true`.');
t.equals(user.p('email'), 'asdasd', 'Setting a property did not actually set the property to the correct value');
t.ok(user.p('email', null), 'Setting a property without validation did not return `true`.');
user.p('email', 'test@test.de');
t.ok(user.p('email') !== controlUser.p('email'), 'Creating a new instance of an Object does not create fresh properties.');
user.p({
name: 'objectTest',
email: 'object@test.de'
});
t.equals(user.p('name'), 'objectTest', 'Setting multiple properties by providing one object did not work correctly for the name.');
t.equals(user.p('email'), 'object@test.de', 'Setting multiple properties by providing one object did not work correctly for the email.');
user.p('json', {
test: function () {
t.ok(true, 'Yep...');
}
});
user.p('json').test();
t.done();
};
exports.propertyDiff = function (t) {
var user = new UserMockup(),
should = [],
beforeName = user.p('name'),
beforeEmail = user.p('email');
t.expect(5);
t.ok(user.propertyDiff(), 'Property diff returned changes even though there were none');
user.p('name', 'hurgelwurz');
should.push({
key: 'name',
before: beforeName,
after: 'hurgelwurz'
});
t.same(should, user.propertyDiff(), 'Property diff did not correctly recognize the changed property `name`.');
user.p('email', 'asdasd');
t.same(should, user.propertyDiff('name'), 'Property diff did not correctly search for changes only in `name`.');
should.push({
key: 'email',
before: beforeEmail,
after: 'asdasd'
});
t.same(should, user.propertyDiff(), 'Property diff did not correctly recognize the changed properties `name` and `email`.');
should.shift();
user.p('name', beforeName);
t.same(should, user.propertyDiff(), 'Property diff did not correctly recognize the reset property `name`.');
t.done();
};
exports.propertyReset = function (t) {
var user = new UserMockup(),
beforeName = user.p('name'),
beforeEmail = user.p('email');
t.expect(4);
user.p('name', user.p('name') + 'hurgelwurz');
user.p('email', user.p('email') + 'asdasd');
t.ok(user.propertyReset('name'), 'Property reset did not return true.'); // uhm... needed? i don't know
t.ok(user.p('name') === beforeName, 'Property reset did not properly reset `name`.');
t.ok(user.p('email') !== beforeEmail, 'Property reset reset `email` when it shouldn\'t have.');
user.p('name', user.p('name') + 'hurgelwurz');
user.propertyReset();
t.ok(user.p('name') === beforeName && user.p('email') === beforeEmail, 'Property reset did not properly reset `name` and `email`.');
t.done();
};
exports.allProperties = function (t) {
var user = new UserMockup(),
should;
t.expect(2);
user.p('name', 'hurgelwurz');
user.p('email', 'hurgelwurz@test.de');
should = {
name: user.p('name'),
visits: user.p('visits'),
email: user.p('email'),
country: user.p('country'),
json: {},
id: user.id
}; // yes, this absolutely must be set correct for this test to work. sorry
t.same(should, user.allProperties(), 'Getting all properties failed.');
t.ok(user.allProperties(true) === JSON.stringify(should), 'Getting all properties as JSON failed.');
t.done();
};
exports.create = function (t) {
var user = new UserMockup();
t.expect(5);
user.p('name', 'createTest');
user.p('email', 'createTest@asdasd.de');
user.save(function (err) {
t.ok(!err, 'Saving a user did not work.');
if (err) {
t.done();
}
redis.hgetall(prefix + ':hash:UserMockup:' + user.id, function (err, value) {
t.ok(!err, 'There was a redis error in the create test check.');
t.ok(value.name.toString() === 'createTest', 'The user name was not saved properly');
t.ok(value.visits.toString() === '0', 'The user visits were not saved properly');
t.ok(value.email.toString() === 'createTest@asdasd.de', 'The user email was not saved properly');
t.done();
});
});
};
exports.remove = function (t) {
var user = new UserMockup(),
testExists,
testBattery;
t.expect(9);
testExists = function (what, key, callback) {
redis.exists(key, function (err, value) {
t.ok(!err, 'There was a redis error in the remove test check.');
t.ok(value === 0, 'Deleting a user did not work: ' + what);
callback();
});
};
testBattery = new Conduct({
'hashes': ['_1', function (user, callback) {
testExists('hashes', prefix + ':hash:UserMockup:' + user.id, callback);
}],
'index': ['_1', function (user, callback) {
redis.sismember(prefix + ':index:UserMockup:name:' + user.p('name'), user.id, function (err, value) {
t.ok((err === null && value === 0), 'Deleting a model did not properly delete the normal index.');
callback();
});
}],
'scoredindex': ['_1', function (user, callback) {
redis.zscore(prefix + ':scoredindex:UserMockup:visits', user.id, function (err, value) {
t.ok((err === null && value === null), 'Deleting a model did not properly delete the scored index.');
callback();
});
}],
'uniques': ['_1', function (user, callback) {
testExists('uniques', prefix + ':uniques:UserMockup:name:' + user.p('name'), callback);
}],
'done': ['hashes1', 'index1', 'scoredindex1', 'uniques1', t.done]
}, 'done1');
user.p('name', 'deleteTest');
user.p('email', 'deleteTest@asdasd.de');
user.save(function (err) {
t.ok(!err, 'There was an unexpected problem: ' + sys.inspect(err));
if (err) {
t.done();
}
var id = user.id;
user.remove(function (err) {
t.ok(!err, 'There was a redis error in the remove test.');
if (err) {
t.done();
}
t.equals(user.id, 0, 'Removing an object from the db did not set the id to null');
user.id = id; // the other tests need it back. :D
testBattery(user);
});
});
};
exports.idSets = function (t) {
var user = new UserMockup(),
tmpid = 0;
t.expect(6);
user.p('name', 'idSetTest');
user.save(function (err) {
t.ok(!err, 'There was an unexpected redis error.');
tmpid = user.id;
redis.sismember(prefix + ':idsets:' + user.modelName, tmpid, function (err, value) {
t.ok(!err, 'There was an unexpected redis error.');
t.equals(value, 1, 'The userid was not part of the idset after saving.');
user.remove(function (err) {
t.ok(!err, 'There was an unexpected redis error.');
redis.sismember(prefix + ':idsets:' + user.modelName, tmpid, function (err, value) {
t.ok(!err, 'There was an unexpected redis error.');
t.equals(value, 0, 'The userid was still part of the idset after removing.');
t.done();
});
});
});
});
};
exports.update = function (t) {
var user = new UserMockup();
t.expect(5);
user.p('name', 'updateTest1');
user.p('email', 'updateTest1@email.de');
user.save(function (err) {
t.ok(!err, 'There was a redis error in the update test. (creation part)');
if (err) {
t.done();
}
user.p('name', 'updateTest2');
user.p('email', 'updateTest2@email.de');
user.save(function (err) {
t.ok(!err, 'There was a redis error in the update test.');
if (err) {
t.done();
}
redis.hgetall(prefix + ':hash:UserMockup:' + user.id, function (err, value) {
t.ok(!err, 'There was a redis error in the update test check.');
if (err) {
t.done();
}
t.ok(value.name.toString() === 'updateTest2', 'The user name was not updated properly');
t.ok(value.email.toString() === 'updateTest2@email.de', 'The user email was not updated properly');
t.done();
});
});
});
};
exports.partialUpdate = function (t) {
var user = new UserMockup(),
user2 = new UserMockup();
t.expect(6);
user.p('name', 'updateTest1');
user.p('email', 'updateTest1@email.de');
user.save(function (err) {
t.ok(!err, 'There was a redis error in the update test. (creation part)');
if (err) {
t.done();
}
user2.id = user.id;
user2.p({
name: 'updateTest2',
visits: 10
});
user2.partialSave(function (err) {
t.ok(!err, 'There was a redis error in the update test.');
if (err) {
t.done();
}
redis.hgetall(prefix + ':hash:UserMockup:' + user2.id, function (err, value) {
t.ok(!err, 'There was a redis error in the update test check.');
if (err) {
t.done();
}
t.ok(value.name.toString() === 'updateTest2', 'The user name was not updated properly');
t.ok(value.email.toString() === 'updateTest1@email.de', 'The user email was not left correctly.');
t.ok(value.visits.toString() === '10', 'The visits were not set correctly.');
t.done();
});
});
});
};
exports.unique = function (t) {
var user1 = new UserMockup(),
user2 = new UserMockup();
t.expect(8);
user1.p('name', 'dubplicateTest');
user1.p('email', 'dubplicateTest@test.de');
user2.p('name', 'dubplicateTest');
user2.p('email', 'dubbplicateTest@test.de');
user1.save(function (err) {
t.ok(!err, 'There was an unexpected problem: ' + sys.inspect(err));
redis.get(prefix + ':uniques:UserMockup:name:dubplicateTest', function (err, value) {
t.ok(user1.id, 'Userid b0rked while checking uniques');
t.equals(parseInt(value, 10), user1.id, 'The unique key did not have the correct id');
user2.valid(false, false, function (valid) {
t.ok(!valid, 'A unique property was not recognized as a duplicate in valid without setDirectly');
user2.save(function (err) {
t.equals(err, 'invalid', 'A saved unique property was not recognized as a duplicate');
redis.exists(prefix + ':uniques:UserMockup:email:dubbplicateTest@test.de', function (err, value) {
t.equals(value, 0, 'The tmp unique lock was not deleted for a failed save.');
redis.get(prefix + ':uniques:UserMockup:name:dubplicateTest', function (err, value) {
t.ok(!err, 'There was an unexpected probllem: ' + sys.inspect(err));
t.ok(parseInt(value, 10) === user1.id, 'The unique key did not have the correct id after trying to save another unique.');
t.done();
});
});
});
});
});
if (err) {
t.done();
}
});
};
exports.uniqueDeletion = function (t) {
var user = new UserMockup();
t.expect(2);
user.p({
'name': 'dubplicateDeletionTest',
'email': 'dubplicateDeletionTest@test.de',
'country': ''
});
user.save(function (err) {
t.ok(err, 'The invalid property country did not trigger a failure.');
redis.exists(prefix + ':uniques:UserMockup:name:dubplicateDeletionTest', function (err, value) {
t.equals(value, 0, 'The tmp unique key was not deleted if a non-unique saving failure occured.');
t.done();
});
});
};
exports.indexes = function (t) {
var user = new UserMockup();
t.expect(7);
user.p('name', 'indexTest');
user.p('email', 'indexTest@test.de');
user.p('country', 'indexTestCountry');
user.p('visits', 20);
function checkCountryIndex(callback) {
redis.sismember(prefix + ':index:UserMockup:country:indexTestCountry', user.id, function (err, value) {
t.ok(!err, 'There was an unexpected problem: ' + sys.inspect(err));
t.ok(value === 1, 'The country index did not have the user as one of its ids.');
callback();
});
}
function checkVisitsIndex(callback) {
redis.zscore(prefix + ':scoredindex:UserMockup:visits', user.id, function (err, value) {
t.ok(!err, 'There was an unexpected problem: ' + sys.inspect(err));
t.ok(value === user.p('visits'), 'The visits index did not have the correct score.');
redis.sismember(prefix + ':index:UserMockup:visits:' + user.p('visits'), user.id, function (err, value) {
t.ok(!err, 'There was an unexpected problem: ' + sys.inspect(err));
t.ok(value === 1, 'The visits index did not have the user as one of its ids.');
callback();
});
});
}
user.save(function (err) {
t.ok(!err, 'There was an unexpected problem: ' + sys.inspect(err));
checkCountryIndex(function () {
checkVisitsIndex(t.done);
});
});
};
exports.__updated = function (t) {
var user = new UserMockup();
t.expect(2);
user.save(function (err) {
if (err) {
sys.debug('Error while saving user in __updated.');
}
user.p('name', 'hurgelwurz');
user.p('name', 'test');
t.ok(user.properties.name.__updated === false, 'Changing a var manually to the original didn\'t reset the internal __updated var');
user.remove(function (err) {
if (err) {
sys.debug('Error while saving user in __updated.');
}
user = new UserMockup();
user.p('name', 'hurgelwurz');
user.propertyReset();
t.ok(user.properties.name.__updated === false, 'Changing a var by propertyReset to the original didn\'t reset the internal __updated var');
t.done();
});
});
};