-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrud.spec.ts
895 lines (869 loc) · 33.6 KB
/
crud.spec.ts
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
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
'use strict';
import * as mocha from 'mocha';
import { ResourcesAPIBase, ServiceBase, toObject, convertToObject } from '../src/index';
import * as chassis from '@restorecommerce/chassis-srv';
import { Channel, createChannel, createClient } from '@restorecommerce/grpc-client';
import { Events, registerProtoMeta, Topic } from '@restorecommerce/kafka-client';
import { createServiceConfig } from '@restorecommerce/service-config';
import * as should from 'should';
import * as _ from 'lodash';
import {
Filter_Operation,
Filter_ValueType,
protoMetadata as resourceProto,
Sort_SortOrder,
} from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/resource_base';
import {
protoMetadata as testProto,
CRUDDefinition,
CRUDClient
} from '@restorecommerce/rc-grpc-clients/dist/generated-server/test/test';
registerProtoMeta(
resourceProto,
testProto
);
/*
* Note: To run this test, a running ArangoDB and Kafka instance is required.
* (Kafka is needed only if 'events:enableEvents' config is enabled)
*/
/* global describe it before after beforeEach */
describe('converting to filter to object', () => {
it('should convert proto filter to valid DB filter object', () => {
const protoFilter =
{
filters: [{
filters: [
{
field: 'device_id',
operation: 'eq',
value: '12345'
},
{
field: 'overall_status',
operation: 'in',
value: '["BAD", "GOOD"]',
type: 'ARRAY'
},
{
field: 'device_active',
operation: 'eq',
value: 'true',
type: 'BOOLEAN'
},
{
filters: [{
filters: [{
field: 'firstname',
operation: 'eq',
value: 'test_first'
}, {
field: 'lastname',
operation: 'eq',
value: 'test_last'
}, {
field: 'middleName',
operation: 'eq',
value: 'test_middle'
}],
operator: 'and'
}],
}
], // Default And case
operator: 'or'
}]
};
/* eslint-disable */
const expectedDBObject = { "$or": [{ "device_id": "12345" }, { "overall_status": { "$in": ["BAD", "GOOD"] } }, { "device_active": true }, { "$and": [{ "firstname": "test_first" }, { "lastname": "test_last" }, { "middleName": "test_middle" }] }] };
const dbFilter = toObject(protoFilter);
should.exist(dbFilter);
dbFilter.should.deepEqual(expectedDBObject);
});
it('should convert nested proto filter to valid DB filter object', () => {
const protoFilter =
{
filters: [
{
filters: [
{
filters: [
{
filters: [
{
field: 'user_type',
operation: 'neq',
value: 'TECHNICAL_USER'
},
{
field: 'first_name',
operation: 'iLike',
value: '%test%'
},
{
field: 'last_name',
operation: 'iLike',
value: '%test%'
}
],
operator: 'and'
}
]
},
{
filters: [
{
filters: [
{
field: 'state',
operation: 'eq',
value: 'BW'
},
{
field: 'city',
operation: 'eq',
value: 'Stuttgart'
},
],
operator: 'and'
}
]
}
],
operator: 'or' // Final Or operator
}
]
};
/* eslint-disable */
const expectedDBObject = { "$or": [{ "$and": [{ "user_type": { "$not": { "$eq": "TECHNICAL_USER" } } }, { "first_name": { "$iLike": "%test%" } }, { "last_name": { "$iLike": "%test%" } }] }, { "$and": [{ "state": "BW" }, { "city": "Stuttgart" }] }] }
const dbFilter = toObject(protoFilter);
should.exist(dbFilter);
dbFilter.should.deepEqual(expectedDBObject);
});
it('should convert filters array to valid DB filter object', () => {
const protoFilter =
{
filters: [
{
filters: [
{
field: 'id',
operation: 'in',
value: 'test1'
}
],
operator: 'and'
},
{
filters: [
{
field: 'id',
operation: 'eq',
value: 'test2'
}
],
operator: 'or'
}
]
};
/* eslint-disable */
const expectedDBObject = [{ "$and": [{ "id": { "$in": "test1" } }] }, { "$or": [{ "id": "test2" }] }]
const dbFilter = toObject(protoFilter);
should.exist(dbFilter);
dbFilter.should.deepEqual(expectedDBObject);
});
});
let meta = {
created: new Date(),
modified: new Date(),
created_by: 'Admin',
modified_by: 'Admin',
owners: [{
id: 'urn:restorecommerce:acs:names:ownerIndicatoryEntity',
value: 'urn:restorecommerce:acs:model:user.User',
attributes: [{
id: 'urn:restorecommerce:acs:names:ownerInstance',
value: 'Admin'
}]
}]
};
describe('ServiceBase', () => {
let db: chassis.GraphDatabaseProvider;
let server: chassis.Server;
let bufferedServer: chassis.Server;
let events: Events;
let channel: Channel;
let testService: CRUDClient;
let testData: any;
let cfg;
const today = new Date();
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
before(async () => {
// Load test config from chassis service config
cfg = createServiceConfig(process.cwd() + '/test');
server = new chassis.Server(cfg.get('server'));
bufferedServer = new chassis.Server(cfg.get('bufferedServer'));
events = new Events(cfg.get('events:testevents'), server.logger);
await events.start();
const resourceName = 'resource';
const testEvents: Topic = await events.topic('test');
db = await chassis.database.get(cfg.get('database:testdb'), server.logger) as chassis.GraphDatabaseProvider;
db.registerCustomQuery('testFilter', 'filter node.value < @customArguments.testParam', 'filter');
const bufferHandlerConfig: any = cfg.get('fieldHandlers:bufferFields');
const entitiesNames = Object.keys(bufferHandlerConfig);
const requiredFieldsConfig: any = cfg.get('fieldHandlers:requiredFields');
const timeStampFieldsConfigs: any = cfg.get('fieldHandlers:timeStampFields');
let resourceFieldConfig: any = {};
if (bufferHandlerConfig && entitiesNames.includes(resourceName)) {
resourceFieldConfig['bufferFields'] = bufferHandlerConfig[resourceName];
}
if (requiredFieldsConfig && (resourceName in requiredFieldsConfig)) {
resourceFieldConfig['requiredFields'] = requiredFieldsConfig;
}
resourceFieldConfig['timeStampFields'] = [];
for (let timeStampFiledConfig of timeStampFieldsConfigs) {
if (timeStampFiledConfig.entities.includes(resourceName)) {
resourceFieldConfig['timeStampFields'].push(...timeStampFiledConfig.fields);
}
}
const resourceAPI: ResourcesAPIBase = new ResourcesAPIBase(db, `${resourceName}s`, resourceFieldConfig);
let isEventsEnabled = cfg.get('events:enableEvents');
if (isEventsEnabled === 'true') {
isEventsEnabled = true;
} else { // Undefined means events not enabled
isEventsEnabled = false;
}
const service = new ServiceBase<any, any>('Resource', testEvents,
server.logger, resourceAPI, isEventsEnabled);
await server.bind('test', {
service: CRUDDefinition,
implementation: service as any
});
const bufferResourceName = 'testBufferedData';
if (bufferHandlerConfig && entitiesNames.includes(bufferResourceName)) {
if (!resourceFieldConfig) {
resourceFieldConfig = {};
}
resourceFieldConfig['bufferFields'] = bufferHandlerConfig[bufferResourceName];
}
// Create buffered service and bind it to gRPC server
const resourceBufferAPI: ResourcesAPIBase = new ResourcesAPIBase(db, `${bufferResourceName}s`, resourceFieldConfig);
const bufferService = new ServiceBase<any, any>(bufferResourceName, testEvents,
bufferedServer.logger, resourceBufferAPI, isEventsEnabled);
await bufferedServer.bind('testBufferedService', {
service: CRUDDefinition,
implementation: bufferService as any
});
await server.start();
await bufferedServer.start();
channel = createChannel(cfg.get('client:test').address);
testService = createClient({
...cfg.get('client:test'),
logger: server.logger
}, CRUDDefinition, channel);
});
after(async () => {
await channel.close();
await server.stop();
await bufferedServer.stop();
await events.stop();
});
describe('endpoints', () => {
beforeEach(async () => {
db = await chassis.database.get(cfg.get('database:testdb'), server.logger) as chassis.GraphDatabaseProvider;
await db.truncate();
testData = [
{ id: 'test_xy', meta, value: 1, text: 'first simple sentence for searching', active: true, created: today, status: 'GOOD' },
{ id: 'test_xyz', meta, value: 3, text: 'second test data', active: false, created: tomorrow, status: 'BAD' },
{ id: 'test_zy', meta, value: 12, text: 'third search data string', active: false, created: tomorrow, status: 'UNKNOWN' }];
// await db.insert('resources', testData);
await testService.create({ items: testData, subject: { id: 'Admin' } });
});
describe('read', () => {
it('should return all three elements with no arguments', async () => {
const result = await testService.read({});
should.exist(result);
should.exist(result.items);
should.exist(result.total_count);
result.total_count.should.be.equal(3);
result.items.should.be.Array();
result.items.should.length(3);
for (let data of testData) {
delete data?.meta?.modified;
}
_.forEach(result.items, (item) => {
// delete modified field as it will be changed when creating
delete item.payload.meta.modified;
testData.should.matchAny(item.payload);
});
should.exist(result.operation_status);
result.operation_status.code.should.equal(200);
result.operation_status.message.should.equal('success');
});
it('should return two elements with offset 1', async () => {
const compareData = _.drop((await testService.read({})).items, 1);
const result = await testService.read({
offset: 1,
});
should.exist(result);
should.exist(result.items);
should.exist(result.total_count);
result.total_count.should.be.equal(compareData.length);
result.items.should.be.Array();
result.items.should.length(2);
_.sortBy(result.items, 'id').should.deepEqual(_.sortBy(compareData, 'id'));
result.operation_status.code.should.equal(200);
result.operation_status.message.should.equal('success');
});
it('should return two elements with limit 2', async () => {
const compareData = _.dropRight((await testService.read({})).items, 1);
const result = await testService.read({
limit: 2,
});
should.exist(result);
should.exist(result.items);
should.exist(result.total_count);
result.total_count.should.be.equal(compareData.length);
result.items.should.be.Array();
result.items.should.length(2);
_.sortBy(result.items, 'id').should.deepEqual(_.sortBy(compareData, 'id'));
result.operation_status.code.should.equal(200);
result.operation_status.message.should.equal('success');
});
it('should return elements sorted', async () => {
const result = await testService.read({
sorts: [{
field: 'id',
order: Sort_SortOrder.DESCENDING,
}],
});
should.exist(result);
should.exist(result.items);
should.exist(result.total_count);
result.total_count.should.be.equal(3);
result.items.should.be.Array();
result.items.should.length(3);
const testDataDescending = testData.sort((a, b) => {
if (a.value > b.value) {
return -1;
}
if (a.value < b.value) {
return 1;
}
// a must be equal to b
return 0;
});
// match the descending order
for (let i = 0; i < result.items.length; i++) {
delete result.items[i].payload.meta.modified;
result.items[i].payload.should.deepEqual(testDataDescending[i]);
}
result.operation_status.code.should.equal(200);
result.operation_status.message.should.equal('success');
});
it('should return only resources with value higher than 10', async () => {
const filters = [{
filters: [{
field: 'value',
operation: Filter_Operation.gt,
value: '10',
type: Filter_ValueType.NUMBER
}]
}];
const result = await testService.read({
filters
});
should.exist(result);
should.exist(result.items);
should.exist(result.total_count);
result.total_count.should.be.equal(1);
result.items.should.be.Array();
result.items.should.length(1);
delete result.items[0].payload.meta.modified;
result.items[0].payload.should.deepEqual(testData[2]); // testData[2] is object with value > 10
result.operation_status.code.should.equal(200);
result.operation_status.message.should.equal('success');
});
it('should return only resources with string filter value equal to id', async () => {
const filters = [{
filters: [{
field: 'id',
operation: Filter_Operation.eq,
value: 'test_xy'
}]
}];
const result = await testService.read({
filters
});
should.exist(result);
should.exist(result.items);
should.exist(result.total_count);
result.total_count.should.be.equal(1);
result.items.should.be.Array();
result.items.should.length(1);
delete result.items[0].payload.meta.modified;
result.items[0].payload.should.deepEqual(testData[0]); // testData[9] is object with value 'test_xy'
result.operation_status.code.should.equal(200);
result.operation_status.message.should.equal('success');
});
it('should return only resources matching boolean filter', async () => {
const filters = [{
filters: [{
field: 'active',
operation: Filter_Operation.eq,
value: 'true',
type: Filter_ValueType.BOOLEAN
}]
}];
const result = await testService.read({
filters
});
should.exist(result);
should.exist(result.items);
should.exist(result.total_count);
result.total_count.should.be.equal(1);
result.items.should.be.Array();
result.items.should.length(1);
delete result.items[0].payload.meta.modified;
result.items[0].payload.should.deepEqual(testData[0]);
result.operation_status.code.should.equal(200);
result.operation_status.message.should.equal('success');
});
it('should return resources matching date filter', async () => {
const todayDatePlusOneMin = new Date();
todayDatePlusOneMin.setSeconds(todayDatePlusOneMin.getSeconds() + 60);
// timeObject.setSeconds(timeObject.getSeconds() + 60);
const filters = [{
filters: [{
field: 'created',
operation: Filter_Operation.lt,
value: todayDatePlusOneMin.toString(),
type: Filter_ValueType.DATE,
}]
}];
const result = await testService.read({
filters
});
should.exist(result);
should.exist(result.items);
should.exist(result.total_count);
result.total_count.should.be.equal(1);
result.items.should.be.Array();
result.items.should.length(1);
let resultPayload = [];
for (let item of result.items) {
resultPayload.push(item.payload);
}
_.sortBy(resultPayload[0].id, 'id').should.deepEqual(_.sortBy(_.filter(testData, (data) => {
return data.created <= today.getTime();
})[0].id, 'id'));
result.operation_status.code.should.equal(200);
result.operation_status.message.should.equal('success');
});
it('should return resources matching array filter', async () => {
const filters = [{
filters: [{
field: 'status',
operation: Filter_Operation.in,
value: '["BAD", "UNKNOWN"]',
type: Filter_ValueType.ARRAY,
}]
}];
const result = await testService.read({
filters
});
should.exist(result);
should.exist(result.items);
should.exist(result.total_count);
result.total_count.should.be.equal(2);
result.items.should.be.Array();
result.items.should.length(2);
let resultPayload = [];
// delete modified properties as the modified date would have changed
for (let item of result.items) {
delete item.payload.meta.modified;
resultPayload.push(item.payload);
}
_.sortBy(resultPayload, 'id').should.deepEqual(_.sortBy(_.filter(testData, (data) => {
// data.created = new Date(data.created);
delete data.meta.modified;
return (data.status === "BAD" || data.status === "UNKNOWN");
}), 'id'));
result.operation_status.code.should.equal(200);
result.operation_status.message.should.equal('success');
});
it('should return only resources with not equal filter', async () => {
const filters = [{
filters: [{
field: 'id',
operation: Filter_Operation.neq,
value: 'test_xy',
}]
}];
const result = await testService.read({
filters
});
should.exist(result);
should.exist(result.items);
should.exist(result.total_count);
result.total_count.should.be.equal(2);
result.items.should.be.Array();
result.items.should.length(2);
let resultPayload = [];
// delete modified property
for (let item of result.items) {
delete item.payload.meta.modified;
resultPayload.push(item.payload);
}
_.sortBy(resultPayload, 'id').should.deepEqual(_.sortBy(_.filter(testData, (data) => {
delete data.meta.modified;
return data.id != 'test_xy';
}), 'id'));
result.operation_status.code.should.equal(200);
result.operation_status.message.should.equal('success');
}).timeout(4000);
it('should return elements only with field value', async () => {
const result = await testService.read({
fields: [{
name: 'value',
include: true,
}],
});
should.exist(result);
should.exist(result.items);
should.exist(result.total_count);
result.total_count.should.be.equal(3);
result.items.should.be.Array();
result.items.should.length(3);
const testDataReduced = [
{ value: testData[0].value },
{ value: testData[1].value },
{ value: testData[2].value },
];
let resultPayload = [];
for (let item of result.items) {
resultPayload.push(item.payload);
}
_.sortBy(resultPayload, 'value').should.deepEqual(_.sortBy(testDataReduced, 'value'));
result.operation_status.code.should.equal(200);
result.operation_status.message.should.equal('success');
});
it('should apply a custom filter', async () => {
const result = await testService.read({
fields: [{
name: 'value',
include: true,
}],
custom_queries: ['testFilter'],
custom_arguments: {
value: Buffer.from(JSON.stringify({ testParam: 12 }))
}
});
should.exist(result);
should.exist(result.items);
should.exist(result.total_count);
result.total_count.should.be.equal(2);
result.items.should.be.Array();
result.items.should.length(2);
const testDataReduced = [
{ value: testData[0].value },
{ value: testData[1].value },
];
let resultPayload = [];
for (let item of result.items) {
resultPayload.push(item.payload);
}
_.sortBy(resultPayload, 'value').should.deepEqual(_.sortBy(testDataReduced, 'value'));
result.operation_status.code.should.equal(200);
result.operation_status.message.should.equal('success');
});
it('fulltext search - should return only matching documents as per search string (default case insensitive)', async () => {
await new Promise((resolve, reject) => {
setTimeout(resolve, 2000);
});
const result = await testService.read({
search: {
search: 'EaRc' // will match search text from above `text` data and return 2 documents
}
});
result.items.length.should.equal(2);
result.items[0].payload.id.should.equal('test_xy');
result.items[0].payload.text.should.equal('first simple sentence for searching');
result.items[1].payload.id.should.equal('test_zy');
result.items[1].payload.text.should.equal('third search data string');
}).timeout(5000);
it('fulltext search - should return only matching documents as per search string (default case insensitive)', async () => {
await new Promise((resolve, reject) => {
setTimeout(resolve, 2000);
});
const result = await testService.read({
search: {
search: 'data' // will match search text from above `text` data and return 2 documents
}
});
result.items.length.should.equal(2);
result.items[0].payload.id.should.equal('test_xyz');
result.items[0].payload.text.should.equal('second test data');
result.items[1].payload.id.should.equal('test_zy');
result.items[1].payload.text.should.equal('third search data string');
}).timeout(5000);
it('fulltext search - should not return any matching documents as per search string with case sensitive search', async () => {
await new Promise((resolve, reject) => {
setTimeout(resolve, 2000);
});
const result = await testService.read({
search: {
search: 'DATA', // will not match search text from above `text` data and should not return any documents
case_sensitive: true
}
});
should.not.exist(result.items);
}).timeout(5000);
});
describe('create', () => {
it('should create new documents and validate duplicate element error', async () => {
const meta = {
acl: [],
created_by: 'Admin',
modified_by: 'Admin',
owners: [{
id: 'urn:restorecommerce:acs:names:ownerIndicatoryEntity',
value: 'urn:restorecommerce:acs:model:user.User',
attributes: [{
attribute: [],
id: 'urn:restorecommerce:acs:names:ownerInstance',
value: 'Admin'
}]
}]
};
const newTestDataFirst = {
id: 'test_newdata',
value: -10,
text: 'new data',
meta
};
const newTestDataSecond = {
id: 'test_newdata2',
value: -10,
text: 'new second data',
meta
};
const testDuplicate = {
id: 'test_newdata2',
value: -10,
text: 'new second data',
meta
};
const newTestData = [newTestDataFirst, newTestDataSecond, testDuplicate];
const result = await testService.create({ items: newTestData, subject: { id: 'Admin' } });
should.exist(result);
should.exist(result.items);
result.items.should.be.length(3);
result.items.should.matchEach((e) => {
if (e.payload) { // since there is one element with payload undefined for duplicate element with error status
return e.payload.value === -10 && e.payload.text.length > 0;
}
});
// validate overall status
should.exist(result.operation_status);
result.operation_status.code.should.equal(200);
result.operation_status.message.should.equal('success');
// validate error status for duplicate element
result.items[2].status.message.should.equal(`unique constraint violated - in index primary of type primary over '_key'; conflicting key: test_newdata2`);
result.items[2].status.code.should.equal(409);
const allTestData = await testService.read({});
should.exist(allTestData);
should.exist(allTestData.operation_status);
// total 5 items should exist (3 from beginning, 2 from this test case)
allTestData.items.length.should.equal(5);
const compareData = _.concat(testData, _.map(result.items, (item) => item.payload));
// delete modified property from meta data
for (let data of compareData) {
delete data?.meta?.modified;
}
_.forEach(allTestData.items, (e) => {
delete e.payload?.meta?.modified;
compareData.should.matchAny(e.payload);
});
});
});
describe('delete', () => {
it('should delete collection when requested', async () => {
const result = await testService.delete({ collection: true });
should.exist(result);
should.exist(result.status);
result.status.length.should.equal(3);
result.status.should.matchEach((status) => {
return status.code === 200 && status.message === 'success';
});
should.exist(result.operation_status);
result.operation_status.code.should.equal(200);
result.operation_status.message.should.equal('success');
const allTestData = await testService.read({});
should.exist(allTestData);
should.exist(allTestData.operation_status);
should.not.exist(allTestData.items);
allTestData.operation_status.code.should.equal(200);
allTestData.operation_status.message.should.equal('success');
});
it('should delete specified documents and return error if document does not exist', async () => {
const result = await testService.delete({ ids: [testData[1].id, 'invalidID'] });
should.exist(result);
should.exist(result.status);
// success for 1st id and failure message for second invalid id
result.status[0].code.should.equal(200);
result.status[0].message.should.equal('success');
result.status[1].code.should.equal(404);
result.status[1].message.should.equal('document not found');
should.exist(result.operation_status);
result.operation_status.code.should.equal(200);
result.operation_status.message.should.equal('success');
const allTestData = await testService.read({});
should.exist(allTestData);
should.exist(allTestData.operation_status);
should.exist(allTestData);
should.exist(allTestData.items);
allTestData.items.should.length(2);
allTestData.operation_status.code.should.equal(200);
allTestData.operation_status.message.should.equal('success');
let resultPayload = [];
for (let item of allTestData.items) {
delete item?.payload?.meta?.modified;
resultPayload.push(item.payload);
}
// delete modified property for testData[0] and testData[2]
delete testData[0]?.meta?.modified;
delete testData[2]?.meta?.modified;
_.sortBy(resultPayload, 'id')
.should.deepEqual(_.sortBy([testData[0], testData[2]], 'id'));
});
});
describe('update', () => {
it('should update all specified documents and validate status message', async () => {
const patch = _.map(testData, (data) => {
data.value = 100;
data.text = 'test-patch';
return data;
});
const result = await testService.update({ items: patch, subject: { id: 'Admin' } });
should.exist(result);
should.exist(result.operation_status);
should.exist(result.items);
result.items.should.matchEach((e) => {
return e.payload.value === 100 && e.payload.text.length === 10;
});
result.operation_status.code.should.equal(200);
result.operation_status.message.should.equal('success');
const allTestData = await testService.read({});
should.exist(allTestData);
should.exist(allTestData.items);
should.exist(allTestData.operation_status);
allTestData.items.length.should.equal(3);
result.items.should.matchEach((e) => {
return e.payload.value === 100 && e.payload.text.length === 10;
});
});
it('should return an error when trying to update invalid document', async () => {
const patch = [{
id: 'invalidDocument',
value: 2,
text: 'new value'
}];
const result = await testService.update({ items: patch, subject: { id: 'Admin' } });
result.items.should.length(1);
should.exist(result.operation_status);
// validate status of item
result.items[0].status.code.should.equal(404);
result.items[0].status.message.should.equal('document not found');
// overall status
result.operation_status.code.should.equal(200);
result.operation_status.message.should.equal('success');
});
});
describe('upsert', () => {
it('should create or update specified documents', async () => {
const now = Date.now();
const replace = [{
id: testData[2].id,
value: 0,
text: '',
meta
}, {
id: testData[0].id,
value: 0,
text: 'patched',
meta
}, {
id: 'test_newput',
value: 0,
text: '',
meta
}];
const result = await testService.upsert({ items: replace, subject: { id: 'Admin' } });
should.exist(result);
result.items.length.should.equal(3);
result.items[0].payload.id.should.equal('test_newput');
should.exist(result.operation_status);
should.exist(result.items);
result.items.should.matchEach((e) => {
return e.payload.value === 0;
});
// overall status
result.operation_status.code.should.equal(200);
result.operation_status.message.should.equal('success');
const allTestData = await testService.read({});
should.exist(allTestData);
should.exist(allTestData.operation_status);
should.exist(allTestData.items);
});
});
// Test to check required field
describe('check required fileds', () => {
it('should return an error when trying to insert with missing requried fields', async () => {
let result = await testService.delete({ collection: true });
should.exist(result);
should.exist(result.operation_status);
result.operation_status.code.should.equal(200);
result.operation_status.message.should.equal('success');
const objectMissingField = [
{ id: 'test_xy', value: 1, meta },
{ id: 'test_xyz', value: 3, meta },
{ id: 'test_zy', value: 12, meta }];
const result2 = await testService.create({ items: objectMissingField, subject: { id: 'Admin' } });
should.exist(result2);
should.exist(result2.operation_status);
should.exist(result2.items);
result2.items.should.length(3);
for (let item of result2.items) {
item.status.code.should.equal(400);
item.status.message.should.startWith('Field text is necessary for resource for documentID');
}
});
});
// Test to check buffered fields
describe('check buffered fileds', () => {
it('should decode the buffered field before storing in DB',
async () => {
// delete existing data and create new bufferdata message
await testService.delete({ collection: true });
const bufData = {
type_url: '',
value: Buffer.from(JSON.stringify({ testkey: 'testValue' }))
};
const bufferObjects = [
{ value: 1, data: bufData, meta, text: 'test1' },
{ value: 2, data: bufData, meta, text: 'test2' }];
let resp = await testService.create({ items: bufferObjects, subject: { id: 'Admin' } });
// Read directly from DB and compare the JSON data
// because normal read() operation again encodes and sends the data back.
// This way, we check if the data was actually encoded by reading it from the DB.
const result = await db.find('resources');
should.exist(result);
should.exist(result[0]);
should.exist(result[0].data.testkey);
result[0].data.testkey.should.equal('testValue');
// delete the collection
await db.truncate('resources');
});
});
});
});