forked from node-js-libs/node.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.test.js
executable file
·132 lines (98 loc) · 3.8 KB
/
job.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
var nodeio = require('node.io'),
processor = new nodeio.Processor(),
assert = require('assert');
function createJob(options, methods) {
if (typeof methods === 'undefined') {
methods = options;
options = {};
}
return nodeio.Job(options, methods);
}
module.exports = {
'test job creation with no options': function() {
var job = createJob({testmethod:function(){return 'a';}});
assert.equal('function', typeof job.testmethod);
assert.equal('a', job.testmethod());
},
'test job extend': function() {
var job = createJob({foo:'bar'}, {testmethod:function(){return 'a';}});
assert.equal('bar', job.options.foo);
assert.equal('function', typeof job.testmethod);
assert.equal('a', job.testmethod());
//Create a new job that extends the old one
var new_job = job.extend({foo:'foo'}, {testmethod:false,testmethodb:function(){return 'b';}});
assert.equal('foo', new_job.options.foo);
assert.equal(false, new_job.testmethod);
assert.equal('function', typeof new_job.testmethodb);
assert.equal('b', new_job.testmethodb());
//Create a new job that extends the old one (just methods)
var new_job_b = job.extend({testmethodb:function(){return 'd';}});
assert.equal('d', new_job_b.testmethodb());
//Test calling a parent method
assert.equal('function', typeof new_job.__super__.testmethod);
assert.equal('a', new_job.__super__.testmethod());
//Ensure extend doesn't modify the original job
assert.equal('bar', job.options.foo);
assert.equal('function', typeof job.testmethod);
job = job.extend({foo:'foo'}, {testmethod:false});
assert.equal('foo', job.options.foo);
assert.equal(false, new_job.testmethod);
},
'test job running once': function() {
var job = createJob({input:false});
//input() should return false on the second call
assert.ok(job.input().length);
assert.ok(job.input() === false);
},
'test job emit() as return': function() {
var job = createJob();
job.run = function() {
return 1;
}
job.output = function(data) {
assert.equal(1, data);
}
job.run();
},
'test job emit() async': function() {
var job = createJob();
job.run = function() {
process.nextTick(function() {
job.emit(1);
});
}
job.output = function(data) {
assert.equal(1, data);
}
job.run();
},
'test job skip()': function() {
var job = createJob();
job.run = function() {
this.skip();
this.emit(1);
}
job.output = function(num) {
assert.isUndefined(num);
}
job.run();
assert.ok(job.is_complete);
},
'test job validation': function() {
//Just test bindings, full test suite for validation methods can be found at:
//https://github.com/chriso/node-validator/blob/master/test/validator.test.js
var job = createJob();
job.assert(123).isInt();
job.assert('abc').is(/^[a-z]+$/);
try {
job.assert('abc').isInt();
assert.ok(false, 'job.assert should have failed');
} catch (e) {}
},
'test job filtering / sanitization': function() {
//Just test bindings, full test suite for filter methods can be found at:
//https://github.com/chriso/node-validator/blob/master/test/filter.test.js
var job = createJob();
assert.equal(1, job.filter('00000001').ltrim(0));
},
}