Add tests for BasicJSONAPIError and tweak documentation

This commit is contained in:
Mathew Polzin
2019-09-29 15:20:08 -07:00
parent 88c5d400aa
commit b0801f7cee
3 changed files with 105 additions and 0 deletions
@@ -70,4 +70,9 @@ public struct BasicJSONAPIErrorPayload<IdType: Codable & Equatable>: Codable, Eq
/// a good option if you do not know what to expect. You could also use
/// `Either<Int, String>` (provided by the `Poly` package that is
/// already a dependency of `JSONAPI`).
///
/// - Important: The `definedFields` property will include fields
/// with non-nil values in a flattened way. There will be no `source` key
/// but there will be `pointer` and `parameter` keys (if those values
/// are non-nil).
public typealias BasicJSONAPIError<IdType: Codable & Equatable> = GenericJSONAPIError<BasicJSONAPIErrorPayload<IdType>>
@@ -0,0 +1,92 @@
//
// BasicJSONAPIErrorTests.swift
// JSONAPITests
//
// Created by Mathew Polzin on 9/29/19.
//
import Foundation
@testable import JSONAPI
import XCTest
final class BasicJSONAPIErrorTests: XCTestCase {
func test_initAndEquality() {
let unknown1 = BasicJSONAPIError<String>.unknown
let unknown2 = BasicJSONAPIError<String>.unknownError
XCTAssertEqual(unknown1, unknown2)
let unknown3 = BasicJSONAPIError<Int>.unknownError
XCTAssertEqual(unknown3, .unknown)
let _ = BasicJSONAPIError<Int>.error(.init(id: nil,
status: nil,
code: nil,
title: nil,
detail: nil,
source: nil))
let _ = BasicJSONAPIError<String>.error(.init(id: nil,
status: nil,
code: nil,
title: nil,
detail: nil,
source: nil))
let intError = BasicJSONAPIError<Int>.error(.init(id: 2,
status: nil,
code: nil,
title: nil,
detail: nil,
source: nil))
XCTAssertEqual(intError.payload?.id, 2)
XCTAssertNotEqual(intError, unknown3)
let stringError = BasicJSONAPIError<String>.error(.init(id: "hello",
status: nil,
code: nil,
title: nil,
detail: nil,
source: nil))
XCTAssertEqual(stringError.payload?.id, "hello")
XCTAssertNotEqual(stringError, unknown1)
let wellPopulatedError = BasicJSONAPIError<Int>.error(.init(id: 10,
status: "404",
code: "12",
title: "Missing",
detail: "Resource was not found",
source: .init(pointer: "/data/attributes/id", parameter: "id")))
XCTAssertEqual(wellPopulatedError.payload?.id, 10)
XCTAssertEqual(wellPopulatedError.payload?.status, "404")
XCTAssertEqual(wellPopulatedError.payload?.code, "12")
XCTAssertEqual(wellPopulatedError.payload?.title, "Missing")
XCTAssertEqual(wellPopulatedError.payload?.detail, "Resource was not found")
XCTAssertEqual(wellPopulatedError.payload?.source?.pointer, "/data/attributes/id")
XCTAssertEqual(wellPopulatedError.payload?.source?.parameter, "id")
XCTAssertNotEqual(wellPopulatedError, intError)
}
func test_definedFields() {
let unpopulatedError = BasicJSONAPIError<Int>.error(.init(id: nil,
status: nil,
code: nil,
title: nil,
detail: nil,
source: nil))
XCTAssertEqual(unpopulatedError.definedFields.count, 0)
let wellPopulatedError = BasicJSONAPIError<Int>.error(.init(id: 10,
status: "404",
code: "12",
title: "Missing",
detail: "Resource was not found",
source: .init(pointer: "/data/attributes/id", parameter: "id")))
XCTAssertEqual(wellPopulatedError.definedFields.count, 7)
XCTAssertEqual(wellPopulatedError.definedFields["id"], "10")
XCTAssertEqual(wellPopulatedError.definedFields["status"], "404")
XCTAssertEqual(wellPopulatedError.definedFields["code"], "12")
XCTAssertEqual(wellPopulatedError.definedFields["title"], "Missing")
XCTAssertEqual(wellPopulatedError.definedFields["detail"], "Resource was not found")
XCTAssertEqual(wellPopulatedError.definedFields["pointer"], "/data/attributes/id")
XCTAssertEqual(wellPopulatedError.definedFields["parameter"], "id")
}
}
@@ -92,6 +92,14 @@ final class GenericJSONAPIErrorTests: XCTestCase {
}
}
func test_encodeUnknown() {
let error = TestGenericJSONAPIError.unknownError
let encodedError = encoded(value: ["errors": [error]])
XCTAssertEqual(String(data: encodedError, encoding: .utf8)!, #"{"errors":["unknown"]}"#)
}
func test_payloadAccess() {
let error1 = TestGenericJSONAPIError.error(.init(hello: "world", world: 3))
let error2 = TestGenericJSONAPIError.error(.init(hello: "there", world: nil))