sequelize-mock-v5 1.3.4 → 1.3.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -5
- package/sequelize-mock-v5-1.3.5.tgz +0 -0
- package/sequelize-mock-v5-1.3.6.tgz +0 -0
- package/src/model.js +31 -24
- package/test/model.spec.js +0 -29
- package/test/queryinterface.spec.js +171 -172
- package/sequelize-mock-v5-1.3.4.tgz +0 -0
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "sequelize-mock-v5",
|
3
|
-
"version": "1.3.
|
3
|
+
"version": "1.3.6",
|
4
4
|
"description": "A simple mock interface specifically for testing code relying on Sequelize models",
|
5
5
|
"main": "src/index.js",
|
6
6
|
"scripts": {
|
@@ -13,7 +13,7 @@
|
|
13
13
|
},
|
14
14
|
"repository": {
|
15
15
|
"type": "git",
|
16
|
-
"url": "git+https://github.com/
|
16
|
+
"url": "git+https://github.com/foyer-inc/sequelize-mock.git"
|
17
17
|
},
|
18
18
|
"keywords": [
|
19
19
|
"sequelize",
|
@@ -21,12 +21,12 @@
|
|
21
21
|
"test",
|
22
22
|
"testing"
|
23
23
|
],
|
24
|
-
"author": "
|
24
|
+
"author": "Foyer Inc",
|
25
25
|
"license": "MIT",
|
26
26
|
"bugs": {
|
27
|
-
"url": "https://github.com/
|
27
|
+
"url": "https://github.com/foyer-inc/sequelize-mock/issues"
|
28
28
|
},
|
29
|
-
"homepage": "https://github.com/
|
29
|
+
"homepage": "https://github.com/foyer-inc/sequelize-mock#readme",
|
30
30
|
"dependencies": {
|
31
31
|
"inflection": "^1.10.0",
|
32
32
|
"lodash": "^4.17.20"
|
Binary file
|
Binary file
|
package/src/model.js
CHANGED
@@ -69,6 +69,14 @@ function fakeModel(name, defaults, opts) {
|
|
69
69
|
**/
|
70
70
|
this.name = name;
|
71
71
|
this._defaults = defaults || {};
|
72
|
+
this.primaryKeys = {
|
73
|
+
id: {
|
74
|
+
type: 'INTEGER',
|
75
|
+
allowNull: false,
|
76
|
+
autoIncrement: true,
|
77
|
+
primaryKey: true
|
78
|
+
}
|
79
|
+
}
|
72
80
|
|
73
81
|
/**
|
74
82
|
* Model definition containing the attributes/fields defined for this model
|
@@ -260,7 +268,7 @@ fakeModel.prototype.getTableName = function () {
|
|
260
268
|
fakeModel.prototype.unscoped =
|
261
269
|
/**
|
262
270
|
* No-op that returns the current object
|
263
|
-
*
|
271
|
+
*
|
264
272
|
* @instance
|
265
273
|
* @return {Model} Self
|
266
274
|
**/
|
@@ -651,8 +659,7 @@ fakeModel.prototype.bulkCreate = async function (set, options) {
|
|
651
659
|
queryOptions: arguments,
|
652
660
|
fallbackFn: !this.options.autoQueryFallback ? async function () { return } : async function () {
|
653
661
|
return await Promise.all(set.map(async function (val) {
|
654
|
-
|
655
|
-
return await res.fallbackFn();
|
662
|
+
return await self.build(val).save();
|
656
663
|
}));
|
657
664
|
},
|
658
665
|
});
|
@@ -680,13 +687,8 @@ fakeModel.prototype.destroy = async function (options) {
|
|
680
687
|
});
|
681
688
|
}
|
682
689
|
else {
|
683
|
-
|
684
|
-
|
685
|
-
queryOptions: arguments,
|
686
|
-
fallbackFn: !this.options.autoQueryFallback ? async function () { return } : async function () {
|
687
|
-
return Promise.resolve(options && typeof options.limit == 'number' ? options.limit : 1);
|
688
|
-
},
|
689
|
-
});
|
690
|
+
const instanceLevelDestroy = this.__proto__.__proto__.destroy.bind(this);
|
691
|
+
return await instanceLevelDestroy()
|
690
692
|
}
|
691
693
|
|
692
694
|
};
|
@@ -707,21 +709,26 @@ fakeModel.prototype.destroy = async function (options) {
|
|
707
709
|
fakeModel.prototype.update = async function (values, options) {
|
708
710
|
var self = this;
|
709
711
|
options = options || {};
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
712
|
+
if (this.$query) {
|
713
|
+
return await this.$query({
|
714
|
+
query: "update",
|
715
|
+
queryOptions: arguments,
|
716
|
+
options: options,
|
717
|
+
//if options.returning are empty or false, we are doing the default (returning both)
|
718
|
+
includeAffectedRows: !!!options.returning,
|
719
|
+
fallbackFn: !this.options.autoQueryFallback ? null : async function () {
|
720
|
+
if (!options.returning) {
|
721
|
+
return [1];
|
722
|
+
}
|
723
|
+
return [1, [self.build(values)]];
|
721
724
|
}
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
+
});
|
726
|
+
}
|
727
|
+
else {
|
728
|
+
const instanceLevelUpdate = this.__proto__.__proto__.update.bind(this, values);
|
729
|
+
return await instanceLevelUpdate()
|
730
|
+
}
|
731
|
+
|
725
732
|
};
|
726
733
|
|
727
734
|
// Noops
|
package/test/model.spec.js
CHANGED
@@ -188,35 +188,6 @@ describe('Model', function () {
|
|
188
188
|
mdl.should.have.property('update').which.is.a.Function();
|
189
189
|
// mdl.should.have.property('describe').which.is.a.Function();
|
190
190
|
});
|
191
|
-
|
192
|
-
it('should store the model definition for access', function () {
|
193
|
-
var modelDef = {
|
194
|
-
'name': 'VARCHAR(255)',
|
195
|
-
'email': 'VARCHAR(255)',
|
196
|
-
'age': 'INTEGER',
|
197
|
-
};
|
198
|
-
var mdl = new Model('User', modelDef);
|
199
|
-
|
200
|
-
mdl.should.have.property('modelDefinition').which.is.an.Object();
|
201
|
-
mdl.modelDefinition.should.equal(modelDef);
|
202
|
-
mdl.modelDefinition.should.have.property('name').which.is.exactly('VARCHAR(255)');
|
203
|
-
mdl.modelDefinition.should.have.property('email').which.is.exactly('VARCHAR(255)');
|
204
|
-
mdl.modelDefinition.should.have.property('age').which.is.exactly('INTEGER');
|
205
|
-
});
|
206
|
-
|
207
|
-
it('should have modelDefinition accessible through instances', function () {
|
208
|
-
var modelDef = {
|
209
|
-
'title': 'VARCHAR(255)',
|
210
|
-
'content': 'TEXT',
|
211
|
-
};
|
212
|
-
var mdl = new Model('Post', modelDef);
|
213
|
-
var instance = mdl.build();
|
214
|
-
|
215
|
-
instance.Model.should.have.property('modelDefinition').which.is.an.Object();
|
216
|
-
instance.Model.modelDefinition.should.equal(modelDef);
|
217
|
-
instance.Model.modelDefinition.should.have.property('title').which.is.exactly('VARCHAR(255)');
|
218
|
-
instance.Model.modelDefinition.should.have.property('content').which.is.exactly('TEXT');
|
219
|
-
});
|
220
191
|
});
|
221
192
|
|
222
193
|
describe('#scope', function () {
|
@@ -5,9 +5,9 @@ var proxyquire = require('proxyquire').noCallThru();
|
|
5
5
|
var util = require('util');
|
6
6
|
|
7
7
|
var ErrorsMock = {
|
8
|
-
BaseError: function () {},
|
9
|
-
InvalidQueryResultError: function () {},
|
10
|
-
EmptyQueryQueueError: function () {},
|
8
|
+
BaseError: function () { },
|
9
|
+
InvalidQueryResultError: function () { },
|
10
|
+
EmptyQueryQueueError: function () { },
|
11
11
|
};
|
12
12
|
util.inherits(ErrorsMock.BaseError, Error);
|
13
13
|
util.inherits(ErrorsMock.InvalidQueryResultError, Error);
|
@@ -18,7 +18,7 @@ var QueryInterface = proxyquire('../src/queryinterface', {
|
|
18
18
|
});
|
19
19
|
|
20
20
|
describe('QueryInterface', function () {
|
21
|
-
|
21
|
+
|
22
22
|
describe('__constructor', function () {
|
23
23
|
it('should save the options object', function () {
|
24
24
|
var qi = new QueryInterface({
|
@@ -29,260 +29,260 @@ describe('QueryInterface', function () {
|
|
29
29
|
qi.options.should.have.property('createdDefault');
|
30
30
|
qi.options.should.have.property('fallbackFn');
|
31
31
|
qi.options.should.have.property('foo').which.is.exactly('bar');
|
32
|
-
|
32
|
+
|
33
33
|
var qi = new QueryInterface();
|
34
34
|
qi.options.should.be.an.Object();
|
35
35
|
});
|
36
|
-
|
36
|
+
|
37
37
|
it('should create a new results queue object', function () {
|
38
38
|
var qi = new QueryInterface();
|
39
39
|
qi._results.should.be.an.Array();
|
40
40
|
});
|
41
41
|
});
|
42
|
-
|
42
|
+
|
43
43
|
describe('#$queueResult', function () {
|
44
44
|
var qi;
|
45
45
|
beforeEach(function () {
|
46
46
|
qi = new QueryInterface({});
|
47
47
|
});
|
48
|
-
|
48
|
+
|
49
49
|
it('should queue the result as the next item in the result list', function () {
|
50
50
|
qi._results.length.should.equal(0);
|
51
|
-
|
51
|
+
|
52
52
|
qi.$queueResult('foo');
|
53
53
|
qi._results.length.should.equal(1);
|
54
54
|
qi._results[0].content.should.equal('foo');
|
55
|
-
|
55
|
+
|
56
56
|
qi.$queueResult('bar');
|
57
57
|
qi._results.length.should.equal(2);
|
58
58
|
qi._results[0].content.should.equal('foo');
|
59
59
|
qi._results[1].content.should.equal('bar');
|
60
60
|
});
|
61
|
-
|
61
|
+
|
62
62
|
it('should default to an empty options object', function () {
|
63
63
|
qi.$queueResult('foo');
|
64
64
|
qi._results[0].options.should.be.an.Object();
|
65
65
|
});
|
66
|
-
|
66
|
+
|
67
67
|
it('should save the options from the result', function () {
|
68
68
|
qi.$queueResult('foo', 'bar');
|
69
69
|
qi._results[0].options.should.equal('bar');
|
70
70
|
});
|
71
|
-
|
71
|
+
|
72
72
|
it('should mark the result as a "Success" result', function () {
|
73
73
|
qi.$queueResult('foo');
|
74
74
|
qi._results[0].type.should.equal('Success');
|
75
75
|
});
|
76
76
|
});
|
77
|
-
|
77
|
+
|
78
78
|
describe('#$queueError', function () {
|
79
79
|
var qi;
|
80
80
|
beforeEach(function () {
|
81
81
|
qi = new QueryInterface({});
|
82
82
|
});
|
83
|
-
|
83
|
+
|
84
84
|
it('should queue the result as the next item in the result list', function () {
|
85
85
|
qi._results.length.should.equal(0);
|
86
|
-
|
86
|
+
|
87
87
|
var errFoo = new Error('foo'),
|
88
88
|
errBar = new Error('bar');
|
89
|
-
|
89
|
+
|
90
90
|
qi.$queueError(errFoo);
|
91
91
|
qi._results.length.should.equal(1);
|
92
92
|
qi._results[0].content.should.equal(errFoo);
|
93
|
-
|
93
|
+
|
94
94
|
qi.$queueError(errBar);
|
95
95
|
qi._results.length.should.equal(2);
|
96
96
|
qi._results[0].content.should.equal(errFoo);
|
97
97
|
qi._results[1].content.should.equal(errBar);
|
98
98
|
});
|
99
|
-
|
99
|
+
|
100
100
|
it('should convert non Error object to Error by default', function () {
|
101
101
|
qi.$queueError('foo');
|
102
102
|
qi._results[0].content.should.be.instanceof(ErrorsMock.BaseError);
|
103
103
|
});
|
104
|
-
|
104
|
+
|
105
105
|
it('should not convert non Error object if `convertNonErrors` is false', function () {
|
106
106
|
qi.$queueError('foo', { convertNonErrors: false });
|
107
107
|
qi._results[0].content.should.not.instanceof(ErrorsMock.BaseError);
|
108
108
|
qi._results[0].content.should.equal('foo');
|
109
109
|
});
|
110
|
-
|
110
|
+
|
111
111
|
it('should default to an empty options object', function () {
|
112
112
|
qi.$queueError('foo');
|
113
113
|
qi._results[0].options.should.be.an.Object();
|
114
114
|
});
|
115
|
-
|
115
|
+
|
116
116
|
it('should save the options from the result', function () {
|
117
117
|
qi.$queueError('foo', 'bar');
|
118
118
|
qi._results[0].options.should.equal('bar');
|
119
119
|
});
|
120
|
-
|
120
|
+
|
121
121
|
it('should mark the result as a "Failure" result', function () {
|
122
122
|
qi.$queueError('foo');
|
123
123
|
qi._results[0].type.should.equal('Failure');
|
124
124
|
});
|
125
125
|
});
|
126
|
-
|
126
|
+
|
127
127
|
describe('#$useHandler', function () {
|
128
128
|
var qi;
|
129
129
|
beforeEach(function () {
|
130
130
|
qi = new QueryInterface({});
|
131
131
|
});
|
132
|
-
|
132
|
+
|
133
133
|
it('should add the handler as the next item in the handlers list', function () {
|
134
134
|
qi._handlers.length.should.equal(0);
|
135
|
-
var handler1 = function(){};
|
136
|
-
var handler2 = function(){};
|
135
|
+
var handler1 = function () { };
|
136
|
+
var handler2 = function () { };
|
137
137
|
|
138
138
|
qi.$useHandler(handler1);
|
139
139
|
qi._handlers.length.should.equal(1);
|
140
140
|
qi._handlers[0].should.equal(handler1);
|
141
|
-
|
141
|
+
|
142
142
|
qi.$useHandler(handler2);
|
143
143
|
qi._handlers.length.should.equal(2);
|
144
144
|
qi._handlers[0].should.equal(handler1);
|
145
145
|
qi._handlers[1].should.equal(handler2);
|
146
146
|
});
|
147
147
|
});
|
148
|
-
|
148
|
+
|
149
149
|
describe('#$clearQueue', function () {
|
150
150
|
var qi;
|
151
151
|
beforeEach(function () {
|
152
152
|
qi = new QueryInterface({});
|
153
153
|
});
|
154
|
-
|
154
|
+
|
155
155
|
it('should clear the results queue from the current QueryInterface', function () {
|
156
156
|
qi._results = [1, 2, 3];
|
157
157
|
qi.$clearQueue();
|
158
158
|
qi._results.length.should.equal(0);
|
159
159
|
});
|
160
|
-
|
160
|
+
|
161
161
|
it('should not clear the results queue from a parent QueryInterface by default', function () {
|
162
162
|
qi._results = [1, 2, 3];
|
163
|
-
|
163
|
+
|
164
164
|
var run = 0;
|
165
165
|
qi.options.parent = {
|
166
166
|
$clearQueue: function () { run++; },
|
167
167
|
};
|
168
|
-
|
168
|
+
|
169
169
|
qi.$clearQueue();
|
170
170
|
qi._results.length.should.equal(0);
|
171
171
|
run.should.equal(0);
|
172
172
|
});
|
173
|
-
|
173
|
+
|
174
174
|
it('should clear the results queue from a parent QueryInterface if option is passed in to', function () {
|
175
175
|
qi._results = [1, 2, 3];
|
176
|
-
|
176
|
+
|
177
177
|
var run = 0;
|
178
178
|
qi.options.parent = {
|
179
179
|
$clearQueue: function () { run++; },
|
180
180
|
};
|
181
|
-
|
181
|
+
|
182
182
|
qi.$clearQueue({ propagateClear: true });
|
183
183
|
qi._results.length.should.equal(0);
|
184
184
|
run.should.equal(1);
|
185
185
|
});
|
186
|
-
|
186
|
+
|
187
187
|
});
|
188
|
-
|
188
|
+
|
189
189
|
describe('#$clearHandlers', function () {
|
190
190
|
var qi;
|
191
191
|
beforeEach(function () {
|
192
192
|
qi = new QueryInterface({});
|
193
193
|
});
|
194
|
-
|
194
|
+
|
195
195
|
it('should clear the handlers from the current QueryInterface', function () {
|
196
|
-
qi._handlers = [function(){}, function(){}];
|
197
|
-
|
196
|
+
qi._handlers = [function () { }, function () { }];
|
197
|
+
|
198
198
|
qi.$clearHandlers();
|
199
|
-
|
199
|
+
|
200
200
|
qi._handlers.length.should.equal(0);
|
201
201
|
});
|
202
|
-
|
202
|
+
|
203
203
|
it('should not clear the handlers from a parent QueryInterface by default', function () {
|
204
|
-
qi._handlers = [function(){}, function(){}];
|
205
|
-
|
204
|
+
qi._handlers = [function () { }, function () { }];
|
205
|
+
|
206
206
|
var run = 0;
|
207
207
|
qi.options.parent = {
|
208
208
|
$clearHandlers: function () { run++; },
|
209
209
|
};
|
210
|
-
|
210
|
+
|
211
211
|
qi.$clearHandlers();
|
212
212
|
qi._handlers.length.should.equal(0);
|
213
213
|
run.should.equal(0);
|
214
214
|
});
|
215
|
-
|
215
|
+
|
216
216
|
it('should clear the handlers from a parent QueryInterface if option is passed in to', function () {
|
217
|
-
qi._handlers = [function(){}, function(){}];
|
218
|
-
|
217
|
+
qi._handlers = [function () { }, function () { }];
|
218
|
+
|
219
219
|
var run = 0;
|
220
220
|
qi.options.parent = {
|
221
221
|
$clearHandlers: function () { run++; },
|
222
222
|
};
|
223
|
-
|
223
|
+
|
224
224
|
qi.$clearHandlers({ propagateClear: true });
|
225
225
|
qi._results.length.should.equal(0);
|
226
226
|
run.should.equal(1);
|
227
227
|
});
|
228
|
-
|
228
|
+
|
229
229
|
});
|
230
|
-
|
230
|
+
|
231
231
|
describe('#$clearResults', function () {
|
232
232
|
var qi;
|
233
233
|
beforeEach(function () {
|
234
234
|
qi = new QueryInterface({});
|
235
235
|
});
|
236
|
-
|
236
|
+
|
237
237
|
it('should clear the handlers and results queue from the current QueryInterface', function () {
|
238
238
|
qi._results = [1, 2, 3];
|
239
|
-
qi._handlers = [function(){}, function(){}];
|
240
|
-
|
239
|
+
qi._handlers = [function () { }, function () { }];
|
240
|
+
|
241
241
|
qi.$clearResults();
|
242
|
-
|
242
|
+
|
243
243
|
qi._results.length.should.equal(0);
|
244
244
|
qi._handlers.length.should.equal(0);
|
245
245
|
});
|
246
|
-
|
246
|
+
|
247
247
|
it('should not clear the handlers and results queue from a parent QueryInterface by default', function () {
|
248
248
|
qi._results = [1, 2, 3];
|
249
|
-
qi._handlers = [function(){}, function(){}];
|
250
|
-
|
249
|
+
qi._handlers = [function () { }, function () { }];
|
250
|
+
|
251
251
|
var run = 0;
|
252
252
|
qi.options.parent = {
|
253
253
|
$clearResults: function () { run++; },
|
254
254
|
};
|
255
|
-
|
255
|
+
|
256
256
|
qi.$clearResults();
|
257
257
|
qi._results.length.should.equal(0);
|
258
258
|
qi._handlers.length.should.equal(0);
|
259
259
|
run.should.equal(0);
|
260
260
|
});
|
261
|
-
|
261
|
+
|
262
262
|
it('should clear the handlers and results queue from a parent QueryInterface if option is passed in to', function () {
|
263
263
|
qi._results = [1, 2, 3];
|
264
|
-
qi._handlers = [function(){}, function(){}];
|
264
|
+
qi._handlers = [function () { }, function () { }];
|
265
265
|
|
266
266
|
var run = 0;
|
267
267
|
qi.options.parent = {
|
268
268
|
$clearHandlers: function () { run++; },
|
269
269
|
$clearQueue: function () { run++; },
|
270
270
|
};
|
271
|
-
|
271
|
+
|
272
272
|
qi.$clearResults({ propagateClear: true });
|
273
273
|
qi._results.length.should.equal(0);
|
274
274
|
qi._handlers.length.should.equal(0);
|
275
275
|
run.should.equal(2);
|
276
276
|
});
|
277
|
-
|
277
|
+
|
278
278
|
});
|
279
|
-
|
279
|
+
|
280
280
|
describe('#$query', function () {
|
281
281
|
var qi;
|
282
282
|
beforeEach(function () {
|
283
283
|
qi = new QueryInterface({});
|
284
284
|
});
|
285
|
-
|
285
|
+
|
286
286
|
it('should return a resolved promise with a Success result', function (done) {
|
287
287
|
qi._results = [{
|
288
288
|
content: 'foo',
|
@@ -293,7 +293,7 @@ describe('QueryInterface', function () {
|
|
293
293
|
options: {},
|
294
294
|
type: 'Success'
|
295
295
|
}];
|
296
|
-
|
296
|
+
|
297
297
|
var result = qi.$query();
|
298
298
|
result.then(function (content) {
|
299
299
|
content.should.equal('foo');
|
@@ -301,7 +301,7 @@ describe('QueryInterface', function () {
|
|
301
301
|
done();
|
302
302
|
}).catch(done);
|
303
303
|
});
|
304
|
-
|
304
|
+
|
305
305
|
it('should return a rejected promise with a Failure result', function (done) {
|
306
306
|
qi._results = [{
|
307
307
|
content: 'foo',
|
@@ -312,7 +312,7 @@ describe('QueryInterface', function () {
|
|
312
312
|
options: {},
|
313
313
|
type: 'Failure'
|
314
314
|
}];
|
315
|
-
|
315
|
+
|
316
316
|
var result = qi.$query();
|
317
317
|
result.then(function () {
|
318
318
|
done(new Error('Query returned a resolved promise instead of a rejected one'));
|
@@ -322,133 +322,132 @@ describe('QueryInterface', function () {
|
|
322
322
|
done();
|
323
323
|
}).catch(done);
|
324
324
|
});
|
325
|
-
|
325
|
+
|
326
326
|
it('should throw an error if an invalid result is added to the queue', function () {
|
327
327
|
qi._results = [{
|
328
328
|
content: 'foo',
|
329
329
|
options: {},
|
330
330
|
type: 'Test'
|
331
331
|
}];
|
332
|
-
|
332
|
+
|
333
333
|
should.throws(qi.$query.bind(qi), ErrorsMock.InvalidQueryResultError);
|
334
334
|
});
|
335
|
-
|
336
|
-
it('should pass the query information to the handlers', function(done) {
|
337
|
-
qi.$useHandler(function(query, options) {
|
335
|
+
|
336
|
+
it('should pass the query information to the handlers', function (done) {
|
337
|
+
qi.$useHandler(function (query, options) {
|
338
338
|
query.should.equal('findAll');
|
339
|
-
options.should.deepEqual({ where: {id: 1}});
|
339
|
+
options.should.deepEqual({ where: { id: 1 } });
|
340
340
|
done();
|
341
341
|
});
|
342
342
|
qi.$query({
|
343
343
|
query: 'findAll',
|
344
|
-
queryOptions: { where: {id: 1}}
|
344
|
+
queryOptions: { where: { id: 1 } }
|
345
345
|
})
|
346
346
|
});
|
347
|
-
|
348
|
-
it('should return the promise of the handler', function(done) {
|
349
|
-
qi.$useHandler(function(query, options) {
|
347
|
+
|
348
|
+
it('should return the promise of the handler', function (done) {
|
349
|
+
qi.$useHandler(function (query, options) {
|
350
350
|
return Promise.resolve('handler value');
|
351
351
|
});
|
352
|
-
qi.$query().then(function(value) {
|
352
|
+
qi.$query().then(function (value) {
|
353
353
|
value.should.equal('handler value');
|
354
354
|
done();
|
355
355
|
});
|
356
356
|
});
|
357
357
|
|
358
|
-
it('should respect rejected promises returned from handler', function(done) {
|
359
|
-
qi.$useHandler(function(query, options) {
|
358
|
+
it('should respect rejected promises returned from handler', function (done) {
|
359
|
+
qi.$useHandler(function (query, options) {
|
360
360
|
return Promise.reject('error');
|
361
361
|
});
|
362
|
-
|
363
|
-
|
364
|
-
qi.$query().catch(function(error) {
|
362
|
+
|
363
|
+
qi.$query().catch(function (error) {
|
365
364
|
error.should.equal('error');
|
366
365
|
done();
|
367
366
|
});
|
368
367
|
});
|
369
|
-
|
370
|
-
it('should return a promise even if the value is undefined', function(done) {
|
371
|
-
qi.$useHandler(function(query, options) {
|
368
|
+
|
369
|
+
it('should return a promise even if the value is undefined', function (done) {
|
370
|
+
qi.$useHandler(function (query, options) {
|
372
371
|
return Promise.resolve(undefined);
|
373
372
|
});
|
374
|
-
qi.$query().then(function(value) {
|
373
|
+
qi.$query().then(function (value) {
|
375
374
|
(typeof target).should.be.equal('undefined');
|
376
375
|
done();
|
377
376
|
});
|
378
377
|
});
|
379
|
-
|
380
|
-
it('should re-throw errors from the handler', function() {
|
378
|
+
|
379
|
+
it('should re-throw errors from the handler', function () {
|
381
380
|
var handler1 = false;
|
382
|
-
|
383
|
-
qi.$useHandler(function(query, options) {
|
381
|
+
|
382
|
+
qi.$useHandler(function (query, options) {
|
384
383
|
throw new Error('error');
|
385
384
|
});
|
386
|
-
|
385
|
+
|
387
386
|
(function () {
|
388
|
-
|
387
|
+
qi.$query()
|
389
388
|
}).should.throw(Error);
|
390
389
|
});
|
391
|
-
|
392
|
-
it('should work with async handlers', function(done) {
|
393
|
-
qi.$useHandler(function(query, options) {
|
390
|
+
|
391
|
+
it('should work with async handlers', function (done) {
|
392
|
+
qi.$useHandler(function (query, options) {
|
394
393
|
return 'done';
|
395
394
|
});
|
396
|
-
|
397
|
-
qi.$query().then(function(value) {
|
395
|
+
|
396
|
+
qi.$query().then(function (value) {
|
398
397
|
value.should.equal('done');
|
399
398
|
done();
|
400
399
|
});
|
401
400
|
});
|
402
|
-
|
403
|
-
it('should convert regular values from the handler to resolved promises', function(done) {
|
404
|
-
qi.$useHandler(function(query, options) {
|
401
|
+
|
402
|
+
it('should convert regular values from the handler to resolved promises', function (done) {
|
403
|
+
qi.$useHandler(function (query, options) {
|
405
404
|
return 'regular value';
|
406
405
|
});
|
407
|
-
qi.$query().then(function(value) {
|
406
|
+
qi.$query().then(function (value) {
|
408
407
|
value.should.equal('regular value');
|
409
408
|
done();
|
410
409
|
});
|
411
410
|
});
|
412
|
-
|
413
|
-
it('should call next handler in the chain if the handler does not return a value', function(done) {
|
411
|
+
|
412
|
+
it('should call next handler in the chain if the handler does not return a value', function (done) {
|
414
413
|
var handler1 = false;
|
415
414
|
var handler2 = false;
|
416
|
-
|
417
|
-
qi.$useHandler(function(query, options) {
|
418
|
-
handler1=true;
|
415
|
+
|
416
|
+
qi.$useHandler(function (query, options) {
|
417
|
+
handler1 = true;
|
419
418
|
});
|
420
|
-
qi.$useHandler(function(query, options) {
|
421
|
-
handler2=true;
|
419
|
+
qi.$useHandler(function (query, options) {
|
420
|
+
handler2 = true;
|
422
421
|
});
|
423
|
-
qi.$useHandler(function(query, options) {
|
422
|
+
qi.$useHandler(function (query, options) {
|
424
423
|
return 'called';
|
425
424
|
});
|
426
|
-
|
427
|
-
qi.$query().then(function(value) {
|
425
|
+
|
426
|
+
qi.$query().then(function (value) {
|
428
427
|
value.should.equal('called');
|
429
428
|
handler1.should.be.true();
|
430
429
|
handler2.should.be.true();
|
431
430
|
done();
|
432
431
|
});
|
433
432
|
});
|
434
|
-
|
435
|
-
it('should not call next handler in the chain if a handler returns a value', function(done) {
|
433
|
+
|
434
|
+
it('should not call next handler in the chain if a handler returns a value', function (done) {
|
436
435
|
var handler1 = false;
|
437
436
|
var handler2 = false;
|
438
437
|
var handler3 = false;
|
439
|
-
|
440
|
-
qi.$useHandler(function(query, options) {
|
441
|
-
handler1=true;
|
438
|
+
|
439
|
+
qi.$useHandler(function (query, options) {
|
440
|
+
handler1 = true;
|
442
441
|
});
|
443
|
-
qi.$useHandler(function(query, options) {
|
444
|
-
handler2=true;
|
442
|
+
qi.$useHandler(function (query, options) {
|
443
|
+
handler2 = true;
|
445
444
|
return 'called';
|
446
445
|
});
|
447
|
-
qi.$useHandler(function(query, options) {
|
448
|
-
handler3=true;
|
446
|
+
qi.$useHandler(function (query, options) {
|
447
|
+
handler3 = true;
|
449
448
|
});
|
450
|
-
|
451
|
-
qi.$query().then(function(value) {
|
449
|
+
|
450
|
+
qi.$query().then(function (value) {
|
452
451
|
value.should.equal('called');
|
453
452
|
handler1.should.be.true();
|
454
453
|
handler2.should.be.true();
|
@@ -456,54 +455,54 @@ describe('QueryInterface', function () {
|
|
456
455
|
done();
|
457
456
|
});
|
458
457
|
});
|
459
|
-
|
460
|
-
it('should fall back to results queue if the handlers do not return values', function(done) {
|
458
|
+
|
459
|
+
it('should fall back to results queue if the handlers do not return values', function (done) {
|
461
460
|
var handler1 = false;
|
462
|
-
|
463
|
-
qi.$useHandler(function(query, options) {
|
464
|
-
handler1=true;
|
461
|
+
|
462
|
+
qi.$useHandler(function (query, options) {
|
463
|
+
handler1 = true;
|
465
464
|
});
|
466
465
|
qi.$queueResult('foo');
|
467
|
-
|
468
|
-
qi.$query().then(function(value) {
|
466
|
+
|
467
|
+
qi.$query().then(function (value) {
|
469
468
|
value.should.equal('foo');
|
470
469
|
done();
|
471
470
|
});
|
472
471
|
});
|
473
|
-
|
472
|
+
|
474
473
|
describe('[options.includeCreated]', function () {
|
475
474
|
var qi;
|
476
475
|
beforeEach(function () {
|
477
476
|
qi = new QueryInterface();
|
478
477
|
});
|
479
|
-
|
480
|
-
it('should default the created parameter to true when there is no default specified', async function() {
|
478
|
+
|
479
|
+
it('should default the created parameter to true when there is no default specified', async function () {
|
481
480
|
qi._results = [{
|
482
481
|
content: 'foo',
|
483
482
|
options: {},
|
484
483
|
type: 'Success'
|
485
484
|
}];
|
486
|
-
|
485
|
+
|
487
486
|
var result = await qi.$query({ includeCreated: true });
|
488
487
|
result[0].should.equal('foo');
|
489
488
|
result[1].should.be.true();
|
490
|
-
|
489
|
+
|
491
490
|
});
|
492
|
-
|
493
|
-
it('should default to the createdDefault from the QueryInterface if specified', async function() {
|
491
|
+
|
492
|
+
it('should default to the createdDefault from the QueryInterface if specified', async function () {
|
494
493
|
qi.options.createdDefault = false;
|
495
494
|
qi._results = [{
|
496
495
|
content: 'foo',
|
497
496
|
options: {},
|
498
497
|
type: 'Success'
|
499
498
|
}];
|
500
|
-
|
499
|
+
|
501
500
|
var result = await qi.$query({ includeCreated: true });
|
502
501
|
result[0].should.equal('foo');
|
503
502
|
result[1].should.be.false();
|
504
503
|
});
|
505
|
-
|
506
|
-
it('should use the wasCreated from the query if specified', async function() {
|
504
|
+
|
505
|
+
it('should use the wasCreated from the query if specified', async function () {
|
507
506
|
qi.options.createdDefault = false;
|
508
507
|
qi._results = [{
|
509
508
|
content: 'foo',
|
@@ -514,7 +513,7 @@ describe('QueryInterface', function () {
|
|
514
513
|
options: { wasCreated: false },
|
515
514
|
type: 'Success'
|
516
515
|
}];
|
517
|
-
|
516
|
+
|
518
517
|
var result = await qi.$query({ includeCreated: true });
|
519
518
|
result[0].should.equal('foo');
|
520
519
|
result[1].should.be.true();
|
@@ -523,62 +522,62 @@ describe('QueryInterface', function () {
|
|
523
522
|
query[0].should.equal('bar');
|
524
523
|
query[1].should.be.false();
|
525
524
|
});
|
526
|
-
|
525
|
+
|
527
526
|
});
|
528
|
-
|
527
|
+
|
529
528
|
describe('[options.includeAffectedRows]', function () {
|
530
529
|
var qi;
|
531
530
|
beforeEach(function () {
|
532
531
|
qi = new QueryInterface();
|
533
532
|
});
|
534
|
-
|
535
|
-
it('should default the affectedRows parameter to an empty array', async function() {
|
533
|
+
|
534
|
+
it('should default the affectedRows parameter to an empty array', async function () {
|
536
535
|
qi._results = [{
|
537
536
|
content: 'foo',
|
538
|
-
options: {
|
537
|
+
options: {},
|
539
538
|
type: 'Success'
|
540
539
|
}];
|
541
|
-
|
540
|
+
|
542
541
|
let [content, affectedRows] = await qi.$query({ includeAffectedRows: true });
|
543
|
-
|
542
|
+
|
544
543
|
content.should.equal('foo');
|
545
544
|
affectedRows.should.be.an.Array();
|
546
545
|
affectedRows.length.should.equal(0);
|
547
546
|
});
|
548
|
-
|
549
|
-
it('should pass along affectedRows option specified in result', async function() {
|
547
|
+
|
548
|
+
it('should pass along affectedRows option specified in result', async function () {
|
550
549
|
var rows = [1, 2, 3];
|
551
550
|
qi._results = [{
|
552
551
|
content: 'foo',
|
553
552
|
options: { affectedRows: rows },
|
554
553
|
type: 'Success'
|
555
554
|
}];
|
556
|
-
|
555
|
+
|
557
556
|
var [content, affectedRows] = await qi.$query({ includeAffectedRows: true });
|
558
557
|
content.should.equal('foo');
|
559
558
|
affectedRows.should.equal(rows);
|
560
559
|
});
|
561
|
-
|
560
|
+
|
562
561
|
});
|
563
|
-
|
562
|
+
|
564
563
|
describe('[options.stopPropagation]', function () {
|
565
564
|
var qi;
|
566
565
|
beforeEach(function () {
|
567
566
|
qi = new QueryInterface();
|
568
567
|
});
|
569
|
-
|
570
|
-
it('should default to calling into parent queue if a parent exists', async function() {
|
568
|
+
|
569
|
+
it('should default to calling into parent queue if a parent exists', async function () {
|
571
570
|
var run = 0;
|
572
571
|
qi._results = [];
|
573
572
|
qi.options.parent = {
|
574
|
-
$query: async function() { run++; return 'foo'; }
|
573
|
+
$query: async function () { run++; return 'foo'; }
|
575
574
|
};
|
576
|
-
|
575
|
+
|
577
576
|
var content = await qi.$query();
|
578
577
|
content.should.equal('foo');
|
579
578
|
run.should.equal(1);
|
580
579
|
});
|
581
|
-
|
580
|
+
|
582
581
|
it('should not call into parent if QueryInterface option is to stopPropagation', async function () {
|
583
582
|
var run = 0;
|
584
583
|
qi._results = [];
|
@@ -589,7 +588,7 @@ describe('QueryInterface', function () {
|
|
589
588
|
should.throws(qi.$query.bind(qi), ErrorsMock.EmptyQueryQueueError);
|
590
589
|
run.should.equal(0);
|
591
590
|
});
|
592
|
-
|
591
|
+
|
593
592
|
it('should not call into parent if query option is to stopPropagation', function () {
|
594
593
|
var run = 0;
|
595
594
|
qi._results = [];
|
@@ -598,54 +597,54 @@ describe('QueryInterface', function () {
|
|
598
597
|
$query: async function () { run++; return 'foo'; }
|
599
598
|
};
|
600
599
|
qi.options.stopPropagation = false;
|
601
|
-
|
600
|
+
|
602
601
|
should.throws(qi.$query.bind(qi, { stopPropagation: true }), ErrorsMock.EmptyQueryQueueError);
|
603
602
|
run.should.equal(0);
|
604
603
|
});
|
605
|
-
|
604
|
+
|
606
605
|
it('should not try to call into parent if there is no parent', function () {
|
607
606
|
qi._results = [];
|
608
607
|
should.throws(qi.$query.bind(qi, { stopPropagation: true }), ErrorsMock.EmptyQueryQueueError);
|
609
608
|
});
|
610
|
-
|
609
|
+
|
611
610
|
});
|
612
|
-
|
611
|
+
|
613
612
|
describe('[options.fallbackFn]', function () {
|
614
613
|
var qi;
|
615
614
|
beforeEach(function () {
|
616
615
|
qi = new QueryInterface();
|
617
616
|
});
|
618
|
-
|
617
|
+
|
619
618
|
it('should call fallback function if it exists on the QueryInterface', function (done) {
|
620
619
|
var run = 0;
|
621
620
|
qi._results = [];
|
622
621
|
qi.options.fallbackFn = function () { run++; return 'foo'; };
|
623
|
-
|
622
|
+
|
624
623
|
qi.$query().then(function (content) {
|
625
624
|
content.should.equal('foo');
|
626
625
|
run.should.equal(1);
|
627
626
|
done();
|
628
627
|
}).catch(done);
|
629
628
|
});
|
630
|
-
|
629
|
+
|
631
630
|
it('should call fallback function on query options if it exists', function (done) {
|
632
631
|
var run = 0;
|
633
632
|
qi._results = [];
|
634
633
|
qi.options.fallbackFn = function () { run++; return 'foo'; };
|
635
|
-
|
634
|
+
|
636
635
|
qi.$query({ fallbackFn: function () { run++; return 'bar'; } }).then(function (content) {
|
637
636
|
content.should.equal('bar');
|
638
637
|
run.should.equal(1);
|
639
638
|
done();
|
640
639
|
}).catch(done);
|
641
640
|
});
|
642
|
-
|
641
|
+
|
643
642
|
});
|
644
|
-
|
643
|
+
|
645
644
|
it('should throw a EmptyQueryQueueError if there are no results to show', function () {
|
646
645
|
qi._results = [];
|
647
646
|
should.throws(qi.$query.bind(qi), ErrorsMock.EmptyQueryQueueError);
|
648
647
|
});
|
649
|
-
|
648
|
+
|
650
649
|
});
|
651
650
|
});
|
Binary file
|