Skip to content

Commit

Permalink
TMDbErrorTests
Browse files Browse the repository at this point in the history
  • Loading branch information
adamayoung committed Nov 19, 2024
1 parent 64b355d commit 773b909
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 31 deletions.
8 changes: 1 addition & 7 deletions Sources/TMDb/Domain/APIClient/APIRequestQueryItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@ import Foundation

typealias APIRequestQueryItems = [APIRequestQueryItem.Name: CustomStringConvertible]

struct APIRequestQueryItem {

private init() {}

}

extension APIRequestQueryItem {
enum APIRequestQueryItem {

struct Name: ExpressibleByStringLiteral, CustomStringConvertible, Equatable, Hashable {

Expand Down
28 changes: 5 additions & 23 deletions Sources/TMDb/Domain/APIClient/CodableAPIRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@

import Foundation

class CodableAPIRequest<Body: Encodable & Equatable, Response: Decodable>: APIRequest,
CustomStringConvertible
{
class CodableAPIRequest<Body: Encodable & Equatable, Response: Decodable>: APIRequest {

let id: UUID
let path: String
Expand All @@ -30,23 +28,6 @@ class CodableAPIRequest<Body: Encodable & Equatable, Response: Decodable>: APIRe
let headers: [String: String]
let body: Body?

var description: String {
var description = """
APIRequest:
\tid: \(id.uuidString)
\tpath: \(path)
\tqueryItems: \(queryItems.map { "\($0)=\($1)" }.joined(separator: "&"))
\tmethod: \(method)
\theaders: \(headers.map { "\($0): \(1)" }.joined(separator: " "))
"""

if let body {
description += "\nBody:\n\(body)"
}

return description
}

init(
id: UUID = UUID(),
path: String,
Expand All @@ -67,9 +48,10 @@ class CodableAPIRequest<Body: Encodable & Equatable, Response: Decodable>: APIRe
self.headers = headers
}

static func == (lhs: CodableAPIRequest<Body, Response>, rhs: CodableAPIRequest<Body, Response>)
-> Bool
{
static func == (
lhs: CodableAPIRequest<Body, Response>,
rhs: CodableAPIRequest<Body, Response>
) -> Bool {
lhs.path == rhs.path
&& lhs.queryItems == rhs.queryItems
&& lhs.method == rhs.method
Expand Down
2 changes: 1 addition & 1 deletion Sources/TMDb/Domain/Models/TMDbError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public enum TMDbError: Equatable, LocalizedError, Sendable {
/// An error indicating the resource could not be found.
case notFound

case unauthorised(String?)
case unauthorised(String? = nil)

/// An error indicating there was a network problem.
case network(Error)
Expand Down
113 changes: 113 additions & 0 deletions Tests/TMDbTests/Domain/Models/TMDbErrorTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//
// TikTokLinkTests.swift
// TMDb
//
// Copyright © 2024 Adam Young.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an AS IS BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Foundation
import Testing

@testable import TMDb

@Suite(.tags(.models))
struct TMDbErrorTests {

@Test("not found errors equal")
func notFoundErrorsEqual() {
let lhs = TMDbError.notFound
let rhs = TMDbError.notFound

#expect(lhs == rhs)
}

@Test("unauthorised errors with same message equal")
func unauthorisedErrorsWithSameMessageEqual() {
let lhs = TMDbError.unauthorised("Message 1")
let rhs = TMDbError.unauthorised("Message 1")

#expect(lhs == rhs)
}

@Test("unauthorised errors with different messages do not equal")
func unauthorisedErrorsWithDifferentMessagesDoNotEqual() {
let lhs = TMDbError.unauthorised("Message 1")
let rhs = TMDbError.unauthorised("Message 2")

#expect(lhs != rhs)
}

@Test("network errors equal")
func networkErrorsEqual() {
let lhs = TMDbError.network(MockError.test)
let rhs = TMDbError.network(MockError.test)

#expect(lhs == rhs)
}

@Test("unknown errors equal")
func unknownErrorsEqual() {
let lhs = TMDbError.unknown
let rhs = TMDbError.unknown

#expect(lhs == rhs)
}

@Test("not found and unknown errors do not equal")
func notFoundAndUnknowErrorsDoNotEqual() {
let lhs = TMDbError.notFound
let rhs = TMDbError.unknown

#expect(lhs != rhs)
}

@Test("not found error description is correct")
func notFoundErrorDescriptionIsCorrect() {
let description = TMDbError.notFound.errorDescription

#expect(description == "Not found")
}

@Test("unauthorised error description is correct")
func unauthorisedErrorDescriptionIsCorrect() {
let description = TMDbError.unauthorised().errorDescription

#expect(description == "Unauthorised")
}

@Test("network error description is correct")
func networkErrorDescriptionIsCorrect() {
let description = TMDbError.network(MockError.test).errorDescription

#expect(description == "Network error")
}

@Test("unknown error description is correct")
func unknownErrorDescriptionIsCorrect() {
let description = TMDbError.unknown.errorDescription

#expect(description == "Unknown")
}

}

extension TMDbErrorTests {

fileprivate enum MockError: Error {
case test
case test2
}

}

0 comments on commit 773b909

Please sign in to comment.