Skip to content

Commit 3a8237e

Browse files
committed
Add test coverage for Meta and Links on Entity
1 parent 49e33ba commit 3a8237e

4 files changed

Lines changed: 257 additions & 2 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ Note that Playground support for importing non-system Frameworks is still a bit
4747
- [x] `type`
4848
- [x] `attributes`
4949
- [x] `relationships`
50-
- [x] `links` (untested)
51-
- [x] `meta` (untested)
50+
- [x] `links`
51+
- [x] `meta`
5252

5353
#### Relationship Object
5454
- [x] `data`

Sources/JSONAPI/Meta/Links.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ public protocol JSONAPIURL: Codable, Equatable {}
2121
public struct Link<URL: JSONAPI.JSONAPIURL, Meta: JSONAPI.Meta>: Equatable, Codable {
2222
public let url: URL
2323
public let meta: Meta
24+
25+
public init(url: URL, meta: Meta) {
26+
self.url = url
27+
self.meta = meta
28+
}
29+
}
30+
31+
extension Link where Meta == NoMetadata {
32+
public init(url: URL) {
33+
self.init(url: url, meta: .none)
34+
}
2435
}
2536

2637
public extension Link {

Tests/JSONAPITests/Entity/EntityTests.swift

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class EntityTests: XCTestCase {
5757
let _ = TestEntity10(id: .init(rawValue: "10"), relationships: .init(selfRef: .init(id: e10id1), selfRefs: .init(ids: [e10id2, e10id3])))
5858
XCTAssertNoThrow(try TestEntity11(id: .init(rawValue: "11"), attributes: .init(number: .init(rawValue: 11))))
5959
let _ = UnidentifiedTestEntity(attributes: .init(me: .init(value: "hello")))
60+
let _ = UnidentifiedTestEntityWithMeta(attributes: .init(me: .init(value: "hello")), meta: .init(x: "world", y: nil))
61+
let _ = UnidentifiedTestEntityWithLinks(attributes: .init(me: .init(value: "hello")), links: .init(link1: .init(url: "hmmm")))
6062
}
6163
}
6264

@@ -336,6 +338,109 @@ extension EntityTests {
336338
}
337339
}
338340

341+
// MARK: With Meta and/or Links
342+
343+
extension EntityTests {
344+
func test_UnidentifiedEntityWithAttributesAndMeta() {
345+
let entity = decoded(type: UnidentifiedTestEntityWithMeta.self,
346+
data: entity_unidentified_with_attributes_and_meta)
347+
348+
XCTAssertEqual(entity[\.me], "unknown")
349+
XCTAssertEqual(entity.id, .unidentified)
350+
XCTAssertEqual(entity.meta.x, "world")
351+
XCTAssertEqual(entity.meta.y, 5)
352+
XCTAssertNoThrow(try UnidentifiedTestEntityWithMeta.check(entity))
353+
}
354+
355+
func test_UnidentifiedEntityWithAttributesAndMeta_encode() {
356+
test_DecodeEncodeEquality(type: UnidentifiedTestEntityWithMeta.self,
357+
data: entity_unidentified_with_attributes_and_meta)
358+
}
359+
360+
func test_UnidentifiedEntityWithAttributesAndLinks() {
361+
let entity = decoded(type: UnidentifiedTestEntityWithLinks.self,
362+
data: entity_unidentified_with_attributes_and_links)
363+
364+
XCTAssertEqual(entity[\.me], "unknown")
365+
XCTAssertEqual(entity.id, .unidentified)
366+
XCTAssertEqual(entity.links.link1, .init(url: "https://image.com/image.png"))
367+
XCTAssertNoThrow(try UnidentifiedTestEntityWithLinks.check(entity))
368+
}
369+
370+
func test_UnidentifiedEntityWithAttributesAndLinks_encode() {
371+
test_DecodeEncodeEquality(type: UnidentifiedTestEntityWithLinks.self,
372+
data: entity_unidentified_with_attributes_and_links)
373+
}
374+
375+
func test_UnidentifiedEntityWithAttributesAndMetaAndLinks() {
376+
let entity = decoded(type: UnidentifiedTestEntityWithMetaAndLinks.self,
377+
data: entity_unidentified_with_attributes_and_meta_and_links)
378+
379+
XCTAssertEqual(entity[\.me], "unknown")
380+
XCTAssertEqual(entity.id, .unidentified)
381+
XCTAssertEqual(entity.meta.x, "world")
382+
XCTAssertEqual(entity.meta.y, 5)
383+
XCTAssertEqual(entity.links.link1, .init(url: "https://image.com/image.png"))
384+
XCTAssertNoThrow(try UnidentifiedTestEntityWithMetaAndLinks.check(entity))
385+
}
386+
387+
func test_UnidentifiedEntityWithAttributesAndMetaAndLinks_encode() {
388+
test_DecodeEncodeEquality(type: UnidentifiedTestEntityWithMetaAndLinks.self,
389+
data: entity_unidentified_with_attributes_and_meta_and_links)
390+
}
391+
392+
func test_EntitySomeRelationshipsSomeAttributesWithMeta() {
393+
let entity = decoded(type: TestEntity4WithMeta.self,
394+
data: entity_some_relationships_some_attributes_with_meta)
395+
396+
XCTAssertEqual(entity[\.word], "coolio")
397+
XCTAssertEqual(entity[\.number], 992299)
398+
XCTAssertEqual((entity ~> \.other).rawValue, "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF")
399+
XCTAssertEqual(entity.meta.x, "world")
400+
XCTAssertEqual(entity.meta.y, 5)
401+
XCTAssertNoThrow(try TestEntity4WithMeta.check(entity))
402+
}
403+
404+
func test_EntitySomeRelationshipsSomeAttributesWithMeta_encode() {
405+
test_DecodeEncodeEquality(type: TestEntity4WithMeta.self,
406+
data: entity_some_relationships_some_attributes_with_meta)
407+
}
408+
409+
func test_EntitySomeRelationshipsSomeAttributesWithLinks() {
410+
let entity = decoded(type: TestEntity4WithLinks.self,
411+
data: entity_some_relationships_some_attributes_with_links)
412+
413+
XCTAssertEqual(entity[\.word], "coolio")
414+
XCTAssertEqual(entity[\.number], 992299)
415+
XCTAssertEqual((entity ~> \.other).rawValue, "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF")
416+
XCTAssertEqual(entity.links.link1, .init(url: "https://image.com/image.png"))
417+
XCTAssertNoThrow(try TestEntity4WithLinks.check(entity))
418+
}
419+
420+
func test_EntitySomeRelationshipsSomeAttributesWithLinks_encode() {
421+
test_DecodeEncodeEquality(type: TestEntity4WithLinks.self,
422+
data: entity_some_relationships_some_attributes_with_links)
423+
}
424+
425+
func test_EntitySomeRelationshipsSomeAttributesWithMetaAndLinks() {
426+
let entity = decoded(type: TestEntity4WithMetaAndLinks.self,
427+
data: entity_some_relationships_some_attributes_with_meta_and_links)
428+
429+
XCTAssertEqual(entity[\.word], "coolio")
430+
XCTAssertEqual(entity[\.number], 992299)
431+
XCTAssertEqual((entity ~> \.other).rawValue, "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF")
432+
XCTAssertEqual(entity.meta.x, "world")
433+
XCTAssertEqual(entity.meta.y, 5)
434+
XCTAssertEqual(entity.links.link1, .init(url: "https://image.com/image.png"))
435+
XCTAssertNoThrow(try TestEntity4WithMetaAndLinks.check(entity))
436+
}
437+
438+
func test_EntitySomeRelationshipsSomeAttributesWithMetaAndLinks_encode() {
439+
test_DecodeEncodeEquality(type: TestEntity4WithMetaAndLinks.self,
440+
data: entity_some_relationships_some_attributes_with_meta_and_links)
441+
}
442+
}
443+
339444
// MARK: - Test Types
340445
extension EntityTests {
341446

@@ -388,6 +493,12 @@ extension EntityTests {
388493

389494
typealias TestEntity4 = BasicEntity<TestEntityType4>
390495

496+
typealias TestEntity4WithMeta = Entity<TestEntityType4, TestEntityMeta, NoLinks>
497+
498+
typealias TestEntity4WithLinks = Entity<TestEntityType4, NoMetadata, TestEntityLinks>
499+
500+
typealias TestEntity4WithMetaAndLinks = Entity<TestEntityType4, TestEntityMeta, TestEntityLinks>
501+
391502
enum TestEntityType5: EntityDescription {
392503
static var type: String { return "fifth_test_entities"}
393504

@@ -504,6 +615,12 @@ extension EntityTests {
504615

505616
typealias UnidentifiedTestEntity = NewEntity<UnidentifiedTestEntityType, NoMetadata, NoLinks>
506617

618+
typealias UnidentifiedTestEntityWithMeta = NewEntity<UnidentifiedTestEntityType, TestEntityMeta, NoLinks>
619+
620+
typealias UnidentifiedTestEntityWithLinks = NewEntity<UnidentifiedTestEntityType, NoMetadata, TestEntityLinks>
621+
622+
typealias UnidentifiedTestEntityWithMetaAndLinks = NewEntity<UnidentifiedTestEntityType, TestEntityMeta, TestEntityLinks>
623+
507624
enum IntToString: Transformer {
508625
public static func transform(_ from: Int) -> String {
509626
return String(from)
@@ -540,4 +657,13 @@ extension EntityTests {
540657
return from
541658
}
542659
}
660+
661+
struct TestEntityMeta: JSONAPI.Meta {
662+
let x: String
663+
let y: Int?
664+
}
665+
666+
struct TestEntityLinks: JSONAPI.Links {
667+
let link1: Link<String, NoMetadata>
668+
}
543669
}

Tests/JSONAPITests/Entity/stubs/EntityStubs.swift

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,83 @@ let entity_some_relationships_some_attributes = """
5757
}
5858
""".data(using: .utf8)!
5959

60+
let entity_some_relationships_some_attributes_with_meta = """
61+
{
62+
"id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333",
63+
"type": "fourth_test_entities",
64+
"attributes": {
65+
"word": "coolio",
66+
"number": 992299,
67+
"array": [12.3, 4, 0.1]
68+
},
69+
"relationships": {
70+
"other": {
71+
"data": {
72+
"type": "second_test_entities",
73+
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"
74+
}
75+
}
76+
},
77+
"meta": {
78+
"x": "world",
79+
"y": 5
80+
},
81+
"links": {
82+
"link1": "https://image.com/image.png"
83+
}
84+
}
85+
""".data(using: .utf8)!
86+
87+
let entity_some_relationships_some_attributes_with_links = """
88+
{
89+
"id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333",
90+
"type": "fourth_test_entities",
91+
"attributes": {
92+
"word": "coolio",
93+
"number": 992299,
94+
"array": [12.3, 4, 0.1]
95+
},
96+
"relationships": {
97+
"other": {
98+
"data": {
99+
"type": "second_test_entities",
100+
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"
101+
}
102+
}
103+
},
104+
"links": {
105+
"link1": "https://image.com/image.png"
106+
}
107+
}
108+
""".data(using: .utf8)!
109+
110+
let entity_some_relationships_some_attributes_with_meta_and_links = """
111+
{
112+
"id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333",
113+
"type": "fourth_test_entities",
114+
"attributes": {
115+
"word": "coolio",
116+
"number": 992299,
117+
"array": [12.3, 4, 0.1]
118+
},
119+
"relationships": {
120+
"other": {
121+
"data": {
122+
"type": "second_test_entities",
123+
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"
124+
}
125+
}
126+
},
127+
"meta": {
128+
"x": "world",
129+
"y": 5
130+
},
131+
"links": {
132+
"link1": "https://image.com/image.png"
133+
}
134+
}
135+
""".data(using: .utf8)!
136+
60137
let entity_one_omitted_attribute = """
61138
{
62139
"id": "1",
@@ -241,3 +318,44 @@ let entity_unidentified_with_attributes = """
241318
}
242319
}
243320
""".data(using: .utf8)!
321+
322+
let entity_unidentified_with_attributes_and_meta = """
323+
{
324+
"type": "unidentified_test_entities",
325+
"attributes": {
326+
"me": "unknown"
327+
},
328+
"meta": {
329+
"x": "world",
330+
"y": 5
331+
}
332+
}
333+
""".data(using: .utf8)!
334+
335+
let entity_unidentified_with_attributes_and_links = """
336+
{
337+
"type": "unidentified_test_entities",
338+
"attributes": {
339+
"me": "unknown"
340+
},
341+
"links": {
342+
"link1": "https://image.com/image.png"
343+
}
344+
}
345+
""".data(using: .utf8)!
346+
347+
let entity_unidentified_with_attributes_and_meta_and_links = """
348+
{
349+
"type": "unidentified_test_entities",
350+
"attributes": {
351+
"me": "unknown"
352+
},
353+
"meta": {
354+
"x": "world",
355+
"y": 5
356+
},
357+
"links": {
358+
"link1": "https://image.com/image.png"
359+
}
360+
}
361+
""".data(using: .utf8)!

0 commit comments

Comments
 (0)