diff --git a/JSONAPI.playground/Pages/Test Library.xcplaygroundpage/Contents.swift b/JSONAPI.playground/Pages/Test Library.xcplaygroundpage/Contents.swift new file mode 100644 index 0000000..3c67cca --- /dev/null +++ b/JSONAPI.playground/Pages/Test Library.xcplaygroundpage/Contents.swift @@ -0,0 +1,16 @@ +//: [Previous](@previous) + +import Foundation +import JSONAPI +import JSONAPITestLib + +/******* + +Please enjoy these examples, but allow me the forced casting and the lack of error checking for the sake of brevity. + +********/ + +// MARK: - Literal Expressibility +// The JSONAPITestLib provides literal expressibility for key types to +// make creating tests easier +let dog = Dog(id: "1234", attributes: Dog.Attributes(name: "Buddy"), relationships: Dog.Relationships(owner: nil)) diff --git a/JSONAPI.playground/Contents.swift b/JSONAPI.playground/Pages/Usage.xcplaygroundpage/Contents.swift similarity index 100% rename from JSONAPI.playground/Contents.swift rename to JSONAPI.playground/Pages/Usage.xcplaygroundpage/Contents.swift diff --git a/JSONAPI.playground/Sources/Entities.swift b/JSONAPI.playground/Sources/Entities.swift index e466b35..2ad13b2 100644 --- a/JSONAPI.playground/Sources/Entities.swift +++ b/JSONAPI.playground/Sources/Entities.swift @@ -34,12 +34,23 @@ public enum PersonDescription: EntityDescription { public struct Attributes: JSONAPI.Attributes { public let name: Attribute<[String]> public let favoriteColor: Attribute + + public init(name: Attribute<[String]>, favoriteColor: Attribute) { + self.name = name + self.favoriteColor = favoriteColor + } } public struct Relationships: JSONAPI.Relationships { public let friends: ToManyRelationship public let dogs: ToManyRelationship public let home: ToOneRelationship + + public init(friends: ToManyRelationship, dogs: ToManyRelationship, home: ToOneRelationship) { + self.friends = friends + self.dogs = dogs + self.home = home + } } } @@ -57,10 +68,18 @@ public enum DogDescription: EntityDescription { public struct Attributes: JSONAPI.Attributes { public let name: Attribute + + public init(name: Attribute) { + self.name = name + } } public struct Relationships: JSONAPI.Relationships { public let owner: ToOneRelationship + + public init(owner: ToOneRelationship) { + self.owner = owner + } } } diff --git a/JSONAPI.playground/contents.xcplayground b/JSONAPI.playground/contents.xcplayground index a93d484..8df8fa3 100644 --- a/JSONAPI.playground/contents.xcplayground +++ b/JSONAPI.playground/contents.xcplayground @@ -1,4 +1,7 @@ - - + + + + + \ No newline at end of file diff --git a/Sources/JSONAPI/Resource/Relationship.swift b/Sources/JSONAPI/Resource/Relationship.swift index 27e7d1b..23d1958 100644 --- a/Sources/JSONAPI/Resource/Relationship.swift +++ b/Sources/JSONAPI/Resource/Relationship.swift @@ -12,10 +12,6 @@ public struct ToOneRelationship: Equatable, Codable { public let id: Relatable.WrappedIdentifier - - public var ids: [Relatable.WrappedIdentifier] { - return [id] - } public init(id: Relatable.WrappedIdentifier) { self.id = id diff --git a/Sources/JSONAPITestLib/Relationship+Literal.swift b/Sources/JSONAPITestLib/Relationship+Literal.swift new file mode 100644 index 0000000..ce22903 --- /dev/null +++ b/Sources/JSONAPITestLib/Relationship+Literal.swift @@ -0,0 +1,14 @@ +// +// Relationship+Literal.swift +// JSONAPITestLib +// +// Created by Mathew Polzin on 11/27/18. +// + +import JSONAPI + +extension ToOneRelationship: ExpressibleByNilLiteral where Relatable.WrappedIdentifier: ExpressibleByNilLiteral { + public init(nilLiteral: ()) { + self.init(id: Relatable.WrappedIdentifier(nilLiteral: ())) + } +} diff --git a/Tests/JSONAPITests/Entity/EntityTests.swift b/Tests/JSONAPITests/Entity/EntityTests.swift index d69ae03..236d558 100644 --- a/Tests/JSONAPITests/Entity/EntityTests.swift +++ b/Tests/JSONAPITests/Entity/EntityTests.swift @@ -37,7 +37,7 @@ class EntityTests: XCTestCase { let entity1 = TestEntity1() let entity2 = TestEntity2(other: entity1.pointer) - XCTAssertEqual(entity2.relationships.other.ids, [entity1.id]) + XCTAssertEqual(entity2.relationships.other.id, entity1.id) } } diff --git a/Tests/JSONAPITests/Relationships/RelationshipTests.swift b/Tests/JSONAPITests/Relationships/RelationshipTests.swift index 3c36d3a..35fe44d 100644 --- a/Tests/JSONAPITests/Relationships/RelationshipTests.swift +++ b/Tests/JSONAPITests/Relationships/RelationshipTests.swift @@ -40,7 +40,6 @@ extension RelationshipTests { data: to_one_relationship) XCTAssertEqual(relationship.id.rawValue, "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF") - XCTAssertEqual(relationship.ids.count, 1) } func test_ToOneRelationship_encode() { diff --git a/Tests/JSONAPITests/TestLib/Relationship+LiteralTests.swift b/Tests/JSONAPITests/TestLib/Relationship+LiteralTests.swift new file mode 100644 index 0000000..2558328 --- /dev/null +++ b/Tests/JSONAPITests/TestLib/Relationship+LiteralTests.swift @@ -0,0 +1,29 @@ +// +// Relationship+LiteralTests.swift +// JSONAPITests +// +// Created by Mathew Polzin on 11/27/18. +// + +import XCTest +import JSONAPI +import JSONAPITestLib + +class Relationship_LiteralTests: XCTestCase { + + func test_NilLiteral() { + XCTAssertEqual(ToOneRelationship(id: nil), nil) + } +} + +// MARK: - Test types +extension Relationship_LiteralTests { + enum TestDescription: EntityDescription { + public static var type: String { return "test" } + + public typealias Attributes = NoAttributes + public typealias Relationships = NoRelationships + } + + typealias TestEntity = Entity +}