Skip to content

Commit 9521e2c

Browse files
committed
Add test for include failure not related to type mismatch
1 parent 914e8c7 commit 9521e2c

2 files changed

Lines changed: 103 additions & 48 deletions

File tree

Tests/JSONAPITests/Includes/IncludesDecodingErrorTests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,29 @@ final class IncludesDecodingErrorTests: XCTestCase {
8484
)
8585
}
8686
}
87+
88+
func test_missingProperty() {
89+
XCTAssertThrowsError(
90+
try testDecoder.decode(
91+
Includes<Include3<TestEntity, TestEntity2, TestEntity4>>.self,
92+
from: three_includes_one_missing_attributes
93+
)
94+
) { (error: Error) -> Void in
95+
XCTAssertEqual(
96+
(error as? IncludesDecodingError).map(String.init(describing:)),
97+
"""
98+
Out of 3 includes, the 3rd one failed to parse: \nCould not have been Include Type `test_entity1` because:
99+
found JSON:API type "test_entity2" but expected "test_entity1"
100+
101+
Could not have been Include Type `test_entity2` because:
102+
'foo' attribute is required and missing.
103+
104+
Could not have been Include Type `test_entity4` because:
105+
found JSON:API type "test_entity2" but expected "test_entity4"
106+
"""
107+
)
108+
}
109+
}
87110
}
88111

89112
// MARK: - Test Types

Tests/JSONAPITests/Includes/stubs/IncludeStubs.swift

Lines changed: 80 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,65 +7,65 @@
77

88
let one_include = """
99
[
10-
{
11-
"type": "test_entity1",
12-
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF",
13-
"attributes": {
14-
"foo": "Hello",
15-
"bar": 123
16-
}
17-
}
10+
{
11+
"type": "test_entity1",
12+
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF",
13+
"attributes": {
14+
"foo": "Hello",
15+
"bar": 123
16+
}
17+
}
1818
]
1919
""".data(using: .utf8)!
2020

2121
let two_same_type_includes = """
2222
[
23-
{
24-
"type": "test_entity1",
25-
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF",
26-
"attributes": {
27-
"foo": "Hello",
28-
"bar": 123
29-
}
30-
},
31-
{
32-
"type": "test_entity1",
33-
"id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333",
34-
"attributes": {
35-
"foo": "World",
36-
"bar": 456
37-
}
38-
}
23+
{
24+
"type": "test_entity1",
25+
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF",
26+
"attributes": {
27+
"foo": "Hello",
28+
"bar": 123
29+
}
30+
},
31+
{
32+
"type": "test_entity1",
33+
"id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333",
34+
"attributes": {
35+
"foo": "World",
36+
"bar": 456
37+
}
38+
}
3939
]
4040
4141
""".data(using: .utf8)!
4242

4343
let two_different_type_includes = """
4444
[
45-
{
46-
"type": "test_entity1",
47-
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF",
48-
"attributes": {
49-
"foo": "Hello",
50-
"bar": 123
51-
}
52-
},
53-
{
54-
"type": "test_entity2",
55-
"id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333",
56-
"attributes": {
57-
"foo": "World",
58-
"bar": 456
59-
},
60-
"relationships": {
61-
"entity1": {
62-
"data": {
63-
"type": "test_entity1",
64-
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"
65-
}
66-
}
67-
}
68-
}
45+
{
46+
"type": "test_entity1",
47+
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF",
48+
"attributes": {
49+
"foo": "Hello",
50+
"bar": 123
51+
}
52+
},
53+
{
54+
"type": "test_entity2",
55+
"id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333",
56+
"attributes": {
57+
"foo": "World",
58+
"bar": 456
59+
},
60+
"relationships": {
61+
"entity1": {
62+
"data": {
63+
"type": "test_entity1",
64+
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"
65+
}
66+
}
67+
}
68+
}
6969
]
7070
7171
""".data(using: .utf8)!
@@ -688,6 +688,38 @@ let eleven_different_type_includes = """
688688
""".data(using: .utf8)!
689689

690690

691+
let three_includes_one_missing_attributes = """
692+
[
693+
{
694+
"type": "test_entity1",
695+
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF",
696+
"attributes": {
697+
"foo": "Hello",
698+
"bar": 123
699+
}
700+
},
701+
{
702+
"type": "test_entity4",
703+
"id": "364B3B69-4DF1-467F-B52E-B0C9E44F666E"
704+
},
705+
{
706+
"type": "test_entity2",
707+
"id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333",
708+
"attributes": {
709+
"bar": 456
710+
},
711+
"relationships": {
712+
"entity1": {
713+
"data": {
714+
"type": "test_entity1",
715+
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"
716+
}
717+
}
718+
}
719+
}
720+
]
721+
""".data(using: .utf8)!
722+
691723
let six_includes_one_bad_type = """
692724
[
693725
{

0 commit comments

Comments
 (0)