diff --git a/README.md b/README.md index e555373..5dbc7c8 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ The primary goals of this framework are: - [x] Support ability to distinguish between `Attributes` fields that are optional (i.e. the key might not be there) and `Attributes` values that are optional (i.e. the key is guaranteed to be there but it might be `null`). - [x] Fix `ToOneRelationship` so that it is possible to specify an optional relationship where the value is `null` rather than the key being omitted. - [x] Conform to `CustomStringConvertible` -- [ ] More tests around failing to decode improperly structured JSON (not bad JSON, but JSON that is not to spec) +- [x] More tests around failing to decode improperly structured JSON (not bad JSON, but JSON that is not to spec) - [ ] Use `KeyPath` to specify `Includes` thus creating type safety around the relationship between a primary resource type and the types of included resources???? - [x] For `NoIncludes`, do not even loop over the "included" JSON API section if it exists. - [ ] Property-based testing (using `SwiftCheck`) diff --git a/Tests/JSONAPITests/Relationships/RelationshipTests.swift b/Tests/JSONAPITests/Relationships/RelationshipTests.swift index 3460e51..459dd3b 100644 --- a/Tests/JSONAPITests/Relationships/RelationshipTests.swift +++ b/Tests/JSONAPITests/Relationships/RelationshipTests.swift @@ -61,6 +61,17 @@ extension RelationshipTests { } } +// MARK: Failure tests +extension RelationshipTests { + func test_ToManyTypeMismatch() { + XCTAssertThrowsError(try JSONDecoder().decode(ToManyRelationship.self, from: to_many_relationship_type_mismatch)) + } + + func test_ToOneTypeMismatch() { + XCTAssertThrowsError(try JSONDecoder().decode(ToOneRelationship.self, from: to_one_relationship_type_mismatch)) + } +} + // MARK: - Test types extension RelationshipTests { enum TestEntityType1: EntityDescription { diff --git a/Tests/JSONAPITests/Relationships/stubs/RelationshipStubs.swift b/Tests/JSONAPITests/Relationships/stubs/RelationshipStubs.swift index e2508ad..6be0d90 100644 --- a/Tests/JSONAPITests/Relationships/stubs/RelationshipStubs.swift +++ b/Tests/JSONAPITests/Relationships/stubs/RelationshipStubs.swift @@ -14,6 +14,15 @@ let to_one_relationship = """ } """.data(using: .utf8)! +let to_one_relationship_type_mismatch = """ +{ + "data": { + "type": "not_a_type", + "id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF" + } +} +""".data(using: .utf8)! + let to_many_relationship = """ { "data": [ @@ -32,3 +41,22 @@ let to_many_relationship = """ ] } """.data(using: .utf8)! + +let to_many_relationship_type_mismatch = """ +{ + "data": [ + { + "type": "test_entity1", + "id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF" + }, + { + "type": "test_entity1", + "id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333" + }, + { + "type": "not_a_type", + "id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF" + } + ] +} +""".data(using: .utf8)!