From 7bb5c9ac9f0eea3c0df348c8385b164deaf942f9 Mon Sep 17 00:00:00 2001 From: Mathew Polzin Date: Fri, 16 Nov 2018 08:23:36 -0800 Subject: [PATCH] Add missing unidentified decoding tests --- Examples.swift | 2 +- Tests/JSONAPITests/Entity/EntityTests.swift | 33 +++++++++++++++++-- .../Entity/stubs/EntityStubs.swift | 16 +++++++++ 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/Examples.swift b/Examples.swift index e7e95db..0ebf4f5 100644 --- a/Examples.swift +++ b/Examples.swift @@ -11,7 +11,7 @@ import JSONAPI /******* -Please enjoy these examples, but allow me the lack of error checking and forced casting for the sake of brevity. +Please enjoy these examples, but allow me the forced casting and the lack of error checking for the sake of brevity. ********/ diff --git a/Tests/JSONAPITests/Entity/EntityTests.swift b/Tests/JSONAPITests/Entity/EntityTests.swift index 04fc193..c28eefc 100644 --- a/Tests/JSONAPITests/Entity/EntityTests.swift +++ b/Tests/JSONAPITests/Entity/EntityTests.swift @@ -58,7 +58,11 @@ class EntityTests: XCTestCase { XCTAssertEqual(e[\.floater], 123.321) } - +} + +// MARK: - Encode/Decode +extension EntityTests { + func test_EntitySomeRelationshipsNoAttributes() { let entity = try? JSONDecoder().decode(TestEntity3.self, from: entity_some_relationships_no_attributes) @@ -224,10 +228,30 @@ extension EntityTests { // MARK: Unidentified extension EntityTests { + func test_UnidentifiedEntity() { + let entity = try? JSONDecoder().decode(UnidentifiedTestEntity.self, from: entity_unidentified) + XCTAssertNotNil(entity) + + guard let e = entity else { return } + + XCTAssertNil(e[\.me]) + XCTAssertEqual(e.id, Unidentified()) + } + + func test_UnidentifiedEntityWithAttributes() { + let entity = try? JSONDecoder().decode(UnidentifiedTestEntity.self, from: entity_unidentified_with_attributes) + + XCTAssertNotNil(entity) + + guard let e = entity else { return } + + XCTAssertEqual(e[\.me], "unknown") + XCTAssertEqual(e.id, Unidentified()) + } } -// MARK: Test Types +// MARK: - Test Types extension EntityTests { enum TestEntityType1: EntityDescription { @@ -374,7 +398,10 @@ extension EntityTests { enum UnidentifiedTestEntityType: EntityDescription { public static var type: String { return "unidentified_test_entities" } - typealias Attributes = NoAttributes + struct Attributes: JSONAPI.Attributes { + let me: Attribute? + } + typealias Relationships = NoRelatives } diff --git a/Tests/JSONAPITests/Entity/stubs/EntityStubs.swift b/Tests/JSONAPITests/Entity/stubs/EntityStubs.swift index d5f9d82..44e3965 100644 --- a/Tests/JSONAPITests/Entity/stubs/EntityStubs.swift +++ b/Tests/JSONAPITests/Entity/stubs/EntityStubs.swift @@ -205,3 +205,19 @@ let entity_self_ref_relationship = """ } } """.data(using: .utf8)! + +let entity_unidentified = """ +{ + "type": "unidentified_test_entities", + "attributes": {} +} +""".data(using: .utf8)! + +let entity_unidentified_with_attributes = """ +{ + "type": "unidentified_test_entities", + "attributes": { + "me": "unknown" + } +} +""".data(using: .utf8)!