-
Notifications
You must be signed in to change notification settings - Fork 0
/
seederTest.ts
968 lines (834 loc) · 46.1 KB
/
seederTest.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
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
/// <reference path="../seeder.ts" />
import Member = dSeeder.Member;
import TreeNode = dSeeder.TreeNode;
import TreeNodeMarriage = dSeeder.TreeNodeMarriage;
import SeederOptions = dSeeder.SeederOptions;
//TODO: replace with namespace import or replace testing framework
import 'mocha';
import Chai from 'chai';
const assert = Chai.assert;
//TODO: convert to namespace import
import MockMembers from './data/mockMembers';
const testData = MockMembers.getAll();
let dTreeSampleData: any;
fetch('./data/dTreeSampleData.json')
.then((response) => dTreeSampleData = response.json());
//TODO: convert to namespace import
import dTreeSampleMockMembers from './data/dTreeSampleMockMembers';
describe('_private._getRelatives', () => {
it('gets empty array, should throw error', () => {
// Assert
assert.throws(() => dSeeder._private._getRelatives([]));
})
it('gets no targetId, should return an empty array', () => {
// Assert
assert.throws(() => dSeeder._private._getRelatives(testData));
})
describe('gets targetId', () => {
it('target is not in data, should throw error', () => {
// Arrange
const targetId = 999;
// Assert
assert.throw(() => dSeeder._private._getRelatives(testData, targetId));
})
describe('target is in data', () => {
it('should return array with no duplicates', () => {
// Arrange
const targetId = MockMembers.LyarraStark.id;
// Act
const result = dSeeder._private._getRelatives(testData, targetId);
// Assert
const resultIds = result.map((member) => member.id);
const duplicateIds = resultIds.filter((value, index) =>
index !== resultIds.indexOf(value));
assert.isEmpty(duplicateIds);
})
it('should return array including target with their parent ids', () => {
// Arrange
const targetId = MockMembers.EdardStark.id;
// Act
const result = dSeeder._private._getRelatives(testData, targetId);
// Assert
const target = result.find((member) => member.id === targetId);
assert.isDefined(target);
assert.isNotNull(target?.parent1Id);
assert.isNotNull(target?.parent2Id);
})
describe('has parents', () => {
it('only has parent1, should return array including parent1 without their parent ids', () => {
// Arrange
const targetId = MockMembers.OnlyHasParent1.id;
// Act
const result = dSeeder._private._getRelatives(testData, targetId);
// Assert
const resultIds = result.map((member) => member.id);
const target = result.find((member) => member.id === targetId);
assert.include(resultIds, target?.parent1Id);
assert.isNull(target?.parent2Id);
const parent1 = result.find((member) => member.id === target?.parent1Id);
assert.isNull(parent1?.parent1Id);
assert.isNull(parent1?.parent2Id);
})
it('only has parent2, should return array including parent2 without their parent ids', () => {
// Arrange
// Arrange
const targetId = MockMembers.OnlyHasParent2.id;
// Act
const result = dSeeder._private._getRelatives(testData, targetId);
// Assert
const resultIds = result.map((member) => member.id);
const target = result.find((member) => member.id === targetId);
assert.isNull(target?.parent1Id);
assert.include(resultIds, target?.parent2Id);
const parent2 = result.find((member) => member.id === target?.parent2Id);
assert.isNull(parent2?.parent1Id);
assert.isNull(parent2?.parent2Id);
})
it('has 2 parents, should return array including both parents without their parent ids', () => {
// Arrange
const targetId = MockMembers.LyannaStark.id;
// Act
const result = dSeeder._private._getRelatives(testData, targetId);
// Assert
const resultIds = result.map((member) => member.id);
const target = result.find((member) => member.id === targetId);
assert.include(resultIds, target?.parent1Id);
assert.include(resultIds, target?.parent2Id);
const parent1 = result.find((member) => member.id === target?.parent1Id);
const parent2 = result.find((member) => member.id === target?.parent2Id);
assert.isNull(parent1?.parent1Id);
assert.isNull(parent1?.parent2Id);
assert.isNull(parent2?.parent1Id);
assert.isNull(parent2?.parent2Id);
})
it('parent1 is not in data, should throw error', () => {
// Arrange
const targetId = MockMembers.Parent1IsNotInData.id;
// Assert
assert.throw(() => dSeeder._private._getRelatives(testData, targetId));
})
it('parent2 is not in data, should throw error', () => {
// Arrange
const targetId = MockMembers.Parent2IsNotInData.id;
// Assert
assert.throw(() => dSeeder._private._getRelatives(testData, targetId));
})
})
describe('has siblings', () => {
it('with same parent order, should return array including siblings with their parent ids', () => {
// Arrange
const targetId = MockMembers.Child.id;
// Act
const result = dSeeder._private._getRelatives(testData, targetId);
// Assert
const resultIds = result.map((member) => member.id);
assert.include(resultIds, MockMembers.SiblingWithParentsInSameOrder.id);
const sibling = result.find((member) => member.id === MockMembers.SiblingWithParentsInSameOrder.id);
assert.isNotNull(sibling?.parent1Id);
assert.isNotNull(sibling?.parent2Id);
})
it('with reverse parent order, should return array including siblings with their parent ids', () => {
// Arrange
const targetId = MockMembers.Child.id;
// Act
const result = dSeeder._private._getRelatives(testData, targetId);
// Assert
const resultIds = result.map((member) => member.id);
assert.include(resultIds, MockMembers.SiblingWithParentsInReverseOrder.id);
const sibling = result.find((member) => member.id === MockMembers.SiblingWithParentsInSameOrder.id);
assert.isNotNull(sibling?.parent1Id);
assert.isNotNull(sibling?.parent2Id);
})
it('with both parent orders, should return array including siblings with their parent ids', () => {
// Arrange
const targetId = MockMembers.Child.id;
// Act
const result = dSeeder._private._getRelatives(testData, targetId);
// Assert
const resultIds = result.map((member) => member.id);
const sibling1Id = MockMembers.SiblingWithParentsInSameOrder.id;
const sibling2Id = MockMembers.SiblingWithParentsInReverseOrder.id;
assert.include(resultIds, sibling1Id);
assert.include(resultIds, sibling2Id);
const sibling1 = result.find((member) => member.id === sibling1Id);
const sibling2 = result.find((member) => member.id === sibling2Id);
assert.isNotNull(sibling1?.parent1Id);
assert.isNotNull(sibling1?.parent2Id);
assert.isNotNull(sibling2?.parent1Id);
assert.isNotNull(sibling2?.parent2Id);
})
})
describe('has children', () => {
it('should return array including children with their parent ids', () => {
// Arrange
const targetId = MockMembers.LyarraStark.id;
// Act
const result = dSeeder._private._getRelatives(testData, targetId);
// Assert
const resultIds = result.map((member) => member.id);
const child1Id = MockMembers.EdardStark.id;
const child2Id = MockMembers.LyannaStark.id;
assert.include(resultIds, child1Id);
assert.include(resultIds, child2Id);
const child1 = result.find((member) => member.id === child1Id);
const child2 = result.find((member) => member.id === child2Id);
assert.isNotNull(child1?.parent1Id);
assert.isNotNull(child1?.parent2Id);
assert.isNotNull(child2?.parent1Id);
assert.isNotNull(child2?.parent2Id);
})
it('should return array including children\'s other parent without their parent ids', () => {
// Arrange
const targetId = MockMembers.EdardStark.id;
// Act
const result = dSeeder._private._getRelatives(testData, targetId);
// Assert
const resultIds = result.map((member) => member.id);
const spouseId = MockMembers.CatelynStark.id;
assert.include(resultIds, spouseId);
const spouse = result.find((member) => member.id === spouseId);
assert.isNull(spouse?.parent1Id);
assert.isNull(spouse?.parent2Id);
})
it('other parent (parent1) is not in data, should throw error', () => {
// Arrange
const targetId = MockMembers.Parent1IsNotInData.id;
// Assert
assert.throw(() => dSeeder._private._getRelatives(testData, targetId));
})
it('other parent (parent2) is not in data, should throw error', () => {
// Arrange
const targetId = MockMembers.Parent2IsNotInData.id;
// Assert
assert.throw(() => dSeeder._private._getRelatives(testData, targetId));
})
})
describe('has grandchildren', () => {
it('should return array including grandchildren with their parent ids', () => {
// Arrange
const targetId = MockMembers.LyarraStark.id;
// Act
const result = dSeeder._private._getRelatives(testData, targetId);
// Assert
const resultIds = result.map((member) => member.id);
const grandchildId = MockMembers.AryaStark.id;
assert.include(resultIds, grandchildId);
const grandchild = result.find((member) => member.id === grandchildId);
assert.isNotNull(grandchild?.parent1Id);
assert.isNotNull(grandchild?.parent2Id);
})
it('should return array including grandchildren\'s other parents without their parent ids', () => {
// Arrange
const targetId = MockMembers.LyarraStark.id;
// Act
const result = dSeeder._private._getRelatives(testData, targetId);
// Assert
const resultIds = result.map((member) => member.id);
const grandchildOtherParentId = MockMembers.CatelynStark.id;
assert.include(resultIds, grandchildOtherParentId);
const grandchildOtherParent = result.find((member) => member.id === grandchildOtherParentId);
assert.isNull(grandchildOtherParent?.parent1Id);
assert.isNull(grandchildOtherParent?.parent2Id);
})
})
})
})
});
describe('_private._combineIntoMarriages', () => {
it('gets empty array, should throw error', () => {
// Assert
assert.throws(() => dSeeder._private._combineIntoMarriages([]));
})
it('gets 1 member, returns array of 1 treeNode', () => {
// Arrange
const members = [MockMembers.AryaStark];
// Act
const result = dSeeder._private._combineIntoMarriages(members);
// Assert
assert.isArray(result);
assert.lengthOf(result, 1);
assert.equal(result[0].id, members[0].id);
})
it('gets only members with no parents, should throw error', () => {
// Arrange
const members = [
{
id: 1,
name: 'No Parents 1',
parent1Id: null,
parent2Id: null
},
{
id: 2,
name: 'No Parents 2',
parent1Id: null,
parent2Id: null
}
];
// Assert
assert.throw(() => dSeeder._private._combineIntoMarriages(members));
})
it('gets 1 child with 1 parent as parent1, returns array of 1 tree node with 1 marrige to no spouse and 1 child', () => {
// Arrange
const parent = {
id: 1,
name: 'Parent',
parent1Id: null,
parent2Id: null
};
const child = {
id: 2,
name: 'Child',
parent1Id: 1,
parent2Id: null
};
const members = [parent, child];
// Act
const result = dSeeder._private._combineIntoMarriages(members);
// Assert
assert.isNull(result[0].marriages[0].spouse);
assert.equal(result[0].marriages[0].children[0].id, child.id);
assert.equal(result[0].marriages[0].children.length, 1);
})
it('gets 1 child with 1 parent as parent2, returns array of 1 tree node with 1 marrige to no spouse and 1 child', () => {
// Arrange
const parent = {
id: 1,
name: 'Parent',
parent1Id: null,
parent2Id: null
};
const child = {
id: 2,
name: 'Child',
parent1Id: null,
parent2Id: 1
};
const members = [parent, child];
// Act
const result = dSeeder._private._combineIntoMarriages(members);
// Assert
assert.isNull(result[0].marriages[0].spouse);
assert.equal(result[0].marriages[0].children[0].id, child.id);
assert.equal(result[0].marriages[0].children.length, 1);
})
it('gets 1 child with 2 parents, returns array of 1 tree node with 1 marrige to spouse with 1 child', () => {
// Arrange
const parent1 = {
id: 1,
name: 'Parent1',
parent1Id: null,
parent2Id: null
};
const parent2 = {
id: 2,
name: 'Parent2',
parent1Id: null,
parent2Id: null
};
const child = {
id: 3,
name: 'Child',
parent1Id: 1,
parent2Id: 2
};
const members = [parent1, parent2, child];
// Act
const result = dSeeder._private._combineIntoMarriages(members);
// Assert
assert.equal(result[0].marriages[0].spouse?.id, parent2.id);
assert.equal(result[0].marriages[0].children[0].id, child.id);
assert.equal(result[0].marriages[0].children.length, 1);
})
it('gets 1 parent with multiple spouses and children, returns array of 1 tree node with multiple marriages to spouses with children', () => {
// Arrange
const parent = {
id: 1,
name: 'Parent',
parent1Id: null,
parent2Id: null
};
const spouse1 = {
id: 2,
name: 'Spouse1',
parent1Id: null,
parent2Id: null
};
const spouse2 = {
id: 3,
name: 'Spouse2',
parent1Id: null,
parent2Id: null
};
const child1 = {
id: 4,
name: 'Child1',
parent1Id: 1,
parent2Id: 2
};
const child2 = {
id: 5,
name: 'Child2',
parent1Id: 1,
parent2Id: 3
};
const members = [parent, spouse1, spouse2, child1, child2];
// Act
const result = dSeeder._private._combineIntoMarriages(members);
// Assert
assert.equal(result.length, 1);
assert.equal(result[0].marriages.length, 2);
assert.equal(result[0].marriages[0].spouse?.id, spouse1.id);
assert.equal(result[0].marriages[0].children[0].id, child1.id);
assert.equal(result[0].marriages[0].children.length, 1);
assert.equal(result[0].marriages[1].spouse?.id, spouse2.id);
assert.equal(result[0].marriages[1].children[0].id, child2.id);
assert.equal(result[0].marriages[1].children.length, 1);
})
it('gets 2 children with 2 parents in different order, returns array of 1 tree node with 1 marriage to spouse with 2 children', () => {
// Arrange
const parent1 = {
id: 1,
name: 'Parent1',
parent1Id: null,
parent2Id: null
};
const parent2 = {
id: 2,
name: 'Parent2',
parent1Id: null,
parent2Id: null
};
const child1 = {
id: 3,
name: 'Child1',
parent1Id: 1,
parent2Id: 2
};
const child2 = {
id: 4,
name: 'Child2',
parent1Id: 2,
parent2Id: 1
};
const members = [parent1, parent2, child1, child2];
// Act
const result = dSeeder._private._combineIntoMarriages(members);
// Assert
assert.equal(result.length, 1);
assert.equal(result[0].marriages[0].spouse?.id, parent2.id);
assert.equal(result[0].marriages[0].children[0].id, child1.id);
assert.equal(result[0].marriages[0].children[1].id, child2.id);
assert.equal(result[0].marriages[0].children.length, 2);
})
});
describe('_private._coalesce', () => {
it('gets empty array, should throw error', () => {
// Assert
assert.throws(() => dSeeder._private._coalesce([]));
})
it('gets 1 generation, should return valid tree', () => {
// Arrange
const node = new TreeNode(MockMembers.EdardStark);
const marriage = new TreeNodeMarriage();
marriage.spouse = new TreeNode(MockMembers.CatelynStark);
marriage.children = [
new TreeNode(MockMembers.AryaStark),
new TreeNode(MockMembers.BranStark),
new TreeNode(MockMembers.RickonStark),
new TreeNode(MockMembers.SansaStark)
];
node.marriages.push(marriage);
// Assert
const result = dSeeder._private._coalesce([node]);
// Assert
assert.equal(result.length, 1);
assert.equal(result[0].id, MockMembers.EdardStark.id);
assert.equal(result[0].marriages[0].spouse?.id, MockMembers.CatelynStark.id);
assert.deepEqual(result[0].marriages[0].children.map(child => child.id), marriage.children.map(child => child.id));
})
it('gets 2 generations, should return valid tree', () => {
// Arrange
const nedStarkNode = new TreeNode(MockMembers.EdardStark);
const nedStarkMarriage = new TreeNodeMarriage();
nedStarkMarriage.spouse = new TreeNode(MockMembers.CatelynStark);
nedStarkMarriage.children = [
new TreeNode(MockMembers.AryaStark),
new TreeNode(MockMembers.BranStark),
new TreeNode(MockMembers.RickonStark),
new TreeNode(MockMembers.SansaStark)
];
nedStarkNode.marriages.push(nedStarkMarriage);
const rickardStarkNode = new TreeNode(MockMembers.RickardStark);
const rickardStarkMarriage = new TreeNodeMarriage();
rickardStarkMarriage.spouse = new TreeNode(MockMembers.LyarraStark);
rickardStarkMarriage.children = [
new TreeNode(MockMembers.EdardStark),
new TreeNode(MockMembers.BenjenStark),
new TreeNode(MockMembers.BrandonStark),
new TreeNode(MockMembers.LyannaStark)
];
rickardStarkNode.marriages.push(rickardStarkMarriage);
// Assert
const result = dSeeder._private._coalesce([nedStarkNode, rickardStarkNode]);
// Assert
assert.equal(result.length, 1);
assert.equal(result[0].id, MockMembers.RickardStark.id);
assert.equal(result[0].marriages[0].spouse?.id, MockMembers.LyarraStark.id);
assert.deepEqual(result[0].marriages[0].children.map(child => child.id), rickardStarkMarriage.children.map(child => child.id));
const nedStarkInResult = result[0].marriages[0].children.find(child => child.id === MockMembers.EdardStark.id);
assert.equal(nedStarkInResult?.marriages[0].spouse?.id, MockMembers.CatelynStark.id);
assert.deepEqual(nedStarkInResult?.marriages[0].children.map(child => child.id), nedStarkMarriage.children.map(child => child.id));
})
it('descendent is listed as spouse in marriage, should pivot node on marriage and return valid tree', () => {
// Arrange
const gen1Node = new TreeNode(MockMembers.Gen1Parent1);
const gen1Marriage = new TreeNodeMarriage();
gen1Marriage.spouse = new TreeNode(MockMembers.Gen1Parent2);
gen1Marriage.children = [new TreeNode(MockMembers.Gen1ChildGen2Parent2)];
gen1Node.marriages.push(gen1Marriage);
const gen2Node = new TreeNode(MockMembers.Gen2Parent1);
const gen2Marriage = new TreeNodeMarriage();
gen2Marriage.spouse = new TreeNode(MockMembers.Gen1ChildGen2Parent2);
gen2Marriage.children = [new TreeNode(MockMembers.Gen2ChildGen3Parent2)];
gen2Node.marriages.push(gen2Marriage);
// Act
const result = dSeeder._private._coalesce([gen1Node, gen2Node]);
// Assert
assert.equal(result.length, 1);
const gen1InResult = result[0];
assert.equal(gen1InResult.id, MockMembers.Gen1Parent1.id);
assert.equal(gen1InResult.marriages[0].spouse?.id, MockMembers.Gen1Parent2.id);
assert.deepEqual(gen1InResult.marriages[0].children.map(child => child.id), gen1Marriage.children.map(child => child.id));
const gen2InResult = gen1InResult.marriages[0].children.find(child => child.id === MockMembers.Gen1ChildGen2Parent2.id);
assert.equal(gen2InResult?.marriages[0].spouse?.id, MockMembers.Gen2Parent1.id);
assert.deepEqual(gen2InResult?.marriages[0].children.map(child => child.id), gen2Marriage.children.map(child => child.id));
})
it('gets 5 generations, should return valid tree', () => {
// Arrange
const gen1Node = new TreeNode(MockMembers.Gen1Parent1);
const gen1Marriage = new TreeNodeMarriage();
gen1Marriage.spouse = new TreeNode(MockMembers.Gen1Parent2);
gen1Marriage.children = [new TreeNode(MockMembers.Gen1ChildGen2Parent2)];
gen1Node.marriages.push(gen1Marriage);
const gen2Node = new TreeNode(MockMembers.Gen1ChildGen2Parent2);
const gen2Marriage = new TreeNodeMarriage();
gen2Marriage.spouse = new TreeNode(MockMembers.Gen2Parent1);
gen2Marriage.children = [new TreeNode(MockMembers.Gen2ChildGen3Parent2)];
gen2Node.marriages.push(gen2Marriage);
const gen3Node = new TreeNode(MockMembers.Gen2ChildGen3Parent2);
const gen3Marriage = new TreeNodeMarriage();
gen3Marriage.spouse = new TreeNode(MockMembers.Gen3Parent1);
gen3Marriage.children = [new TreeNode(MockMembers.Gen3ChildGen4Parent1)];
gen3Node.marriages.push(gen3Marriage);
const gen4Node = new TreeNode(MockMembers.Gen3ChildGen4Parent1);
const gen4Marriage = new TreeNodeMarriage();
gen4Marriage.spouse = new TreeNode(MockMembers.Gen4Parent2);
gen4Marriage.children = [new TreeNode(MockMembers.Gen4ChildGen5Parent1)];
gen4Node.marriages.push(gen4Marriage);
const gen5Node = new TreeNode(MockMembers.Gen4ChildGen5Parent1);
const gen5Marriage = new TreeNodeMarriage();
gen5Marriage.spouse = new TreeNode(MockMembers.Gen5Parent2);
gen5Marriage.children = [new TreeNode(MockMembers.Gen5Child)];
gen5Node.marriages.push(gen5Marriage);
// Assert
const result = dSeeder._private._coalesce([gen1Node, gen2Node, gen3Node, gen4Node, gen5Node]);
// Assert
assert.equal(result.length, 1);
const gen1InResult = result[0];
assert.equal(gen1InResult.id, MockMembers.Gen1Parent1.id);
assert.equal(gen1InResult.marriages[0].spouse?.id, MockMembers.Gen1Parent2.id);
assert.deepEqual(gen1InResult.marriages[0].children.map(child => child.id), gen1Marriage.children.map(child => child.id));
const gen2InResult = gen1InResult.marriages[0].children.find(child => child.id === MockMembers.Gen1ChildGen2Parent2.id);
assert.equal(gen2InResult?.marriages[0].spouse?.id, MockMembers.Gen2Parent1.id);
assert.deepEqual(gen2InResult?.marriages[0].children.map(child => child.id), gen2Marriage.children.map(child => child.id));
const gen3InResult = gen2InResult?.marriages[0].children.find(child => child.id === MockMembers.Gen2ChildGen3Parent2.id);
assert.equal(gen3InResult?.marriages[0].spouse?.id, MockMembers.Gen3Parent1.id);
assert.deepEqual(gen3InResult?.marriages[0].children.map(child => child.id), gen3Marriage.children.map(child => child.id));
const gen4InResult = gen3InResult?.marriages[0].children.find(child => child.id === MockMembers.Gen3ChildGen4Parent1.id);;
assert.equal(gen4InResult?.marriages[0].spouse?.id, MockMembers.Gen4Parent2.id);
assert.deepEqual(gen4InResult?.marriages[0].children.map(child => child.id), gen4Marriage.children.map(child => child.id));
const gen5InResult = gen4InResult?.marriages[0].children.find(child => child.id === MockMembers.Gen4ChildGen5Parent1.id);;
assert.equal(gen5InResult?.marriages[0].spouse?.id, MockMembers.Gen5Parent2.id);
assert.deepEqual(gen5InResult?.marriages[0].children.map(child => child.id), gen5Marriage.children.map(child => child.id));
})
it('gets multiple root nodes, should throw error', () => {
// Arrange
const nodes = [
new TreeNode(MockMembers.AryaStark),
new TreeNode(MockMembers.Parent1)
];
// Assert
assert.throws(() => dSeeder._private._coalesce(nodes));
})
it('get duplicate tree nodes, should throw error', () => {
// Arrange
const nodes = [
new TreeNode(MockMembers.AryaStark),
new TreeNode(MockMembers.AryaStark)
];
// Assert
assert.throws(() => dSeeder._private._coalesce(nodes));
})
});
describe('seed', () => {
it('gets empty array, should throw error', () => {
// Assert
assert.throws(() => dSeeder.seed([], 0));
})
it('gets dTree sample data, should return valid tree in JSON format', () => {
// Act
const result = dSeeder.seed(dTreeSampleMockMembers.getAll(), dTreeSampleMockMembers.NiclasSuperLongsurname.id);
// Assert
assert.typeOf<TreeNode[]>(result, 'array');
assert.equal(JSON.stringify(result), JSON.stringify(dTreeSampleData));
})
describe('target has no parents', () => {
it('target should have depthOffset 1', () => {
// Arrange
const targetId = MockMembers.Gen1Parent1.id;
// Act
const result = dSeeder.seed(testData, targetId);
// Assert
const gen1: TreeNode[] = result;
assert.include(gen1.map(node => node.id), targetId);
assert.isTrue(gen1.every((node) => node.depthOffset === 1));
})
it('target has spouse, target and spouse should have same depthOffset', () => {
// Arrange
const targetId = MockMembers.Gen1Parent1.id;
const spouseId = MockMembers.Gen1Parent2.id;
// Act
const result = dSeeder.seed(testData, targetId);
// Assert
const gen1: TreeNode[] = result;
assert.include(gen1.flatMap((node) => node.marriages.map((marriage) => marriage.spouse?.id)), spouseId);
const targetDepthOffset = gen1.find(node => node.id === targetId)?.depthOffset as number;
assert.isTrue(gen1.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.depthOffset === targetDepthOffset));
})
it('target has children, children should have depthOffset 1 more than target', () => {
// Arrange
const targetId = MockMembers.Gen1Parent1.id;
const childId = MockMembers.Gen1ChildGen2Parent2.id;
// Act
const result = dSeeder.seed(testData, targetId);
// Assert
const gen1: TreeNode[] = result;
const gen2 = gen1.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.include(gen2.map((node) => node.id), childId);
const targetDepthOffset = gen1.find(node => node.id === targetId)?.depthOffset as number;
assert.isTrue(gen2.every((node) => node.depthOffset === targetDepthOffset + 1));
})
})
describe('target has parents', () => {
it('target should have depthOffset 2', () => {
// Arrange
const targetId = MockMembers.Gen1ChildGen2Parent2.id;
// Act
const result = dSeeder.seed(testData, targetId);
// Assert
const gen1: TreeNode[] = result;
const gen2 = gen1.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.include(gen2.map(node => node.id), targetId);
assert.isTrue(gen2.every((node) => node.depthOffset === 2));
})
it('target has spouse, target and spouse should have same depthOffset', () => {
// Arrange
const targetId = MockMembers.Gen1ChildGen2Parent2.id;
const spouseId = MockMembers.Gen2Parent1.id;
// Act
const result = dSeeder.seed(testData, targetId);
// Assert
const gen1: TreeNode[] = result;
const gen2 = gen1.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.include(gen2.flatMap((node) => node.marriages.map((marriage) => marriage.spouse?.id)), spouseId);
const targetDepthOffset = gen2.find(node => node.id === targetId)?.depthOffset as number;
assert.isTrue(gen2.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.depthOffset === targetDepthOffset));
})
it('target has children, children should have depthOffset 1 more than target', () => {
// Arrange
const targetId = MockMembers.Gen1ChildGen2Parent2.id;
const childId = MockMembers.Gen2ChildGen3Parent2.id;
// Act
const result = dSeeder.seed(testData, targetId);
// Assert
const gen1: TreeNode[] = result;
const gen2 = gen1.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
const gen3 = gen2.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.include(gen3.map((node) => node.id), childId);
const targetDepthOffset = gen2.find(node => node.id === targetId)?.depthOffset as number;
assert.isTrue(gen3.every((node) => node.depthOffset === targetDepthOffset + 1));
})
})
it('should add depthOffset to each generation', () => {
// Act
const result = dSeeder.seed(testData, MockMembers.Gen1ChildGen2Parent2.id);
// Assert
const gen1: TreeNode[] = result;
assert.isTrue(gen1.every((node) => node.depthOffset === 1));
assert.isTrue(gen1.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.depthOffset == 1));
const gen2 = gen1.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.isTrue(gen2.every((node) => node.depthOffset === 2));
assert.isTrue(gen2.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.depthOffset === 2));
const gen3 = gen2.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.isTrue(gen3.every((node) => node.depthOffset === 3));
assert.isTrue(gen3.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.depthOffset === 3));
const gen4 = gen3.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.isTrue(gen4.every((node) => node.depthOffset === 4));
assert.isTrue(gen4.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.depthOffset === 4));
const gen5 = gen4.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.isTrue(gen5.every((node) => node.depthOffset === 5));
assert.isTrue(gen5.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.depthOffset === 5));
})
describe('gets SeederOptions object', () => {
it('gets callback for class, populates class for each node', () => {
// Arrange
const options: SeederOptions = {
class: (member: Member) => member.id.toString()
};
// Act
const result = dSeeder.seed(testData, MockMembers.Gen1ChildGen2Parent2.id, options);
// Assert
const gen1: TreeNode[] = result;
assert.isTrue(gen1.every((node) => node.class === node?.id.toString()));
assert.isTrue(gen1.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.class == spouse?.id.toString()));
const gen2 = gen1.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.isTrue(gen2.every((node) => node.depthOffset === 2));
assert.isTrue(gen2.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.class === spouse?.id.toString()));
const gen3 = gen2.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.isTrue(gen3.every((node) => node.depthOffset === 3));
assert.isTrue(gen3.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.class === spouse?.id.toString()));
const gen4 = gen3.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.isTrue(gen4.every((node) => node.depthOffset === 4));
assert.isTrue(gen4.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.class === spouse?.id.toString()));
const gen5 = gen4.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.isTrue(gen5.every((node) => node.depthOffset === 5));
assert.isTrue(gen5.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.class === spouse?.id.toString()));
})
it('gets no callback for class, class should be an empty string for each node', () => {
// Act
const result = dSeeder.seed(testData, MockMembers.Gen1ChildGen2Parent2.id, {});
// Assert
const gen1: TreeNode[] = result;
assert.isTrue(gen1.every((node) => node.class === ''));
assert.isTrue(gen1.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.class == ''));
const gen2 = gen1.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.isTrue(gen2.every((node) => node.depthOffset === 2));
assert.isTrue(gen2.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.class === ''));
const gen3 = gen2.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.isTrue(gen3.every((node) => node.depthOffset === 3));
assert.isTrue(gen3.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.class === ''));
const gen4 = gen3.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.isTrue(gen4.every((node) => node.depthOffset === 4));
assert.isTrue(gen4.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.class === ''));
const gen5 = gen4.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.isTrue(gen5.every((node) => node.depthOffset === 5));
assert.isTrue(gen5.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.class === ''));
})
it('gets callback for textClass, populates textClass for each node', () => {
// Arrange
const options: SeederOptions = {
textClass: (member: Member) => member.id.toString()
};
// Act
const result = dSeeder.seed(testData, MockMembers.Gen1ChildGen2Parent2.id, options);
// Assert
const gen1: TreeNode[] = result;
assert.isTrue(gen1.every((node) => node.textClass === node.id.toString()));
assert.isTrue(gen1.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.textClass == spouse?.id.toString()));
const gen2 = gen1.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.isTrue(gen2.every((node) => node.depthOffset === 2));
assert.isTrue(gen2.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.textClass === spouse?.id.toString()));
const gen3 = gen2.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.isTrue(gen3.every((node) => node.depthOffset === 3));
assert.isTrue(gen3.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.textClass === spouse?.id.toString()));
const gen4 = gen3.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.isTrue(gen4.every((node) => node.depthOffset === 4));
assert.isTrue(gen4.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.textClass === spouse?.id.toString()));
const gen5 = gen4.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.isTrue(gen5.every((node) => node.depthOffset === 5));
assert.isTrue(gen5.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.textClass === spouse?.id.toString()));
})
it('gets no callback for textClass, textClass should be an empty string for each node', () => {
// Act
const result = dSeeder.seed(testData, MockMembers.Gen1ChildGen2Parent2.id, {});
// Assert
const gen1: TreeNode[] = result;
assert.isTrue(gen1.every((node) => node.textClass === ''));
assert.isTrue(gen1.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.textClass == ''));
const gen2 = gen1.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.isTrue(gen2.every((node) => node.depthOffset === 2));
assert.isTrue(gen2.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.textClass === ''));
const gen3 = gen2.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.isTrue(gen3.every((node) => node.depthOffset === 3));
assert.isTrue(gen3.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.textClass === ''));
const gen4 = gen3.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.isTrue(gen4.every((node) => node.depthOffset === 4));
assert.isTrue(gen4.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.textClass === ''));
const gen5 = gen4.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.isTrue(gen5.every((node) => node.depthOffset === 5));
assert.isTrue(gen5.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.textClass === ''));
})
it('gets callback for extra, populates extra for each node', () => {
// Arrange
const options: SeederOptions = {
extra: (member: Member) => {
return { id: member.id.toString() };
}
};
// Act
const result = dSeeder.seed(testData, MockMembers.Gen1ChildGen2Parent2.id, options);
// Assert
const gen1: TreeNode[] = result;
assert.isTrue(gen1.every((node) => node.extra.toString() === { id: node.id.toString() }.toString()));
assert.isTrue(gen1.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.extra.toString() === { id: spouse?.id.toString() }.toString()));
const gen2 = gen1.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.isTrue(gen2.every((node) => node.depthOffset === 2));
assert.isTrue(gen2.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.extra.toString() === { id: spouse?.id.toString() }.toString()));
const gen3 = gen2.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.isTrue(gen3.every((node) => node.depthOffset === 3));
assert.isTrue(gen3.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.extra.toString() === { id: spouse?.id.toString() }.toString()));
const gen4 = gen3.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.isTrue(gen4.every((node) => node.depthOffset === 4));
assert.isTrue(gen4.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.extra.toString() === { id: spouse?.id.toString() }.toString()));
const gen5 = gen4.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.isTrue(gen5.every((node) => node.depthOffset === 5));
assert.isTrue(gen5.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) => spouse?.extra.toString() === { id: spouse?.id.toString() }.toString()));
})
it('gets no callback for extra, extra should be an empty object for each node', () => {
// Act
const result = dSeeder.seed(testData, MockMembers.Gen1ChildGen2Parent2.id, {});
// Assert
const gen1: TreeNode[] = result;
assert.isTrue(gen1.every((node) => Object.keys(node.extra).length === 0));
assert.isTrue(gen1.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) =>
spouse?.extra == undefined
? false
: Object.keys(spouse.extra).length === 0));
const gen2 = gen1.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.isTrue(gen2.every((node) => node.depthOffset === 2));
assert.isTrue(gen2.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) =>
spouse?.extra == undefined
? false
: Object.keys(spouse.extra).length === 0));
const gen3 = gen2.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.isTrue(gen3.every((node) => node.depthOffset === 3));
assert.isTrue(gen3.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) =>
spouse?.extra == undefined
? false
: Object.keys(spouse.extra).length === 0));
const gen4 = gen3.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.isTrue(gen4.every((node) => node.depthOffset === 4));
assert.isTrue(gen4.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) =>
spouse?.extra == undefined
? false
: Object.keys(spouse.extra).length === 0));
const gen5 = gen4.flatMap((node) => node.marriages.flatMap((marriage) => marriage.children));
assert.isTrue(gen5.every((node) => node.depthOffset === 5));
assert.isTrue(gen5.flatMap((node) => node.marriages.map((marriage) => marriage.spouse)).every((spouse) =>
spouse?.extra == undefined
? false
: Object.keys(spouse.extra).length === 0));
})
})
});