-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathRelationshipTests.swift
More file actions
349 lines (273 loc) · 13.5 KB
/
RelationshipTests.swift
File metadata and controls
349 lines (273 loc) · 13.5 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
//
// RelationshipTests.swift
// JSONAPITests
//
// Created by Mathew Polzin on 11/12/18.
//
import XCTest
@testable import JSONAPI
class RelationshipTests: XCTestCase {
func test_initToManyWithEntities() {
let entity1 = TestEntity1(attributes: .none, relationships: .none, meta: .none, links: .none)
let entity2 = TestEntity1(attributes: .none, relationships: .none, meta: .none, links: .none)
let entity3 = TestEntity1(attributes: .none, relationships: .none, meta: .none, links: .none)
let entity4 = TestEntity1(attributes: .none, relationships: .none, meta: .none, links: .none)
let relationship = ToManyRelationship<TestEntity1, NoIdMetadata, NoMetadata, NoLinks>(resourceObjects: [entity1, entity2, entity3, entity4])
XCTAssertEqual(relationship.ids.count, 4)
XCTAssertEqual(relationship.ids, [entity1, entity2, entity3, entity4].map(\.id))
}
func test_initToManyWithRelationships() {
let entity1 = TestEntity1(attributes: .none, relationships: .none, meta: .none, links: .none)
let entity2 = TestEntity1(attributes: .none, relationships: .none, meta: .none, links: .none)
let entity3 = TestEntity1(attributes: .none, relationships: .none, meta: .none, links: .none)
let entity4 = TestEntity1(attributes: .none, relationships: .none, meta: .none, links: .none)
let relationship = ToManyRelationship<TestEntity1, NoIdMetadata, NoMetadata, NoLinks>(pointers: [entity1.pointer, entity2.pointer, entity3.pointer, entity4.pointer])
XCTAssertEqual(relationship.ids.count, 4)
XCTAssertEqual(relationship.ids, [entity1, entity2, entity3, entity4].map(\.id))
}
func test_initToOneWithIdMeta() {
let entity1 = TestEntity1(attributes: .none, relationships: .none, meta: .none, links: .none)
let relationship = ToOneWithMetaInIds(
id: (entity1.id, .init(a: "hello"))
)
XCTAssertEqual(relationship.id, entity1.id)
XCTAssertEqual(relationship.idMeta, .init(a: "hello"))
}
func test_initToManyWithIdMeta() {
let entity1 = TestEntity1(attributes: .none, relationships: .none, meta: .none, links: .none)
let relationship = ToManyWithMetaInIds(
idsWithMetadata: [
(entity1.id, .init(a: "hello"))
]
)
XCTAssertEqual(relationship.ids, [entity1.id])
XCTAssertEqual(relationship.idsWithMeta, [.init(id: entity1.id, meta: .init(a: "hello"))])
}
}
// MARK: - Encode/Decode
extension RelationshipTests {
func test_MetaRelationshipWithMeta() {
let relationship = decoded(
type: MetaRelationship<TestMeta, NoLinks>.self,
data: meta_relationship_with_meta
)
XCTAssertEqual(relationship.meta, TestMeta(a: "hello"))
XCTAssertEqual(relationship.links, .none)
}
func test_MetaRelationshipWithMeta_encode() {
test_DecodeEncodeEquality(
type: MetaRelationship<TestMeta, NoLinks>.self,
data: meta_relationship_with_meta
)
}
func test_MetaRelationshipWithLinks() {
let relationship = decoded(
type: MetaRelationship<NoMetadata, TestLinks>.self,
data: meta_relationship_with_links
)
XCTAssertEqual(relationship.meta, .none)
XCTAssertEqual(relationship.links, TestLinks(b: .init(url: "world")))
}
func test_MetaRelationshipWithLinks_encode() {
test_DecodeEncodeEquality(
type: MetaRelationship<NoMetadata, TestLinks>.self,
data: meta_relationship_with_links
)
}
func test_MetaRelationshipWithMetaAndLinks() {
let relationship = decoded(
type: MetaRelationship<TestMeta, TestLinks>.self,
data: meta_relationship_with_meta_and_links
)
XCTAssertEqual(relationship.meta, TestMeta(a: "hello"))
XCTAssertEqual(relationship.links, TestLinks(b: .init(url: "world")))
}
func test_MetaRelationshipWithMetaAndLinks_encode() {
test_DecodeEncodeEquality(
type: MetaRelationship<TestMeta, TestLinks>.self,
data: meta_relationship_with_meta_and_links
)
}
func test_ToOneRelationship() {
let relationship = decoded(type: ToOneRelationship<TestEntity1, NoIdMetadata, NoMetadata, NoLinks>.self,
data: to_one_relationship)
XCTAssertEqual(relationship.id.rawValue, "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF")
}
func test_ToOneRelationship_encode() {
test_DecodeEncodeEquality(type: ToOneRelationship<TestEntity1, NoIdMetadata, NoMetadata, NoLinks>.self,
data: to_one_relationship)
}
func test_ToOneRelationshipWithMeta() {
let relationship = decoded(type: ToOneWithMeta.self,
data: to_one_relationship_with_meta)
XCTAssertEqual(relationship.id.rawValue, "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF")
XCTAssertEqual(relationship.meta.a, "hello")
}
func test_ToOneRelationshipWithMeta_encode() {
test_DecodeEncodeEquality(type: ToOneWithMeta.self,
data: to_one_relationship_with_meta)
}
func test_ToOneRelationshipWithMetaInsideIdentifier() {
let relationship = decoded(type: ToOneWithMetaInIds.self,
data: to_one_relationship_with_meta_inside_identifier)
XCTAssertEqual(relationship.id.rawValue, "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF")
XCTAssertEqual(relationship.idMeta.a, "hello")
}
func test_ToOneRelationshipWithMetaInsideIdentifier_encode() {
test_DecodeEncodeEquality(type: ToOneWithMetaInIds.self,
data: to_one_relationship_with_meta_inside_identifier)
}
func test_ToOneRelationshipWithLinks() {
let relationship = decoded(type: ToOneWithLinks.self,
data: to_one_relationship_with_links)
XCTAssertEqual(relationship.id.rawValue, "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF")
XCTAssertEqual(relationship.links.b, .init(url: "world"))
}
func test_ToOneRelationshipWithLinks_encode() {
test_DecodeEncodeEquality(type: ToOneWithLinks.self,
data: to_one_relationship_with_links)
}
func test_ToOneRelationshipWithMetaAndLinks() {
let relationship = decoded(type: ToOneWithMetaAndLinks.self,
data: to_one_relationship_with_meta_and_links)
XCTAssertEqual(relationship.id.rawValue, "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF")
XCTAssertEqual(relationship.meta.a, "hello")
XCTAssertEqual(relationship.links.b, .init(url: "world"))
}
func test_ToOneRelationshipWithMetaAndLinks_encode() {
test_DecodeEncodeEquality(type: ToOneWithMetaAndLinks.self,
data: to_one_relationship_with_meta_and_links)
}
func test_ToManyRelationship() {
let relationship = decoded(type: ToManyRelationship<TestEntity1, NoIdMetadata, NoMetadata, NoLinks>.self,
data: to_many_relationship)
XCTAssertEqual(relationship.ids.map(\.rawValue), ["2DF03B69-4B0A-467F-B52E-B0C9E44FCECF", "90F03B69-4DF1-467F-B52E-B0C9E44FC333", "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"])
}
func test_ToManyRelationship_encode() {
test_DecodeEncodeEquality(type: ToManyRelationship<TestEntity1, NoIdMetadata, NoMetadata, NoLinks>.self,
data: to_many_relationship)
}
func test_ToManyRelationshipWithMeta() {
let relationship = decoded(type: ToManyWithMeta.self,
data: to_many_relationship_with_meta)
XCTAssertEqual(relationship.ids.map(\.rawValue), ["2DF03B69-4B0A-467F-B52E-B0C9E44FCECF", "90F03B69-4DF1-467F-B52E-B0C9E44FC333", "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"])
XCTAssertEqual(relationship.meta.a, "hello")
}
func test_ToManyRelationshipWithMeta_encode() {
test_DecodeEncodeEquality(type: ToManyWithMeta.self,
data: to_many_relationship_with_meta)
}
func test_ToManyRelationshipWithMetaInsideIdentifier() {
let relationship = decoded(type: ToManyWithMetaInIds.self,
data: to_many_relationship_with_meta_inside_identifier)
XCTAssertEqual(relationship.ids.map(\.rawValue), ["2DF03B69-4B0A-467F-B52E-B0C9E44FCECF", "90F03B69-4DF1-467F-B52E-B0C9E44FC333", "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"])
XCTAssertEqual(relationship.idsWithMeta[0].meta.a, "hello")
}
func test_ToManyRelationshipWithMetaInsideIdentifier_encode() {
test_DecodeEncodeEquality(type: ToManyWithMetaInIds.self,
data: to_many_relationship_with_meta_inside_identifier)
}
func test_ToManyRelationshipWithLinks() {
let relationship = decoded(type: ToManyWithLinks.self,
data: to_many_relationship_with_links)
XCTAssertEqual(relationship.ids.map(\.rawValue), ["2DF03B69-4B0A-467F-B52E-B0C9E44FCECF", "90F03B69-4DF1-467F-B52E-B0C9E44FC333", "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"])
XCTAssertEqual(relationship.links.b, .init(url: "world"))
}
func test_ToManyRelationshipWithLinks_encode() {
test_DecodeEncodeEquality(type: ToManyWithLinks.self,
data: to_many_relationship_with_links)
}
func test_ToManyRelationshipWithMetaAndLinks() {
let relationship = decoded(type: ToManyWithMetaAndLinks.self,
data: to_many_relationship_with_meta_and_links)
XCTAssertEqual(relationship.ids.map(\.rawValue), ["2DF03B69-4B0A-467F-B52E-B0C9E44FCECF", "90F03B69-4DF1-467F-B52E-B0C9E44FC333", "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"])
XCTAssertEqual(relationship.meta.a, "hello")
XCTAssertEqual(relationship.links.b, .init(url: "world"))
}
func test_ToManyRelationshipWithMetaAndLinks_encode() {
test_DecodeEncodeEquality(type: ToManyWithMetaAndLinks.self,
data: to_many_relationship_with_meta_and_links)
}
func test_ToManyRelationshipWithMetaNoDataOmittable() {
TestEntityType1.canHaveNoDataInRelationships = true
let relationship = decoded(type: ToManyWithMeta.self,
data: to_many_relationship_with_meta_no_data)
XCTAssertEqual(relationship.ids, [])
XCTAssertEqual(relationship.meta.a, "hello")
TestEntityType1.canHaveNoDataInRelationships = false
}
func test_ToManyRelationshipWithMetaNoDataNotOmittable() {
TestEntityType1.canHaveNoDataInRelationships = false
do {
_ = try decodedThrows(type: ToManyWithMeta.self,
data: to_many_relationship_with_meta_no_data)
XCTFail("Expected decoding to fail.")
} catch let error as DecodingError {
switch error {
case .keyNotFound(ResourceLinkageCodingKeys.data, _):
break
default:
XCTFail("Expected error to be DecodingError.keyNotFound(.data), but got \(error)")
}
} catch {
XCTFail("Expected to have DecodingError.keyNotFound(.data), but got \(error)")
}
}
}
// MARK: Nullable
extension RelationshipTests {
func test_ToOneNullableIsNullIfNil() {
let relationship = ToOneNullable(resourceObject: nil)
let relationshipData = try! JSONEncoder().encode(relationship)
let relationshipString = String(data: relationshipData, encoding: .utf8)!
XCTAssertEqual(relationshipString, "{\"data\":null}")
}
func test_ToOneNullableIsEqualToNonNullableIfNotNil() {
let entity = TestEntity1(attributes: .none, relationships: .none, meta: .none, links: .none)
let relationship1 = ToOneNonNullable(resourceObject: entity)
let relationship2 = ToOneNullable(resourceObject: entity)
XCTAssertEqual(encoded(value: relationship1), encoded(value: relationship2))
}
func test_nullableIdMeta() {
let _ = decoded(type: ToOneRelationship<TestEntity1?, TestMeta?, NoMetadata, NoLinks>.self, data: to_one_relationship_nulled_out)
}
}
// MARK: Failure tests
extension RelationshipTests {
func test_ToManyTypeMismatch() {
XCTAssertThrowsError(try JSONDecoder().decode(ToManyRelationship<TestEntity1, NoIdMetadata, NoMetadata, NoLinks>.self, from: to_many_relationship_type_mismatch))
}
func test_ToOneTypeMismatch() {
XCTAssertThrowsError(try JSONDecoder().decode(ToOneRelationship<TestEntity1, NoIdMetadata, NoMetadata, NoLinks>.self, from: to_one_relationship_type_mismatch))
}
func test_toOneNulledOutWithExpectedIdMetadata() {
XCTAssertThrowsError(try JSONDecoder().decode(ToOneRelationship<TestEntity1?, TestMeta, NoMetadata, NoLinks>.self, from: to_one_relationship_nulled_out))
}
}
// MARK: - Test types
extension RelationshipTests {
enum TestEntityType1: ResourceObjectDescription, ResourceObjectWithOptionalDataInRelationships {
typealias Attributes = NoAttributes
typealias Relationships = NoRelationships
public static var jsonType: String { return "test_entity1" }
static var canHaveNoDataInRelationships: Bool = false
}
typealias TestEntity1 = BasicEntity<TestEntityType1>
typealias ToOneWithMeta = ToOneRelationship<TestEntity1, NoIdMetadata, TestMeta, NoLinks>
typealias ToOneWithMetaInIds = ToOneRelationship<TestEntity1, TestMeta, NoMetadata, NoLinks>
typealias ToOneWithLinks = ToOneRelationship<TestEntity1, NoIdMetadata, NoMetadata, TestLinks>
typealias ToOneWithMetaAndLinks = ToOneRelationship<TestEntity1, NoIdMetadata, TestMeta, TestLinks>
typealias ToManyWithMeta = ToManyRelationship<TestEntity1, NoIdMetadata, TestMeta, NoLinks>
typealias ToManyWithMetaInIds = ToManyRelationship<TestEntity1, TestMeta, NoMetadata, NoLinks>
typealias ToManyWithLinks = ToManyRelationship<TestEntity1, NoIdMetadata, NoMetadata, TestLinks>
typealias ToManyWithMetaAndLinks = ToManyRelationship<TestEntity1, NoIdMetadata, TestMeta, TestLinks>
typealias ToOneNullable = ToOneRelationship<TestEntity1?, NoIdMetadata, NoMetadata, NoLinks>
typealias ToOneNonNullable = ToOneRelationship<TestEntity1, NoIdMetadata, NoMetadata, NoLinks>
struct TestMeta: JSONAPI.Meta {
let a: String
}
typealias TestLink = JSONAPI.Link<String, NoMetadata>
struct TestLinks: JSONAPI.Links {
let b: TestLink
}
}