From 16b83ddbef3b1d47a36a94076c314e7c58d8f717 Mon Sep 17 00:00:00 2001 From: Mathew Polzin Date: Fri, 23 Nov 2018 19:09:53 -0800 Subject: [PATCH] lots of tests of Poly --- Sources/JSONAPI/Resource/Poly.swift | 11 +- Sources/JSONAPI/Resource/Relationship.swift | 1 + .../JSONAPITests/Includes/IncludeTests.swift | 8 +- Tests/JSONAPITests/Poly/PolyTests.swift | 331 ++++++++++++++++++ Tests/JSONAPITests/Poly/stubs/PolyStubs.swift | 89 +++++ 5 files changed, 434 insertions(+), 6 deletions(-) diff --git a/Sources/JSONAPI/Resource/Poly.swift b/Sources/JSONAPI/Resource/Poly.swift index 20f64e9..3b534cf 100644 --- a/Sources/JSONAPI/Resource/Poly.swift +++ b/Sources/JSONAPI/Resource/Poly.swift @@ -19,14 +19,14 @@ public protocol Poly: Codable, Equatable {} // MARK: - Generic Decoding -private func decode(_ type: Entity.Type, from container: SingleValueDecodingContainer) throws -> Result { - let ret: Result +private func decode(_ type: Entity.Type, from container: SingleValueDecodingContainer) throws -> Result { + let ret: Result do { ret = try .success(container.decode(Entity.self)) - } catch (let err as EncodingError) { + } catch (let err as DecodingError) { ret = .failure(err) } catch (let err) { - ret = .failure(EncodingError.invalidValue(Entity.Description.self, + ret = .failure(DecodingError.typeMismatch(Entity.Description.self, .init(codingPath: container.codingPath, debugDescription: err.localizedDescription, underlyingError: err))) @@ -41,10 +41,11 @@ public struct Poly0: _Poly0 { public init() {} public init(from decoder: Decoder) throws { + throw JSONAPIEncodingError.illegalDecoding("Attempted to decode Poly0, which should represent a thing that is not expected to be found in a document.") } public func encode(to encoder: Encoder) throws { - throw JSONAPIEncodingError.illegalEncoding("Attempted to encode Include0, which should be represented by the absence of an 'included' entry altogether.") + throw JSONAPIEncodingError.illegalEncoding("Attempted to encode Poly0, which should represent a thing that is not expected to be found in a document.") } } diff --git a/Sources/JSONAPI/Resource/Relationship.swift b/Sources/JSONAPI/Resource/Relationship.swift index b2073de..47b5564 100644 --- a/Sources/JSONAPI/Resource/Relationship.swift +++ b/Sources/JSONAPI/Resource/Relationship.swift @@ -96,6 +96,7 @@ private enum ResourceIdentifierCodingKeys: String, CodingKey { public enum JSONAPIEncodingError: Swift.Error { case typeMismatch(expected: String, found: String) case illegalEncoding(String) + case illegalDecoding(String) case missingOrMalformedMetadata } diff --git a/Tests/JSONAPITests/Includes/IncludeTests.swift b/Tests/JSONAPITests/Includes/IncludeTests.swift index b5c2d18..6036e66 100644 --- a/Tests/JSONAPITests/Includes/IncludeTests.swift +++ b/Tests/JSONAPITests/Includes/IncludeTests.swift @@ -5,7 +5,12 @@ import JSONAPI class IncludedTests: XCTestCase { let decoder = JSONDecoder() - + + func test_zeroIncludes_init() { + let includes = Includes() + XCTAssertEqual(includes.count, 0) + } + func test_zeroIncludes() { let includes = decoded(type: Includes.self, data: two_same_type_includes) @@ -118,6 +123,7 @@ class IncludedTests: XCTestCase { } } +// MARK: - Test types extension IncludedTests { enum TestEntityType: EntityDescription { diff --git a/Tests/JSONAPITests/Poly/PolyTests.swift b/Tests/JSONAPITests/Poly/PolyTests.swift index 87a8eb7..515ac03 100644 --- a/Tests/JSONAPITests/Poly/PolyTests.swift +++ b/Tests/JSONAPITests/Poly/PolyTests.swift @@ -8,6 +8,337 @@ import XCTest import JSONAPI +// MARK: - init class PolyTests: XCTestCase { + func test_init_Poly0() { + let _ = Poly0() + } + func test_init_Poly1() { + let entity = TestEntity5() + let poly = Poly1(entity) + XCTAssertEqual(poly.a, entity) + } + + func test_init_Poly2() { + let entity = TestEntity5() + let poly = Poly2(entity) + XCTAssertEqual(poly.a, entity) + XCTAssertNil(poly.b) + + let poly2 = Poly2(entity) + XCTAssertEqual(poly2.b, entity) + XCTAssertNil(poly2.a) + } + + func test_init_Poly3() { + let entity = TestEntity5() + let poly = Poly3(entity) + XCTAssertEqual(poly.a, entity) + XCTAssertNil(poly.b) + XCTAssertNil(poly.c) + + let poly2 = Poly3(entity) + XCTAssertEqual(poly2.b, entity) + XCTAssertNil(poly2.a) + XCTAssertNil(poly2.c) + + let poly3 = Poly3(entity) + XCTAssertEqual(poly3.c, entity) + XCTAssertNil(poly3.a) + XCTAssertNil(poly3.b) + } + + func test_init_Poly4() { + let entity = TestEntity5() + let poly = Poly4(entity) + XCTAssertEqual(poly.a, entity) + XCTAssertNil(poly.b) + XCTAssertNil(poly.c) + XCTAssertNil(poly.d) + + let poly2 = Poly4(entity) + XCTAssertEqual(poly2.b, entity) + XCTAssertNil(poly2.a) + XCTAssertNil(poly2.c) + XCTAssertNil(poly2.d) + + let poly3 = Poly4(entity) + XCTAssertEqual(poly3.c, entity) + XCTAssertNil(poly3.a) + XCTAssertNil(poly3.b) + XCTAssertNil(poly3.d) + + let poly4 = Poly4(entity) + XCTAssertEqual(poly4.d, entity) + XCTAssertNil(poly4.a) + XCTAssertNil(poly4.b) + XCTAssertNil(poly4.c) + } + + func test_init_Poly5() { + let entity = TestEntity5() + let poly = Poly5(entity) + XCTAssertEqual(poly.a, entity) + XCTAssertNil(poly.b) + XCTAssertNil(poly.c) + XCTAssertNil(poly.d) + XCTAssertNil(poly.e) + + let poly2 = Poly5(entity) + XCTAssertEqual(poly2.b, entity) + XCTAssertNil(poly2.a) + XCTAssertNil(poly2.c) + XCTAssertNil(poly2.d) + XCTAssertNil(poly2.e) + + let poly3 = Poly5(entity) + XCTAssertEqual(poly3.c, entity) + XCTAssertNil(poly3.a) + XCTAssertNil(poly3.b) + XCTAssertNil(poly3.d) + XCTAssertNil(poly3.e) + + let poly4 = Poly5(entity) + XCTAssertEqual(poly4.d, entity) + XCTAssertNil(poly4.a) + XCTAssertNil(poly4.b) + XCTAssertNil(poly4.c) + XCTAssertNil(poly4.e) + + let poly5 = Poly5(entity) + XCTAssertEqual(poly5.e, entity) + XCTAssertNil(poly5.a) + XCTAssertNil(poly5.b) + XCTAssertNil(poly5.c) + XCTAssertNil(poly5.d) + } + + func test_init_Poly6() { + let entity = TestEntity5() + let poly = Poly6(entity) + XCTAssertEqual(poly.a, entity) + XCTAssertNil(poly.b) + XCTAssertNil(poly.c) + XCTAssertNil(poly.d) + XCTAssertNil(poly.e) + XCTAssertNil(poly.f) + + let poly2 = Poly6(entity) + XCTAssertEqual(poly2.b, entity) + XCTAssertNil(poly2.a) + XCTAssertNil(poly2.c) + XCTAssertNil(poly2.d) + XCTAssertNil(poly2.e) + XCTAssertNil(poly2.f) + + let poly3 = Poly6(entity) + XCTAssertEqual(poly3.c, entity) + XCTAssertNil(poly3.a) + XCTAssertNil(poly3.b) + XCTAssertNil(poly3.d) + XCTAssertNil(poly3.e) + XCTAssertNil(poly3.f) + + let poly4 = Poly6(entity) + XCTAssertEqual(poly4.d, entity) + XCTAssertNil(poly4.a) + XCTAssertNil(poly4.b) + XCTAssertNil(poly4.c) + XCTAssertNil(poly4.e) + XCTAssertNil(poly4.f) + + let poly5 = Poly6(entity) + XCTAssertEqual(poly5.e, entity) + XCTAssertNil(poly5.a) + XCTAssertNil(poly5.b) + XCTAssertNil(poly5.c) + XCTAssertNil(poly5.d) + XCTAssertNil(poly5.f) + + let poly6 = Poly6(entity) + XCTAssertEqual(poly6.f, entity) + XCTAssertNil(poly6.a) + XCTAssertNil(poly6.b) + XCTAssertNil(poly6.c) + XCTAssertNil(poly6.d) + XCTAssertNil(poly6.e) + } +} + +// MARK: - subscript lookup +extension PolyTests { + func test_Poly1_lookup() { + let entity = decoded(type: TestEntity.self, data: poly_entity1) + let poly = decoded(type: Poly1.self, data: poly_entity1) + + XCTAssertEqual(entity, poly[TestEntity.self]) + } + + func test_Poly2_lookup() { + let entity = decoded(type: TestEntity2.self, data: poly_entity2) + let poly = decoded(type: Poly2.self, data: poly_entity2) + + XCTAssertNil(poly[TestEntity.self]) + XCTAssertEqual(entity, poly[TestEntity2.self]) + } + + func test_Poly3_lookup() { + let entity = decoded(type: TestEntity3.self, data: poly_entity3) + let poly = decoded(type: Poly3.self, data: poly_entity3) + + XCTAssertNil(poly[TestEntity.self]) + XCTAssertNil(poly[TestEntity2.self]) + XCTAssertEqual(entity, poly[TestEntity3.self]) + } + + func test_Poly4_lookup() { + let entity = decoded(type: TestEntity4.self, data: poly_entity4) + let poly = decoded(type: Poly4.self, data: poly_entity4) + + XCTAssertNil(poly[TestEntity.self]) + XCTAssertNil(poly[TestEntity2.self]) + XCTAssertNil(poly[TestEntity3.self]) + XCTAssertEqual(entity, poly[TestEntity4.self]) + } + + func test_Poly5_lookup() { + let entity = decoded(type: TestEntity5.self, data: poly_entity5) + let poly = decoded(type: Poly5.self, data: poly_entity5) + + XCTAssertNil(poly[TestEntity.self]) + XCTAssertNil(poly[TestEntity2.self]) + XCTAssertNil(poly[TestEntity3.self]) + XCTAssertNil(poly[TestEntity4.self]) + XCTAssertEqual(entity, poly[TestEntity5.self]) + } + + func test_Poly6_lookup() { + let entity = decoded(type: TestEntity6.self, data: poly_entity6) + let poly = decoded(type: Poly6.self, data: poly_entity6) + + XCTAssertNil(poly[TestEntity.self]) + XCTAssertNil(poly[TestEntity2.self]) + XCTAssertNil(poly[TestEntity3.self]) + XCTAssertNil(poly[TestEntity4.self]) + XCTAssertNil(poly[TestEntity5.self]) + XCTAssertEqual(entity, poly[TestEntity6.self]) + } +} + +// MARK: - failures +extension PolyTests { + func test_Poly0_encode_throws() { + XCTAssertThrowsError(try JSONEncoder().encode(Poly0())) + } + + func test_Poly0_decode_throws() { + XCTAssertThrowsError(try JSONDecoder().decode(Poly0.self, from: poly_entity1)) + } + + func test_Poly1_decode_throws_typeNotFound() { + XCTAssertThrowsError(try JSONDecoder().decode(Poly1.self, from: poly_entity2)) + } + + func test_Poly2_decode_throws_typeNotFound() { + XCTAssertThrowsError(try JSONDecoder().decode(Poly2.self, from: poly_entity3)) + } + + func test_Poly3_decode_throws_typeNotFound() { + XCTAssertThrowsError(try JSONDecoder().decode(Poly3.self, from: poly_entity4)) + } + + func test_Poly4_decode_throws_typeNotFound() { + XCTAssertThrowsError(try JSONDecoder().decode(Poly4.self, from: poly_entity5)) + } + + func test_Poly5_decode_throws_typeNotFound() { + XCTAssertThrowsError(try JSONDecoder().decode(Poly5.self, from: poly_entity6)) + } + + func test_Poly6_decode_throws_typeNotFound() { + XCTAssertThrowsError(try JSONDecoder().decode(Poly6.self, from: poly_entity7)) + } +} + +// MARK: - Test types +extension PolyTests { + enum TestEntityType: EntityDescription { + + typealias Relationships = NoRelatives + + public static var type: String { return "test_entity1" } + + public struct Attributes: JSONAPI.Attributes { + let foo: Attribute + let bar: Attribute + } + } + + typealias TestEntity = Entity + + enum TestEntityType2: EntityDescription { + + public static var type: String { return "test_entity2" } + + public struct Relationships: JSONAPI.Relationships { + let entity1: ToOneRelationship + } + + public struct Attributes: JSONAPI.Attributes { + let foo: Attribute + let bar: Attribute + } + } + + typealias TestEntity2 = Entity + + enum TestEntityType3: EntityDescription { + + typealias Attributes = NoAttributes + + public static var type: String { return "test_entity3" } + + public struct Relationships: JSONAPI.Relationships { + let entity1: ToOneRelationship + let entity2: ToManyRelationship + } + } + + typealias TestEntity3 = Entity + + enum TestEntityType4: EntityDescription { + + typealias Attributes = NoAttributes + + typealias Relationships = NoRelatives + + public static var type: String { return "test_entity4" } + } + + typealias TestEntity4 = Entity + + enum TestEntityType5: EntityDescription { + + typealias Attributes = NoAttributes + + typealias Relationships = NoRelatives + + public static var type: String { return "test_entity5" } + } + + typealias TestEntity5 = Entity + + enum TestEntityType6: EntityDescription { + + typealias Attributes = NoAttributes + + public static var type: String { return "test_entity6" } + + struct Relationships: JSONAPI.Relationships { + let entity4: ToOneRelationship + } + } + + typealias TestEntity6 = Entity } diff --git a/Tests/JSONAPITests/Poly/stubs/PolyStubs.swift b/Tests/JSONAPITests/Poly/stubs/PolyStubs.swift index 0373e59..ae34f2a 100644 --- a/Tests/JSONAPITests/Poly/stubs/PolyStubs.swift +++ b/Tests/JSONAPITests/Poly/stubs/PolyStubs.swift @@ -4,3 +4,92 @@ // // Created by Mathew Polzin on 11/23/18. // + +let poly_entity1 = """ +{ + "type": "test_entity1", + "id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF", + "attributes": { + "foo": "Hello", + "bar": 123 + } +} +""".data(using: .utf8)! + +let poly_entity2 = """ +{ + "type": "test_entity2", + "id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333", + "attributes": { + "foo": "World", + "bar": 456 + }, + "relationships": { + "entity1": { + "data": { + "type": "test_entity1", + "id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF" + } + } + } +} +""".data(using: .utf8)! + +let poly_entity3 = """ +{ + "type": "test_entity3", + "id": "11223B69-4DF1-467F-B52E-B0C9E44FC443", + "relationships": { + "entity1": { + "data": { + "type": "test_entity1", + "id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF" + } + }, + "entity2": { + "data": [ + { + "type": "test_entity2", + "id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333" + } + ] + } + } +} +""".data(using: .utf8)! + +let poly_entity4 = """ +{ + "type": "test_entity4", + "id": "364B3B69-4DF1-467F-B52E-B0C9E44F666E" +} +""".data(using: .utf8)! + +let poly_entity5 = """ +{ + "type": "test_entity5", + "id": "A24B3B69-4DF1-467F-B52E-B0C9E44F436A" +} +""".data(using: .utf8)! + +let poly_entity6 = """ +{ + "type": "test_entity6", + "id": "11113B69-4DF1-467F-B52E-B0C9E44FC444", + "relationships": { + "entity4": { + "data": { + "type": "test_entity4", + "id": "364B3B69-4DF1-467F-B52E-B0C9E44F666E" + } + } + } +} +""".data(using: .utf8)! + +let poly_entity7 = """ +{ + "type": "test_entity7", + "id": "A24B3444-4DF1-467F-B52E-B0C9E44F436A" +} +""".data(using: .utf8)!