-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathIncludeTests.swift
More file actions
737 lines (543 loc) · 29.8 KB
/
IncludeTests.swift
File metadata and controls
737 lines (543 loc) · 29.8 KB
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
import XCTest
@testable import JSONAPI
class IncludedTests: XCTestCase {
func test_zeroIncludes_init() {
let includes = Includes()
XCTAssertEqual(includes.count, 0)
}
func test_zeroIncludes() {
let includes = decoded(type: Includes<NoIncludes>.self,
data: two_same_type_includes)
XCTAssertEqual(includes.count, 0)
}
func test_zeroIncludes_encode() {
XCTAssertThrowsError(try JSONEncoder().encode(decoded(type: Includes<NoIncludes>.self,
data: two_same_type_includes)))
}
func test_OneInclude() {
let includes = decoded(type: Includes<Include1<TestEntity>>.self,
data: one_include)
XCTAssertEqual(includes[TestEntity.self].count, 1)
}
func test_OneInclude_encode() {
test_DecodeEncodeEquality(type: Includes<Include1<TestEntity>>.self,
data: one_include)
}
func test_TwoSameIncludes() {
let includes = decoded(type: Includes<Include1<TestEntity>>.self,
data: two_same_type_includes)
XCTAssertEqual(includes[TestEntity.self].count, 2)
}
func test_TwoSameIncludes_encode() {
test_DecodeEncodeEquality(type: Includes<Include1<TestEntity>>.self,
data: two_same_type_includes)
}
func test_TwoDifferentIncludes() {
let includes = decoded(type: Includes<Include2<TestEntity, TestEntity2>>.self,
data: two_different_type_includes)
XCTAssertEqual(includes[TestEntity.self].count, 1)
XCTAssertEqual(includes[TestEntity2.self].count, 1)
}
func test_TwoDifferentIncludes_encode() {
test_DecodeEncodeEquality(type: Includes<Include2<TestEntity, TestEntity2>>.self,
data: two_different_type_includes)
}
func test_ThreeDifferentIncludes() {
let includes = decoded(type: Includes<Include3<TestEntity, TestEntity2, TestEntity4>>.self,
data: three_different_type_includes)
XCTAssertEqual(includes[TestEntity.self].count, 1)
XCTAssertEqual(includes[TestEntity2.self].count, 1)
XCTAssertEqual(includes[TestEntity4.self].count, 1)
}
func test_ThreeDifferentIncludes_encode() {
test_DecodeEncodeEquality(type: Includes<Include3<TestEntity, TestEntity2, TestEntity4>>.self,
data: three_different_type_includes)
}
func test_OneKnownAndTwoGenericIncludes() {
let includes = decoded(type: Includes<Include2<TestEntity, TestEntityOther>>.self,
data: three_different_type_includes)
XCTAssertEqual(includes[TestEntity.self].count, 1)
XCTAssertEqual(includes[TestEntityOther.self].count, 2)
XCTAssert(includes[TestEntityOther.self].contains { $0.type == "test_entity2"})
XCTAssert(includes[TestEntityOther.self].contains { $0.type == "test_entity4"})
}
func test_FourDifferentIncludes() {
let includes = decoded(type: Includes<Include4<TestEntity, TestEntity2, TestEntity4, TestEntity6>>.self,
data: four_different_type_includes)
XCTAssertEqual(includes[TestEntity.self].count, 1)
XCTAssertEqual(includes[TestEntity2.self].count, 1)
XCTAssertEqual(includes[TestEntity4.self].count, 1)
XCTAssertEqual(includes[TestEntity6.self].count, 1)
}
func test_FourDifferentIncludes_encode() {
test_DecodeEncodeEquality(type: Includes<Include4<TestEntity, TestEntity2, TestEntity4, TestEntity6>>.self,
data: four_different_type_includes)
}
func test_FiveDifferentIncludes() {
let includes = decoded(type: Includes<Include5<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity6>>.self,
data: five_different_type_includes)
XCTAssertEqual(includes[TestEntity.self].count, 1)
XCTAssertEqual(includes[TestEntity2.self].count, 1)
XCTAssertEqual(includes[TestEntity3.self].count, 1)
XCTAssertEqual(includes[TestEntity4.self].count, 1)
XCTAssertEqual(includes[TestEntity6.self].count, 1)
}
func test_FiveDifferentIncludes_encode() {
test_DecodeEncodeEquality(type: Includes<Include5<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity6>>.self,
data: five_different_type_includes)
}
func test_SixDifferentIncludes() {
let includes = decoded(type: Includes<Include6<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6>>.self,
data: six_different_type_includes)
XCTAssertEqual(includes[TestEntity.self].count, 1)
XCTAssertEqual(includes[TestEntity2.self].count, 1)
XCTAssertEqual(includes[TestEntity3.self].count, 1)
XCTAssertEqual(includes[TestEntity4.self].count, 1)
XCTAssertEqual(includes[TestEntity5.self].count, 1)
XCTAssertEqual(includes[TestEntity6.self].count, 1)
}
func test_SixDifferentIncludes_encode() {
test_DecodeEncodeEquality(type: Includes<Include6<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6>>.self,
data: six_different_type_includes)
}
func test_SevenDifferentIncludes() {
let includes = decoded(type: Includes<Include7<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7>>.self,
data: seven_different_type_includes)
XCTAssertEqual(includes[TestEntity.self].count, 1)
XCTAssertEqual(includes[TestEntity2.self].count, 1)
XCTAssertEqual(includes[TestEntity3.self].count, 1)
XCTAssertEqual(includes[TestEntity4.self].count, 1)
XCTAssertEqual(includes[TestEntity5.self].count, 1)
XCTAssertEqual(includes[TestEntity6.self].count, 1)
XCTAssertEqual(includes[TestEntity7.self].count, 1)
}
func test_SevenDifferentIncludes_encode() {
test_DecodeEncodeEquality(type: Includes<Include7<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7>>.self,
data: seven_different_type_includes)
}
func test_EightDifferentIncludes() {
let includes = decoded(type: Includes<Include8<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8>>.self,
data: eight_different_type_includes)
XCTAssertEqual(includes[TestEntity.self].count, 1)
XCTAssertEqual(includes[TestEntity2.self].count, 1)
XCTAssertEqual(includes[TestEntity3.self].count, 1)
XCTAssertEqual(includes[TestEntity4.self].count, 1)
XCTAssertEqual(includes[TestEntity5.self].count, 1)
XCTAssertEqual(includes[TestEntity6.self].count, 1)
XCTAssertEqual(includes[TestEntity7.self].count, 1)
XCTAssertEqual(includes[TestEntity8.self].count, 1)
}
func test_EightDifferentIncludes_encode() {
test_DecodeEncodeEquality(type: Includes<Include8<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8>>.self,
data: eight_different_type_includes)
}
func test_NineDifferentIncludes() {
let includes = decoded(type: Includes<Include9<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8, TestEntity9>>.self,
data: nine_different_type_includes)
XCTAssertEqual(includes[TestEntity.self].count, 1)
XCTAssertEqual(includes[TestEntity2.self].count, 1)
XCTAssertEqual(includes[TestEntity3.self].count, 1)
XCTAssertEqual(includes[TestEntity4.self].count, 1)
XCTAssertEqual(includes[TestEntity5.self].count, 1)
XCTAssertEqual(includes[TestEntity6.self].count, 1)
XCTAssertEqual(includes[TestEntity7.self].count, 1)
XCTAssertEqual(includes[TestEntity8.self].count, 1)
XCTAssertEqual(includes[TestEntity9.self].count, 1)
}
func test_NineDifferentIncludes_encode() {
test_DecodeEncodeEquality(type: Includes<Include9<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8, TestEntity9>>.self,
data: nine_different_type_includes)
}
func test_TenDifferentIncludes() {
let includes = decoded(type: Includes<Include10<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8, TestEntity9, TestEntity10>>.self,
data: ten_different_type_includes)
XCTAssertEqual(includes[TestEntity.self].count, 1)
XCTAssertEqual(includes[TestEntity2.self].count, 1)
XCTAssertEqual(includes[TestEntity3.self].count, 1)
XCTAssertEqual(includes[TestEntity4.self].count, 1)
XCTAssertEqual(includes[TestEntity5.self].count, 1)
XCTAssertEqual(includes[TestEntity6.self].count, 1)
XCTAssertEqual(includes[TestEntity7.self].count, 1)
XCTAssertEqual(includes[TestEntity8.self].count, 1)
XCTAssertEqual(includes[TestEntity9.self].count, 1)
XCTAssertEqual(includes[TestEntity10.self].count, 1)
}
func test_TenDifferentIncludes_encode() {
test_DecodeEncodeEquality(type: Includes<Include10<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8, TestEntity9, TestEntity10>>.self,
data: ten_different_type_includes)
}
func test_ElevenDifferentIncludes() {
let includes = decoded(type: Includes<Include11<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8, TestEntity9, TestEntity10, TestEntity11>>.self,
data: eleven_different_type_includes)
XCTAssertEqual(includes[TestEntity.self].count, 1)
XCTAssertEqual(includes[TestEntity2.self].count, 1)
XCTAssertEqual(includes[TestEntity3.self].count, 1)
XCTAssertEqual(includes[TestEntity4.self].count, 1)
XCTAssertEqual(includes[TestEntity5.self].count, 1)
XCTAssertEqual(includes[TestEntity6.self].count, 1)
XCTAssertEqual(includes[TestEntity7.self].count, 1)
XCTAssertEqual(includes[TestEntity8.self].count, 1)
XCTAssertEqual(includes[TestEntity9.self].count, 1)
XCTAssertEqual(includes[TestEntity10.self].count, 1)
XCTAssertEqual(includes[TestEntity11.self].count, 1)
}
func test_ElevenDifferentIncludes_encode() {
test_DecodeEncodeEquality(type: Includes<Include11<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8, TestEntity9, TestEntity10, TestEntity11>>.self,
data: eleven_different_type_includes)
}
func test_TwelveDifferentIncludes() {
let includes = decoded(type: Includes<Include12<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8, TestEntity9, TestEntity10, TestEntity11, TestEntity12>>.self,
data: twelve_different_type_includes)
XCTAssertEqual(includes[TestEntity.self].count, 1)
XCTAssertEqual(includes[TestEntity2.self].count, 1)
XCTAssertEqual(includes[TestEntity3.self].count, 1)
XCTAssertEqual(includes[TestEntity4.self].count, 1)
XCTAssertEqual(includes[TestEntity5.self].count, 1)
XCTAssertEqual(includes[TestEntity6.self].count, 1)
XCTAssertEqual(includes[TestEntity7.self].count, 1)
XCTAssertEqual(includes[TestEntity8.self].count, 1)
XCTAssertEqual(includes[TestEntity9.self].count, 1)
XCTAssertEqual(includes[TestEntity10.self].count, 1)
XCTAssertEqual(includes[TestEntity11.self].count, 1)
XCTAssertEqual(includes[TestEntity12.self].count, 1)
}
func test_TwelveDifferentIncludes_encode() {
test_DecodeEncodeEquality(type: Includes<Include12<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8, TestEntity9, TestEntity10, TestEntity11, TestEntity12>>.self,
data: twelve_different_type_includes)
}
func test_ThirteenDifferentIncludes() {
let includes = decoded(type: Includes<Include13<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8, TestEntity9, TestEntity10, TestEntity11, TestEntity12, TestEntity13>>.self,
data: thirteen_different_type_includes)
XCTAssertEqual(includes[TestEntity.self].count, 1)
XCTAssertEqual(includes[TestEntity2.self].count, 1)
XCTAssertEqual(includes[TestEntity3.self].count, 1)
XCTAssertEqual(includes[TestEntity4.self].count, 1)
XCTAssertEqual(includes[TestEntity5.self].count, 1)
XCTAssertEqual(includes[TestEntity6.self].count, 1)
XCTAssertEqual(includes[TestEntity7.self].count, 1)
XCTAssertEqual(includes[TestEntity8.self].count, 1)
XCTAssertEqual(includes[TestEntity9.self].count, 1)
XCTAssertEqual(includes[TestEntity10.self].count, 1)
XCTAssertEqual(includes[TestEntity11.self].count, 1)
XCTAssertEqual(includes[TestEntity12.self].count, 1)
XCTAssertEqual(includes[TestEntity13.self].count, 1)
}
func test_ThirteenDifferentIncludes_encode() {
test_DecodeEncodeEquality(type: Includes<Include13<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8, TestEntity9, TestEntity10, TestEntity11, TestEntity12, TestEntity13>>.self,
data: thirteen_different_type_includes)
}
func test_FourteenDifferentIncludes() {
let includes = decoded(type: Includes<Include14<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8, TestEntity9, TestEntity10, TestEntity11, TestEntity12, TestEntity13, TestEntity14>>.self,
data: fourteen_different_type_includes)
XCTAssertEqual(includes[TestEntity.self].count, 1)
XCTAssertEqual(includes[TestEntity2.self].count, 1)
XCTAssertEqual(includes[TestEntity3.self].count, 1)
XCTAssertEqual(includes[TestEntity4.self].count, 1)
XCTAssertEqual(includes[TestEntity5.self].count, 1)
XCTAssertEqual(includes[TestEntity6.self].count, 1)
XCTAssertEqual(includes[TestEntity7.self].count, 1)
XCTAssertEqual(includes[TestEntity8.self].count, 1)
XCTAssertEqual(includes[TestEntity9.self].count, 1)
XCTAssertEqual(includes[TestEntity10.self].count, 1)
XCTAssertEqual(includes[TestEntity11.self].count, 1)
XCTAssertEqual(includes[TestEntity12.self].count, 1)
XCTAssertEqual(includes[TestEntity13.self].count, 1)
XCTAssertEqual(includes[TestEntity14.self].count, 1)
}
func test_FourteenDifferentIncludes_encode() {
test_DecodeEncodeEquality(type: Includes<Include14<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8, TestEntity9, TestEntity10, TestEntity11, TestEntity12, TestEntity13, TestEntity14>>.self,
data: fourteen_different_type_includes)
}
func test_FifteenDifferentIncludes() {
let includes = decoded(type: Includes<Include15<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8, TestEntity9, TestEntity10, TestEntity11, TestEntity12, TestEntity13, TestEntity14, TestEntity15>>.self,
data: fifteen_different_type_includes)
XCTAssertEqual(includes[TestEntity.self].count, 1)
XCTAssertEqual(includes[TestEntity2.self].count, 1)
XCTAssertEqual(includes[TestEntity3.self].count, 1)
XCTAssertEqual(includes[TestEntity4.self].count, 1)
XCTAssertEqual(includes[TestEntity5.self].count, 1)
XCTAssertEqual(includes[TestEntity6.self].count, 1)
XCTAssertEqual(includes[TestEntity7.self].count, 1)
XCTAssertEqual(includes[TestEntity8.self].count, 1)
XCTAssertEqual(includes[TestEntity9.self].count, 1)
XCTAssertEqual(includes[TestEntity10.self].count, 1)
XCTAssertEqual(includes[TestEntity11.self].count, 1)
XCTAssertEqual(includes[TestEntity12.self].count, 1)
XCTAssertEqual(includes[TestEntity13.self].count, 1)
XCTAssertEqual(includes[TestEntity14.self].count, 1)
XCTAssertEqual(includes[TestEntity15.self].count, 1)
}
func test_FifteenDifferentIncludes_encode() {
test_DecodeEncodeEquality(type: Includes<Include15<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8, TestEntity9, TestEntity10, TestEntity11, TestEntity12, TestEntity13, TestEntity14, TestEntity15>>.self,
data: fifteen_different_type_includes)
}
}
// MARK: - Appending
extension IncludedTests {
func test_appending() {
let include1 = Includes<Include2<TestEntity8, TestEntity9>>(values: [.init(TestEntity8(attributes: .none, relationships: .none, meta: .none, links: .none)), .init(TestEntity9(attributes: .none, relationships: .none, meta: .none, links: .none)), .init(TestEntity8(attributes: .none, relationships: .none, meta: .none, links: .none))])
let include2 = Includes<Include2<TestEntity8, TestEntity9>>(values: [.init(TestEntity8(attributes: .none, relationships: .none, meta: .none, links: .none)), .init(TestEntity9(attributes: .none, relationships: .none, meta: .none, links: .none)), .init(TestEntity8(attributes: .none, relationships: .none, meta: .none, links: .none))])
let combined = include1 + include2
XCTAssertEqual(combined.values, include1.values + include2.values)
}
}
// MARK: - Sparse Fieldsets
extension IncludedTests {
func test_OneSparseIncludeType() {
let include1 = TestEntity(attributes: .init(foo: "hello",
bar: 10),
relationships: .none,
meta: .none,
links: .none)
.sparse(with: [.foo])
let includes: Includes<Include1<TestEntity.SparseType>> = .init(values: [.init(include1)])
let encoded = try! JSONEncoder().encode(includes)
let deserialized = try! JSONSerialization.jsonObject(with: encoded,
options: [])
let deserializedObj = deserialized as? [Any]
XCTAssertEqual(deserializedObj?.count, 1)
guard let deserializedObj1 = deserializedObj?.first as? [String: Any] else {
XCTFail("Expected to deserialize one object from array")
return
}
XCTAssertNotNil(deserializedObj1["id"])
XCTAssertEqual(deserializedObj1["id"] as? String, include1.resourceObject.id.rawValue)
XCTAssertNotNil(deserializedObj1["type"])
XCTAssertEqual(deserializedObj1["type"] as? String, TestEntity.jsonType)
XCTAssertEqual((deserializedObj1["attributes"] as? [String: Any])?.count, 1)
XCTAssertEqual((deserializedObj1["attributes"] as? [String: Any])?["foo"] as? String, "hello")
XCTAssertNil(deserializedObj1["relationships"])
}
func test_TwoSparseIncludeTypes() {
let include1 = TestEntity(attributes: .init(foo: "hello",
bar: 10),
relationships: .none,
meta: .none,
links: .none)
.sparse(with: [.foo])
let include2 = TestEntity2(attributes: .init(foo: "world",
bar: 2),
relationships: .init(entity1: "1234"),
meta: .none,
links: .none)
.sparse(with: [.bar])
let includes: Includes<Include2<TestEntity.SparseType, TestEntity2.SparseType>> = .init(values: [.init(include1), .init(include2)])
let encoded = try! JSONEncoder().encode(includes)
let deserialized = try! JSONSerialization.jsonObject(with: encoded,
options: [])
let deserializedObj = deserialized as? [Any]
XCTAssertEqual(deserializedObj?.count, 2)
guard let deserializedObj1 = deserializedObj?.first as? [String: Any],
let deserializedObj2 = deserializedObj?.last as? [String: Any] else {
XCTFail("Expected to deserialize two objects from array")
return
}
// first include
XCTAssertNotNil(deserializedObj1["id"])
XCTAssertEqual(deserializedObj1["id"] as? String, include1.resourceObject.id.rawValue)
XCTAssertNotNil(deserializedObj1["type"])
XCTAssertEqual(deserializedObj1["type"] as? String, TestEntity.jsonType)
XCTAssertEqual((deserializedObj1["attributes"] as? [String: Any])?.count, 1)
XCTAssertEqual((deserializedObj1["attributes"] as? [String: Any])?["foo"] as? String, "hello")
XCTAssertNil(deserializedObj1["relationships"])
// second include
XCTAssertNotNil(deserializedObj2["id"])
XCTAssertEqual(deserializedObj2["id"] as? String, include2.resourceObject.id.rawValue)
XCTAssertNotNil(deserializedObj2["type"])
XCTAssertEqual(deserializedObj2["type"] as? String, TestEntity2.jsonType)
XCTAssertEqual((deserializedObj2["attributes"] as? [String: Any])?.count, 1)
XCTAssertEqual((deserializedObj2["attributes"] as? [String: Any])?["bar"] as? Int, 2)
XCTAssertNotNil(deserializedObj2["relationships"])
XCTAssertNotNil((deserializedObj2["relationships"] as? [String: Any])?["entity1"])
}
func test_ComboSparseAndFullIncludeTypes() {
let include1 = TestEntity(attributes: .init(foo: "hello",
bar: 10),
relationships: .none,
meta: .none,
links: .none)
.sparse(with: [.foo])
let include2 = TestEntity2(attributes: .init(foo: "world",
bar: 2),
relationships: .init(entity1: "1234"),
meta: .none,
links: .none)
let includes: Includes<Include2<TestEntity.SparseType, TestEntity2>> = .init(values: [.init(include1), .init(include2)])
let encoded = try! JSONEncoder().encode(includes)
let deserialized = try! JSONSerialization.jsonObject(with: encoded,
options: [])
let deserializedObj = deserialized as? [Any]
XCTAssertEqual(deserializedObj?.count, 2)
guard let deserializedObj1 = deserializedObj?.first as? [String: Any],
let deserializedObj2 = deserializedObj?.last as? [String: Any] else {
XCTFail("Expected to deserialize two objects from array")
return
}
// first include
XCTAssertNotNil(deserializedObj1["id"])
XCTAssertEqual(deserializedObj1["id"] as? String, include1.resourceObject.id.rawValue)
XCTAssertNotNil(deserializedObj1["type"])
XCTAssertEqual(deserializedObj1["type"] as? String, TestEntity.jsonType)
XCTAssertEqual((deserializedObj1["attributes"] as? [String: Any])?.count, 1)
XCTAssertEqual((deserializedObj1["attributes"] as? [String: Any])?["foo"] as? String, "hello")
XCTAssertNil(deserializedObj1["relationships"])
// second include
XCTAssertNotNil(deserializedObj2["id"])
XCTAssertEqual(deserializedObj2["id"] as? String, include2.id.rawValue)
XCTAssertNotNil(deserializedObj2["type"])
XCTAssertEqual(deserializedObj2["type"] as? String, TestEntity2.jsonType)
XCTAssertEqual((deserializedObj2["attributes"] as? [String: Any])?.count, 2)
XCTAssertEqual((deserializedObj2["attributes"] as? [String: Any])?["foo"] as? String, "world")
XCTAssertEqual((deserializedObj2["attributes"] as? [String: Any])?["bar"] as? Int, 2)
XCTAssertNotNil(deserializedObj2["relationships"])
XCTAssertNotNil((deserializedObj2["relationships"] as? [String: Any])?["entity1"])
}
}
// MARK: - Test types
extension IncludedTests {
enum TestEntityType: ResourceObjectDescription {
typealias Relationships = NoRelationships
public static var jsonType: String { return "test_entity1" }
public struct Attributes: JSONAPI.SparsableAttributes {
let foo: Attribute<String>
let bar: Attribute<Int>
public enum CodingKeys: String, Equatable, CodingKey {
case foo
case bar
}
}
}
typealias TestEntity = BasicEntity<TestEntityType>
enum TestEntityType2: ResourceObjectDescription {
public static var jsonType: String { return "test_entity2" }
public struct Relationships: JSONAPI.Relationships {
let entity1: ToOneRelationship<TestEntity, NoIdMetadata, NoMetadata, NoLinks>
}
public struct Attributes: JSONAPI.SparsableAttributes {
let foo: Attribute<String>
let bar: Attribute<Int>
public enum CodingKeys: String, Equatable, CodingKey {
case foo
case bar
}
}
}
typealias TestEntity2 = BasicEntity<TestEntityType2>
enum TestEntityType3: ResourceObjectDescription {
typealias Attributes = NoAttributes
public static var jsonType: String { return "test_entity3" }
public struct Relationships: JSONAPI.Relationships {
let entity1: ToOneRelationship<TestEntity, NoIdMetadata, NoMetadata, NoLinks>
let entity2: ToManyRelationship<TestEntity2, NoIdMetadata, NoMetadata, NoLinks>
}
}
typealias TestEntity3 = BasicEntity<TestEntityType3>
enum TestEntityType4: ResourceObjectDescription {
typealias Attributes = NoAttributes
typealias Relationships = NoRelationships
public static var jsonType: String { return "test_entity4" }
}
typealias TestEntity4 = BasicEntity<TestEntityType4>
enum TestEntityType5: ResourceObjectDescription {
typealias Attributes = NoAttributes
typealias Relationships = NoRelationships
public static var jsonType: String { return "test_entity5" }
}
typealias TestEntity5 = BasicEntity<TestEntityType5>
enum TestEntityType6: ResourceObjectDescription {
typealias Attributes = NoAttributes
public static var jsonType: String { return "test_entity6" }
struct Relationships: JSONAPI.Relationships {
let entity4: ToOneRelationship<TestEntity4, NoIdMetadata, NoMetadata, NoLinks>
}
}
typealias TestEntity6 = BasicEntity<TestEntityType6>
enum TestEntityType7: ResourceObjectDescription {
typealias Attributes = NoAttributes
public static var jsonType: String { return "test_entity7" }
typealias Relationships = NoRelationships
}
typealias TestEntity7 = BasicEntity<TestEntityType7>
enum TestEntityType8: ResourceObjectDescription {
typealias Attributes = NoAttributes
public static var jsonType: String { return "test_entity8" }
typealias Relationships = NoRelationships
}
typealias TestEntity8 = BasicEntity<TestEntityType8>
enum TestEntityType9: ResourceObjectDescription {
typealias Attributes = NoAttributes
public static var jsonType: String { return "test_entity9" }
typealias Relationships = NoRelationships
}
typealias TestEntity9 = BasicEntity<TestEntityType9>
enum TestEntityType10: ResourceObjectDescription {
typealias Attributes = NoAttributes
public static var jsonType: String { return "test_entity10" }
typealias Relationships = NoRelationships
}
typealias TestEntity10 = BasicEntity<TestEntityType10>
enum TestEntityType11: ResourceObjectDescription {
typealias Attributes = NoAttributes
public static var jsonType: String { return "test_entity11" }
typealias Relationships = NoRelationships
}
typealias TestEntity11 = BasicEntity<TestEntityType11>
enum TestEntityType12: ResourceObjectDescription {
typealias Attributes = NoAttributes
public static var jsonType: String { return "test_entity12" }
typealias Relationships = NoRelationships
}
typealias TestEntity12 = BasicEntity<TestEntityType12>
enum TestEntityType13: ResourceObjectDescription {
typealias Attributes = NoAttributes
public static var jsonType: String { return "test_entity13" }
typealias Relationships = NoRelationships
}
typealias TestEntity13 = BasicEntity<TestEntityType13>
enum TestEntityType14: ResourceObjectDescription {
typealias Attributes = NoAttributes
public static var jsonType: String { return "test_entity14" }
typealias Relationships = NoRelationships
}
typealias TestEntity14 = BasicEntity<TestEntityType14>
enum TestEntityType15: ResourceObjectDescription {
typealias Attributes = NoAttributes
public static var jsonType: String { return "test_entity15" }
typealias Relationships = NoRelationships
}
typealias TestEntity15 = BasicEntity<TestEntityType15>
enum TestEntityTypeOther: ResourceObjectProxyDescription {
public static var jsonType: String { return "_generic" }
typealias Attributes = NoAttributes
typealias Relationships = NoRelationships
}
struct TestEntityOther: ResourceObjectProxy, Codable {
typealias Description = TestEntityTypeOther
typealias EntityRawIdType = String
public let type : String
public let id : JSONAPI.Id<String, Self>
public let attributes = NoAttributes()
public let relationships = NoRelationships()
enum CodingKeys: CodingKey {
case id
case type
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let type: String
do {
type = try container.decode(String.self, forKey: .type)
} catch let error as DecodingError {
throw ResourceObjectDecodingError(error, jsonAPIType: Self.jsonType)
?? error
}
id = .init(rawValue: try container.decode(String.self, forKey: .id))
self.type = type
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(type, forKey: .type)
try container.encode(id, forKey: .id)
}
}
}