Refactored the game characters cases for the AmiiboServiceLiveTests tests in the tests target.

This commit is contained in:
2025-10-01 23:40:00 +02:00
parent eb06c36201
commit 0fceb6f493
@@ -874,126 +874,33 @@ struct AmiiboServiceLiveTests {
#expect(amiiboTypes.first?.key == "0x00") #expect(amiiboTypes.first?.key == "0x00")
#expect(amiiboTypes.last?.key == "0x03") #expect(amiiboTypes.last?.key == "0x03")
} }
@Test @Test(arguments: zip(
func `get Game characters`() async throws { Input.gameCharacters,
// GIVEN Output.gameCharacters
// WHEN ))
let gameCharacters = try await service.getGameCharacters() func `get game characters`(
filter: GameCharacterFilter,
// THEN expects numberOfItems: Int
#expect(!gameCharacters.isEmpty) ) async throws {
#expect(gameCharacters.count == 668) try await assertGameCharacters(
#expect(gameCharacters.first?.key == "0x0000") with: filter,
#expect(gameCharacters.last?.key == "0x3f00") expects: numberOfItems
)
} }
@Test @Test(arguments: zip(
func `get Game characters by an existing key`() async throws { Input.gameCharactersThrows,
// GIVEN Output.gameCharactersThrows
let key = "0x0001" ))
func `get game characters throws`(
// WHEN filter: GameCharacterFilter,
let gameCharacters = try await service.getGameCharacters(.init(key: key)) expects error: AmiiboServiceError
) async throws {
// THEN try await assertsGameCharactersThrows(
#expect(!gameCharacters.isEmpty) error: error,
#expect(gameCharacters.count == 1) when: filter
#expect(gameCharacters.first?.key == key) )
}
@Test
func `get Game characters by a non-existing key`() async throws {
// GIVEN
let key = "0xffff"
// WHEN
// THEN
await #expect(throws: AmiiboServiceError.notFound) {
try await service.getGameCharacters(.init(key: key))
}
}
@Test
func `get Game characters by an incomplete key`() async throws {
// GIVEN
let key = "0x"
// WHEN
// THEN
await #expect(throws: AmiiboServiceError.badRequest) {
try await service.getGameCharacters(.init(key: key))
}
}
@Test
func `get Game characters by an empty key`() async throws {
// GIVEN
let key = ""
// WHEN
// THEN
await #expect(throws: AmiiboServiceError.badRequest) {
try await service.getGameCharacters(.init(key: key))
}
}
@Test
func `get Game characters by an existing name`() async throws {
// GIVEN
let name = "Zelda"
// WHEN
let gameCharacters = try await service.getGameCharacters(.init(name: name))
// THEN
#expect(!gameCharacters.isEmpty)
#expect(gameCharacters.count == 1)
#expect(gameCharacters.first?.name == name)
}
@Test
func `get Game characters by a non-existing name`() async throws {
// GIVEN
let name = "Something"
// WHEN
// THEN
await #expect(throws: AmiiboServiceError.notFound) {
try await service.getGameCharacters(.init(name: name))
}
}
@Test
func `get Game characters by an incomplete name`() async throws {
// GIVEN
let name = "Zeld"
// WHEN
let gameCharacters = try await service.getGameCharacters(.init(name: name))
// THEN
#expect(!gameCharacters.isEmpty)
#expect(gameCharacters.count == 1)
let gameCharactersName = try #require(gameCharacters.first)
#expect(gameCharactersName.name.contains(name))
}
@Test
func `get Game characters by an empty name`() async throws {
// GIVEN
let name = ""
// WHEN
let gameCharacters = try await service.getGameCharacters(.init(name: name))
// THEN
#expect(!gameCharacters.isEmpty)
#expect(gameCharacters.count == 668)
#expect(gameCharacters.first?.key == "0x0000")
#expect(gameCharacters.last?.key == "0x3f00")
} }
@Test(arguments: zip( @Test(arguments: zip(
@@ -1877,126 +1784,33 @@ struct AmiiboServiceLiveTests {
#expect(amiiboTypes.first?.key == "0x00") #expect(amiiboTypes.first?.key == "0x00")
#expect(amiiboTypes.last?.key == "0x04") #expect(amiiboTypes.last?.key == "0x04")
} }
@Test("Get game characters") @Test("get game characters", arguments: zip(
func getGameCharacters() async throws { Input.gameCharacters,
// GIVEN Output.gameCharacters
// WHEN ))
let gameCharacters = try await service.getGameCharacters() func getGameCharacters(
filter: GameCharacterFilter,
// THEN expects numberOfItems: Int
#expect(!gameCharacters.isEmpty) ) async throws {
#expect(gameCharacters.count == 668) try await assertGameCharacters(
#expect(gameCharacters.first?.key == "0x0000") with: filter,
#expect(gameCharacters.last?.key == "0x3f00") expects: numberOfItems
)
} }
@Test("Get game characters by an existing key") @Test("get game characters throws", arguments: zip(
func getGameCharacters_byExistingKey() async throws { Input.gameCharactersThrows,
// GIVEN Output.gameCharactersThrows
let key = "0x0001" ))
func getGameCharactersThrows(
// WHEN filter: GameCharacterFilter,
let gameCharacters = try await service.getGameCharacters(.init(key: key)) expects error: AmiiboServiceError
) async throws {
// THEN try await assertsGameCharactersThrows(
#expect(!gameCharacters.isEmpty) error: error,
#expect(gameCharacters.count == 1) when: filter
#expect(gameCharacters.first?.key == key) )
}
@Test("Get game characters by a non-existing key")
func getGameCharacters_byNonExistingKey() async throws {
// GIVEN
let key = "0xffff"
// WHEN
// THEN
await #expect(throws: AmiiboServiceError.notFound) {
try await service.getGameCharacters(.init(key: key))
}
}
@Test("Get game characters by an incomplete key")
func getGameCharacters_byIncompleteKey() async throws {
// GIVEN
let key = "0x"
// WHEN
// THEN
await #expect(throws: AmiiboServiceError.badRequest) {
try await service.getGameCharacters(.init(key: key))
}
}
@Test("Get game characters by an empty key")
func getGameCharacters_byEmptyKey() async throws {
// GIVEN
let key = ""
// WHEN
// THEN
await #expect(throws: AmiiboServiceError.badRequest) {
try await service.getGameCharacters(.init(key: key))
}
}
@Test("Get game characters by an existing name")
func getGameCharacters_byExistingName() async throws {
// GIVEN
let name = "Zelda"
// WHEN
let gameCharacters = try await service.getGameCharacters(.init(name: name))
// THEN
#expect(!gameCharacters.isEmpty)
#expect(gameCharacters.count == 1)
#expect(gameCharacters.first?.name == name)
}
@Test("Get game characters by a non-existing name")
func getGameCharacters_byNonExistingName() async throws {
// GIVEN
let name = "Something"
// WHEN
// THEN
await #expect(throws: AmiiboServiceError.notFound) {
try await service.getGameCharacters(.init(name: name))
}
}
@Test("Get game characters by an incomplete name")
func getGameCharacters_byIncompleteName() async throws {
// GIVEN
let name = "Zeld"
// WHEN
let gameCharacters = try await service.getGameCharacters(.init(name: name))
// THEN
#expect(!gameCharacters.isEmpty)
#expect(gameCharacters.count == 1)
let gameCharactersName = try #require(gameCharacters.first)
#expect(gameCharactersName.name.contains(name))
}
@Test("Get game characters by an empty name")
func getGameCharacters_byEmptyName() async throws {
// GIVEN
let name = ""
// WHEN
let gameCharacters = try await service.getGameCharacters(.init(name: name))
// THEN
#expect(!gameCharacters.isEmpty)
#expect(gameCharacters.count == 668)
#expect(gameCharacters.first?.key == "0x0000")
#expect(gameCharacters.last?.key == "0x3f00")
} }
@Test("get game series", arguments: zip( @Test("get game series", arguments: zip(
@@ -2045,6 +1859,39 @@ private extension AmiiboServiceLiveTests {
// MARK: Functions // MARK: Functions
/// Asserts the number of items returned by the `gameCharacters` endpoint that matched a given filter.
/// - Parameters:
/// - filter: A game character filter type.
/// - numberOfItems: An expected number of items returned.
func assertGameCharacters(
with filter: GameCharacterFilter,
expects numberOfItems: Int
) async throws {
// GIVEN
// WHEN
let gameCharacters = try await service.getGameCharacters(filter)
// THEN
#expect(!gameCharacters.isEmpty)
#expect(gameCharacters.count == numberOfItems)
}
/// Asserts the error thrown by the `gameCharacters` endpoint.
/// - Parameters:
/// - error: An expected error.
/// - filter: A game character filter type.
func assertsGameCharactersThrows(
error: AmiiboServiceError,
when filter: GameCharacterFilter
) async throws {
// GIVEN
// WHEN
// THEN
await #expect(throws: error) {
try await service.getGameCharacters(filter)
}
}
/// Asserts the number of items returned by the `gameSeries` endpoint that matched a given filter. /// Asserts the number of items returned by the `gameSeries` endpoint that matched a given filter.
/// - Parameters: /// - Parameters:
/// - filter: A game series filter type. /// - filter: A game series filter type.
@@ -2108,7 +1955,22 @@ private extension AmiiboServiceLiveTests {
// MARK: - Arguments // MARK: - Arguments
enum Input { enum Input {
/// A list of game series filter to input to the `assertGameSeries` assertion. /// A list of game character filters to input to the `assertGameCharacters` assertion.
static let gameCharacters: [GameCharacterFilter] = [
.init(),
.init(key: "0x0001"),
.init(name: "Zelda"),
.init(name: "Zeld"),
.init(name: .empty)
]
/// A list of game character filters to input to the `assertGameCharactersThrows` assertion.
static let gameCharactersThrows: [GameCharacterFilter] = [
.init(key: "0xffff"),
.init(key: "0x"),
.init(key: .empty),
.init(name: "Something")
]
/// A list of game series filters to input to the `assertGameSeries` assertion.
static let gameSeries: [GameSeriesFilter] = [ static let gameSeries: [GameSeriesFilter] = [
.init(), .init(),
.init(key: "0x001"), .init(key: "0x001"),
@@ -2116,7 +1978,7 @@ enum Input {
.init(name: "Pik"), .init(name: "Pik"),
.init(name: .empty) .init(name: .empty)
] ]
/// A list of game series filter to input to the `assertGameSeriesThrows` assertion. /// A list of game series filters to input to the `assertGameSeriesThrows` assertion.
static let gameSeriesThrows: [GameSeriesFilter] = [ static let gameSeriesThrows: [GameSeriesFilter] = [
.init(key: "0xffff"), .init(key: "0xffff"),
.init(key: "0x"), .init(key: "0x"),
@@ -2126,6 +1988,10 @@ enum Input {
} }
enum Output { enum Output {
/// A list of number of items that are expected from the `assertGameCharacters` assertion.
static let gameCharacters: [Int] = [.totalGameCharacters, 1, 1, 1, .totalGameCharacters]
/// A list of errors are expected to be thrown from the `assertGameCharactersThrows` assertion.
static let gameCharactersThrows: [AmiiboServiceError] = [.notFound, .badRequest, .badRequest, .notFound]
/// A list of number of items that are expected from the `assertGameSeries` assertion. /// A list of number of items that are expected from the `assertGameSeries` assertion.
static let gameSeries: [Int] = [.totalGameSeries, 1, 1, 1, .totalGameSeries] static let gameSeries: [Int] = [.totalGameSeries, 1, 1, 1, .totalGameSeries]
/// A list of errors are expected to be thrown from the `assertGameSeriesThrows` assertion. /// A list of errors are expected to be thrown from the `assertGameSeriesThrows` assertion.
@@ -2135,6 +2001,8 @@ enum Output {
// MARK: - Constants // MARK: - Constants
private extension Int { private extension Int {
/// A number that represents the total number of game characters currently available at the live service.
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.
static let totalGameSeries = 117 static let totalGameSeries = 117
} }