Skip to content

Commit 6e6973c

Browse files
committed
Adding test coverage for Relationship Meta and Links
1 parent 4f52cc2 commit 6e6973c

3 files changed

Lines changed: 210 additions & 0 deletions

File tree

Tests/JSONAPITests/Entity/EntityTests.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,15 @@ class EntityTests: XCTestCase {
4545
let entity1 = TestEntity1(id: .init(rawValue: "wow"))
4646
let entity2 = TestEntity2(id: .init(rawValue: "cool"), relationships: .init(other: .init(entity: entity1)))
4747
let _ = TestEntity3(id: .init(rawValue: "3"), relationships: .init(others: .init(ids: [.init(rawValue: "10"), .init(rawValue: "20"), entity1.id])))
48+
let _ = TestEntity3(id: .init(rawValue: "3"), relationships: .init(others: .none))
4849
let _ = TestEntity4(id: .init(rawValue: "4"), attributes: .init(word: .init(value: "hello"), number: .init(value: 10), array: .init(value: [10.2, 10.3])), relationships: .init(other: entity2.pointer))
4950
let _ = TestEntity5(id: .init(rawValue: "5"), attributes: .init(floater: .init(value: 10.2)))
5051
let _ = TestEntity6(id: .init(rawValue: "6"), attributes: .init(here: .init(value: "here"), maybeHere: nil, maybeNull: .init(value: nil)))
5152
let _ = TestEntity7(id: .init(rawValue: "7"), attributes: .init(here: .init(value: "hello"), maybeHereMaybeNull: .init(value: "world")))
5253
XCTAssertNoThrow(try TestEntity8(id: .init(rawValue: "8"), attributes: .init(string: .init(value: "hello"), int: .init(value: 10), stringFromInt: .init(rawValue: 20), plus: .init(rawValue: 30), doubleFromInt: .init(rawValue: 32), omitted: nil, nullToString: .init(rawValue: nil))))
5354
let _ = TestEntity9(id: .init(rawValue: "9"), relationships: .init(one: entity1.pointer, nullableOne: nil))
55+
let _ = TestEntity9(id: .init(rawValue: "9"), relationships: .init(one: entity1.pointer, nullableOne: .init(entity: nil)))
56+
let _ = TestEntity9(id: .init(rawValue: "9"), relationships: .init(one: entity1.pointer, nullableOne: .init(entity: entity1, meta: .none, links: .none)))
5457
let e10id1 = TestEntity10.Identifier(rawValue: "hello")
5558
let e10id2 = TestEntity10.Id(rawValue: "world")
5659
let e10id3: TestEntity10.Id = "!"

Tests/JSONAPITests/Relationships/RelationshipTests.swift

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,46 @@ extension RelationshipTests {
4747
data: to_one_relationship)
4848
}
4949

50+
func test_ToOneRelationshipWithMeta() {
51+
let relationship = decoded(type: ToOneWithMeta.self,
52+
data: to_one_relationship_with_meta)
53+
54+
XCTAssertEqual(relationship.id.rawValue, "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF")
55+
XCTAssertEqual(relationship.meta.a, "hello")
56+
}
57+
58+
func test_ToOneRelationshipWithMeta_encode() {
59+
test_DecodeEncodeEquality(type: ToOneWithMeta.self,
60+
data: to_one_relationship_with_meta)
61+
}
62+
63+
func test_ToOneRelationshipWithLinks() {
64+
let relationship = decoded(type: ToOneWithLinks.self,
65+
data: to_one_relationship_with_links)
66+
67+
XCTAssertEqual(relationship.id.rawValue, "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF")
68+
XCTAssertEqual(relationship.links.b, .init(url: "world"))
69+
}
70+
71+
func test_ToOneRelationshipWithLinks_encode() {
72+
test_DecodeEncodeEquality(type: ToOneWithLinks.self,
73+
data: to_one_relationship_with_links)
74+
}
75+
76+
func test_ToOneRelationshipWithMetaAndLinks() {
77+
let relationship = decoded(type: ToOneWithMetaAndLinks.self,
78+
data: to_one_relationship_with_meta_and_links)
79+
80+
XCTAssertEqual(relationship.id.rawValue, "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF")
81+
XCTAssertEqual(relationship.meta.a, "hello")
82+
XCTAssertEqual(relationship.links.b, .init(url: "world"))
83+
}
84+
85+
func test_ToOneRelationshipWithMetaAndLinks_encode() {
86+
test_DecodeEncodeEquality(type: ToOneWithMetaAndLinks.self,
87+
data: to_one_relationship_with_meta_and_links)
88+
}
89+
5090
func test_ToManyRelationship() {
5191
let relationship = decoded(type: ToManyRelationship<TestEntity1, NoMetadata, NoLinks>.self,
5292
data: to_many_relationship)
@@ -58,6 +98,46 @@ extension RelationshipTests {
5898
test_DecodeEncodeEquality(type: ToManyRelationship<TestEntity1, NoMetadata, NoLinks>.self,
5999
data: to_many_relationship)
60100
}
101+
102+
func test_ToManyRelationshipWithMeta() {
103+
let relationship = decoded(type: ToManyWithMeta.self,
104+
data: to_many_relationship_with_meta)
105+
106+
XCTAssertEqual(relationship.ids.map { $0.rawValue }, ["2DF03B69-4B0A-467F-B52E-B0C9E44FCECF", "90F03B69-4DF1-467F-B52E-B0C9E44FC333", "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"])
107+
XCTAssertEqual(relationship.meta.a, "hello")
108+
}
109+
110+
func test_ToManyRelationshipWithMeta_encode() {
111+
test_DecodeEncodeEquality(type: ToManyWithMeta.self,
112+
data: to_many_relationship_with_meta)
113+
}
114+
115+
func test_ToManyRelationshipWithLinks() {
116+
let relationship = decoded(type: ToManyWithLinks.self,
117+
data: to_many_relationship_with_links)
118+
119+
XCTAssertEqual(relationship.ids.map { $0.rawValue }, ["2DF03B69-4B0A-467F-B52E-B0C9E44FCECF", "90F03B69-4DF1-467F-B52E-B0C9E44FC333", "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"])
120+
XCTAssertEqual(relationship.links.b, .init(url: "world"))
121+
}
122+
123+
func test_ToManyRelationshipWithLinks_encode() {
124+
test_DecodeEncodeEquality(type: ToManyWithLinks.self,
125+
data: to_many_relationship_with_links)
126+
}
127+
128+
func test_ToManyRelationshipWithMetaAndLinks() {
129+
let relationship = decoded(type: ToManyWithMetaAndLinks.self,
130+
data: to_many_relationship_with_meta_and_links)
131+
132+
XCTAssertEqual(relationship.ids.map { $0.rawValue }, ["2DF03B69-4B0A-467F-B52E-B0C9E44FCECF", "90F03B69-4DF1-467F-B52E-B0C9E44FC333", "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"])
133+
XCTAssertEqual(relationship.meta.a, "hello")
134+
XCTAssertEqual(relationship.links.b, .init(url: "world"))
135+
}
136+
137+
func test_ToManyRelationshipWithMetaAndLinks_encode() {
138+
test_DecodeEncodeEquality(type: ToManyWithMetaAndLinks.self,
139+
data: to_many_relationship_with_meta_and_links)
140+
}
61141
}
62142

63143
// MARK: Failure tests
@@ -82,4 +162,23 @@ extension RelationshipTests {
82162
}
83163

84164
typealias TestEntity1 = BasicEntity<TestEntityType1>
165+
166+
typealias ToOneWithMeta = ToOneRelationship<TestEntity1, TestMeta, NoLinks>
167+
168+
typealias ToOneWithLinks = ToOneRelationship<TestEntity1, NoMetadata, TestLinks>
169+
typealias ToOneWithMetaAndLinks = ToOneRelationship<TestEntity1, TestMeta, TestLinks>
170+
171+
typealias ToManyWithMeta = ToManyRelationship<TestEntity1, TestMeta, NoLinks>
172+
typealias ToManyWithLinks = ToManyRelationship<TestEntity1, NoMetadata, TestLinks>
173+
typealias ToManyWithMetaAndLinks = ToManyRelationship<TestEntity1, TestMeta, TestLinks>
174+
175+
struct TestMeta: JSONAPI.Meta {
176+
let a: String
177+
}
178+
179+
typealias TestLink = JSONAPI.Link<String, NoMetadata>
180+
181+
struct TestLinks: JSONAPI.Links {
182+
let b: TestLink
183+
}
85184
}

Tests/JSONAPITests/Relationships/stubs/RelationshipStubs.swift

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,45 @@ let to_one_relationship_type_mismatch = """
2323
}
2424
""".data(using: .utf8)!
2525

26+
let to_one_relationship_with_meta = """
27+
{
28+
"data": {
29+
"type": "test_entity1",
30+
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"
31+
},
32+
"meta": {
33+
"a": "hello"
34+
}
35+
}
36+
""".data(using: .utf8)!
37+
38+
let to_one_relationship_with_links = """
39+
{
40+
"data": {
41+
"type": "test_entity1",
42+
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"
43+
},
44+
"links": {
45+
"b": "world"
46+
}
47+
}
48+
""".data(using: .utf8)!
49+
50+
let to_one_relationship_with_meta_and_links = """
51+
{
52+
"data": {
53+
"type": "test_entity1",
54+
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"
55+
},
56+
"meta": {
57+
"a": "hello"
58+
},
59+
"links": {
60+
"b": "world"
61+
}
62+
}
63+
""".data(using: .utf8)!
64+
2665
let to_many_relationship = """
2766
{
2867
"data": [
@@ -42,6 +81,75 @@ let to_many_relationship = """
4281
}
4382
""".data(using: .utf8)!
4483

84+
let to_many_relationship_with_meta = """
85+
{
86+
"data": [
87+
{
88+
"type": "test_entity1",
89+
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"
90+
},
91+
{
92+
"type": "test_entity1",
93+
"id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333"
94+
},
95+
{
96+
"type": "test_entity1",
97+
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"
98+
}
99+
],
100+
"meta": {
101+
"a": "hello"
102+
}
103+
}
104+
""".data(using: .utf8)!
105+
106+
let to_many_relationship_with_links = """
107+
{
108+
"data": [
109+
{
110+
"type": "test_entity1",
111+
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"
112+
},
113+
{
114+
"type": "test_entity1",
115+
"id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333"
116+
},
117+
{
118+
"type": "test_entity1",
119+
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"
120+
}
121+
],
122+
"links": {
123+
"b": "world"
124+
}
125+
}
126+
""".data(using: .utf8)!
127+
128+
let to_many_relationship_with_meta_and_links = """
129+
{
130+
"data": [
131+
{
132+
"type": "test_entity1",
133+
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"
134+
},
135+
{
136+
"type": "test_entity1",
137+
"id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333"
138+
},
139+
{
140+
"type": "test_entity1",
141+
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"
142+
}
143+
],
144+
"meta": {
145+
"a": "hello"
146+
},
147+
"links": {
148+
"b": "world"
149+
}
150+
}
151+
""".data(using: .utf8)!
152+
45153
let to_many_relationship_type_mismatch = """
46154
{
47155
"data": [

0 commit comments

Comments
 (0)