-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathindex.test.js
803 lines (652 loc) · 23.7 KB
/
index.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
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
const pg = require('pg');
const assert = require('assert');
const { expect } = require('chai');
const Sequelize = require('sequelize');
const errors = require('@feathersjs/errors');
const feathers = require('@feathersjs/feathers');
const adaptertests = require('@feathersjs/adapter-tests');
const service = require('../lib');
const testSuite = adaptertests([
'.options',
'.events',
'._get',
'._find',
'._create',
'._update',
'._patch',
'._remove',
'.get',
'.get + $select',
'.get + id + query',
'.get + NotFound',
'.find',
'.remove',
'.remove + $select',
'.remove + id + query',
'.remove + multi',
'.update',
'.update + $select',
'.update + id + query',
'.update + NotFound',
'.patch',
'.patch + $select',
'.patch + id + query',
'.patch multiple',
'.patch multi query',
'.patch + NotFound',
'.create',
'.create + $select',
'.create multi',
'internal .find',
'internal .get',
'internal .create',
'internal .update',
'internal .patch',
'internal .remove',
'.find + equal',
'.find + equal multiple',
'.find + $sort',
'.find + $sort + string',
'.find + $limit',
'.find + $limit 0',
'.find + $skip',
'.find + $select',
'.find + $or',
'.find + $in',
'.find + $nin',
'.find + $lt',
'.find + $lte',
'.find + $gt',
'.find + $gte',
'.find + $ne',
'.find + $gt + $lt + $sort',
'.find + $or nested + $sort',
'.find + paginate',
'.find + paginate + $limit + $skip',
'.find + paginate + $limit 0',
'.find + paginate + params',
'.remove + id + query id',
'.update + id + query id',
'.patch + id + query id',
'.get + id + query id'
]);
// The base tests require the use of Sequelize.BIGINT to avoid 'out of range errors'
// Unfortunetly BIGINT's are serialized as Strings:
// https://github.com/sequelize/sequelize/issues/1774
pg.defaults.parseInt8 = true;
const sequelize = require('./connection')();
const Model = sequelize.define('people', {
name: {
type: Sequelize.STRING,
allowNull: false
},
age: {
type: Sequelize.INTEGER
},
created: {
type: Sequelize.BOOLEAN
},
time: {
type: Sequelize.BIGINT
},
status: {
type: Sequelize.STRING,
defaultValue: 'pending'
}
}, {
freezeTableName: true,
scopes: {
active: {
where: {
status: 'active'
}
},
pending: {
where: {
status: 'pending'
}
}
}
});
const Order = sequelize.define('orders', {
name: {
type: Sequelize.STRING,
allowNull: false
}
}, {
freezeTableName: true
});
const CustomId = sequelize.define('people-customid', {
customid: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
name: {
type: Sequelize.STRING,
allowNull: false
},
age: {
type: Sequelize.INTEGER
},
created: {
type: Sequelize.BOOLEAN
},
time: {
type: Sequelize.BIGINT
}
}, {
freezeTableName: true
});
const CustomGetterSetter = sequelize.define('custom-getter-setter', {
addsOneOnSet: {
type: Sequelize.INTEGER,
set: function (val) {
this.setDataValue('addsOneOnSet', val + 1);
}
},
addsOneOnGet: {
type: Sequelize.INTEGER,
get: function () {
return this.getDataValue('addsOneOnGet') + 1;
}
}
}, {
freezeTableName: true
});
Model.hasMany(Order);
Order.belongsTo(Model);
describe('Feathers Sequelize Service', () => {
before(async () => {
await Model.sync({ force: true });
await CustomId.sync({ force: true });
await CustomGetterSetter.sync({ force: true });
await Order.sync({ force: true });
});
describe('Initialization', () => {
it('throws an error when missing a Model', () => {
expect(service.bind(null, { name: 'Test' })).to.throw(/You must provide a Sequelize Model/);
});
});
describe('Common Tests', () => {
const app = feathers()
.use('/people', service({
Model,
events: [ 'testing' ]
}))
.use('/people-customid', service({
Model: CustomId,
events: [ 'testing' ]
}));
it('has .Model', () => {
assert.ok(app.service('people').Model);
});
testSuite(app, errors, 'people', 'id');
testSuite(app, errors, 'people-customid', 'customid');
});
describe('Feathers-Sequelize Specific Tests', () => {
const app = feathers()
.use('/people', service({
Model,
paginate: {
default: 10
},
events: [ 'testing' ],
multi: true
}))
.use('/orders', service({
Model: Order,
multi: true
}))
.use('/custom-getter-setter', service({
Model: CustomGetterSetter,
events: [ 'testing' ],
multi: true
}));
before(() => app.service('people')
.remove(null, { query: { $limit: 1000 } })
);
after(() => app.service('people')
.remove(null, { query: { $limit: 1000 } })
);
describe('Common functionality', () => {
const people = app.service('people');
let kirsten;
beforeEach(async () => {
kirsten = await people.create({ name: 'Kirsten', age: 30 });
});
afterEach(() => people.remove(kirsten.id).catch(() => {}));
it('allows querying for null values (#45)', async () => {
const name = 'Null test';
const person = await people.create({ name });
const { data } = await people.find({ query: { age: null } });
assert.strictEqual(data.length, 1);
assert.strictEqual(data[0].name, name);
assert.strictEqual(data[0].age, null);
await people.remove(person.id);
});
it('still allows querying with Sequelize operators', async () => {
const name = 'Age test';
const person = await people.create({ name, age: 10 });
const { data } = await people.find({ query:
{ age: { [Sequelize.Op.eq]: 10 } }
});
assert.strictEqual(data.length, 1);
assert.strictEqual(data[0].name, name);
assert.strictEqual(data[0].age, 10);
await people.remove(person.id);
});
it('$like works', async () => {
const name = 'Like test';
const person = await people.create({ name, age: 10 });
const { data } = await people.find({ query:
{ name: { $like: '%ike%' } }
});
assert.strictEqual(data.length, 1);
assert.strictEqual(data[0].name, name);
assert.strictEqual(data[0].age, 10);
await people.remove(person.id);
});
it('hides the Sequelize error in ERROR symbol', async () => {
try {
await people.create({
age: 10
});
assert.ok(false, 'Should never get here');
} catch (error) {
assert.ok(error[service.ERROR]);
assert.strictEqual(error.name, 'BadRequest');
}
});
it('correctly persists updates (#125)', async () => {
const updateName = 'Ryan';
await people.update(kirsten.id, { name: updateName });
const updatedPerson = await people.get(kirsten.id);
assert.strictEqual(updatedPerson.name, updateName);
});
it('correctly updates records using optional query param', async () => {
const updateAge = 40;
const updateName = 'Kirtsten';
await people.update(kirsten.id, { name: updateName, age: updateAge }, {
query: { name: 'Kirsten' }
});
const updatedPerson = await people.get(kirsten.id);
assert.strictEqual(updatedPerson.age, updateAge);
});
it('fails update when query prevents result in no record match for id', async () => {
const updateAge = 50;
const updateName = 'Kirtsten';
try {
await people.update(kirsten.id, { name: updateName, age: updateAge }, {
query: { name: 'John' }
});
assert.ok(false, 'Should never get here');
} catch (error) {
assert(error.message.indexOf('No record found') >= 0);
}
});
it('filterQuery does not convert dates and symbols', () => {
const mySymbol = Symbol('test');
const date = new Date();
const query = {
test: { sub: date },
[mySymbol]: 'hello'
};
const filtered = app.service('people').filterQuery({ query });
assert.deepStrictEqual(filtered.query, query);
});
});
describe('Association Tests', () => {
const people = app.service('people');
const orders = app.service('orders');
let kirsten, ryan;
beforeEach(async () => {
kirsten = await people.create({ name: 'Kirsten', age: 30 });
await orders.create([
{ name: 'Order 1', personId: kirsten.id },
{ name: 'Order 2', personId: kirsten.id },
{ name: 'Order 3', personId: kirsten.id }
]);
ryan = await people.create({ name: 'Ryan', age: 30 });
await orders.create([
{ name: 'Order 4', personId: ryan.id },
{ name: 'Order 5', personId: ryan.id },
{ name: 'Order 6', personId: ryan.id }
]);
});
afterEach(() =>
orders.remove(null, { query: { $limit: 1000 } })
.then(() => people.remove(kirsten.id))
.then(() => people.remove(ryan.id))
.catch(() => {})
);
it('find() returns correct total when using includes for non-raw requests (#137)', async () => {
const options = { sequelize: { raw: false, include: Order } };
const result = await people.find(options);
assert.strictEqual(result.total, 2);
});
it('find() returns correct total when using includes for raw requests', async () => {
const options = { sequelize: { include: Order } };
const result = await people.find(options);
assert.strictEqual(result.total, 2);
});
});
describe('Custom getters and setters', () => {
it('calls custom getters and setters (#113)', async () => {
const value = 0;
const service = app.service('custom-getter-setter');
const data = { addsOneOnGet: value, addsOneOnSet: value };
const result = await service.create(data);
assert.strictEqual(result.addsOneOnGet, value + 1);
assert.strictEqual(result.addsOneOnSet, value + 1);
});
it('can ignore custom getters and setters (#113)', async () => {
const value = 0;
const service = app.service('custom-getter-setter');
const data = { addsOneOnGet: value, addsOneOnSet: value };
const IGNORE_SETTERS = { sequelize: { ignoreSetters: true } };
const result = await service.create(data, IGNORE_SETTERS);
assert.strictEqual(result.addsOneOnGet, value + 1);
assert.strictEqual(result.addsOneOnSet, value);
});
});
it('can set the scope of an operation#130', async () => {
const people = app.service('people');
const data = { name: 'Active', status: 'active' };
const SCOPE_TO_ACTIVE = { sequelize: { scope: 'active' } };
const SCOPE_TO_PENDING = { sequelize: { scope: 'pending' } };
const person = await people.create(data);
const staPeople = await people.find(SCOPE_TO_ACTIVE);
assert.strictEqual(staPeople.data.length, 1);
const stpPeople = await people.find(SCOPE_TO_PENDING);
assert.strictEqual(stpPeople.data.length, 0);
await people.remove(person.id);
});
});
describe('ORM functionality', () => {
const app = feathers();
app.use('/raw-people', service({
Model,
events: [ 'testing' ],
multi: true
}));
const rawPeople = app.service('raw-people');
describe('Non-raw Service Config', () => {
app.use('/people', service({
Model,
events: [ 'testing' ],
multi: true,
raw: false // -> this is what we are testing
}));
const people = app.service('people');
let david;
beforeEach(async () => {
david = await people.create({ name: 'David' });
});
afterEach(() => people.remove(david.id).catch(() => {}));
it('find() returns model instances', async () => {
const results = await people.find();
expect(results[0] instanceof Model);
});
it('get() returns a model instance', async () => {
const instance = await people.get(david.id);
expect(instance instanceof Model);
});
it('create() returns a model instance', async () => {
const instance = await people.create({ name: 'Sarah' });
expect(instance instanceof Model);
await people.remove(instance.id);
});
it('bulk create() returns model instances', async () => {
const results = await people.create([
{ name: 'Sarah' },
{ name: 'Connor' }
]);
expect(results.length).to.equal(2);
expect(results[0] instanceof Model);
assert.ok(results[0].id);
assert.ok(results[1].id);
await people.remove(results[0].id);
await people.remove(results[1].id);
});
it('patch() returns a model instance', async () => {
const instance = await people.patch(david.id, { name: 'Sarah' });
expect(instance instanceof Model);
});
it('patch() with $returning=false returns empty array', async () => {
const response = await people.patch(david.id, { name: 'Sarah' }, { $returning: false });
expect(response).to.deep.equal([]);
});
it('update() returns a model instance', async () => {
const instance = await people.update(david.id, david);
expect(instance instanceof Model);
});
it('remove() returns a model instance', async () => {
const instance = await people.remove(david.id);
expect(instance instanceof Model);
});
it('remove() with $returning=false returns empty array', async () => {
const response = await people.remove(david.id, { $returning: false });
expect(response).to.deep.equal([]);
});
});
describe('Non-raw Service Method Calls', () => {
const NOT_RAW = { sequelize: { raw: false } };
let david;
beforeEach(async () => {
david = await rawPeople.create({ name: 'David' });
});
afterEach(() => rawPeople.remove(david.id).catch(() => {}));
it('`raw: false` works for find()', async () => {
const results = await rawPeople.find(NOT_RAW);
expect(results[0] instanceof Model);
});
it('`raw: false` works for get()', async () => {
const instance = await rawPeople.get(david.id, NOT_RAW);
expect(instance instanceof Model);
});
it('`raw: false` works for create()', async () => {
const instance = await rawPeople.create({ name: 'Sarah' }, NOT_RAW);
expect(instance instanceof Model);
await rawPeople.remove(instance.id);
});
it('`raw: false` works for bulk create()', async () => {
const results = await rawPeople.create([{ name: 'Sarah' }], NOT_RAW);
expect(results.length).to.equal(1);
expect(results[0] instanceof Model);
await rawPeople.remove(results[0].id);
});
it('`raw: false` works for patch()', async () => {
const instance = await rawPeople.patch(david.id, { name: 'Sarah' }, NOT_RAW);
expect(instance instanceof Model);
});
it('`raw: false` works for update()', async () => {
const instance = await rawPeople.update(david.id, david, NOT_RAW);
expect(instance instanceof Model);
});
it('`raw: false` works for remove()', async () => {
const instance = await rawPeople.remove(david.id, NOT_RAW);
expect(instance instanceof Model);
});
});
});
describe('ORM functionality with overidden getModel method', () => {
const EXPECTED_ATTRIBUTE_VALUE = 42;
function getExtraParams (additionalTopLevelParams, additionalSequelizeParams) {
additionalTopLevelParams = additionalTopLevelParams || {};
additionalSequelizeParams = additionalSequelizeParams || {};
return Object.assign({
sequelize: Object.assign({
expectedAttribute: EXPECTED_ATTRIBUTE_VALUE,
getModelCalls: { count: 0 }
}, additionalSequelizeParams)
}, additionalTopLevelParams);
}
class ExtendedService extends service.Service {
getModel (params) {
if (!params.sequelize || params.sequelize.expectedAttribute !== EXPECTED_ATTRIBUTE_VALUE) {
throw new Error('Expected custom attribute not found in overridden getModel()!');
}
if (params.sequelize.getModelCalls === undefined) {
throw new Error('getModelCalls not defined on params.sequelize!');
}
params.sequelize.getModelCalls.count++;
return this.options.Model;
}
get Model () {
// Extended service classes that override getModel will often
// depend upon having certain params provided from further up
// the calstack (e.g. part of the request object to make a decision
// on which model/db to return based on the hostname being accessed).
// If feathers-sequelize wants access to the model, it should always
// call getModel(params).
// Returning null here is a way to ensure that a regression isn't
// introduced later whereby feathers-sequelize attempts to access a
// model obtained via the Model getter rather than via getModel(params).
return null;
}
}
function extendedService (options) {
return new ExtendedService(options);
}
const app = feathers();
app.use('/raw-people', extendedService({
Model,
events: [ 'testing' ],
multi: true
}));
const rawPeople = app.service('raw-people');
describe('Non-raw Service Config', () => {
app.use('/people', extendedService({
Model,
events: [ 'testing' ],
multi: true,
raw: false // -> this is what we are testing
}));
const people = app.service('people');
let david;
beforeEach(async () => {
david = await people.create({ name: 'David' }, getExtraParams());
});
afterEach(() => people.remove(david.id, getExtraParams()).catch(() => {}));
it('find() returns model instances', async () => {
const params = getExtraParams();
const results = await people.find(params);
expect(params.sequelize.getModelCalls.count).to.gte(1);
expect(results[0] instanceof Model);
});
it('get() returns a model instance', async () => {
const params = getExtraParams();
const instance = await people.get(david.id, params);
expect(params.sequelize.getModelCalls.count).to.gte(1);
expect(instance instanceof Model);
});
it('create() returns a model instance', async () => {
const params = getExtraParams();
const instance = await people.create({ name: 'Sarah' }, params);
expect(params.sequelize.getModelCalls.count).to.gte(1);
expect(instance instanceof Model);
const removeParams = getExtraParams();
await people.remove(instance.id, removeParams);
expect(removeParams.sequelize.getModelCalls.count).to.gte(1);
});
it('bulk create() returns model instances', async () => {
const params = getExtraParams();
const results = await people.create([{ name: 'Sarah' }], params);
expect(params.sequelize.getModelCalls.count).to.gte(1);
expect(results.length).to.equal(1);
expect(results[0] instanceof Model);
const removeParams = getExtraParams();
await people.remove(results[0].id, removeParams);
expect(removeParams.sequelize.getModelCalls.count).to.gte(1);
});
it('patch() returns a model instance', async () => {
const params = getExtraParams();
const instance = await people.patch(david.id, { name: 'Sarah' }, params);
expect(params.sequelize.getModelCalls.count).to.gte(1);
expect(instance instanceof Model);
});
it('patch() with $returning=false returns empty array', async () => {
const params = getExtraParams({ $returning: false });
const response = await people.patch(david.id, { name: 'Sarah' }, params);
expect(params.sequelize.getModelCalls.count).to.gte(1);
expect(response).to.deep.equal([]);
});
it('update() returns a model instance', async () => {
const params = getExtraParams();
const instance = await people.update(david.id, david, params);
expect(params.sequelize.getModelCalls.count).to.gte(1);
expect(instance instanceof Model);
});
it('remove() returns a model instance', async () => {
const params = getExtraParams();
const instance = await people.remove(david.id, params);
expect(params.sequelize.getModelCalls.count).to.gte(1);
expect(instance instanceof Model);
});
it('remove() with $returning=false returns empty array', async () => {
const params = getExtraParams({ $returning: false });
const response = await people.remove(david.id, params);
expect(params.sequelize.getModelCalls.count).to.gte(1);
expect(response).to.deep.equal([]);
});
});
describe('Non-raw Service Method Calls', () => {
const NOT_RAW = { raw: false };
let david;
beforeEach(async () => {
david = await rawPeople.create({ name: 'David' }, getExtraParams());
});
afterEach(() => rawPeople.remove(david.id, getExtraParams()).catch(() => {}));
it('`raw: false` works for find()', async () => {
const params = getExtraParams({}, NOT_RAW);
const results = await rawPeople.find(params);
expect(params.sequelize.getModelCalls.count).to.gte(1);
expect(results[0] instanceof Model);
});
it('`raw: false` works for get()', async () => {
const params = getExtraParams({}, NOT_RAW);
const instance = await rawPeople.get(david.id, params);
expect(params.sequelize.getModelCalls.count).to.gte(1);
expect(instance instanceof Model);
});
it('`raw: false` works for create()', async () => {
const params = getExtraParams({}, NOT_RAW);
const instance = await rawPeople.create({ name: 'Sarah' }, params);
expect(params.sequelize.getModelCalls.count).to.gte(1);
expect(instance instanceof Model);
const removeParams = getExtraParams();
await rawPeople.remove(instance.id, removeParams);
expect(removeParams.sequelize.getModelCalls.count).to.gte(1);
});
it('`raw: false` works for bulk create()', async () => {
const params = getExtraParams({}, NOT_RAW);
const results = await rawPeople.create([{ name: 'Sarah' }], params);
expect(params.sequelize.getModelCalls.count).to.gte(1);
expect(results.length).to.equal(1);
expect(results[0] instanceof Model);
const removeParams = getExtraParams();
await rawPeople.remove(results[0].id, removeParams);
expect(removeParams.sequelize.getModelCalls.count).to.gte(1);
});
it('`raw: false` works for patch()', async () => {
const params = getExtraParams({}, NOT_RAW);
const instance = await rawPeople.patch(david.id, { name: 'Sarah' }, params);
expect(params.sequelize.getModelCalls.count).to.gte(1);
expect(instance instanceof Model);
});
it('`raw: false` works for update()', async () => {
const params = getExtraParams({}, NOT_RAW);
const instance = await rawPeople.update(david.id, david, params);
expect(params.sequelize.getModelCalls.count).to.gte(1);
expect(instance instanceof Model);
});
it('`raw: false` works for remove()', async () => {
const params = getExtraParams({}, NOT_RAW);
const instance = await rawPeople.remove(david.id, params);
expect(params.sequelize.getModelCalls.count).to.gte(1);
expect(instance instanceof Model);
});
});
});
});