diff --git a/Tests/JSONAPITests/Document/DocumentTests.swift b/Tests/JSONAPITests/Document/DocumentTests.swift index f38c250..5e99d50 100644 --- a/Tests/JSONAPITests/Document/DocumentTests.swift +++ b/Tests/JSONAPITests/Document/DocumentTests.swift @@ -49,6 +49,27 @@ class DocumentTests: XCTestCase { data: error_document_no_metadata) } + func test_unknownErrorDocumentMissingMeta() { + let document = decoded(type: JSONAPIDocument.self, data: error_document_no_metadata) + + XCTAssertTrue(document.body.isError) + XCTAssertNil(document.body.meta) + XCTAssertNil(document.body.data) + + guard case let .errors(errors) = document.body else { + XCTFail("Needed body to be in errors case but it was not.") + return + } + + XCTAssertEqual(errors.0.count, 1) + XCTAssertEqual(errors.0[0], .unknown) + XCTAssertNil(errors.meta) + } + + func test_unknownErrorDocumentMissingMeta_encode() { + test_DecodeEncodeEquality(type: JSONAPIDocument.self, data: error_document_no_metadata) + } + func test_errorDocumentNoMeta() { let document = decoded(type: JSONAPIDocument.self, data: error_document_no_metadata) @@ -109,6 +130,12 @@ class DocumentTests: XCTestCase { data: metadata_document) } + func test_metaDocumentMissingMeta() { + XCTAssertThrowsError(try JSONDecoder().decode(JSONAPIDocument.self, from: metadata_document_missing_metadata)) + + XCTAssertThrowsError(try JSONDecoder().decode(JSONAPIDocument.self, from: metadata_document_missing_metadata2)) + } + func test_singleDocumentNoIncludes() { let document = decoded(type: JSONAPIDocument, NoMetadata, NoIncludes, BasicJSONAPIError>.self, data: single_document_no_includes) @@ -140,6 +167,10 @@ class DocumentTests: XCTestCase { test_DecodeEncodeEquality(type: JSONAPIDocument, TestPageMetadata, NoIncludes, BasicJSONAPIError>.self, data: single_document_no_includes_with_metadata) } + + func test_singleDocumentNoIncludesMissingMetadata() { + XCTAssertThrowsError(try JSONDecoder().decode(JSONAPIDocument, TestPageMetadata, NoIncludes, BasicJSONAPIError>.self, from: single_document_no_includes)) + } func test_singleDocumentSomeIncludes() { let document = decoded(type: JSONAPIDocument, NoMetadata, Include1, BasicJSONAPIError>.self, diff --git a/Tests/JSONAPITests/Document/stubs/DocumentStubs.swift b/Tests/JSONAPITests/Document/stubs/DocumentStubs.swift index ec6c42c..4fc3418 100644 --- a/Tests/JSONAPITests/Document/stubs/DocumentStubs.swift +++ b/Tests/JSONAPITests/Document/stubs/DocumentStubs.swift @@ -237,3 +237,14 @@ let metadata_document = """ } } """.data(using: .utf8)! + +let metadata_document_missing_metadata = """ +{ +} +""".data(using: .utf8)! + +let metadata_document_missing_metadata2 = """ +{ + "meta": null +} +""".data(using: .utf8)!