lots of tests of Poly

This commit is contained in:
Mathew Polzin
2018-11-23 19:09:53 -08:00
parent 6dff02bd51
commit 16b83ddbef
5 changed files with 434 additions and 6 deletions
+6 -5
View File
@@ -19,14 +19,14 @@ public protocol Poly: Codable, Equatable {}
// MARK: - Generic Decoding
private func decode<Entity: JSONAPI.EntityType>(_ type: Entity.Type, from container: SingleValueDecodingContainer) throws -> Result<Entity, EncodingError> {
let ret: Result<Entity, EncodingError>
private func decode<Entity: JSONAPI.EntityType>(_ type: Entity.Type, from container: SingleValueDecodingContainer) throws -> Result<Entity, DecodingError> {
let ret: Result<Entity, DecodingError>
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.")
}
}
@@ -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
}
@@ -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<NoIncludes>.self,
data: two_same_type_includes)
@@ -118,6 +123,7 @@ class IncludedTests: XCTestCase {
}
}
// MARK: - Test types
extension IncludedTests {
enum TestEntityType: EntityDescription {
+331
View File
@@ -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<TestEntity5, TestEntity>(entity)
XCTAssertEqual(poly.a, entity)
XCTAssertNil(poly.b)
let poly2 = Poly2<TestEntity, TestEntity5>(entity)
XCTAssertEqual(poly2.b, entity)
XCTAssertNil(poly2.a)
}
func test_init_Poly3() {
let entity = TestEntity5()
let poly = Poly3<TestEntity5, TestEntity, TestEntity2>(entity)
XCTAssertEqual(poly.a, entity)
XCTAssertNil(poly.b)
XCTAssertNil(poly.c)
let poly2 = Poly3<TestEntity, TestEntity5, TestEntity2>(entity)
XCTAssertEqual(poly2.b, entity)
XCTAssertNil(poly2.a)
XCTAssertNil(poly2.c)
let poly3 = Poly3<TestEntity, TestEntity2, TestEntity5>(entity)
XCTAssertEqual(poly3.c, entity)
XCTAssertNil(poly3.a)
XCTAssertNil(poly3.b)
}
func test_init_Poly4() {
let entity = TestEntity5()
let poly = Poly4<TestEntity5, TestEntity, TestEntity2, TestEntity3>(entity)
XCTAssertEqual(poly.a, entity)
XCTAssertNil(poly.b)
XCTAssertNil(poly.c)
XCTAssertNil(poly.d)
let poly2 = Poly4<TestEntity, TestEntity5, TestEntity2, TestEntity3>(entity)
XCTAssertEqual(poly2.b, entity)
XCTAssertNil(poly2.a)
XCTAssertNil(poly2.c)
XCTAssertNil(poly2.d)
let poly3 = Poly4<TestEntity, TestEntity2, TestEntity5, TestEntity3>(entity)
XCTAssertEqual(poly3.c, entity)
XCTAssertNil(poly3.a)
XCTAssertNil(poly3.b)
XCTAssertNil(poly3.d)
let poly4 = Poly4<TestEntity, TestEntity2, TestEntity3, TestEntity5>(entity)
XCTAssertEqual(poly4.d, entity)
XCTAssertNil(poly4.a)
XCTAssertNil(poly4.b)
XCTAssertNil(poly4.c)
}
func test_init_Poly5() {
let entity = TestEntity5()
let poly = Poly5<TestEntity5, TestEntity, TestEntity2, TestEntity3, TestEntity4>(entity)
XCTAssertEqual(poly.a, entity)
XCTAssertNil(poly.b)
XCTAssertNil(poly.c)
XCTAssertNil(poly.d)
XCTAssertNil(poly.e)
let poly2 = Poly5<TestEntity, TestEntity5, TestEntity2, TestEntity3, TestEntity4>(entity)
XCTAssertEqual(poly2.b, entity)
XCTAssertNil(poly2.a)
XCTAssertNil(poly2.c)
XCTAssertNil(poly2.d)
XCTAssertNil(poly2.e)
let poly3 = Poly5<TestEntity, TestEntity2, TestEntity5, TestEntity3, TestEntity4>(entity)
XCTAssertEqual(poly3.c, entity)
XCTAssertNil(poly3.a)
XCTAssertNil(poly3.b)
XCTAssertNil(poly3.d)
XCTAssertNil(poly3.e)
let poly4 = Poly5<TestEntity, TestEntity2, TestEntity3, TestEntity5, TestEntity4>(entity)
XCTAssertEqual(poly4.d, entity)
XCTAssertNil(poly4.a)
XCTAssertNil(poly4.b)
XCTAssertNil(poly4.c)
XCTAssertNil(poly4.e)
let poly5 = Poly5<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5>(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<TestEntity5, TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity6>(entity)
XCTAssertEqual(poly.a, entity)
XCTAssertNil(poly.b)
XCTAssertNil(poly.c)
XCTAssertNil(poly.d)
XCTAssertNil(poly.e)
XCTAssertNil(poly.f)
let poly2 = Poly6<TestEntity, TestEntity5, TestEntity2, TestEntity3, TestEntity4, TestEntity6>(entity)
XCTAssertEqual(poly2.b, entity)
XCTAssertNil(poly2.a)
XCTAssertNil(poly2.c)
XCTAssertNil(poly2.d)
XCTAssertNil(poly2.e)
XCTAssertNil(poly2.f)
let poly3 = Poly6<TestEntity, TestEntity2, TestEntity5, TestEntity3, TestEntity4, TestEntity6>(entity)
XCTAssertEqual(poly3.c, entity)
XCTAssertNil(poly3.a)
XCTAssertNil(poly3.b)
XCTAssertNil(poly3.d)
XCTAssertNil(poly3.e)
XCTAssertNil(poly3.f)
let poly4 = Poly6<TestEntity, TestEntity2, TestEntity3, TestEntity5, TestEntity4, TestEntity6>(entity)
XCTAssertEqual(poly4.d, entity)
XCTAssertNil(poly4.a)
XCTAssertNil(poly4.b)
XCTAssertNil(poly4.c)
XCTAssertNil(poly4.e)
XCTAssertNil(poly4.f)
let poly5 = Poly6<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6>(entity)
XCTAssertEqual(poly5.e, entity)
XCTAssertNil(poly5.a)
XCTAssertNil(poly5.b)
XCTAssertNil(poly5.c)
XCTAssertNil(poly5.d)
XCTAssertNil(poly5.f)
let poly6 = Poly6<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity6, TestEntity5>(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<TestEntity>.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<TestEntity, TestEntity2>.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<TestEntity, TestEntity2, TestEntity3>.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<TestEntity, TestEntity2, TestEntity3, TestEntity4>.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<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5>.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<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6>.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<TestEntity>.self, from: poly_entity2))
}
func test_Poly2_decode_throws_typeNotFound() {
XCTAssertThrowsError(try JSONDecoder().decode(Poly2<TestEntity, TestEntity2>.self, from: poly_entity3))
}
func test_Poly3_decode_throws_typeNotFound() {
XCTAssertThrowsError(try JSONDecoder().decode(Poly3<TestEntity, TestEntity2, TestEntity3>.self, from: poly_entity4))
}
func test_Poly4_decode_throws_typeNotFound() {
XCTAssertThrowsError(try JSONDecoder().decode(Poly4<TestEntity, TestEntity2, TestEntity3, TestEntity4>.self, from: poly_entity5))
}
func test_Poly5_decode_throws_typeNotFound() {
XCTAssertThrowsError(try JSONDecoder().decode(Poly5<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5>.self, from: poly_entity6))
}
func test_Poly6_decode_throws_typeNotFound() {
XCTAssertThrowsError(try JSONDecoder().decode(Poly6<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6>.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<String>
let bar: Attribute<Int>
}
}
typealias TestEntity = Entity<TestEntityType>
enum TestEntityType2: EntityDescription {
public static var type: String { return "test_entity2" }
public struct Relationships: JSONAPI.Relationships {
let entity1: ToOneRelationship<TestEntity>
}
public struct Attributes: JSONAPI.Attributes {
let foo: Attribute<String>
let bar: Attribute<Int>
}
}
typealias TestEntity2 = Entity<TestEntityType2>
enum TestEntityType3: EntityDescription {
typealias Attributes = NoAttributes
public static var type: String { return "test_entity3" }
public struct Relationships: JSONAPI.Relationships {
let entity1: ToOneRelationship<TestEntity>
let entity2: ToManyRelationship<TestEntity2>
}
}
typealias TestEntity3 = Entity<TestEntityType3>
enum TestEntityType4: EntityDescription {
typealias Attributes = NoAttributes
typealias Relationships = NoRelatives
public static var type: String { return "test_entity4" }
}
typealias TestEntity4 = Entity<TestEntityType4>
enum TestEntityType5: EntityDescription {
typealias Attributes = NoAttributes
typealias Relationships = NoRelatives
public static var type: String { return "test_entity5" }
}
typealias TestEntity5 = Entity<TestEntityType5>
enum TestEntityType6: EntityDescription {
typealias Attributes = NoAttributes
public static var type: String { return "test_entity6" }
struct Relationships: JSONAPI.Relationships {
let entity4: ToOneRelationship<TestEntity4>
}
}
typealias TestEntity6 = Entity<TestEntityType6>
}
@@ -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)!