From 35d6cbb4400003c3763708a6500d615d300e5b81 Mon Sep 17 00:00:00 2001 From: Mathew Polzin Date: Wed, 5 Dec 2018 09:14:38 -0800 Subject: [PATCH] Add support for Relationship Meta and Links (untested) --- JSONAPI.playground/Sources/Entities.swift | 16 ++- README.md | 4 +- Sources/JSONAPI/Resource/Entity.swift | 12 +- Sources/JSONAPI/Resource/Relationship.swift | 126 +++++++++++++++--- Sources/JSONAPITestLib/EntityCheck.swift | 6 +- .../JSONAPITestLib/Relationship+Literal.swift | 10 +- .../ComputedPropertiesTests.swift | 4 +- .../JSONAPITests/Document/DocumentTests.swift | 2 +- Tests/JSONAPITests/Entity/EntityTests.swift | 14 +- .../JSONAPITests/Includes/IncludeTests.swift | 8 +- .../JSONAPITestLib/EntityCheckTests.swift | 2 +- .../Relationship+LiteralTests.swift | 8 +- Tests/JSONAPITests/Poly/PolyTests.swift | 8 +- .../Relationships/RelationshipTests.swift | 16 +-- 14 files changed, 171 insertions(+), 65 deletions(-) diff --git a/JSONAPI.playground/Sources/Entities.swift b/JSONAPI.playground/Sources/Entities.swift index 6df63c9..c47dd34 100644 --- a/JSONAPI.playground/Sources/Entities.swift +++ b/JSONAPI.playground/Sources/Entities.swift @@ -23,8 +23,10 @@ extension String: CreatableRawIdType { } } -// MARK: - Entity typealias for convenience +// MARK: - typealiases for convenience public typealias ExampleEntity = Entity +public typealias ToOne = ToOneRelationship +public typealias ToMany = ToManyRelationship // MARK: - A few resource objects (entities) public enum PersonDescription: EntityDescription { @@ -46,11 +48,11 @@ public enum PersonDescription: EntityDescription { } public struct Relationships: JSONAPI.Relationships { - public let friends: ToManyRelationship - public let dogs: ToManyRelationship - public let home: ToOneRelationship + public let friends: ToMany + public let dogs: ToMany + public let home: ToOne - public init(friends: ToManyRelationship, dogs: ToManyRelationship, home: ToOneRelationship) { + public init(friends: ToMany, dogs: ToMany, home: ToOne) { self.friends = friends self.dogs = dogs self.home = home @@ -79,9 +81,9 @@ public enum DogDescription: EntityDescription { } public struct Relationships: JSONAPI.Relationships { - public let owner: ToOneRelationship + public let owner: ToOne - public init(owner: ToOneRelationship) { + public init(owner: ToOne) { self.owner = owner } } diff --git a/README.md b/README.md index ec3ade7..92ffaea 100644 --- a/README.md +++ b/README.md @@ -52,8 +52,8 @@ Note that Playground support for importing non-system Frameworks is still a bit #### Relationship Object - [x] `data` -- [ ] `links` -- [ ] `meta` +- [x] `links` (untested) +- [x] `meta` (untested) #### Links Object - [x] `href` diff --git a/Sources/JSONAPI/Resource/Entity.swift b/Sources/JSONAPI/Resource/Entity.swift index 59ceb30..763a784 100644 --- a/Sources/JSONAPI/Resource/Entity.swift +++ b/Sources/JSONAPI/Resource/Entity.swift @@ -354,8 +354,12 @@ extension Entity where MetaType == NoMetadata, LinksType == NoLinks, EntityRawId public extension Entity where EntityRawIdType: JSONAPI.RawIdType { /// Get a pointer to this entity that can be used as a /// relationship to another entity. - public var pointer: ToOneRelationship { - return ToOneRelationship(entity: self) + public var pointer: ToOneRelationship { + return ToOneRelationship(entity: self, meta: .none, links: .none) + } + + public func pointer(withMeta meta: MType, andLinks links: LType) -> ToOneRelationship { + return ToOneRelationship(entity: self, meta: meta, links: links) } } @@ -388,14 +392,14 @@ public extension EntityProxy { /// Access to an Id of a `ToOneRelationship`. /// This allows you to write `entity ~> \.other` instead /// of `entity.relationships.other.id`. - public static func ~>(entity: Self, path: KeyPath>) -> OtherEntity.WrappedIdentifier { + public static func ~>(entity: Self, path: KeyPath>) -> OtherEntity.WrappedIdentifier { return entity.relationships[keyPath: path].id } /// Access to all Ids of a `ToManyRelationship`. /// This allows you to write `entity ~> \.others` instead /// of `entity.relationships.others.ids`. - public static func ~>(entity: Self, path: KeyPath>) -> [OtherEntity.Identifier] { + public static func ~>(entity: Self, path: KeyPath>) -> [OtherEntity.Identifier] { return entity.relationships[keyPath: path].ids } } diff --git a/Sources/JSONAPI/Resource/Relationship.swift b/Sources/JSONAPI/Resource/Relationship.swift index 2a5069e..921a36b 100644 --- a/Sources/JSONAPI/Resource/Relationship.swift +++ b/Sources/JSONAPI/Resource/Relationship.swift @@ -5,30 +5,59 @@ // Created by Mathew Polzin on 8/31/18. // -public protocol RelationshipType: Codable {} +public protocol RelationshipType: Codable { + associatedtype LinksType + associatedtype MetaType + + var links: LinksType { get } + var meta: MetaType { get } +} /// An Entity relationship that can be encoded to or decoded from /// a JSON API "Resource Linkage." /// See https://jsonapi.org/format/#document-resource-object-linkage /// A convenient typealias might make your code much more legible: `One` -public struct ToOneRelationship: RelationshipType, Equatable { +public struct ToOneRelationship: RelationshipType, Equatable { public let id: Relatable.WrappedIdentifier - public init(id: Relatable.WrappedIdentifier) { + public let meta: MetaType + public let links: LinksType + + public init(id: Relatable.WrappedIdentifier, meta: MetaType, links: LinksType) { self.id = id + self.meta = meta + self.links = links + } +} + +extension ToOneRelationship where MetaType == NoMetadata, LinksType == NoLinks { + public init(id: Relatable.WrappedIdentifier) { + self.init(id: id, meta: .none, links: .none) } } extension ToOneRelationship where Relatable.WrappedIdentifier == Relatable.Identifier { + public init(entity: E, meta: MetaType, links: LinksType) where E.Description == Relatable.Description, E.Id == Relatable.Identifier { + self.init(id: entity.id, meta: meta, links: links) + } +} + +extension ToOneRelationship where Relatable.WrappedIdentifier == Relatable.Identifier, MetaType == NoMetadata, LinksType == NoLinks { public init(entity: E) where E.Description == Relatable.Description, E.Id == Relatable.Identifier { - id = entity.id + self.init(id: entity.id, meta: .none, links: .none) } } extension ToOneRelationship where Relatable.WrappedIdentifier == Relatable.Identifier? { + public init(entity: E?, meta: MetaType, links: LinksType) where E.Description == Relatable.Description, E.Id == Relatable.Identifier { + self.init(id: entity?.id, meta: meta, links: links) + } +} + +extension ToOneRelationship where Relatable.WrappedIdentifier == Relatable.Identifier?, MetaType == NoMetadata, LinksType == NoLinks { public init(entity: E?) where E.Description == Relatable.Description, E.Id == Relatable.Identifier { - id = entity?.id + self.init(id: entity?.id, meta: .none, links: .none) } } @@ -36,30 +65,54 @@ extension ToOneRelationship where Relatable.WrappedIdentifier == Relatable.Ident /// a JSON API "Resource Linkage." /// See https://jsonapi.org/format/#document-resource-object-linkage /// A convenient typealias might make your code much more legible: `Many` -public struct ToManyRelationship: RelationshipType, Equatable { +public struct ToManyRelationship: RelationshipType, Equatable { public let ids: [Relatable.Identifier] - public init(ids: [Relatable.Identifier]) { + public let meta: MetaType + public let links: LinksType + + public init(ids: [Relatable.Identifier], meta: MetaType, links: LinksType) { self.ids = ids + self.meta = meta + self.links = links } - public init(relationships: [ToOneRelationship]) where T.WrappedIdentifier == Relatable.Identifier { + public init(relationships: [ToOneRelationship], meta: MetaType, links: LinksType) where T.WrappedIdentifier == Relatable.Identifier { ids = relationships.map { $0.id } + self.meta = meta + self.links = links } - private init() { - ids = [] + public init(entities: [E], meta: MetaType, links: LinksType) where E.Description == Relatable.Description, E.Id == Relatable.Identifier { + self.init(ids: entities.map { $0.id }, meta: meta, links: links) + } + + private init(meta: MetaType, links: LinksType) { + self.init(ids: [], meta: meta, links: links) } - public static var none: ToManyRelationship { - return .init() + public static func none(withMeta meta: MetaType, links: LinksType) -> ToManyRelationship { + return ToManyRelationship(meta: meta, links: links) } } -extension ToManyRelationship { +extension ToManyRelationship where MetaType == NoMetadata, LinksType == NoLinks { + + public init(ids: [Relatable.Identifier]) { + self.init(ids: ids, meta: .none, links: .none) + } + + public init(relationships: [ToOneRelationship]) where T.WrappedIdentifier == Relatable.Identifier { + self.init(relationships: relationships, meta: .none, links: .none) + } + + public static var none: ToManyRelationship { + return .none(withMeta: .none, links: .none) + } + public init(entities: [E]) where E.Description == Relatable.Description, E.Id == Relatable.Identifier { - ids = entities.map { $0.id } + self.init(entities: entities, meta: .none, links: .none) } } @@ -85,6 +138,8 @@ extension Optional: OptionalRelatable where Wrapped: Relatable { // MARK: Codable private enum ResourceLinkageCodingKeys: String, CodingKey { case data = "data" + case meta = "meta" + case links = "links" } private enum ResourceIdentifierCodingKeys: String, CodingKey { case id = "id" @@ -103,6 +158,18 @@ extension ToOneRelationship { public init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: ResourceLinkageCodingKeys.self) + if let noMeta = NoMetadata() as? MetaType { + meta = noMeta + } else { + meta = try container.decode(MetaType.self, forKey: .meta) + } + + if let noLinks = NoLinks() as? LinksType { + links = noLinks + } else { + links = try container.decode(LinksType.self, forKey: .links) + } + // A little trickery follows. If the id is nil, the // container.decode(Identifier.self) will fail even if Identifier // is Optional. However, we can check if decoding nil @@ -133,6 +200,14 @@ extension ToOneRelationship { try container.encodeNil(forKey: .data) } + if MetaType.self != NoMetadata.self { + try container.encode(meta, forKey: .meta) + } + + if LinksType.self != NoLinks.self { + try container.encode(links, forKey: .links) + } + var identifier = container.nestedContainer(keyedBy: ResourceIdentifierCodingKeys.self, forKey: .data) try identifier.encode(id, forKey: .id) @@ -143,7 +218,19 @@ extension ToOneRelationship { extension ToManyRelationship { public init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: ResourceLinkageCodingKeys.self) - + + if let noMeta = NoMetadata() as? MetaType { + meta = noMeta + } else { + meta = try container.decode(MetaType.self, forKey: .meta) + } + + if let noLinks = NoLinks() as? LinksType { + links = noLinks + } else { + links = try container.decode(LinksType.self, forKey: .links) + } + var identifiers = try container.nestedUnkeyedContainer(forKey: .data) var newIds = [Relatable.Identifier]() @@ -163,6 +250,15 @@ extension ToManyRelationship { public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: ResourceLinkageCodingKeys.self) + + if MetaType.self != NoMetadata.self { + try container.encode(meta, forKey: .meta) + } + + if LinksType.self != NoLinks.self { + try container.encode(links, forKey: .links) + } + var identifiers = container.nestedUnkeyedContainer(forKey: .data) for id in ids { diff --git a/Sources/JSONAPITestLib/EntityCheck.swift b/Sources/JSONAPITestLib/EntityCheck.swift index 27389bc..4260bc3 100644 --- a/Sources/JSONAPITestLib/EntityCheck.swift +++ b/Sources/JSONAPITestLib/EntityCheck.swift @@ -45,6 +45,10 @@ extension TransformedAttribute: AttributeTypeWithOptionalArray where RawValue: O private protocol OptionalRelationshipType {} extension Optional: OptionalRelationshipType where Wrapped: RelationshipType {} +private protocol _RelationshipType {} +extension ToOneRelationship: _RelationshipType {} +extension ToManyRelationship: _RelationshipType {} + public extension Entity { public static func check(_ entity: Entity) throws { var problems = [EntityCheckError]() @@ -72,7 +76,7 @@ public extension Entity { } for relationship in relationshipsMirror.children { - if relationship.value as? RelationshipType == nil { + if relationship.value as? _RelationshipType == nil { problems.append(.nonRelationship(named: relationship.label ?? "unnamed")) } } diff --git a/Sources/JSONAPITestLib/Relationship+Literal.swift b/Sources/JSONAPITestLib/Relationship+Literal.swift index 0dc586e..384efad 100644 --- a/Sources/JSONAPITestLib/Relationship+Literal.swift +++ b/Sources/JSONAPITestLib/Relationship+Literal.swift @@ -7,14 +7,14 @@ import JSONAPI -extension ToOneRelationship: ExpressibleByNilLiteral where Relatable.WrappedIdentifier: ExpressibleByNilLiteral { +extension ToOneRelationship: ExpressibleByNilLiteral where Relatable.WrappedIdentifier: ExpressibleByNilLiteral, MetaType == NoMetadata, LinksType == NoLinks { public init(nilLiteral: ()) { self.init(id: Relatable.WrappedIdentifier(nilLiteral: ())) } } -extension ToOneRelationship: ExpressibleByUnicodeScalarLiteral where Relatable.WrappedIdentifier: ExpressibleByUnicodeScalarLiteral { +extension ToOneRelationship: ExpressibleByUnicodeScalarLiteral where Relatable.WrappedIdentifier: ExpressibleByUnicodeScalarLiteral, MetaType == NoMetadata, LinksType == NoLinks { public typealias UnicodeScalarLiteralType = Relatable.WrappedIdentifier.UnicodeScalarLiteralType public init(unicodeScalarLiteral value: UnicodeScalarLiteralType) { @@ -22,7 +22,7 @@ extension ToOneRelationship: ExpressibleByUnicodeScalarLiteral where Relatable.W } } -extension ToOneRelationship: ExpressibleByExtendedGraphemeClusterLiteral where Relatable.WrappedIdentifier: ExpressibleByExtendedGraphemeClusterLiteral { +extension ToOneRelationship: ExpressibleByExtendedGraphemeClusterLiteral where Relatable.WrappedIdentifier: ExpressibleByExtendedGraphemeClusterLiteral, MetaType == NoMetadata, LinksType == NoLinks { public typealias ExtendedGraphemeClusterLiteralType = Relatable.WrappedIdentifier.ExtendedGraphemeClusterLiteralType public init(extendedGraphemeClusterLiteral value: ExtendedGraphemeClusterLiteralType) { @@ -30,7 +30,7 @@ extension ToOneRelationship: ExpressibleByExtendedGraphemeClusterLiteral where R } } -extension ToOneRelationship: ExpressibleByStringLiteral where Relatable.WrappedIdentifier: ExpressibleByStringLiteral { +extension ToOneRelationship: ExpressibleByStringLiteral where Relatable.WrappedIdentifier: ExpressibleByStringLiteral, MetaType == NoMetadata, LinksType == NoLinks { public typealias StringLiteralType = Relatable.WrappedIdentifier.StringLiteralType public init(stringLiteral value: StringLiteralType) { @@ -38,7 +38,7 @@ extension ToOneRelationship: ExpressibleByStringLiteral where Relatable.WrappedI } } -extension ToManyRelationship: ExpressibleByArrayLiteral { +extension ToManyRelationship: ExpressibleByArrayLiteral where MetaType == NoMetadata, LinksType == NoLinks { public typealias ArrayLiteralElement = Relatable.Identifier public init(arrayLiteral elements: ArrayLiteralElement...) { diff --git a/Tests/JSONAPITests/Computed Properties/ComputedPropertiesTests.swift b/Tests/JSONAPITests/Computed Properties/ComputedPropertiesTests.swift index 6cefcb1..674cb23 100644 --- a/Tests/JSONAPITests/Computed Properties/ComputedPropertiesTests.swift +++ b/Tests/JSONAPITests/Computed Properties/ComputedPropertiesTests.swift @@ -49,9 +49,9 @@ extension ComputedPropertiesTests { } public struct Relationships: JSONAPI.Relationships { - public let other: ToOneRelationship + public let other: ToOneRelationship - public var computed: ToOneRelationship { + public var computed: ToOneRelationship { return other } } diff --git a/Tests/JSONAPITests/Document/DocumentTests.swift b/Tests/JSONAPITests/Document/DocumentTests.swift index cad7a4a..a3f4aa8 100644 --- a/Tests/JSONAPITests/Document/DocumentTests.swift +++ b/Tests/JSONAPITests/Document/DocumentTests.swift @@ -978,7 +978,7 @@ extension DocumentTests { typealias Attributes = NoAttributes struct Relationships: JSONAPI.Relationships { - let author: ToOneRelationship + let author: ToOneRelationship } } diff --git a/Tests/JSONAPITests/Entity/EntityTests.swift b/Tests/JSONAPITests/Entity/EntityTests.swift index bd70c9b..01d37b7 100644 --- a/Tests/JSONAPITests/Entity/EntityTests.swift +++ b/Tests/JSONAPITests/Entity/EntityTests.swift @@ -459,7 +459,7 @@ extension EntityTests { typealias Attributes = NoAttributes struct Relationships: JSONAPI.Relationships { - let other: ToOneRelationship + let other: ToOneRelationship } } @@ -471,7 +471,7 @@ extension EntityTests { typealias Attributes = NoAttributes struct Relationships: JSONAPI.Relationships { - let others: ToManyRelationship + let others: ToManyRelationship } } @@ -481,7 +481,7 @@ extension EntityTests { static var type: String { return "fourth_test_entities"} struct Relationships: JSONAPI.Relationships { - let other: ToOneRelationship + let other: ToOneRelationship } struct Attributes: JSONAPI.Attributes { @@ -562,9 +562,9 @@ extension EntityTests { typealias Attributes = NoAttributes public struct Relationships: JSONAPI.Relationships { - let one: ToOneRelationship + let one: ToOneRelationship - let nullableOne: ToOneRelationship + let nullableOne: ToOneRelationship // a nullable many is not allowed. it should // just be an empty array. @@ -584,8 +584,8 @@ extension EntityTests { typealias Attributes = NoAttributes public struct Relationships: JSONAPI.Relationships { - let selfRef: ToOneRelationship - let selfRefs: ToManyRelationship + let selfRef: ToOneRelationship + let selfRefs: ToManyRelationship } } diff --git a/Tests/JSONAPITests/Includes/IncludeTests.swift b/Tests/JSONAPITests/Includes/IncludeTests.swift index 971df36..92fd09b 100644 --- a/Tests/JSONAPITests/Includes/IncludeTests.swift +++ b/Tests/JSONAPITests/Includes/IncludeTests.swift @@ -144,7 +144,7 @@ extension IncludedTests { public static var type: String { return "test_entity2" } public struct Relationships: JSONAPI.Relationships { - let entity1: ToOneRelationship + let entity1: ToOneRelationship } public struct Attributes: JSONAPI.Attributes { @@ -162,8 +162,8 @@ extension IncludedTests { public static var type: String { return "test_entity3" } public struct Relationships: JSONAPI.Relationships { - let entity1: ToOneRelationship - let entity2: ToManyRelationship + let entity1: ToOneRelationship + let entity2: ToManyRelationship } } @@ -198,7 +198,7 @@ extension IncludedTests { public static var type: String { return "test_entity6" } struct Relationships: JSONAPI.Relationships { - let entity4: ToOneRelationship + let entity4: ToOneRelationship } } diff --git a/Tests/JSONAPITests/JSONAPITestLib/EntityCheckTests.swift b/Tests/JSONAPITests/JSONAPITestLib/EntityCheckTests.swift index cfda469..7f68480 100644 --- a/Tests/JSONAPITests/JSONAPITestLib/EntityCheckTests.swift +++ b/Tests/JSONAPITests/JSONAPITestLib/EntityCheckTests.swift @@ -115,7 +115,7 @@ extension EntityCheckTests { public typealias Attributes = NoAttributes public struct Relationships: JSONAPI.Relationships { - let x: ToOneRelationship + let x: ToOneRelationship let y: Id } } diff --git a/Tests/JSONAPITests/JSONAPITestLib/Relationship+LiteralTests.swift b/Tests/JSONAPITests/JSONAPITestLib/Relationship+LiteralTests.swift index 87ee0f0..77aa83d 100644 --- a/Tests/JSONAPITests/JSONAPITestLib/Relationship+LiteralTests.swift +++ b/Tests/JSONAPITests/JSONAPITestLib/Relationship+LiteralTests.swift @@ -12,16 +12,16 @@ import JSONAPITestLib class Relationship_LiteralTests: XCTestCase { func test_NilLiteral() { - XCTAssertEqual(ToOneRelationship(id: nil), nil) + XCTAssertEqual(ToOneRelationship(id: nil), nil) } func test_ArrayLiteral() { - XCTAssertEqual(ToManyRelationship(ids: ["1", "2", "3"]), ["1", "2", "3"]) + XCTAssertEqual(ToManyRelationship(ids: ["1", "2", "3"]), ["1", "2", "3"]) } func test_StringLiteral() { - XCTAssertEqual(ToOneRelationship(id: "123"), "123") - XCTAssertEqual(ToOneRelationship(id: "123"), "123") + XCTAssertEqual(ToOneRelationship(id: "123"), "123") + XCTAssertEqual(ToOneRelationship(id: "123"), "123") } } diff --git a/Tests/JSONAPITests/Poly/PolyTests.swift b/Tests/JSONAPITests/Poly/PolyTests.swift index 614afbb..5aaa1d2 100644 --- a/Tests/JSONAPITests/Poly/PolyTests.swift +++ b/Tests/JSONAPITests/Poly/PolyTests.swift @@ -282,7 +282,7 @@ extension PolyTests { public static var type: String { return "test_entity2" } public struct Relationships: JSONAPI.Relationships { - let entity1: ToOneRelationship + let entity1: ToOneRelationship } public struct Attributes: JSONAPI.Attributes { @@ -300,8 +300,8 @@ extension PolyTests { public static var type: String { return "test_entity3" } public struct Relationships: JSONAPI.Relationships { - let entity1: ToOneRelationship - let entity2: ToManyRelationship + let entity1: ToOneRelationship + let entity2: ToManyRelationship } } @@ -336,7 +336,7 @@ extension PolyTests { public static var type: String { return "test_entity6" } struct Relationships: JSONAPI.Relationships { - let entity4: ToOneRelationship + let entity4: ToOneRelationship } } diff --git a/Tests/JSONAPITests/Relationships/RelationshipTests.swift b/Tests/JSONAPITests/Relationships/RelationshipTests.swift index 40ba9e6..1216f0c 100644 --- a/Tests/JSONAPITests/Relationships/RelationshipTests.swift +++ b/Tests/JSONAPITests/Relationships/RelationshipTests.swift @@ -15,7 +15,7 @@ class RelationshipTests: XCTestCase { let entity2 = TestEntity1() let entity3 = TestEntity1() let entity4 = TestEntity1() - let relationship = ToManyRelationship(entities: [entity1, entity2, entity3, entity4]) + let relationship = ToManyRelationship(entities: [entity1, entity2, entity3, entity4]) XCTAssertEqual(relationship.ids.count, 4) XCTAssertEqual(relationship.ids, [entity1, entity2, entity3, entity4].map { $0.id }) @@ -26,7 +26,7 @@ class RelationshipTests: XCTestCase { let entity2 = TestEntity1() let entity3 = TestEntity1() let entity4 = TestEntity1() - let relationship = ToManyRelationship(relationships: [entity1.pointer, entity2.pointer, entity3.pointer, entity4.pointer]) + let relationship = ToManyRelationship(relationships: [entity1.pointer, entity2.pointer, entity3.pointer, entity4.pointer]) XCTAssertEqual(relationship.ids.count, 4) XCTAssertEqual(relationship.ids, [entity1, entity2, entity3, entity4].map { $0.id }) @@ -36,26 +36,26 @@ class RelationshipTests: XCTestCase { // MARK: - Encode/Decode extension RelationshipTests { func test_ToOneRelationship() { - let relationship = decoded(type: ToOneRelationship.self, + let relationship = decoded(type: ToOneRelationship.self, data: to_one_relationship) XCTAssertEqual(relationship.id.rawValue, "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF") } func test_ToOneRelationship_encode() { - test_DecodeEncodeEquality(type: ToOneRelationship.self, + test_DecodeEncodeEquality(type: ToOneRelationship.self, data: to_one_relationship) } func test_ToManyRelationship() { - let relationship = decoded(type: ToManyRelationship.self, + let relationship = decoded(type: ToManyRelationship.self, data: to_many_relationship) XCTAssertEqual(relationship.ids.map { $0.rawValue }, ["2DF03B69-4B0A-467F-B52E-B0C9E44FCECF", "90F03B69-4DF1-467F-B52E-B0C9E44FC333", "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"]) } func test_ToManyRelationship_encode() { - test_DecodeEncodeEquality(type: ToManyRelationship.self, + test_DecodeEncodeEquality(type: ToManyRelationship.self, data: to_many_relationship) } } @@ -63,11 +63,11 @@ extension RelationshipTests { // MARK: Failure tests extension RelationshipTests { func test_ToManyTypeMismatch() { - XCTAssertThrowsError(try JSONDecoder().decode(ToManyRelationship.self, from: to_many_relationship_type_mismatch)) + XCTAssertThrowsError(try JSONDecoder().decode(ToManyRelationship.self, from: to_many_relationship_type_mismatch)) } func test_ToOneTypeMismatch() { - XCTAssertThrowsError(try JSONDecoder().decode(ToOneRelationship.self, from: to_one_relationship_type_mismatch)) + XCTAssertThrowsError(try JSONDecoder().decode(ToOneRelationship.self, from: to_one_relationship_type_mismatch)) } }