-
Notifications
You must be signed in to change notification settings - Fork 2
/
jsDataSetSpec.js
2647 lines (2247 loc) · 94.6 KB
/
jsDataSetSpec.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
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
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*jslint nomen:true*/
/*jslint nomen:true*/
/*globals _ , jasmine, beforeEach, expect, module, it, describe, spyOn, afterEach */
'use strict';
// const helper = require ("jasmine-uti");
//require('jasmine-collection-matchers')
const jsDataSet = require('../../client/components/metadata/jsDataSet');
const jsDataQuery = require('../../client/components/metadata/jsDataQuery');
const _ = require('lodash');
const Deferred = require("JQDeferred");
beforeEach(function() {
jasmine.addMatchers({
toHaveSameItems: function (util, customEqualityTesters) {
function isObject(obj) {
return Object.prototype.toString.apply(obj) === '[object Object]';
}
function craftMessage(actual, expected, mismatches) {
if (mismatches.length === 0) {
return 'The collections do not match in length or objects. \n Expected collection:' + JSON.stringify(actual)
+ ' is not equal to ' + JSON.stringify(expected);
}
return ['The collections have equal length, but do not match.'].concat(mismatches.map(function (m) {
return 'At ' + m.index + ': expected ' + JSON.stringify(m.expected) +
', actual ' + JSON.stringify(m.actual);
})).join('\n ');
}
function compareArraysSorted(actual, expected) {
var mismatches = [];
actual.forEach(function (item, i) {
if (!util.equals(item, expected[i], customEqualityTesters)) {
mismatches.push({index: i, actual: item, expected: expected[i]});
}
});
return mismatches;
}
function compareArraysIgnoreSort(actual, expected) {
expected = expected.slice(0);
var mismatches = [];
actual.forEach(function (item, i) {
var foundIndex = -1;
expected.some(function (expectedItem, i) {
if (util.equals(item, expectedItem, customEqualityTesters)) {
foundIndex = i;
return true;
}
});
if (foundIndex > -1) {
expected.splice(foundIndex, 1)
}
else {
mismatches.push({index: i, actual: item, expected: null});
}
});
mismatches = mismatches.concat(expected.map(function (val, i) {
return {index: actual.length + i, actual: null, expected: val};
}));
return mismatches;
}
function compareHashes(actual, expected) {
var mismatches = {};
Object.keys(actual).forEach(function (key) {
if (!util.equals(actual[key], expected[key], customEqualityTesters)) {
mismatches[key] = {index: key, actual: actual[key], expected: expected[key]};
}
});
Object.keys(expected).forEach(function (key) {
if (!util.equals(actual[key], expected[key], customEqualityTesters) && !mismatches[key]) {
mismatches[key] = {index: key, actual: actual[key], expected: expected[key]};
}
});
return Object.keys(mismatches).map(function (key) {
return mismatches[key];
});
}
return {
compare: function (actual, expected, ignoreOrder) {
if (!Array.isArray(actual) && !isObject(actual)) {
throw new Error('Actual must be an Array or Object. Is type: ' + typeof actual);
}
if (!Array.isArray(expected) && !isObject(expected)) {
throw new Error('Expectation must be an Array or Object. Is type: ' + typeof expected);
}
var mismatches;
if (Array.isArray(actual) && Array.isArray(expected)) {
if (actual.length !== expected.length) {
return {
pass: false,
message: 'Array length differs! Actual length: ' + actual.length + ', expected length: ' + expected.length
};
}
if (ignoreOrder) {
mismatches = compareArraysIgnoreSort(actual, expected);
}
else {
mismatches = compareArraysSorted(actual, expected);
}
}
else {
mismatches = compareHashes(actual, expected);
}
return {
pass: mismatches.length === 0,
message: craftMessage(actual, expected, mismatches)
};
}
};
}
});
});
describe('System status', function () {
const dsSpace = jsDataSet,
$q = jsDataQuery;
let ds;
beforeEach(function () {
ds = new dsSpace.DataSet('temp');
});
it('dsSpace should be defined', function () {
expect(dsSpace).not.toBeNull();
});
it('$q should be defined', function () {
expect($q).not.toBeNull();
});
it('_ should be defined', function () {
expect(_).not.toBeNull();
});
function def(g){
let d = Deferred();
try {
g().then((res)=>d.resolve(res),
(err)=>d.reject(err));
}
catch (e){
d.reject("divide by zero");
}
return d;
}
it ("Throwing exceptions through def generates a rejected promise ",function(done){
let DefHelp = function(){
return Deferred().resolve(null.ciao());
};
let intermediate=0;
let res = def(DefHelp)
.fail((err) => {
//console.log(err);
expect(err).toBe("divide by zero");
intermediate=12;
})
.always((res)=>{
expect(intermediate).toBe(12);
done();
});
});
});
describe('DataSet',
function () {
const dsSpace = jsDataSet,
$q = jsDataQuery;
let ds;
beforeEach(function () {
ds = new dsSpace.DataSet('temp');
});
afterEach(function () {
ds = null;
});
it('dsSpace should be defined', function () {
expect(dsSpace).toBeDefined();
});
describe('DataSet Structure', function () {
it('ds should be defined', function () {
expect(ds).toBeDefined();
});
it('ds.tables should be defined', function () {
expect(ds.tables).toBeDefined();
});
it('ds.relations should be defined', function () {
expect(ds.relations).toBeDefined();
});
it('ds should have a field name', function () {
expect(ds.name).toBeDefined();
});
it('ds name should be not null', function () {
expect(ds.name).not.toBeNull();
});
it('ds should have a string field name', function () {
expect(ds.name).toEqual(jasmine.any(String));
});
it('dsSpace.DataRow should be a function', function () {
expect(dsSpace.DataRow).toEqual(jasmine.any(Function));
});
});
describe('DataSet Functions', function () {
it('ds.name should be writeable', function () {
ds.name = 'D';
expect(ds.name).toMatch('D');
});
it('ds should have a clone function', function () {
expect(ds.clone).toEqual(jasmine.any(Function));
});
it('ds.clone should return a DataSet', function () {
expect(ds.clone() instanceof dsSpace.DataSet).toBeTruthy();
});
it('ds.clone should create tables', function () {
ds.newTable('a');
ds.newTable('b');
ds.newRelation('a_b', 'a', ['x'], 'b', ['y']);
expect(ds.clone().tables.a).toBeDefined();
expect(ds.clone().tables.b).toBeDefined();
});
it('ds.clone should fill tables with DataTables', function () {
ds.newTable('a');
ds.newTable('b');
ds.newRelation('a_b', 'a', ['x'], 'b', ['y']);
expect(ds.clone().tables.a.toString()).toEqual('DataTable a');
});
it('ds.clone should create relations', function () {
ds.newTable('a');
ds.newTable('b');
ds.newRelation('ab', 'a', ['x'], 'b', ['y']);
expect(ds.clone().relations.ab).toBeDefined();
});
it('ds.copy should return something', function () {
ds.newTable('a');
ds.newTable('b');
ds.newRelation('ab', 'a', ['x'], 'b', ['y']);
expect(ds.copy()).not.toBeNull();
});
it('ds.copy should return a dataset', function () {
ds.newTable('a');
ds.newTable('b');
ds.newRelation('ab', 'a', ['x'], 'b', ['y']);
expect(ds.copy() instanceof dsSpace.DataSet).toBeTruthy();
});
it('spyOn(ds, \'clone\') should return something ', function () {
ds.newTable('a');
ds.newTable('b');
ds.newRelation('ab', 'a', ['x'], 'b', ['y']);
expect(spyOn(ds, 'clone')).toBeDefined();
});
it('spyOn(ds, \'clone\').andCallThrough should return something ', function () {
ds.newTable('a');
ds.newTable('b');
ds.newRelation('a_b', 'a', ['x'], 'b', ['y']);
expect(spyOn(ds, 'clone').and.callThrough).toBeDefined();
});
it('ds.copy should call ds.clone', function () {
ds.newTable('a');
ds.newTable('b');
ds.newRelation('a_b', 'a', ['x'], 'b', ['y']);
spyOn(ds, 'clone').and.callThrough();
ds.copy();
expect(ds.clone).toHaveBeenCalled();
});
it('ds.copy should copy rows', function () {
let tA = ds.newTable('a'),
newDs,
newTa;
tA.add({a: 1});
tA.add({a: 2});
ds.newTable('b');
ds.newRelation('a_b', 'a', ['x'], 'b', ['y']);
newDs = ds.copy();
newTa = newDs.tables.a;
expect(newTa.rows.length).toEqual(2);
expect(newTa.select($q.eq('a', 1)).length).toBe(1);
});
});
describe('DataRelation functions', function () {
it('ds.relations should exists', function () {
expect(ds.relations).toBeDefined();
});
it('ds.newRelation should be a function', function () {
expect(ds.newRelation).toEqual(jasmine.any(Function));
});
it('calling ds.newRelation adds a relation to ds.relations', function () {
ds.newTable('a');
ds.newTable('b');
const rel = ds.newRelation('ab', 'a', ['field1'], 'b', ['field2']);
expect(ds.relations.ab).toBe(rel);
});
it('newRelation adds a relation to ds.relations', function () {
ds.newTable('a');
ds.newTable('b');
const rel = ds.newRelation('ab', 'a', ['field1'], 'b', ['field2']);
expect(ds.relations.ab).toBe(rel);
});
it('newRelation adds a relation to ds.relations (case with comma separated field list)', function () {
ds.newTable('a');
ds.newTable('b');
const rel = ds.newRelation('ab', 'a', 'field1,field2', 'b', 'field3,field4');
expect(ds.relations.ab).toBe(rel);
expect(rel.parentCols.length).toBe(2);
expect(rel.childCols.length).toBe(2);
});
it('relation.getChildsFilter should be a function', function () {
ds.newTable('a');
ds.newTable('b');
const rel = ds.newRelation('a_b', 'a', ['field1'], 'b', ['field2']);
expect(rel.getChildFilter).toEqual(jasmine.any(Function));
});
it('relation.getChildsFilter should return a function', function () {
ds.newTable('a');
ds.newTable('b');
const rel = ds.newRelation('a_b', 'a', ['field1'], 'b', ['field2']),
r = {a: 1};
expect(rel.getChildFilter(r)).toEqual(jasmine.any(Function));
});
it('relation.getChildsFilter().toSql should be a function', function () {
ds.newTable('a');
ds.newTable('b');
const rel = ds.newRelation('a_b', 'a', ['A1', 'A2'], 'b', ['B1', 'B2']),
r = {A0: 3, A1: null, A2: 'nino', A3: 'arance'};
expect(rel.getChildFilter(r).toSql).toEqual(jasmine.any(Function));
});
it('relation.getChildsFilter() should return false on non matching objects', function () {
ds.newTable('a');
ds.newTable('b');
const rel = ds.newRelation('a_b', 'a', ['A1', 'A2'], 'b', ['B1', 'B2']),
rA = {A0: 6, A1: 12, A2: 'nino', A3: 'arance'},
rB = {B0: 6, B1: 12, B2: 'anna', B3: 'arance'};
expect(rel.getChildFilter(rA)(rB)).toBeFalsy();
});
it('relation.getChildsFilter() should return true on matching objects', function () {
ds.newTable('a');
ds.newTable('b');
const rel = ds.newRelation('a_b', 'a', ['A1', 'A2'], 'b', ['B1', 'B2']),
rA = {A0: 10, A1: 12, A2: 'nino', A3: 'arance'},
rB = {B0: 10, B1: 12, B2: 'nino', B3: 'pere'};
expect(rel.getChildFilter(rA)(rB)).toBeTruthy();
});
it('relation.getChildsFilter() should return false on (with null)-matching objects', function () {
ds.newTable('a');
ds.newTable('b');
const rel = ds.newRelation('a_b', 'a', ['A1', 'A2'], 'b', ['B1', 'B2']),
rA = {A0: 10, A1: null, A2: 'nino', A3: 'arance'},
rB = {B0: 10, B1: null, B2: 'nino', B3: 'pere'};
expect(rel.getChildFilter(rA)(rB)).toBeFalsy();
});
it('relation.getParentsFilter() should return false on non matching objects', function () {
ds.newTable('a');
ds.newTable('b');
const rel = ds.newRelation('a_b', 'a', ['A1', 'A2'], 'b', ['B1', 'B2']),
rA = {A0: 6, A1: 13, A2: 'nino', A3: 'arance'},
rB = {B0: 6, B1: 13, B2: 'anna', B3: 'arance'};
expect(rel.getParentsFilter(rB)(rA)).toBeFalsy();
});
it('relation.getParentsFilter() should return true on matching objects', function () {
ds.newTable('a');
ds.newTable('b');
const rel = ds.newRelation('a_b', 'a', ['A1', 'A2'], 'b', ['B1', 'B2']),
rA = {A0: 10, A1: 13, A2: 'nino', A3: 'arance'},
rB = {B0: 10, B1: 13, B2: 'nino', B3: 'pere'};
expect(rel.getParentsFilter(rB)(rA)).toBeTruthy();
});
it('relation.getParentsFilter() should return false on (with null)-matching objects', function () {
ds.newTable('a');
ds.newTable('b');
const rel = ds.newRelation('a_b', 'a', ['A1', 'A2'], 'b', ['B1', 'B2']),
rA = {A0: 10, A1: null, A2: 'nino', A3: 'arance'},
rB = {B0: 10, B1: null, B2: 'nino', B3: 'pere'};
expect(rel.getParentsFilter(rB)(rA)).toBeFalsy();
});
it('getting a relation function should call _.map', function () {
ds.newTable('a');
ds.newTable('b');
spyOn(dsSpace.myLoDash, 'map').and.callThrough();
const rel = ds.newRelation('a_b', 'a', ['A1', 'A2'], 'b', ['B1', 'B2']),
rA = {A0: 3, A1: null, A2: 'nino', A3: 'arance'};
rel.getChildFilter(rA, 'qq');
expect(dsSpace.myLoDash.map).toHaveBeenCalled();
});
it('getting a getChildFilter function should call _.map with parentCols as first argument ', function () {
ds.newTable('a');
ds.newTable('b');
const rel = ds.newRelation('a_b', 'a', ['A1', 'A2'], 'b', ['B1', 'B2']),
rA = {A0: 4, A1: null, A2: 'nino', A3: 'arance'};
spyOn(dsSpace.myLoDash, 'map').and.callThrough();
rel.getChildFilter(rA);
expect(dsSpace.myLoDash.map.calls.argsFor(0)[0]).toBe(rel.parentCols);
});
it('getting a getParentsFilter function should call _.map with childCols as first argument ', function () {
ds.newTable('a');
ds.newTable('b');
const rel = ds.newRelation('a_b', 'a', ['A1', 'A2'], 'b', ['B1', 'B2']),
rB = {B0: 3, B1: null, B2: 'nino', B3: 'pere'};
spyOn(dsSpace.myLoDash, 'map').and.callThrough();
rel.getParentsFilter(rB);
expect(dsSpace.myLoDash.map.calls.argsFor(0)[0]).toBe(rel.childCols);
});
it('calling a relation function should not call _.map', function () {
ds.newTable('a');
ds.newTable('b');
const rel = ds.newRelation('a_b', 'a', ['A1', 'A2'], 'b', ['B1', 'B2']),
rA = {A0: 8, A1: null, A2: 'nino', A3: 'arance'},
rB = {B0: 9, B1: null, B2: 'nino', B3: 'pere'},
f = rel.getChildFilter(rA, 'qq');
spyOn(dsSpace.myLoDash, 'map').and.callThrough();
f(rB);
expect(dsSpace.myLoDash.map).not.toHaveBeenCalled();
});
it('dataSet.relationsByParent and should be set relationsByChild after calling newRelation', function () {
ds.newTable('a');
ds.newTable('b');
const rel = ds.newRelation('a_b', 'a', ['A1', 'A2'], 'b', ['B1', 'B2']);
expect(ds.relationsByParent.a).toEqual([rel]);
expect(ds.relationsByChild.b).toEqual([rel]);
expect(ds.relationsByParent.b).toEqual([]);
expect(ds.relationsByChild.a).toEqual([]);
});
it('dataSet.relationsByParent and should be set relationsByChild after calling newRelation', function () {
ds.newTable('a');
ds.newTable('b');
const rel = ds.newRelation('a_b', 'a', ['A1', 'A2'], 'b', ['B1', 'B2']);
expect(ds.getParentChildRelation('b','a')).toEqual([]);
expect(ds.getParentChildRelation('a','c')).toEqual([]);
expect(ds.getParentChildRelation('a','b')).toEqual([rel]);
});
it('clone should preserve relationsByParent relationsByChild and ', function () {
ds.newTable('a');
ds.newTable('b');
const rel = ds.newRelation('a_b', 'a', ['A1', 'A2'], 'b', ['B1', 'B2']),
d2 = ds.clone();
expect(_.map(d2.relationsByParent.a, function(r){
return _.pick(r,['parentTable','parentCols','childTable','childCols']);
} ))
.toEqual([_.pick(rel, ['parentTable', 'parentCols', 'childTable', 'childCols'])]);
expect(_.map(d2.relationsByChild.b, function (r) {
return _.pick(r, ['parentTable', 'parentCols', 'childTable', 'childCols']);
}))
.toEqual([_.pick(rel, ['parentTable', 'parentCols', 'childTable', 'childCols'])]);
expect(d2.relationsByParent.b).toEqual([]);
expect(d2.relationsByChild.a).toEqual([]);
});
});
describe('OptimisticLocking functions', function(){
let $ol, stubEnv;
beforeEach(function(){
$ol = dsSpace.OptimisticLocking;
stubEnv= {
sys:function(field){return 'sys_'+field;},
usr:function(field){return 'usr_'+field;},
field: function(field){return 'field_'+field;}
};
});
it('OptimisticLocking should be defined', function(){
expect($ol).toBeDefined();
});
it('OptimisticLocking should be a constructor', function () {
expect($ol).toEqual(jasmine.any(Function));
expect($ol.prototype.constructor).toEqual($ol);
});
it('OptimisticLocking should return an object with a method prepareForPosting ', function () {
const o = new $ol(['lt', 'lu'], ['ct', 'cu', 'lt', 'lu']);
expect(o.prepareForPosting).toEqual(jasmine.any(Function));
});
it('OptimisticLocking should return an object with a method getOptimisticLock ', function () {
const o = new $ol(['lt', 'lu'], ['ct', 'cu', 'lt', 'lu']);
expect(o.getOptimisticLock).toEqual(jasmine.any(Function));
});
it('prepareForPosting should fill update fields for modified rows', function(){
const t = ds.newTable('a'),
r = t.newRow({a: 1, b: 2, ct: 'ct', lt: 'lt', cu: 'cu', lu: 'lu'}),
p = r.getRow().current,
o = new $ol(['lt', 'lu'], ['ct', 'cu', 'lt', 'lu']);
t.acceptChanges();
p.a=10;
o.prepareForPosting(r, stubEnv);
expect(r).toEqual({a:10,b:2,ct:'ct',cu:'cu',lt:'field_lt',lu:'field_lu'});
});
it('prepareForPosting should fill create fields for added rows', function () {
const t = ds.newTable('a'),
r = t.newRow({a: 1, b: 2, ct: 'ct', lt: 'lt', cu: 'cu', lu: 'lu'}),
o = new $ol(['lt', 'lu'], ['ct', 'cu', 'lt', 'lu']);
o.prepareForPosting(r, stubEnv);
expect(r).toEqual({a: 1, b: 2, ct: 'field_ct', cu: 'field_cu', lt: 'field_lt', lu: 'field_lu'});
});
it('getOptimisticLock should return a function that matches the original row', function(){
const t = ds.newTable('a'),
r = t.newRow({id: 10, a: 1, b: 2, ct: 'ct', lt: 'lt', cu: 'cu', lu: 'lu'}),
o = new $ol(['lt', 'lu'], ['ct', 'cu', 'lt', 'lu']);
t.key(['id']);
const filter = o.getOptimisticLock(r);
expect(t.select(filter)).toEqual([r]);
//check filter is comparing lt
r.lt='no';
expect(t.select(filter)).toEqual([]);
r.lt = 'lt';
//check filter is comparing lu
r.lu = 'no';
expect(t.select(filter)).toEqual([]);
//check again filter is doing something
r.lu = 'lu';
expect(t.select(filter)).toEqual([r]);
//check filter is not comparing ct, cu
r.ct = 'no';
r.cu = 'no';
expect(t.select(filter)).toEqual([r]);
//check filter is comparing key
r.id=13;
expect(t.select(filter)).toEqual([]);
//again we test in the original condition
r.id=10;
expect(t.select(filter)).toEqual([r]);
});
it('getOptimisticLock should return a function that matches the original row (no primary key)', function () {
const t = ds.newTable('a'),
r = t.newRow({id: 10, a: 1, b: 2, ct: 'ct', lt: 'lt', cu: 'cu', lu: 'lu'}),
o = new $ol(['lt', 'lu'], ['ct', 'cu', 'lt', 'lu']);
//t.key(['id']); no key here
const filter = o.getOptimisticLock(r);
expect(t.select(filter)).toEqual([r]);
//check filter is comparing lt
r.lt = 'no';
expect(t.select(filter)).toEqual([]);
r.lt = 'lt';
//check filter is comparing lu
r.lu = 'no';
expect(t.select(filter)).toEqual([]);
//check again filter is doing something
r.lu = 'lu';
expect(t.select(filter)).toEqual([r]);
//check filter IS comparing ct, cu
r.ct = 'no';
r.cu = 'no';
expect(t.select(filter)).toEqual([]);
//check filter IS comparing also b
r.ct = 'ct';
r.cu = 'cu';
r.b = 3;
expect(t.select(filter)).toEqual([]);
r.b = 2;
//check filter is comparing key
r.id = 13;
expect(t.select(filter)).toEqual([]);
//again we test in the original condition
r.id = 10;
expect(t.select(filter)).toEqual([r]);
});
});
describe('serialize/deserialize', function () {
let t, t2, r, r1, r2, s, s1, s2, s3, s4;
beforeEach(function () {
t = ds.newTable('a');
r = t.newRow({id: 10, a: 1, b: 2, ct: 'ct', lt: 'lt', cu: 'cu', lu: 'lu'});
r1 = t.newRow({id: 11, a: 123, b: 2, ct: 'ct', lt: 'lt', cu: 'cu', lu: 'lu'});
r2 = t.newRow({id: 12, a: 1, b: 2, ct: 'ct', lt: 'lt', cu: 'cu', lu: 'lu'});
t2 = ds.newTable('b');
s = t2.newRow({id: 13, a: 1, b: 2, ct: 'ct', lt: 'lt', cu: 'cu', lu: 'lu'});
s1 = t2.newRow({id: 14, a: 1, b: 2, ct: 'ct', lt: 'lt', cu: 'cu', lu: 'lu'});
s2 = t2.newRow({id: 15, a: 1, b: 2, ct: 'ct', lt: 'lt', cu: 'cu', lu: 'lu'});
s3 = t2.newRow({id: 16, a: 2, b: 2, ct: 'ct', lt: 'lt', cu: 'cu', lu: 'lu'});
s4= t2.newRow({id: 17, a: 21, b: 2, ct: 'ct', lt: 'lt', cu: 'cu', lu: 'lu'});
s2.getRow().acceptChanges();
s2.getRow().del();
r1.getRow().acceptChanges();
r1.a = 21;
s4.getRow().acceptChanges();
});
it('should preserve tables', function () {
const sData = JSON.parse(JSON.stringify(ds.serialize()));
const ds2 = new dsSpace.DataSet('dd');
ds2.deSerialize(sData);
expect(_.keys(ds2.tables).length).toBe(2);
});
it('should preserve row count', function () {
const sData = JSON.parse(JSON.stringify(ds.serialize()));
const ds2 = new dsSpace.DataSet('dd');
ds2.deSerialize(sData);
expect(ds2.tables.a.rows.length).toBe(3);
expect(ds2.tables.b.rows.length).toBe(5);
});
it('should preserve row values', function () {
const sData = JSON.parse(JSON.stringify(ds.serialize()));
const ds2 = new dsSpace.DataSet('dd');
ds2.deSerialize(sData);
expect(ds2.tables.a.rows).toEqual(ds.tables.a.rows);
expect(ds2.tables.b.rows).toEqual(ds.tables.b.rows);
});
it('should preserve original values', function () {
const sData = JSON.parse(JSON.stringify(ds.serialize()));
const ds2 = new dsSpace.DataSet('dd');
ds2.deSerialize(sData);
let r = ds2.tables.a.select($q.eq('id',11))[0];
expect(r.getRow().getValue('a', dsSpace.dataRowVersion.original)).toBe(123);
expect(r.getRow().state).toBe(dsSpace.dataRowState.modified);
let s4 = ds2.tables.b.select($q.eq('id',17))[0];
expect(s4.getRow().getValue('a', dsSpace.dataRowVersion.original)).toBe(21);
expect(s4.getRow().state).toBe(dsSpace.dataRowState.unchanged);
s4.a = 99;
expect(s4.getRow().state).toBe(dsSpace.dataRowState.modified);
});
it('should preserve current values', function () {
const sData = JSON.parse(JSON.stringify(ds.serialize()));
const ds2 = new dsSpace.DataSet('dd');
ds2.deSerialize(sData);
expect(ds2.tables.a.select($q.eq('id', 11))[0]['a']).toBe(21);
});
});
// describe('merge functions', function(){
// //TODO all those
// });
});
describe('DataTable module test', function () {
const dsSpace = jsDataSet,
DataTable = dsSpace.DataTable,
$q = jsDataQuery;
let ds, ds2;
beforeEach(function () {
ds = new dsSpace.DataSet('temp');
ds2 = new dsSpace.DataSet('temp2');
});
afterEach(function () {
ds = null;
ds2 = null;
});
describe('DataTable functions', function () {
let t;
beforeEach(function () {
t = ds.newTable('customer');
});
afterEach(function () {
t = null;
});
it('dataTable should be defined', function () {
expect(t).toBeDefined();
});
it('dataTable should have a string name', function () {
expect(t.name).toEqual(jasmine.any(String));
});
it('dataTable should have an object collection (rows)', function () {
expect(t.rows).toEqual(jasmine.any(Array));
});
it('adding object to table should modify collection size', function () {
t.add({a: 1, b: 2});
t.add({a: 2, b: 3});
t.add({a: 2, b: 3});
expect(t.rows.length).toBe(3);
});
it('detaching object to table should modify collection size', function () {
t.add({a: 1, b: 2});
t.add({a: 2, b: 3});
const c = {a: 2, b: 4};
t.add(c);
c.getRow().detach();
expect(t.rows.length).toBe(2);
});
it('adding and detaching object does not change object values', function () {
const c = {a: 2, b: 4};
t.add(c);
c.getRow().detach();
expect(c).toEqual({a: 2, b: 4});
});
it('detaching object does not change object values ', function () {
const c = {a: 2, b: 4};
t.add(c);
c.q = 'n';
c.getRow().detach();
expect(c).toEqual({a: 2, b: 4, q: 'n'});
});
it('detached objects are not linked toDataRow anymore', function () {
t.add({a: 1, b: 2});
t.add({a: 2, b: 3});
const c = {a: 2, b: 4};
t.add(c);
c.getRow().detach();
expect(c.getRow).toBeUndefined();
});
it('adding object are given a state of added', function () {
const o1 = {a: 1, b: 2},
o2 = {a: 2, b: 3};
t.add(o1);
t.load(o2);
t.add({a: 2, b: 4});
t.add({a: 2, b: 3});
expect(o1.getRow().state).toBe(dsSpace.dataRowState.added);
});
it('loaded object are given a state of unchanged', function () {
const o1 = {a: 1, b: 2},
o2 = {a: 2, b: 3};
t.add(o1);
t.load(o2);
t.add({a: 2, b: 4});
t.add({a: 2, b: 3});
expect(o2.getRow().state).toBe(dsSpace.dataRowState.unchanged);
});
it('deleting objects should not modify collection size', function () {
const o1 = {a: 1, b: 2};
t.add(o1);
t.add({a: 2, b: 3});
t.add({a: 2, b: 3});
t.acceptChanges();
o1.getRow().del();
expect(t.rows.length).toBe(3);
});
it('deleting objects should modify collection size after acceptChanges', function () {
const o1 = {a: 1, b: 2};
t.add(o1);
t.load({a: 2, b: 3});
t.add({a: 2, b: 3});
t.acceptChanges();
o1.getRow().del();
t.acceptChanges();
expect(t.rows.length).toBe(2);
});
it('deleted rows becomes detached after acceptChanges', function () {
const o1 = {a: 1, b: 2};
t.add(o1);
t.load({a: 2, b: 3});
t.add({a: 2, b: 3});
t.acceptChanges();
const dr = o1.getRow();
dr.del();
expect(dr.state).toBe(dsSpace.dataRowState.deleted);
t.acceptChanges();
expect(dr.state).toBe(dsSpace.dataRowState.detached);
});
it('objects are no longer linked to datarow when those have been detached', function () {
const o1 = {a: 1, b: 2};
t.add(o1);
t.load({a: 2, b: 3});
t.add({a: 2, b: 3});
t.acceptChanges();
const dr = o1.getRow();
dr.del();
t.acceptChanges();
expect(o1.getRow).toBeUndefined();
});
it('datarows haven\'t any getRow method when detached', function () {
const o1 = {a: 1, b: 2};
t.add(o1);
t.load({a: 2, b: 3});
t.add({a: 2, b: 3});
t.acceptChanges();
let gr = o1.getRow;
expect(gr).toBeDefined();
let dr = o1.getRow();
expect(dr).toBeDefined();
dr.del();
t.acceptChanges();
expect(dr.getRow).toBeUndefined();
});
it('multiple addition of same object should NOT be ignored', function () {
const o1 = {a: 1, b: 2};
const o2 = {a: 1, b: 3};
const o3 = {a: 1, b: 3};
t.add(o1);
t.add(o2);
t.add(o3);
t.add(o1);
t.add(o1);
expect(t.rows.length).toBe(5);
});
it('after acceptChanges no modification are left (update-rejectChanges)', function () {
const o3 = {a: 1, b: 3};
t.add(o3);
t.acceptChanges();
o3.a=2;
t.rejectChanges();
expect(t.hasChanges()).toBeFalsy();
expect(o3.getRow().state).toBe(dsSpace.dataRowState.unchanged);
});
it('after rejectChanges no modification are left (add-acceptChanges)', function () {
const o2 = {a: 1, b: 3};
const o3 = {a: 1, b: 3};
t.add(o2);
t.add(o3);
t.acceptChanges();
t.add({a: 3, c: 5});
t.rejectChanges();
expect(t.hasChanges()).toBeFalsy();
});
it('after rejectChanges no modification are left (del-acceptChanges)', function () {
const o1 = {a: 1, b: 2};
t.add(o1);
t.acceptChanges();
o1.getRow().del();
t.add({a: 3, c: 5});
t.rejectChanges();
expect(t.hasChanges()).toBeFalsy();
});
it('rejectChanges should undo deletions', function () {
const o1 = {a: 1, b: 2};
const o2 = {a: 1, b: 3};
const o3 = {a: 1, b: 3};
t.add(o1);
t.add(o2);
t.add(o3);
t.acceptChanges();
o1.getRow().del();
t.rejectChanges();
expect(o1.getRow().state).toBe(dsSpace.dataRowState.unchanged);
expect(t.rows.length).toBe(3);
});
it('rejectChanges should undo additions', function () {
const o1 = {a: 1, b: 2};
const o2 = {a: 1, b: 3};
const o3 = {a: 1, b: 3};
t.add(o1);
t.add(o2);
t.add(o3);
t.acceptChanges();
const o4 = {k: 1};
t.add(o4);
t.rejectChanges();
expect(o4.getRow).toBeUndefined();
expect(t.rows.length).toBe(3);
});
it('getChanges should contain added rows', function () {
const o1 = {a: 1, b: 2};
const o2 = {a: 1, b: 3};
const o3 = {a: 1, b: 3};
t.add(o1);
t.add(o2);
t.add(o3);
t.acceptChanges();
const o4 = {k: 1};
const p4 = t.add(o4).current;
expect(t.getChanges().indexOf(p4)).toBeGreaterThan(-1);
});
it('getChanges should contain deleted rows', function () {
const o1 = {a: 1, b: 2};
const o2 = {a: 1, b: 3};
const o3 = {a: 1, b: 3};
t.add(o1);
t.add(o2);
const p3 = t.add(o3).current;
t.acceptChanges();
//o3.getRow().del();
p3.$del();
expect(t.getChanges().indexOf(p3)).toBeGreaterThan(-1);
});
it('getChanges should contain modified rows', function () {
const o1 = {a: 1, b: 2};
const o2 = {a: 1, b: 3};
const o3 = {a: 1, b: 3};
const p1 = t.add(o1).current;
const p2 = t.add(o2).current;