Overall improvements and data update (#22)
This PR contains the work done to update the live tests to the latest data plus lots of QoL improvements to the library, including: * added test cases to test the ``AmiiboService` locally; * conformed the models to the `Hashable` protocol; * documented the use of caching with the `AmiiboService` service; * updated the reference to the new AmiiboAPI url; * updated the year on the copyrights and header files; * updated the overall documentation of the source code and the package. Reviewed-on: #22 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
This commit was merged in pull request #22.
This commit is contained in:
+11
-11
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the Amiibo Service open source project
|
||||
//
|
||||
// Copyright (c) 2025 Röck+Cöde VoF. and the Amiibo Service project authors
|
||||
// Copyright (c) 2026 Röck+Cöde VoF. and the Amiibo Service project authors
|
||||
// Licensed under Apache license v2.0
|
||||
//
|
||||
// See LICENSE for license information
|
||||
@@ -175,9 +175,9 @@ struct AmiiboServiceLiveTests {
|
||||
@Test
|
||||
func `get the last updated timestamp`() async throws {
|
||||
try await assertLastUpdated(
|
||||
day: 7,
|
||||
month: 11,
|
||||
year: 2025
|
||||
day: 14,
|
||||
month: 3,
|
||||
year: 2026
|
||||
)
|
||||
}
|
||||
#else
|
||||
@@ -324,9 +324,9 @@ struct AmiiboServiceLiveTests {
|
||||
@Test("get last updated timestamp")
|
||||
func getLastUpdated() async throws {
|
||||
try await assertLastUpdated(
|
||||
day: 7,
|
||||
month: 11,
|
||||
year: 2025
|
||||
day: 14,
|
||||
month: 3,
|
||||
year: 2026
|
||||
)
|
||||
}
|
||||
#endif
|
||||
@@ -661,7 +661,7 @@ enum Input {
|
||||
|
||||
enum Output {
|
||||
/// A list of number of items that are expected from the `assertAmiibos` assertion.
|
||||
static let amiibos: [Int] = [.totalAmiibos, 7, 7, 1, 1, 1, .zero, .zero, 5, .zero, 7, .totalAmiibos, 237, 237, .zero, .zero, .zero, .zero, 96, 26, .zero, .zero, 63, .totalAmiibos, 12, 6, .zero, .zero, .zero, .totalAmiibos, 49, 32, .zero, .zero, 147, .totalAmiibos, .totalAmiibos, .totalAmiibos]
|
||||
static let amiibos: [Int] = [.totalAmiibos, 7, 7, 1, 1, 1, .zero, .zero, 5, .zero, 7, .totalAmiibos, 244, 244, .zero, .zero, .zero, .zero, 96, 26, .zero, .zero, 63, .totalAmiibos, 12, 6, .zero, .zero, .zero, .totalAmiibos, 49, 32, .zero, .zero, 147, .totalAmiibos, .totalAmiibos, .totalAmiibos]
|
||||
/// A list of errors are expected to be thrown from the `assertAmiibosThrows` assertion.
|
||||
static let amiibosThrows: [AmiiboServiceError] = [.badRequest, .badRequest, .badRequest, .badRequest, .badRequest, .badRequest, .badRequest]
|
||||
/// A list of number of items that are expected from the `assertAmiiboSeries` assertion.
|
||||
@@ -686,13 +686,13 @@ enum Output {
|
||||
|
||||
private extension Int {
|
||||
/// A number that represents the total number of amiibo items currently available at the live service.
|
||||
static let totalAmiibos = 929
|
||||
static let totalAmiibos = 936
|
||||
/// A number that represents the total number of amiibo series currently available at the live service.
|
||||
static let totalAmiiboSeries = 29
|
||||
static let totalAmiiboSeries = 30
|
||||
/// A number that represents the total number of amiibo types currently available at the live service.
|
||||
static let totalAmiiboTypes = 5
|
||||
/// A number that represents the total number of game characters currently available at the live service.
|
||||
static let totalGameCharacters = 675
|
||||
static let totalGameCharacters = 679
|
||||
/// A number that represents the total number of game series currently available at the live service.
|
||||
static let totalGameSeries = 117
|
||||
}
|
||||
@@ -0,0 +1,266 @@
|
||||
// ===----------------------------------------------------------------------===
|
||||
//
|
||||
// This source file is part of the Amiibo Service open source project
|
||||
//
|
||||
// Copyright (c) 2026 Röck+Cöde VoF. and the Amiibo Service project authors
|
||||
// Licensed under Apache license v2.0
|
||||
//
|
||||
// See LICENSE for license information
|
||||
// See CONTRIBUTORS for the list of Amiibo Service project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
// ===----------------------------------------------------------------------===
|
||||
|
||||
import AmiiboService
|
||||
import Foundation
|
||||
import Testing
|
||||
|
||||
@Suite("Amiibo Service [Mock]", .tags(.mock))
|
||||
struct AmiiboServiceMockTests {
|
||||
|
||||
// MARK: Get Amiibos tests
|
||||
|
||||
@Test("returns empty amiibos when mock provides an empty list")
|
||||
func getAmiibosEmpty() async throws {
|
||||
// GIVEN
|
||||
let service = AmiiboService(client: AmiiboMockClient(amiibos: []))
|
||||
|
||||
// WHEN
|
||||
let amiibos = try await service.getAmiibos()
|
||||
|
||||
// THEN
|
||||
#expect(amiibos.isEmpty)
|
||||
}
|
||||
|
||||
@Test("throws notFound when mock provides no amiibos")
|
||||
func getAmiibosNotFound() async {
|
||||
// GIVEN
|
||||
let service = AmiiboService(client: AmiiboMockClient())
|
||||
|
||||
// WHEN
|
||||
// THEN
|
||||
await #expect(throws: AmiiboServiceError.notFound) {
|
||||
try await service.getAmiibos()
|
||||
}
|
||||
}
|
||||
|
||||
@Test("throws error for amiibos when mock is configured with error", arguments: Errors.all)
|
||||
func getAmiibosThrows(error: AmiiboServiceError) async {
|
||||
// GIVEN
|
||||
let service = AmiiboService(client: AmiiboMockClient(error: error))
|
||||
|
||||
// WHEN
|
||||
// THEN
|
||||
await #expect(throws: error) {
|
||||
try await service.getAmiibos()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Get Amiibo Series tests
|
||||
|
||||
@Test("returns empty amiibo series when mock provides an empty list")
|
||||
func getAmiiboSeriesEmpty() async throws {
|
||||
// GIVEN
|
||||
let service = AmiiboService(client: AmiiboMockClient(amiiboSeries: []))
|
||||
|
||||
// WHEN
|
||||
let amiiboSeries = try await service.getAmiiboSeries()
|
||||
|
||||
// THEN
|
||||
#expect(amiiboSeries.isEmpty)
|
||||
}
|
||||
|
||||
@Test("throws notFound when mock provides no amiibo series")
|
||||
func getAmiiboSeriesNotFound() async {
|
||||
// GIVEN
|
||||
let service = AmiiboService(client: AmiiboMockClient())
|
||||
|
||||
// WHEN
|
||||
// THEN
|
||||
await #expect(throws: AmiiboServiceError.notFound) {
|
||||
try await service.getAmiiboSeries()
|
||||
}
|
||||
}
|
||||
|
||||
@Test("throws error for amiibo series when mock is configured with error", arguments: Errors.all)
|
||||
func getAmiiboSeriesThrows(error: AmiiboServiceError) async {
|
||||
// GIVEN
|
||||
let service = AmiiboService(client: AmiiboMockClient(error: error))
|
||||
|
||||
// WHEN
|
||||
// THEN
|
||||
await #expect(throws: error) {
|
||||
try await service.getAmiiboSeries()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Get Amiibo Types tests
|
||||
|
||||
@Test("returns empty amiibo types when mock provides an empty list")
|
||||
func getAmiiboTypesEmpty() async throws {
|
||||
// GIVEN
|
||||
let service = AmiiboService(client: AmiiboMockClient(amiiboTypes: []))
|
||||
|
||||
// WHEN
|
||||
let amiiboTypes = try await service.getAmiiboTypes()
|
||||
|
||||
// THEN
|
||||
#expect(amiiboTypes.isEmpty)
|
||||
}
|
||||
|
||||
@Test("throws notFound when mock provides no amiibo types")
|
||||
func getAmiiboTypesNotFound() async {
|
||||
// GIVEN
|
||||
let service = AmiiboService(client: AmiiboMockClient())
|
||||
|
||||
// WHEN
|
||||
// THEN
|
||||
await #expect(throws: AmiiboServiceError.notFound) {
|
||||
try await service.getAmiiboTypes()
|
||||
}
|
||||
}
|
||||
|
||||
@Test("throws error for amiibo types when mock is configured with error", arguments: Errors.all)
|
||||
func getAmiiboTypesThrows(error: AmiiboServiceError) async {
|
||||
// GIVEN
|
||||
let service = AmiiboService(client: AmiiboMockClient(error: error))
|
||||
|
||||
// WHEN
|
||||
// THEN
|
||||
await #expect(throws: error) {
|
||||
try await service.getAmiiboTypes()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Get Game Characters tests
|
||||
|
||||
@Test("returns empty game characters when mock provides an empty list")
|
||||
func getGameCharactersEmpty() async throws {
|
||||
// GIVEN
|
||||
let service = AmiiboService(client: AmiiboMockClient(gameCharacters: []))
|
||||
|
||||
// WHEN
|
||||
let gameCharacters = try await service.getGameCharacters()
|
||||
|
||||
// THEN
|
||||
#expect(gameCharacters.isEmpty)
|
||||
}
|
||||
|
||||
@Test("throws notFound when mock provides no game characters")
|
||||
func getGameCharactersNotFound() async {
|
||||
// GIVEN
|
||||
let service = AmiiboService(client: AmiiboMockClient())
|
||||
|
||||
// WHEN
|
||||
// THEN
|
||||
await #expect(throws: AmiiboServiceError.notFound) {
|
||||
try await service.getGameCharacters()
|
||||
}
|
||||
}
|
||||
|
||||
@Test("throws error for game characters when mock is configured with error", arguments: Errors.all)
|
||||
func getGameCharactersThrows(error: AmiiboServiceError) async {
|
||||
// GIVEN
|
||||
let service = AmiiboService(client: AmiiboMockClient(error: error))
|
||||
|
||||
// WHEN
|
||||
// THEN
|
||||
await #expect(throws: error) {
|
||||
try await service.getGameCharacters()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Get Game Series tests
|
||||
|
||||
@Test("returns empty game series when mock provides an empty list")
|
||||
func getGameSeriesEmpty() async throws {
|
||||
// GIVEN
|
||||
let service = AmiiboService(client: AmiiboMockClient(gameSeries: []))
|
||||
|
||||
// WHEN
|
||||
let gameSeries = try await service.getGameSeries()
|
||||
|
||||
// THEN
|
||||
#expect(gameSeries.isEmpty)
|
||||
}
|
||||
|
||||
@Test("throws notFound when mock provides no game series")
|
||||
func getGameSeriesNotFound() async {
|
||||
// GIVEN
|
||||
let service = AmiiboService(client: AmiiboMockClient())
|
||||
|
||||
// WHEN
|
||||
// THEN
|
||||
await #expect(throws: AmiiboServiceError.notFound) {
|
||||
try await service.getGameSeries()
|
||||
}
|
||||
}
|
||||
|
||||
@Test("throws error for game series when mock is configured with error", arguments: Errors.all)
|
||||
func getGameSeriesThrows(error: AmiiboServiceError) async {
|
||||
// GIVEN
|
||||
let service = AmiiboService(client: AmiiboMockClient(error: error))
|
||||
|
||||
// WHEN
|
||||
// THEN
|
||||
await #expect(throws: error) {
|
||||
try await service.getGameSeries()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Get Last Updated tests
|
||||
|
||||
@Test("returns date when mock provides a last updated date")
|
||||
func getLastUpdated() async throws {
|
||||
// GIVEN
|
||||
let expectedDate = Date(timeIntervalSince1970: 1_700_000_000)
|
||||
let service = AmiiboService(client: AmiiboMockClient(lastUpdated: expectedDate))
|
||||
|
||||
// WHEN
|
||||
let lastUpdated = try await service.getLastUpdated()
|
||||
|
||||
// THEN
|
||||
#expect(lastUpdated == expectedDate)
|
||||
}
|
||||
|
||||
@Test("throws notFound when mock provides no last updated date")
|
||||
func getLastUpdatedNotFound() async {
|
||||
// GIVEN
|
||||
let service = AmiiboService(client: AmiiboMockClient())
|
||||
|
||||
// WHEN
|
||||
// THEN
|
||||
await #expect(throws: AmiiboServiceError.notFound) {
|
||||
try await service.getLastUpdated()
|
||||
}
|
||||
}
|
||||
|
||||
@Test("throws error for last updated when mock is configured with error", arguments: Errors.all)
|
||||
func getLastUpdatedThrows(error: AmiiboServiceError) async {
|
||||
// GIVEN
|
||||
let service = AmiiboService(client: AmiiboMockClient(error: error))
|
||||
|
||||
// WHEN
|
||||
// THEN
|
||||
await #expect(throws: error) {
|
||||
try await service.getLastUpdated()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Arguments
|
||||
|
||||
private enum Errors {
|
||||
/// All possible errors that can be thrown by the service.
|
||||
static let all: [AmiiboServiceError] = [
|
||||
.badRequest,
|
||||
.cancelled,
|
||||
.decoding,
|
||||
.notAvailable,
|
||||
.notFound,
|
||||
.undocumented(500),
|
||||
.unknown
|
||||
]
|
||||
}
|
||||
+11
-11
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the Amiibo Service open source project
|
||||
//
|
||||
// Copyright (c) 2025 Röck+Cöde VoF. and the Amiibo Service project authors
|
||||
// Copyright (c) 2026 Röck+Cöde VoF. and the Amiibo Service project authors
|
||||
// Licensed under Apache license v2.0
|
||||
//
|
||||
// See LICENSE for license information
|
||||
@@ -15,30 +15,30 @@
|
||||
import AmiiboService
|
||||
import Foundation
|
||||
|
||||
/// A type that implements a mock client, for testing purposes.
|
||||
/// A mock implementation of ``AmiiboClient`` that returns pre-configured data or throws pre-configured errors, for testing purposes.
|
||||
struct AmiiboMockClient {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
/// A list of amiibo items to return, if any.
|
||||
/// The list of amiibo items to return when ``getAmiibos(by:)`` is called, or `nil` to trigger a ``AmiiboServiceError/notFound`` error.
|
||||
private let amiibos: [Amiibo]?
|
||||
|
||||
/// A list of amiibo series to return, if any.
|
||||
/// The list of amiibo series to return when ``getAmiiboSeries(by:)`` is called, or `nil` to trigger a ``AmiiboServiceError/notFound`` error.
|
||||
private let amiiboSeries: [AmiiboSeries]?
|
||||
|
||||
/// A list of amiibo types to return, if any.
|
||||
/// The list of amiibo types to return when ``getAmiiboTypes(by:)`` is called, or `nil` to trigger a ``AmiiboServiceError/notFound`` error.
|
||||
private let amiiboTypes: [AmiiboType]?
|
||||
|
||||
/// An error to throw, if any.
|
||||
/// An error to throw before returning any data. Takes precedence over stored data when set.
|
||||
private let error: AmiiboServiceError?
|
||||
|
||||
/// A list of game characters to return, if any.
|
||||
/// The list of game characters to return when ``getGameCharacters(by:)`` is called, or `nil` to trigger a ``AmiiboServiceError/notFound`` error.
|
||||
private let gameCharacters: [GameCharacter]?
|
||||
|
||||
/// A list of game series to return, if any.
|
||||
/// The list of game series to return when ``getGameSeries(by:)`` is called, or `nil` to trigger a ``AmiiboServiceError/notFound`` error.
|
||||
private let gameSeries: [GameSeries]?
|
||||
|
||||
/// A last updated date to return, if any.
|
||||
/// The last updated date to return when ``getLastUpdated()`` is called, or `nil` to trigger a ``AmiiboServiceError/notFound`` error.
|
||||
private let lastUpdated: Date?
|
||||
|
||||
// MARK: Initializers
|
||||
@@ -234,8 +234,8 @@ private extension AmiiboMockClient {
|
||||
return lastUpdated
|
||||
}
|
||||
|
||||
/// Throws an error if it has been provided,
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case an error has been provided.
|
||||
/// Throws the configured error if one has been provided.
|
||||
/// - Throws: An ``AmiiboServiceError`` error if one was configured during initialization.
|
||||
func throwErrorIfExists() throws(AmiiboServiceError) {
|
||||
if let error {
|
||||
throw error
|
||||
+4
-1
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the Amiibo Service open source project
|
||||
//
|
||||
// Copyright (c) 2025 Röck+Cöde VoF. and the Amiibo Service project authors
|
||||
// Copyright (c) 2026 Röck+Cöde VoF. and the Amiibo Service project authors
|
||||
// Licensed under Apache license v2.0
|
||||
//
|
||||
// See LICENSE for license information
|
||||
@@ -21,4 +21,7 @@ extension Tag {
|
||||
/// Tag that indicates tests against a live backend service.
|
||||
@Tag static var live: Self
|
||||
|
||||
/// Tag that indicates tests using a mock client.
|
||||
@Tag static var mock: Self
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user