From 8270aecb0ebff397d68e347fae197eeac684f6a5 Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Wed, 1 Oct 2025 23:22:39 +0200 Subject: [PATCH] Refactored the game series and last updated cases for the AmiiboServiceLiveTests tests in the tests target. --- .../Services/AmiiboServiceLiveTests.swift | 414 +++++++----------- 1 file changed, 159 insertions(+), 255 deletions(-) diff --git a/Tests/AmiiboService/Tests/Public/Services/AmiiboServiceLiveTests.swift b/Tests/AmiiboService/Tests/Public/Services/AmiiboServiceLiveTests.swift index b322fdf..d5a0553 100644 --- a/Tests/AmiiboService/Tests/Public/Services/AmiiboServiceLiveTests.swift +++ b/Tests/AmiiboService/Tests/Public/Services/AmiiboServiceLiveTests.swift @@ -995,142 +995,42 @@ struct AmiiboServiceLiveTests { #expect(gameCharacters.first?.key == "0x0000") #expect(gameCharacters.last?.key == "0x3f00") } - - @Test - func `get Game series`() async throws { - // GIVEN - // WHEN - let gameSeries = try await service.getGameSeries() - - // THEN - #expect(!gameSeries.isEmpty) - #expect(gameSeries.count == 117) - #expect(gameSeries.first?.key == "0x000") - #expect(gameSeries.last?.key == "0x3f0") - } - - @Test - func `get Game series by an existing key`() async throws { - // GIVEN - let key = "0x001" - - // WHEN - let gameSeries = try await service.getGameSeries(.init(key: key)) - - // THEN - #expect(!gameSeries.isEmpty) - #expect(gameSeries.count == 1) - #expect(gameSeries.first?.key == key) - } - - @Test - func `get Game Series by a non-existing key`() async throws { - // GIVEN - let key = "0xffff" - - // WHEN - // THEN - await #expect(throws: AmiiboServiceError.notFound) { - try await service.getGameSeries(.init(key: key)) - } - } - - @Test - func `get Game series by an incomplete key`() async throws { - // GIVEN - let key = "0x" - - // WHEN & THEN - await #expect(throws: AmiiboServiceError.badRequest) { - try await service.getGameSeries(.init(key: key)) - } - } - - @Test - func `get Game series by an empty key`() async throws { - // GIVEN - let key = "" - - // WHEN - // THEN - await #expect(throws: AmiiboServiceError.badRequest) { - try await service.getGameSeries(.init(key: key)) - } - } - - @Test - func `get Game series by an existing name`() async throws { - // GIVEN - let name = "Pikmin" - - // WHEN - let gameSeries = try await service.getGameSeries(.init(name: name)) - - // THEN - #expect(!gameSeries.isEmpty) - #expect(gameSeries.count == 1) - #expect(gameSeries.first?.name == name) - } - - @Test - func `get Game series by a non-existing name`() async throws { - // GIVEN - let name = "Something" - - // WHEN - // THEN - await #expect(throws: AmiiboServiceError.notFound) { - try await service.getGameSeries(.init(name: name)) - } - } - - @Test - func `get Game series by an incomplete name`() async throws { - // GIVEN - let name = "Pik" - - // WHEN - let gameSeries = try await service.getGameSeries(.init(name: name)) - - // THEN - #expect(!gameSeries.isEmpty) - #expect(gameSeries.count == 1) - - let gameSeriesName = try #require(gameSeries.first) - - #expect(gameSeriesName.name.contains(name)) - } - - @Test - func `get Game series by an empty name`() async throws { - // GIVEN - let name = "" - - // WHEN - let gameSeries = try await service.getGameSeries(.init(name: name)) - - // THEN - #expect(!gameSeries.isEmpty) - #expect(gameSeries.count == 117) - #expect(gameSeries.first?.key == "0x000") - #expect(gameSeries.last?.key == "0x3f0") - } - - @Test - func `get the Last Updated timestamp`() async throws { - // GIVEN - // WHEN - let dateLastUpdated = try await service.getLastUpdated() - - // THEN - let dateComponents = Calendar.current.dateComponents( - [.year, .month, .day], - from: dateLastUpdated + + @Test(arguments: zip( + Input.gameSeries, + Output.gameSeries + )) + func `get game series`( + filter: GameSeriesFilter, + expects numberOfItems: Int + ) async throws { + try await assertGameSeries( + with: filter, + expects: numberOfItems + ) + } + + @Test(arguments: zip( + Input.gameSeriesThrows, + Output.gameSeriesThrows + )) + func `get game series throws`( + filter: GameSeriesFilter, + expects error: AmiiboServiceError + ) async throws { + try await assertsGameSeriesThrows( + error: error, + when: filter + ) + } + + @Test + func `get the last updated timestamp`() async throws { + try await assertLastUpdated( + day: 21, + month: 9, + year: 2025 ) - - #expect(dateComponents.year == 2025) - #expect(dateComponents.month == 7) - #expect(dateComponents.day == 18) } #else @Test("Get Amiibo items") @@ -2098,130 +1998,96 @@ struct AmiiboServiceLiveTests { #expect(gameCharacters.first?.key == "0x0000") #expect(gameCharacters.last?.key == "0x3f00") } - - @Test("Get game series") - func getGameSeries() async throws { - // GIVEN - // WHEN - let gameSeries = try await service.getGameSeries() - - // THEN - #expect(!gameSeries.isEmpty) - #expect(gameSeries.count == 117) - #expect(gameSeries.first?.key == "0x000") - #expect(gameSeries.last?.key == "0x3f0") - } - - @Test("Get game series by an existing key") - func getGameSeries_byExistingKey() async throws { - // GIVEN - let key = "0x001" - - // WHEN - let gameSeries = try await service.getGameSeries(.init(key: key)) - - // THEN - #expect(!gameSeries.isEmpty) - #expect(gameSeries.count == 1) - #expect(gameSeries.first?.key == key) - } - - @Test("Get game series by a non-existing key") - func getGameSeries_byNonExistingKey() async throws { - // GIVEN - let key = "0xffff" - - // WHEN - // THEN - await #expect(throws: AmiiboServiceError.notFound) { - try await service.getGameSeries(.init(key: key)) - } - } - - @Test("Get game series by an incomplete key") - func getGameSeries_byIncompleteKey() async throws { - // GIVEN - let key = "0x" - - // WHEN & THEN - await #expect(throws: AmiiboServiceError.badRequest) { - try await service.getGameSeries(.init(key: key)) - } - } - - @Test("Get game series by an empty key") - func getGameSeries_byEmptyKey() async throws { - // GIVEN - let key = "" - - // WHEN - // THEN - await #expect(throws: AmiiboServiceError.badRequest) { - try await service.getGameSeries(.init(key: key)) - } - } - - @Test("Get game series by an existing name") - func getGameSeries_byExistingName() async throws { - // GIVEN - let name = "Pikmin" - - // WHEN - let gameSeries = try await service.getGameSeries(.init(name: name)) - - // THEN - #expect(!gameSeries.isEmpty) - #expect(gameSeries.count == 1) - #expect(gameSeries.first?.name == name) - } - - @Test("Get game series by a non-existing name") - func getGameSeries_byNonExistingName() async throws { - // GIVEN - let name = "Something" - - // WHEN - // THEN - await #expect(throws: AmiiboServiceError.notFound) { - try await service.getGameSeries(.init(name: name)) - } - } - - @Test("Get game series by an incomplete name") - func getGameSeries_byIncompleteName() async throws { - // GIVEN - let name = "Pik" - - // WHEN - let gameSeries = try await service.getGameSeries(.init(name: name)) - - // THEN - #expect(!gameSeries.isEmpty) - #expect(gameSeries.count == 1) - - let gameSeriesName = try #require(gameSeries.first) - - #expect(gameSeriesName.name.contains(name)) - } - - @Test("Get game series by an empty name") - func getGameSeries_byEmptyName() async throws { + @Test("get game series", arguments: zip( + Input.gameSeries, + Output.gameSeries + )) + func getGameSeries( + filter: GameSeriesFilter, + expects numberOfItems: Int + ) async throws { + try await assertGameSeries( + with: filter, + expects: numberOfItems + ) + } + + @Test("get game series throws", arguments: zip( + Input.gameSeriesThrows, + Output.gameSeriesThrows + )) + func getGameSeriesThrows( + filter: GameSeriesFilter, + expects error: AmiiboServiceError + ) async throws { + try await assertsGameSeriesThrows( + error: error, + when: filter + ) + } + + @Test("get last updated timestamp") + func getLastUpdated() async throws { + try await assertLastUpdated( + day: 21, + month: 9, + year: 2025 + ) + } +#endif + +} + +// MARK: - Assertions + +private extension AmiiboServiceLiveTests { + + // MARK: Functions + + /// Asserts the number of items returned by the `gameSeries` endpoint that matched a given filter. + /// - Parameters: + /// - filter: A game series filter type. + /// - numberOfItems: An expected number of items returned. + func assertGameSeries( + with filter: GameSeriesFilter, + expects numberOfItems: Int + ) async throws { // GIVEN - let name = "" - // WHEN - let gameSeries = try await service.getGameSeries(.init(name: name)) + let gameSeries = try await service.getGameSeries(filter) // THEN #expect(!gameSeries.isEmpty) - #expect(gameSeries.count == 117) - #expect(gameSeries.first?.key == "0x000") - #expect(gameSeries.last?.key == "0x3f0") + #expect(gameSeries.count == numberOfItems) } - @Test("Get the last updated timestamp") - func getLastUpdated() async throws { + /// Asserts the error thrown by the `gameSeries` endpoint. + /// - Parameters: + /// - error: An expected error. + /// - filter: A game series filter type. + func assertsGameSeriesThrows( + error: AmiiboServiceError, + when filter: GameSeriesFilter + ) async throws { + // GIVEN + // WHEN + // THEN + await #expect(throws: error) { + try await service.getGameSeries(filter) + } + } + + /// Asserts the date returned by the `lastUpdated` endpoint. + /// - Parameters: + /// - day: A number of day of the last updated date. + /// - month: A number of month of the last updated date. + /// - year: A number of year of the last updated date. + func assertLastUpdated( + day: Int, + month: Int, + year: Int + ) async throws { // GIVEN // WHEN let dateLastUpdated = try await service.getLastUpdated() @@ -2232,10 +2098,48 @@ struct AmiiboServiceLiveTests { from: dateLastUpdated ) - #expect(dateComponents.year == 2025) - #expect(dateComponents.month == 9) - #expect(dateComponents.day == 21) + #expect(dateComponents.year == year) + #expect(dateComponents.month == month) + #expect(dateComponents.day == day) } -#endif } + +// MARK: - Arguments + +enum Input { + /// A list of game series filter to input to the `assertGameSeries` assertion. + static let gameSeries: [GameSeriesFilter] = [ + .init(), + .init(key: "0x001"), + .init(name: "Pikmin"), + .init(name: "Pik"), + .init(name: .empty) + ] + /// A list of game series filter to input to the `assertGameSeriesThrows` assertion. + static let gameSeriesThrows: [GameSeriesFilter] = [ + .init(key: "0xffff"), + .init(key: "0x"), + .init(key: .empty), + .init(name: "Something") + ] +} + +enum Output { + /// A list of number of items that are expected from the `assertGameSeries` assertion. + static let gameSeries: [Int] = [.totalGameSeries, 1, 1, 1, .totalGameSeries] + /// A list of errors are expected to be thrown from the `assertGameSeriesThrows` assertion. + static let gameSeriesThrows: [AmiiboServiceError] = [.notFound, .badRequest, .badRequest, .notFound] +} + +// MARK: - Constants + +private extension Int { + /// A number that represents the total number of game series currently available at the live service. + static let totalGameSeries = 117 +} + +private extension String { + /// An empty string. + static let empty = "" +}