Refactored the amiibo types cases for the AmiiboServiceLiveTests tests in the tests target.

This commit is contained in:
2025-10-01 23:51:06 +02:00
parent 0fceb6f493
commit 5eccef1f64
@@ -754,125 +754,32 @@ struct AmiiboServiceLiveTests {
#expect(amiiboSeries.last?.key == "0xff") #expect(amiiboSeries.last?.key == "0xff")
} }
@Test @Test(arguments: zip(
func `get Amiibo types`() async throws { Input.amiiboTypes,
// GIVEN Output.amiiboTypes
// WHEN ))
let amiiboTypes = try await service.getAmiiboTypes() func `get amiibo types`(
filter: AmiiboTypeFilter,
// THEN expects numberOfItems: Int
#expect(!amiiboTypes.isEmpty) ) async throws {
#expect(amiiboTypes.count == 4) try await assertAmiiboTypes(
#expect(amiiboTypes.first?.key == "0x00") with: filter,
#expect(amiiboTypes.last?.key == "0x03") expects: numberOfItems
)
} }
@Test @Test(arguments: zip(
func `get Amiibo types by an existing key`() async throws { Input.amiiboTypesThrows,
// GIVEN Output.amiiboTypesThrows
let key = "0x01" ))
func `get amiibo types throws`(
// WHEN filter: AmiiboTypeFilter,
let amiiboTypes = try await service.getAmiiboTypes(.init(key: key)) expects error: AmiiboServiceError
) async throws {
// THEN try await assertsAmiiboTypesThrows(
#expect(!amiiboTypes.isEmpty) error: error,
#expect(amiiboTypes.count == 1) when: filter
#expect(amiiboTypes.first?.key == key) )
}
@Test
func `get Amiibo types by a non-existing key`() async throws {
// GIVEN
let key = "0x09"
// WHEN
// THEN
await #expect(throws: AmiiboServiceError.notFound) {
try await service.getAmiiboTypes(.init(key: key))
}
}
@Test
func `get Amiibo types by an incomplete key`() async throws {
// GIVEN
let key = "0x"
// WHEN
// THEN
await #expect(throws: AmiiboServiceError.badRequest) {
try await service.getAmiiboTypes(.init(key: key))
}
}
@Test
func `get Amiibo types by an empty key`() async throws {
// GIVEN
let key = ""
// WHEN
// THEN
await #expect(throws: AmiiboServiceError.badRequest) {
try await service.getAmiiboTypes(.init(key: key))
}
}
@Test
func `get Amiibo types by an existing name`() async throws {
// GIVEN
let name = "Card"
// WHEN
let amiiboTypes = try await service.getAmiiboTypes(.init(name: name))
// THEN
#expect(!amiiboTypes.isEmpty)
#expect(amiiboTypes.count == 1)
#expect(amiiboTypes.first?.name == name)
}
@Test
func `get Amiibo types by a non-existing name`() async throws {
// GIVEN
let name = "Something"
// WHEN
// THEN
await #expect(throws: AmiiboServiceError.notFound) {
try await service.getAmiiboTypes(.init(name: name))
}
}
@Test
func `get Amiibo types by an incomplete name`() async throws {
// GIVEN
let name = "Ca"
// WHEN
let amiiboTypes = try await service.getAmiiboTypes(.init(name: name))
// THEN
#expect(!amiiboTypes.isEmpty)
#expect(amiiboTypes.count == 1)
let amiiboTypeName = try #require(amiiboTypes.first)
#expect(amiiboTypeName.name.contains(name))
}
@Test
func `get Amiibo types by an empty name`() async throws {
// GIVEN
let name = ""
// WHEN
let amiiboTypes = try await service.getAmiiboTypes(.init(name: name))
// THEN
#expect(!amiiboTypes.isEmpty)
#expect(amiiboTypes.count == 4)
#expect(amiiboTypes.first?.key == "0x00")
#expect(amiiboTypes.last?.key == "0x03")
} }
@Test(arguments: zip( @Test(arguments: zip(
@@ -1664,125 +1571,32 @@ struct AmiiboServiceLiveTests {
#expect(amiiboSeries.last?.key == "0xff") #expect(amiiboSeries.last?.key == "0xff")
} }
@Test("Get Amiibo types") @Test("get amiibo types", arguments: zip(
func getAmiiboTypes() async throws { Input.amiiboTypes,
// GIVEN Output.amiiboTypes
// WHEN ))
let amiiboTypes = try await service.getAmiiboTypes() func getAmiiboTypes(
filter: AmiiboTypeFilter,
// THEN expects numberOfItems: Int
#expect(!amiiboTypes.isEmpty) ) async throws {
#expect(amiiboTypes.count == 5) try await assertAmiiboTypes(
#expect(amiiboTypes.first?.key == "0x00") with: filter,
#expect(amiiboTypes.last?.key == "0x04") expects: numberOfItems
)
} }
@Test("Get Amiibo types by an existing key") @Test("get amiibo types throws", arguments: zip(
func getAmiiboTypes_byExistingKey() async throws { Input.amiiboTypesThrows,
// GIVEN Output.amiiboTypesThrows
let key = "0x01" ))
func getAmiiboTypesThrows(
// WHEN filter: AmiiboTypeFilter,
let amiiboTypes = try await service.getAmiiboTypes(.init(key: key)) expects error: AmiiboServiceError
) async throws {
// THEN try await assertsAmiiboTypesThrows(
#expect(!amiiboTypes.isEmpty) error: error,
#expect(amiiboTypes.count == 1) when: filter
#expect(amiiboTypes.first?.key == key) )
}
@Test("Get Amiibo types by a non-existing key")
func getAmiiboTypes_byNonExistingKey() async throws {
// GIVEN
let key = "0x09"
// WHEN
// THEN
await #expect(throws: AmiiboServiceError.notFound) {
try await service.getAmiiboTypes(.init(key: key))
}
}
@Test("Get Amiibo types by an incomplete key")
func getAmiiboTypes_byIncompleteKey() async throws {
// GIVEN
let key = "0x"
// WHEN
// THEN
await #expect(throws: AmiiboServiceError.badRequest) {
try await service.getAmiiboTypes(.init(key: key))
}
}
@Test("Get Amiibo types by an empty key")
func getAmiiboTypes_byEmptyKey() async throws {
// GIVEN
let key = ""
// WHEN
// THEN
await #expect(throws: AmiiboServiceError.badRequest) {
try await service.getAmiiboTypes(.init(key: key))
}
}
@Test("Get Amiibo types by an existing name")
func getAmiiboTypes_byExistingName() async throws {
// GIVEN
let name = "Card"
// WHEN
let amiiboTypes = try await service.getAmiiboTypes(.init(name: name))
// THEN
#expect(!amiiboTypes.isEmpty)
#expect(amiiboTypes.count == 1)
#expect(amiiboTypes.first?.name == name)
}
@Test("Get Amiibo types by a non-existing name")
func getAmiiboTypes_byNonExistingName() async throws {
// GIVEN
let name = "Something"
// WHEN
// THEN
await #expect(throws: AmiiboServiceError.notFound) {
try await service.getAmiiboTypes(.init(name: name))
}
}
@Test("Get Amiibo types by an incomplete name")
func getAmiiboTypes_byIncompleteName() async throws {
// GIVEN
let name = "Ca"
// WHEN
let amiiboTypes = try await service.getAmiiboTypes(.init(name: name))
// THEN
#expect(!amiiboTypes.isEmpty)
#expect(amiiboTypes.count == 1)
let amiiboTypeName = try #require(amiiboTypes.first)
#expect(amiiboTypeName.name.contains(name))
}
@Test("Get Amiibo types by an empty name")
func getAmiiboTypes_byEmptyName() async throws {
// GIVEN
let name = ""
// WHEN
let amiiboTypes = try await service.getAmiiboTypes(.init(name: name))
// THEN
#expect(!amiiboTypes.isEmpty)
#expect(amiiboTypes.count == 5)
#expect(amiiboTypes.first?.key == "0x00")
#expect(amiiboTypes.last?.key == "0x04")
} }
@Test("get game characters", arguments: zip( @Test("get game characters", arguments: zip(
@@ -1859,6 +1673,39 @@ private extension AmiiboServiceLiveTests {
// MARK: Functions // MARK: Functions
/// Asserts the number of items returned by the `amiiboTypes` endpoint that matched a given filter.
/// - Parameters:
/// - filter: An amiibo type filter type.
/// - numberOfItems: An expected number of items returned.
func assertAmiiboTypes(
with filter: AmiiboTypeFilter,
expects numberOfItems: Int
) async throws {
// GIVEN
// WHEN
let amiiboTypes = try await service.getAmiiboTypes(filter)
// THEN
#expect(!amiiboTypes.isEmpty)
#expect(amiiboTypes.count == numberOfItems)
}
/// Asserts the error thrown by the `amiiboTypes` endpoint.
/// - Parameters:
/// - error: An expected error.
/// - filter: An amiibo type filter type.
func assertsAmiiboTypesThrows(
error: AmiiboServiceError,
when filter: AmiiboTypeFilter
) async throws {
// GIVEN
// WHEN
// THEN
await #expect(throws: error) {
try await service.getAmiiboTypes(filter)
}
}
/// Asserts the number of items returned by the `gameCharacters` endpoint that matched a given filter. /// Asserts the number of items returned by the `gameCharacters` endpoint that matched a given filter.
/// - Parameters: /// - Parameters:
/// - filter: A game character filter type. /// - filter: A game character filter type.
@@ -1955,6 +1802,21 @@ private extension AmiiboServiceLiveTests {
// MARK: - Arguments // MARK: - Arguments
enum Input { enum Input {
/// A list of amiibo type filters to input to the `assertAmiiboTypes` assertion.
static let amiiboTypes: [AmiiboTypeFilter] = [
.init(),
.init(key: "0x01"),
.init(name: "Card"),
.init(name: "Ca"),
.init(name: .empty)
]
/// A list of amiibo type filters to input to the `assertAmiiboTypesThrows` assertion.
static let amiiboTypesThrows: [AmiiboTypeFilter] = [
.init(key: "0x09"),
.init(key: "0x"),
.init(key: .empty),
.init(name: "Something")
]
/// A list of game character filters to input to the `assertGameCharacters` assertion. /// A list of game character filters to input to the `assertGameCharacters` assertion.
static let gameCharacters: [GameCharacterFilter] = [ static let gameCharacters: [GameCharacterFilter] = [
.init(), .init(),
@@ -1988,6 +1850,10 @@ enum Input {
} }
enum Output { enum Output {
/// A list of number of items that are expected from the `assertAmiiboTypes` assertion.
static let amiiboTypes: [Int] = [.totalAmiiboTypes, 1, 1, 1, .totalAmiiboTypes]
/// A list of errors are expected to be thrown from the `assertAmiiboTypesThrows` assertion.
static let amiiboTypesThrows: [AmiiboServiceError] = [.notFound, .badRequest, .badRequest, .notFound]
/// A list of number of items that are expected from the `assertGameCharacters` assertion. /// A list of number of items that are expected from the `assertGameCharacters` assertion.
static let gameCharacters: [Int] = [.totalGameCharacters, 1, 1, 1, .totalGameCharacters] static let gameCharacters: [Int] = [.totalGameCharacters, 1, 1, 1, .totalGameCharacters]
/// A list of errors are expected to be thrown from the `assertGameCharactersThrows` assertion. /// A list of errors are expected to be thrown from the `assertGameCharactersThrows` assertion.
@@ -2001,6 +1867,8 @@ enum Output {
// MARK: - Constants // MARK: - Constants
private extension Int { private extension Int {
/// 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. /// A number that represents the total number of game characters currently available at the live service.
static let totalGameCharacters = 668 static let totalGameCharacters = 668
/// A number that represents the total number of game series currently available at the live service. /// A number that represents the total number of game series currently available at the live service.