Compare commits

..

3 Commits

Author SHA1 Message Date
Mathew Polzin dfdc266645 remove a bunch of convenience initializers that appeared to be giving the compiler some strife 2018-12-10 22:45:28 -08:00
Mathew Polzin 585cad0a83 Added literal initialization for nullable Attributes (TestLib) 2018-12-10 21:50:30 -08:00
Mathew Polzin 89abdd4cca fix example code in Playground 2018-12-10 21:11:48 -08:00
16 changed files with 234 additions and 77 deletions
@@ -13,7 +13,7 @@ Please enjoy these examples, but allow me the forced casting and the lack of err
// 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))
let dog = Dog(id: "1234", attributes: Dog.Attributes(name: "Buddy"), relationships: Dog.Relationships(owner: nil), meta: .none, links: .none)
// MARK: - JSON API structure checking
// The JSONAPITestLib provides a `check` function for each Entity type
@@ -13,7 +13,7 @@ let dogFromCode = try! Dog(name: "Buddy", owner: nil)
typealias SingleDogDocument = JSONAPI.Document<SingleResourceBody<Dog>, NoMetadata, NoLinks, NoIncludes, NoAPIDescription, UnknownJSONAPIError>
let singleDogDocument = SingleDogDocument(body: SingleResourceBody(entity: dogFromCode))
let singleDogDocument = SingleDogDocument(apiDescription: .none, body: .init(entity: dogFromCode), includes: .none, meta: .none, links: .none)
let singleDogData = try! JSONEncoder().encode(singleDogDocument)
@@ -24,13 +24,13 @@ let dogFromData = dogResponse.body.primaryData?.value
// MARK: - Create a request or response with multiple people and dogs and houses included
let personIds = [Person.Identifier(), Person.Identifier()]
let dogs = try! [Dog(name: "Buddy", owner: personIds[0]), Dog(name: "Joy", owner: personIds[0]), Dog(name: "Travis", owner: personIds[1])]
let houses = [House(), House()]
let houses = [House(attributes: .none, relationships: .none, meta: .none, links: .none), House(attributes: .none, relationships: .none, meta: .none, links: .none)]
let people = try! [Person(id: personIds[0], name: ["Gary", "Doe"], favoriteColor: "Orange-Red", friends: [], dogs: [dogs[0], dogs[1]], home: houses[0]), Person(id: personIds[1], name: ["Elise", "Joy"], favoriteColor: "Red", friends: [], dogs: [dogs[2]], home: houses[1])]
typealias BatchPeopleDocument = JSONAPI.Document<ManyResourceBody<Person>, NoMetadata, NoLinks, Include2<Dog, House>, NoAPIDescription, UnknownJSONAPIError>
let includes = dogs.map { BatchPeopleDocument.Include($0) } + houses.map { BatchPeopleDocument.Include($0) }
let batchPeopleDocument = BatchPeopleDocument(body: .init(entities: people), includes: .init(values: includes))
let batchPeopleDocument = BatchPeopleDocument(apiDescription: .none, body: .init(entities: people), includes: .init(values: includes), meta: .none, links: .none)
let batchPeopleData = try! JSONEncoder().encode(batchPeopleDocument)
// MARK: - Parse a request or response body with multiple people in it and dogs and houses included
+4 -4
View File
@@ -25,7 +25,7 @@ extension String: CreatableRawIdType {
// MARK: - typealiases for convenience
public typealias ExampleEntity<Description: EntityDescription> = Entity<Description, NoMetadata, NoLinks, String>
public typealias ToOne<E: OptionalRelatable> = ToOneRelationship<E, NoMetadata, NoLinks>
public typealias ToOne<E: Identifiable> = ToOneRelationship<E, NoMetadata, NoLinks>
public typealias ToMany<E: Relatable> = ToManyRelationship<E, NoMetadata, NoLinks>
// MARK: - A few resource objects (entities)
@@ -64,7 +64,7 @@ public typealias Person = ExampleEntity<PersonDescription>
public extension Entity where Description == PersonDescription, MetaType == NoMetadata, LinksType == NoLinks, EntityRawIdType == String {
public init(id: Person.Id? = nil,name: [String], favoriteColor: String, friends: [Person], dogs: [Dog], home: House) throws {
self = try Person(id: id ?? Person.Id(), attributes: .init(name: .init(rawValue: name), favoriteColor: .init(rawValue: favoriteColor)), relationships: .init(friends: .init(entities: friends), dogs: .init(entities: dogs), home: .init(entity: home)))
self = try Person(id: id ?? Person.Id(), attributes: .init(name: .init(rawValue: name), favoriteColor: .init(rawValue: favoriteColor)), relationships: .init(friends: .init(entities: friends), dogs: .init(entities: dogs), home: .init(entity: home)), meta: .none, links: .none)
}
}
@@ -93,11 +93,11 @@ public typealias Dog = ExampleEntity<DogDescription>
public extension Entity where Description == DogDescription, MetaType == NoMetadata, LinksType == NoLinks, EntityRawIdType == String {
public init(name: String, owner: Person?) throws {
self = try Dog(attributes: .init(name: .init(rawValue: name)), relationships: DogDescription.Relationships(owner: .init(entity: owner)))
self = try Dog(attributes: .init(name: .init(rawValue: name)), relationships: DogDescription.Relationships(owner: .init(entity: owner)), meta: .none, links: .none)
}
public init(name: String, owner: Person.Id) throws {
self = try Dog(attributes: .init(name: .init(rawValue: name)), relationships: .init(owner: .init(id: owner)))
self = try Dog(attributes: .init(name: .init(rawValue: name)), relationships: .init(owner: .init(id: owner)), meta: .none, links: .none)
}
}
+2 -1
View File
@@ -110,7 +110,7 @@ public struct Document<PrimaryResourceBody: JSONAPI.ResourceBody, MetaType: JSON
self.apiDescription = apiDescription
}
}
/*
extension Document where IncludeType == NoIncludes {
public init(apiDescription: APIDescription, body: PrimaryResourceBody, meta: MetaType, links: LinksType) {
self.init(apiDescription: apiDescription, body: body, includes: .none, meta: meta, links: links)
@@ -182,6 +182,7 @@ extension Document where IncludeType == NoIncludes, MetaType == NoMetadata, Link
self.init(apiDescription: .none, body: body)
}
}
*/
extension Document {
private enum RootCodingKeys: String, CodingKey {
+2
View File
@@ -143,6 +143,7 @@ extension Entity where EntityRawIdType == Unidentified {
}
}
/*
extension Entity where Description.Attributes == NoAttributes {
public init(id: Entity.Id, relationships: Description.Relationships, meta: MetaType, links: LinksType) {
self.init(id: id, attributes: NoAttributes(), relationships: relationships, meta: meta, links: links)
@@ -370,6 +371,7 @@ extension Entity where MetaType == NoMetadata, LinksType == NoLinks, EntityRawId
self.init(attributes: attributes, relationships: relationships, meta: .none, links: .none)
}
}
*/
// MARK: Pointer for Relationships use.
public extension Entity where EntityRawIdType: JSONAPI.RawIdType {
@@ -39,6 +39,14 @@ extension TransformedAttribute: ExpressibleByFloatLiteral where RawValue: Expres
}
}
extension Optional: ExpressibleByFloatLiteral where Wrapped: ExpressibleByFloatLiteral {
public typealias FloatLiteralType = Wrapped.FloatLiteralType
public init(floatLiteral value: FloatLiteralType) {
self = .some(Wrapped(floatLiteral: value))
}
}
extension TransformedAttribute: ExpressibleByBooleanLiteral where RawValue: ExpressibleByBooleanLiteral, Transformer == IdentityTransformer<RawValue> {
public typealias BooleanLiteralType = RawValue.BooleanLiteralType
@@ -47,6 +55,14 @@ extension TransformedAttribute: ExpressibleByBooleanLiteral where RawValue: Expr
}
}
extension Optional: ExpressibleByBooleanLiteral where Wrapped: ExpressibleByBooleanLiteral {
public typealias BooleanLiteralType = Wrapped.BooleanLiteralType
public init(booleanLiteral value: BooleanLiteralType) {
self = .some(Wrapped(booleanLiteral: value))
}
}
extension TransformedAttribute: ExpressibleByIntegerLiteral where RawValue: ExpressibleByIntegerLiteral, Transformer == IdentityTransformer<RawValue> {
public typealias IntegerLiteralType = RawValue.IntegerLiteralType
@@ -55,6 +71,14 @@ extension TransformedAttribute: ExpressibleByIntegerLiteral where RawValue: Expr
}
}
extension Optional: ExpressibleByIntegerLiteral where Wrapped: ExpressibleByIntegerLiteral {
public typealias IntegerLiteralType = Wrapped.IntegerLiteralType
public init(integerLiteral value: IntegerLiteralType) {
self = .some(Wrapped(integerLiteral: value))
}
}
// regretably, array and dictionary literals are not so easy because Dictionaries and Arrays
// cannot be turned back into variadic arguments to pass onto the RawValue type's constructor.
@@ -79,6 +103,16 @@ extension TransformedAttribute: ExpressibleByDictionaryLiteral where RawValue: D
}
}
extension Optional: DictionaryType where Wrapped: DictionaryType {
public typealias Key = Wrapped.Key
public typealias Value = Wrapped.Value
public init<S>(_ keysAndValues: S, uniquingKeysWith combine: (Dictionary<Key, Value>.Value, Dictionary<Key, Value>.Value) throws -> Dictionary<Key, Value>.Value) rethrows where S : Sequence, S.Element == (Key, Value) {
self = try .some(Wrapped(keysAndValues, uniquingKeysWith: combine))
}
}
public protocol ArrayType {
associatedtype Element
@@ -94,3 +128,11 @@ extension TransformedAttribute: ExpressibleByArrayLiteral where RawValue: ArrayT
self.init(value: RawValue(elements))
}
}
extension Optional: ArrayType where Wrapped: ArrayType {
public typealias Element = Wrapped.Element
public init<S>(_ s: S) where Element == S.Element, S : Sequence {
self = .some(Wrapped(s))
}
}
@@ -11,7 +11,7 @@ import JSONAPITestLib
class Attribute_FunctorTests: XCTestCase {
func test_mapGuaranteed() {
let entity = try? TestType(attributes: .init(name: "Frankie", number: .init(rawValue: 22.0)))
let entity = try? TestType(attributes: .init(name: "Frankie", number: .init(rawValue: 22.0)), relationships: .none, meta: .none, links: .none)
XCTAssertNotNil(entity)
@@ -19,7 +19,7 @@ class Attribute_FunctorTests: XCTestCase {
}
func test_mapOptionalSuccess() {
let entity = try? TestType(attributes: .init(name: "Frankie", number: .init(rawValue: 22.0)))
let entity = try? TestType(attributes: .init(name: "Frankie", number: .init(rawValue: 22.0)), relationships: .none, meta: .none, links: .none)
XCTAssertNotNil(entity)
@@ -27,7 +27,7 @@ class Attribute_FunctorTests: XCTestCase {
}
func test_mapOptionalFailure() {
let entity = try? TestType(attributes: .init(name: "Frankie", number: .init(rawValue: 22.5)))
let entity = try? TestType(attributes: .init(name: "Frankie", number: .init(rawValue: 22.5)), relationships: .none, meta: .none, links: .none)
XCTAssertNotNil(entity)
@@ -847,7 +847,7 @@ extension DocumentTests {
// MARK: Poly PrimaryResource Tests
extension DocumentTests {
func test_singleDocument_PolyPrimaryResource() {
let article = Article(id: Id(rawValue: "1"), relationships: .init(author: ToOneRelationship(id: Id(rawValue: "33"))))
let article = Article(id: Id(rawValue: "1"), attributes: .none, relationships: .init(author: ToOneRelationship(id: Id(rawValue: "33"))), meta: .none, links: .none)
let document = decoded(type: Document<SingleResourceBody<Poly2<Article, Author>>, NoMetadata, NoLinks, NoIncludes, NoAPIDescription, UnknownJSONAPIError>.self, data: single_document_no_includes)
XCTAssertEqual(document.body.primaryData?.value[Article.self], article)
@@ -859,7 +859,7 @@ extension DocumentTests {
}
func test_singleDocument_PolyPrimaryResourceWithAPIDescription() {
let article = Article(id: Id(rawValue: "1"), relationships: .init(author: ToOneRelationship(id: Id(rawValue: "33"))))
let article = Article(id: Id(rawValue: "1"), attributes: .none, relationships: .init(author: ToOneRelationship(id: Id(rawValue: "33"))), meta: .none, links: .none)
let document = decoded(type: Document<SingleResourceBody<Poly2<Article, Author>>, NoMetadata, NoLinks, NoIncludes, TestAPIDescription, UnknownJSONAPIError>.self, data: single_document_no_includes_with_api_description)
XCTAssertEqual(document.body.primaryData?.value[Article.self], article)
+32 -32
View File
@@ -12,15 +12,15 @@ import JSONAPITestLib
class EntityTests: XCTestCase {
func test_relationship_access() {
let entity1 = TestEntity1()
let entity2 = TestEntity2(relationships: .init(other: entity1.pointer))
let entity1 = TestEntity1(attributes: .none, relationships: .none, meta: .none, links: .none)
let entity2 = TestEntity2(attributes: .none, relationships: .init(other: entity1.pointer), meta: .none, links: .none)
XCTAssertEqual(entity2.relationships.other, entity1.pointer)
}
func test_relationship_operator_access() {
let entity1 = TestEntity1()
let entity2 = TestEntity2(relationships: .init(other: entity1.pointer))
let entity1 = TestEntity1(attributes: .none, relationships: .none, meta: .none, links: .none)
let entity2 = TestEntity2(attributes: .none, relationships: .init(other: entity1.pointer), meta: .none, links: .none)
XCTAssertEqual(entity2 ~> \.other, entity1.id)
}
@@ -30,10 +30,10 @@ class EntityTests: XCTestCase {
}
func test_toMany_relationship_operator_access() {
let entity1 = TestEntity1()
let entity2 = TestEntity1()
let entity4 = TestEntity1()
let entity3 = TestEntity3(relationships: .init(others: .init(pointers: [entity1.pointer, entity2.pointer, entity4.pointer])))
let entity1 = TestEntity1(attributes: .none, relationships: .none, meta: .none, links: .none)
let entity2 = TestEntity1(attributes: .none, relationships: .none, meta: .none, links: .none)
let entity4 = TestEntity1(attributes: .none, relationships: .none, meta: .none, links: .none)
let entity3 = TestEntity3(attributes: .none, relationships: .init(others: .init(pointers: [entity1.pointer, entity2.pointer, entity4.pointer])), meta: .none, links: .none)
XCTAssertEqual(entity3 ~> \.others, [entity1.id, entity2.id, entity4.id])
}
@@ -43,8 +43,8 @@ class EntityTests: XCTestCase {
}
func test_relationshipIds() {
let entity1 = TestEntity1()
let entity2 = TestEntity2(relationships: .init(other: entity1.pointer))
let entity1 = TestEntity1(attributes: .none, relationships: .none, meta: .none, links: .none)
let entity2 = TestEntity2(attributes: .none, relationships: .init(other: entity1.pointer), meta: .none, links: .none)
XCTAssertEqual(entity2.relationships.other.id, entity1.id)
}
@@ -60,31 +60,31 @@ class EntityTests: XCTestCase {
}
func test_initialization() {
let entity1 = TestEntity1(id: .init(rawValue: "wow"))
let entity2 = TestEntity2(id: .init(rawValue: "cool"), relationships: .init(other: .init(entity: entity1)))
let _ = TestEntity2(id: .init(rawValue: "cool"), attributes: .none, relationships: .init(other: .init(entity: entity1)))
let _ = TestEntity2(id: .init(rawValue: "cool"), relationships: .init(other: .init(entity: entity1)), meta: .none)
let _ = TestEntity3(id: .init(rawValue: "3"), relationships: .init(others: .init(ids: [.init(rawValue: "10"), .init(rawValue: "20"), entity1.id])))
let _ = TestEntity3(id: .init(rawValue: "3"), relationships: .init(others: .none))
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))
let _ = TestEntity5(id: .init(rawValue: "5"), attributes: .init(floater: .init(value: 10.2)))
let _ = TestEntity6(id: .init(rawValue: "6"), attributes: .init(here: .init(value: "here"), maybeHere: nil, maybeNull: .init(value: nil)))
let _ = TestEntity7(id: .init(rawValue: "7"), attributes: .init(here: .init(value: "hello"), maybeHereMaybeNull: .init(value: "world")))
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))))
let _ = TestEntity9(id: .init(rawValue: "9"), relationships: .init(one: entity1.pointer, nullableOne: nil, optionalOne: nil, optionalNullableOne: nil, optionalMany: nil))
let _ = TestEntity9(id: .init(rawValue: "9"), relationships: .init(one: entity1.pointer, nullableOne: .init(entity: nil), optionalOne: nil, optionalNullableOne: nil, optionalMany: nil))
let _ = TestEntity9(id: .init(rawValue: "9"), relationships: .init(one: entity1.pointer, nullableOne: .init(entity: entity1, meta: .none, links: .none), optionalOne: nil, optionalNullableOne: nil, optionalMany: nil))
let _ = TestEntity9(id: .init(rawValue: "9"), relationships: .init(one: entity1.pointer, nullableOne: nil, optionalOne: entity1.pointer, optionalNullableOne: nil, optionalMany: nil))
let _ = TestEntity9(id: .init(rawValue: "9"), relationships: .init(one: entity1.pointer, nullableOne: nil, optionalOne: nil, optionalNullableOne: .init(entity: entity1, meta: .none, links: .none), optionalMany: nil))
let _ = TestEntity9(id: .init(rawValue: "9"), relationships: .init(one: entity1.pointer, nullableOne: nil, optionalOne: nil, optionalNullableOne: .init(entity: entity1, meta: .none, links: .none), optionalMany: .init(entities: [], meta: .none, links: .none)))
let entity1 = TestEntity1(id: .init(rawValue: "wow"), attributes: .none, relationships: .none, meta: .none, links: .none)
let entity2 = TestEntity2(id: .init(rawValue: "cool"), attributes: .none, relationships: .init(other: .init(entity: entity1)), meta: .none, links: .none)
let _ = TestEntity2(id: .init(rawValue: "cool"), attributes: .none, relationships: .init(other: .init(entity: entity1)), meta: .none, links: .none)
let _ = TestEntity2(id: .init(rawValue: "cool"), attributes: .none, relationships: .init(other: .init(entity: entity1)), meta: .none, links: .none)
let _ = TestEntity3(id: .init(rawValue: "3"), attributes: .none, relationships: .init(others: .init(ids: [.init(rawValue: "10"), .init(rawValue: "20"), entity1.id])), meta: .none, links: .none)
let _ = TestEntity3(id: .init(rawValue: "3"), attributes: .none, relationships: .init(others: .none), meta: .none, links: .none)
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), meta: .none, links: .none)
let _ = TestEntity5(id: .init(rawValue: "5"), attributes: .init(floater: .init(value: 10.2)), relationships: .none, meta: .none, links: .none)
let _ = TestEntity6(id: .init(rawValue: "6"), attributes: .init(here: .init(value: "here"), maybeHere: nil, maybeNull: .init(value: nil)), relationships: .none, meta: .none, links: .none)
let _ = TestEntity7(id: .init(rawValue: "7"), attributes: .init(here: .init(value: "hello"), maybeHereMaybeNull: .init(value: "world")), relationships: .none, meta: .none, links: .none)
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)), relationships: .none, meta: .none, links: .none))
let _ = TestEntity9(id: .init(rawValue: "9"), attributes: .none, relationships: .init(one: entity1.pointer, nullableOne: nil, optionalOne: nil, optionalNullableOne: nil, optionalMany: nil), meta: .none, links: .none)
let _ = TestEntity9(id: .init(rawValue: "9"), attributes: .none, relationships: .init(one: entity1.pointer, nullableOne: .init(entity: nil), optionalOne: nil, optionalNullableOne: nil, optionalMany: nil), meta: .none, links: .none)
let _ = TestEntity9(id: .init(rawValue: "9"), attributes: .none, relationships: .init(one: entity1.pointer, nullableOne: .init(entity: entity1, meta: .none, links: .none), optionalOne: nil, optionalNullableOne: nil, optionalMany: nil), meta: .none, links: .none)
let _ = TestEntity9(id: .init(rawValue: "9"), attributes: .none, relationships: .init(one: entity1.pointer, nullableOne: nil, optionalOne: entity1.pointer, optionalNullableOne: nil, optionalMany: nil), meta: .none, links: .none)
let _ = TestEntity9(id: .init(rawValue: "9"), attributes: .none, relationships: .init(one: entity1.pointer, nullableOne: nil, optionalOne: nil, optionalNullableOne: .init(entity: entity1, meta: .none, links: .none), optionalMany: nil), meta: .none, links: .none)
let _ = TestEntity9(id: .init(rawValue: "9"), attributes: .none, relationships: .init(one: entity1.pointer, nullableOne: nil, optionalOne: nil, optionalNullableOne: .init(entity: entity1, meta: .none, links: .none), optionalMany: .init(entities: [], meta: .none, links: .none)), meta: .none, links: .none)
let e10id1 = TestEntity10.Identifier(rawValue: "hello")
let e10id2 = TestEntity10.Id(rawValue: "world")
let e10id3: TestEntity10.Id = "!"
let _ = TestEntity10(id: .init(rawValue: "10"), relationships: .init(selfRef: .init(id: e10id1), selfRefs: .init(ids: [e10id2, e10id3])))
XCTAssertNoThrow(try TestEntity11(id: .init(rawValue: "11"), attributes: .init(number: .init(rawValue: 11))))
let _ = UnidentifiedTestEntity(attributes: .init(me: .init(value: "hello")))
let _ = UnidentifiedTestEntityWithMeta(attributes: .init(me: .init(value: "hello")), meta: .init(x: "world", y: nil))
let _ = UnidentifiedTestEntityWithLinks(attributes: .init(me: .init(value: "hello")), links: .init(link1: .init(url: "hmmm")))
let _ = TestEntity10(id: .init(rawValue: "10"), attributes: .none, relationships: .init(selfRef: .init(id: e10id1), selfRefs: .init(ids: [e10id2, e10id3])), meta: .none, links: .none)
XCTAssertNoThrow(try TestEntity11(id: .init(rawValue: "11"), attributes: .init(number: .init(rawValue: 11)), relationships: .none, meta: .none, links: .none))
let _ = UnidentifiedTestEntity(attributes: .init(me: .init(value: "hello")), relationships: .none, meta: .none, links: .none)
let _ = UnidentifiedTestEntityWithMeta(attributes: .init(me: .init(value: "hello")), relationships: .none, meta: .init(x: "world", y: nil), links: .none)
let _ = UnidentifiedTestEntityWithLinks(attributes: .init(me: .init(value: "hello")), relationships: .none, meta: .none, links: .init(link1: .init(url: "hmmm")))
}
}
@@ -15,28 +15,121 @@ class Attribute_LiteralTests: XCTestCase {
XCTAssertEqual(Attribute<String>(value: "hello"), "hello")
}
func test_NullableStringLiteral() {
XCTAssertEqual(Attribute<String?>(value: "world"), "world")
}
func test_OptionalStringLiteral() {
let x: Attribute<String>? = .init(value: "ok")
XCTAssertEqual(x, "ok")
}
func test_NullableOptionalStringLiteral() {
let x: Attribute<String?>? = .init(value: "hello")
XCTAssertEqual(x, "hello")
}
func test_BooleanLiteral() {
XCTAssertEqual(Attribute<Bool>(value: false), false)
}
func test_NullableBooleanLiteral() {
XCTAssertEqual(Attribute<Bool?>(value: true), true)
}
func test_OptionalBooleanLiteral() {
let x: Attribute<Bool>? = .init(value: false)
XCTAssertEqual(x, false)
}
func test_NullableOptionalBooleanLiteral() {
let x: Attribute<Bool?>? = .init(value: true)
XCTAssertEqual(x, true)
}
func test_IntegerLiteral() {
XCTAssertEqual(Attribute<Int>(value: 12), 12)
}
func test_NullableIntegerLiteral() {
XCTAssertEqual(Attribute<Int?>(value: 13), 13)
}
func test_OptionalIntegerLiteral() {
let x: Attribute<Int>? = .init(value: 14)
XCTAssertEqual(x, 14)
}
func test_NullableOptionalIntegerLiteral() {
let x: Attribute<Int?>? = .init(value: 15)
XCTAssertEqual(x, 15)
}
func test_FloatLiteral() {
XCTAssertEqual(Attribute<Float>(value: 1.2), 1.2)
XCTAssertEqual(Attribute<Double>(value: 1.2), 1.2)
}
func test_NullableFloatLiteral() {
XCTAssertEqual(Attribute<Float?>(value: 2.3), 2.3)
XCTAssertEqual(Attribute<Double?>(value: 3.4), 3.4)
}
func test_OptionalFloatLiteral() {
let x: Attribute<Float>? = .init(value: 2.3)
let y: Attribute<Double>? = .init(value: 3.4)
XCTAssertEqual(x, 2.3)
XCTAssertEqual(y, 3.4)
}
func test_NullableOptionalFloatLiteral() {
let x: Attribute<Float?>? = .init(value: 2.3)
let y: Attribute<Double?>? = .init(value: 3.4)
XCTAssertEqual(x, 2.3)
XCTAssertEqual(y, 3.4)
}
func test_ArrayLiteral() {
XCTAssertEqual(Attribute<[String]>(value: ["hello", "world"]), ["hello", "world"])
}
func test_NullableArrayLiteral() {
XCTAssertEqual(Attribute<[String]?>(value: ["hello", "world"]), ["hello", "world"])
}
func test_OptionalArrayLiteral() {
let x: Attribute<[String]>? = .init(value: ["hello", "world"])
XCTAssertEqual(x, ["hello", "world"])
}
func test_NullableOptionalArrayLiteral() {
let x: Attribute<[String]?>? = .init(value: ["hello", "world"])
XCTAssertEqual(x, ["hello", "world"])
}
func test_DictionaryLiteral() {
XCTAssertEqual(Attribute<[String : Int]>(value: ["hello": 1]), ["hello": 1])
}
func test_NullableDictionaryLiteral() {
XCTAssertEqual(Attribute<[String: Int]?>(value: ["hello": 1]), ["hello": 1])
}
func test_OptionalDictionaryLiteral() {
let x: Attribute<[String: Int]>? = .init(value: ["hello": 1])
XCTAssertEqual(x, ["hello": 1])
}
func test_NullableOptionalDictionaryLiteral() {
let x: Attribute<[String: Int]?>? = .init(value: ["hello": 1])
XCTAssertEqual(x, ["hello": 1])
}
func test_NilLiteral() {
XCTAssertEqual(Attribute<String?>(value: nil), nil)
}
func test_OptionalNilLiteral() {
let _: Attribute<String?>? = nil
}
}
@@ -13,27 +13,27 @@ import JSONAPITestLib
// in this file.
class EntityCheckTests: XCTestCase {
func test_failsWithEnumAttributes() {
let entity = EnumAttributesEntity(attributes: .hello)
let entity = EnumAttributesEntity(attributes: .hello, relationships: .none, meta: .none, links: .none)
XCTAssertThrowsError(try EnumAttributesEntity.check(entity))
}
func test_failsWithEnumRelationships() {
let entity = EnumRelationshipsEntity(relationships: .hello)
let entity = EnumRelationshipsEntity(attributes: .none, relationships: .hello, meta: .none, links: .none)
XCTAssertThrowsError(try EnumRelationshipsEntity.check(entity))
}
func test_failsWithBadAttribute() {
let entity = BadAttributeEntity(attributes: .init(x: "ok", y: "not ok"))
let entity = BadAttributeEntity(attributes: .init(x: "ok", y: "not ok"), relationships: .none, meta: .none, links: .none)
XCTAssertThrowsError(try BadAttributeEntity.check(entity))
}
func test_failsWithBadRelationship() {
let entity = BadRelationshipEntity(relationships: .init(x: OkEntity().pointer, y: OkEntity().id))
let entity = BadRelationshipEntity(attributes: .none, relationships: .init(x: OkEntity(attributes: .none, relationships: .none, meta: .none, links: .none).pointer, y: OkEntity(attributes: .none, relationships: .none, meta: .none, links: .none).id), meta: .none, links: .none)
XCTAssertThrowsError(try BadRelationshipEntity.check(entity))
}
func test_failsWithOptionalArrayAttribute() {
let entity = OptionalArrayAttributeEntity(attributes: .init(x: ["hello"], y: nil))
let entity = OptionalArrayAttributeEntity(attributes: .init(x: ["hello"], y: nil), relationships: .none, meta: .none, links: .none)
XCTAssertThrowsError(try OptionalArrayAttributeEntity.check(entity))
}
}
@@ -13,7 +13,7 @@ class NonJSONAPIRelatableTests: XCTestCase {
let e1 = NonJSONAPIEntity(id: .init(rawValue: "hello"))
let e2 = NonJSONAPIEntity(id: .init(rawValue: "world"))
let entity = TestEntity(relationships: .init(one: .init(id: e1.id), many: .init(ids: [e1.id, e2.id])))
let entity = TestEntity(attributes: .none, relationships: .init(one: .init(id: e1.id), many: .init(ids: [e1.id, e2.id])), meta: .none, links: .none)
XCTAssertEqual(entity ~> \.one, e1.id)
XCTAssertEqual(entity ~> \.many, [e1.id, e2.id])
@@ -25,7 +25,7 @@ class NonJSONAPIRelatableTests: XCTestCase {
let e1 = NonJSONAPIEntity(id: .init(rawValue: "hello"))
let e2 = NonJSONAPIEntity(id: .init(rawValue: "world"))
let entity = TestEntity2(relationships: .init(nullableOne: .init(id: e1.id), nullableMaybeOne: .init(id: e2.id), maybeOne: .init(id: e2.id), maybeMany: .init(ids: [e2.id, e1.id])))
let entity = TestEntity2(attributes: .none, relationships: .init(nullableOne: .init(id: e1.id), nullableMaybeOne: .init(id: e2.id), maybeOne: .init(id: e2.id), maybeMany: .init(ids: [e2.id, e1.id])), meta: .none, links: .none)
XCTAssertEqual((entity ~> \.nullableOne)?.rawValue, "hello")
XCTAssertEqual((entity ~> \.nullableMaybeOne)?.rawValue, "world")
@@ -35,8 +35,8 @@ class NonJSONAPIRelatableTests: XCTestCase {
func test_initialization2_all_relationships_missing() {
let entity = TestEntity2(relationships: .init(nullableOne: .init(id: nil), nullableMaybeOne: .init(id: nil), maybeOne: nil, maybeMany: nil))
let entity2 = TestEntity2(relationships: .init(nullableOne: .init(id: nil), nullableMaybeOne: nil, maybeOne: nil, maybeMany: nil))
let entity = TestEntity2(attributes: .none, relationships: .init(nullableOne: .init(id: nil), nullableMaybeOne: .init(id: nil), maybeOne: nil, maybeMany: nil), meta: .none, links: .none)
let entity2 = TestEntity2(attributes: .none, relationships: .init(nullableOne: .init(id: nil), nullableMaybeOne: nil, maybeOne: nil, maybeMany: nil), meta: .none, links: .none)
XCTAssertNil((entity ~> \.nullableOne))
XCTAssertNil((entity ~> \.nullableMaybeOne))
+7 -7
View File
@@ -15,13 +15,13 @@ class PolyTests: XCTestCase {
}
func test_init_Poly1() {
let entity = TestEntity5()
let entity = TestEntity5(attributes: .none, relationships: .none, meta: .none, links: .none)
let poly = Poly1(entity)
XCTAssertEqual(poly.a, entity)
}
func test_init_Poly2() {
let entity = TestEntity5()
let entity = TestEntity5(attributes: .none, relationships: .none, meta: .none, links: .none)
let poly = Poly2<TestEntity5, TestEntity>(entity)
XCTAssertEqual(poly.a, entity)
XCTAssertNil(poly.b)
@@ -32,7 +32,7 @@ class PolyTests: XCTestCase {
}
func test_init_Poly3() {
let entity = TestEntity5()
let entity = TestEntity5(attributes: .none, relationships: .none, meta: .none, links: .none)
let poly = Poly3<TestEntity5, TestEntity, TestEntity2>(entity)
XCTAssertEqual(poly.a, entity)
XCTAssertNil(poly.b)
@@ -50,7 +50,7 @@ class PolyTests: XCTestCase {
}
func test_init_Poly4() {
let entity = TestEntity5()
let entity = TestEntity5(attributes: .none, relationships: .none, meta: .none, links: .none)
let poly = Poly4<TestEntity5, TestEntity, TestEntity2, TestEntity3>(entity)
XCTAssertEqual(poly.a, entity)
XCTAssertNil(poly.b)
@@ -77,7 +77,7 @@ class PolyTests: XCTestCase {
}
func test_init_Poly5() {
let entity = TestEntity5()
let entity = TestEntity5(attributes: .none, relationships: .none, meta: .none, links: .none)
let poly = Poly5<TestEntity5, TestEntity, TestEntity2, TestEntity3, TestEntity4>(entity)
XCTAssertEqual(poly.a, entity)
XCTAssertNil(poly.b)
@@ -115,7 +115,7 @@ class PolyTests: XCTestCase {
}
func test_init_Poly6() {
let entity = TestEntity5()
let entity = TestEntity5(attributes: .none, relationships: .none, meta: .none, links: .none)
let poly = Poly6<TestEntity5, TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity6>(entity)
XCTAssertEqual(poly.a, entity)
XCTAssertNil(poly.b)
@@ -166,7 +166,7 @@ class PolyTests: XCTestCase {
}
func test_init_Poly7() {
let entity = TestEntity5()
let entity = TestEntity5(attributes: .none, relationships: .none, meta: .none, links: .none)
let poly = Poly7<TestEntity5, TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity6, TestEntity7>(entity)
XCTAssertEqual(poly.a, entity)
XCTAssertNil(poly.b)
@@ -11,10 +11,10 @@ import JSONAPI
class RelationshipTests: XCTestCase {
func test_initToManyWithEntities() {
let entity1 = TestEntity1()
let entity2 = TestEntity1()
let entity3 = TestEntity1()
let entity4 = TestEntity1()
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, NoMetadata, NoLinks>(entities: [entity1, entity2, entity3, entity4])
XCTAssertEqual(relationship.ids.count, 4)
@@ -22,10 +22,10 @@ class RelationshipTests: XCTestCase {
}
func test_initToManyWithRelationships() {
let entity1 = TestEntity1()
let entity2 = TestEntity1()
let entity3 = TestEntity1()
let entity4 = TestEntity1()
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, NoMetadata, NoLinks>(pointers: [entity1.pointer, entity2.pointer, entity3.pointer, entity4.pointer])
XCTAssertEqual(relationship.ids.count, 4)
@@ -151,7 +151,7 @@ extension RelationshipTests {
}
func test_ToOneNullableIsEqualToNonNullableIfNotNil() {
let entity = TestEntity1()
let entity = TestEntity1(attributes: .none, relationships: .none, meta: .none, links: .none)
let relationship1 = ToOneNonNullable(entity: entity)
let relationship2 = ToOneNullable(entity: entity)
@@ -12,8 +12,8 @@ class ResourceBodyTests: XCTestCase {
func test_initializers() {
let articles = [
Article(attributes: .init(title: "hello")),
Article(attributes: .init(title: "world"))
Article(attributes: .init(title: "hello"), relationships: .none, meta: .none, links: .none),
Article(attributes: .init(title: "world"), relationships: .none, meta: .none, links: .none)
]
let _ = SingleResourceBody(entity: articles[0])
let _ = ManyResourceBody(entities: articles)
@@ -25,7 +25,7 @@ class ResourceBodyTests: XCTestCase {
data: single_resource_body)
XCTAssertEqual(body.value, Article(id: Id<String, Article>(rawValue: "1"),
attributes: ArticleType.Attributes(title: try! .init(rawValue: "JSON:API paints my bikeshed!"))))
attributes: ArticleType.Attributes(title: try! .init(rawValue: "JSON:API paints my bikeshed!")), relationships: .none, meta: .none, links: .none))
}
func test_singleResourceBody_encode() {
@@ -38,9 +38,9 @@ class ResourceBodyTests: XCTestCase {
data: many_resource_body)
XCTAssertEqual(body.values, [
Article(id: .init(rawValue: "1"), attributes: try! .init(title: .init(rawValue: "JSON:API paints my bikeshed!"))),
Article(id: .init(rawValue: "2"), attributes: try! .init(title: .init(rawValue: "Sick"))),
Article(id: .init(rawValue: "3"), attributes: try! .init(title: .init(rawValue: "Hello World")))
Article(id: .init(rawValue: "1"), attributes: try! .init(title: .init(rawValue: "JSON:API paints my bikeshed!")), relationships: .none, meta: .none, links: .none),
Article(id: .init(rawValue: "2"), attributes: try! .init(title: .init(rawValue: "Sick")), relationships: .none, meta: .none, links: .none),
Article(id: .init(rawValue: "3"), attributes: try! .init(title: .init(rawValue: "Hello World")), relationships: .none, meta: .none, links: .none)
])
}
+19
View File
@@ -39,6 +39,25 @@ extension Attribute_LiteralTests {
("test_FloatLiteral", test_FloatLiteral),
("test_IntegerLiteral", test_IntegerLiteral),
("test_NilLiteral", test_NilLiteral),
("test_NullableArrayLiteral", test_NullableArrayLiteral),
("test_NullableBooleanLiteral", test_NullableBooleanLiteral),
("test_NullableDictionaryLiteral", test_NullableDictionaryLiteral),
("test_NullableFloatLiteral", test_NullableFloatLiteral),
("test_NullableIntegerLiteral", test_NullableIntegerLiteral),
("test_NullableOptionalArrayLiteral", test_NullableOptionalArrayLiteral),
("test_NullableOptionalBooleanLiteral", test_NullableOptionalBooleanLiteral),
("test_NullableOptionalDictionaryLiteral", test_NullableOptionalDictionaryLiteral),
("test_NullableOptionalFloatLiteral", test_NullableOptionalFloatLiteral),
("test_NullableOptionalIntegerLiteral", test_NullableOptionalIntegerLiteral),
("test_NullableOptionalStringLiteral", test_NullableOptionalStringLiteral),
("test_NullableStringLiteral", test_NullableStringLiteral),
("test_OptionalArrayLiteral", test_OptionalArrayLiteral),
("test_OptionalBooleanLiteral", test_OptionalBooleanLiteral),
("test_OptionalDictionaryLiteral", test_OptionalDictionaryLiteral),
("test_OptionalFloatLiteral", test_OptionalFloatLiteral),
("test_OptionalIntegerLiteral", test_OptionalIntegerLiteral),
("test_OptionalNilLiteral", test_OptionalNilLiteral),
("test_OptionalStringLiteral", test_OptionalStringLiteral),
("test_StringLiteral", test_StringLiteral),
]
}