From a808b3a7eb263ccaee307236ef856c75c5937ad0 Mon Sep 17 00:00:00 2001 From: Adam Young Date: Wed, 18 Sep 2024 15:09:15 +0100 Subject: [PATCH] REFACTOR: Migrate watch provider tests to Swift Testing --- .../WatchProviderRegionsRequestTests.swift | 46 ++++----- .../WatchProvidersForMoviesRequestTests.swift | 46 +++++---- ...atchProvidersForTVSeriesRequestTests.swift | 46 +++++---- .../TMDbWatchProviderServiceTests.swift | 97 ++++++++----------- 4 files changed, 120 insertions(+), 115 deletions(-) diff --git a/Tests/TMDbTests/Domain/Services/WatchProviders/Requests/WatchProviderRegionsRequestTests.swift b/Tests/TMDbTests/Domain/Services/WatchProviders/Requests/WatchProviderRegionsRequestTests.swift index 61b4779f..60b27a75 100644 --- a/Tests/TMDbTests/Domain/Services/WatchProviders/Requests/WatchProviderRegionsRequestTests.swift +++ b/Tests/TMDbTests/Domain/Services/WatchProviders/Requests/WatchProviderRegionsRequestTests.swift @@ -17,57 +17,59 @@ // limitations under the License. // +import Foundation +import Testing @testable import TMDb -import XCTest -final class WatchProviderRegionsRequestTests: XCTestCase { +@Suite(.tags(.requests, .watchProvider)) +struct WatchProviderRegionsRequestTests { var request: WatchProviderRegionsRequest! - override func setUp() { - super.setUp() - request = WatchProviderRegionsRequest() + init() { + self.request = WatchProviderRegionsRequest() } - override func tearDown() { - request = nil - super.tearDown() - } - - func testPath() { + @Test("path is correct") + func path() { let request = WatchProviderRegionsRequest() - XCTAssertEqual(request.path, "/watch/providers/regions") + #expect(request.path == "/watch/providers/regions") } - func testQueryItemsIsEmpty() { + @Test("queryItems is empty") + func queryItemsIsEmpty() { let request = WatchProviderRegionsRequest() - XCTAssertTrue(request.queryItems.isEmpty) + #expect(request.queryItems.isEmpty) } - func testQueryItemsWithLanguage() { + @Test("queryItems with language") + func queryItemsWithLanguage() { let request = WatchProviderRegionsRequest(language: "en") - XCTAssertEqual(request.queryItems, ["language": "en"]) + #expect(request.queryItems == ["language": "en"]) } - func testMethodIsGet() { + @Test("method is GET") + func methodIsGet() { let request = WatchProviderRegionsRequest() - XCTAssertEqual(request.method, .get) + #expect(request.method == .get) } - func testHeadersIsEmpty() { + @Test("headers is empty") + func headersIsEmpty() { let request = WatchProviderRegionsRequest() - XCTAssertTrue(request.headers.isEmpty) + #expect(request.headers.isEmpty) } - func testBodyIsNil() { + @Test("body is nil") + func bodyIsNil() { let request = WatchProviderRegionsRequest() - XCTAssertNil(request.body) + #expect(request.body == nil) } } diff --git a/Tests/TMDbTests/Domain/Services/WatchProviders/Requests/WatchProvidersForMoviesRequestTests.swift b/Tests/TMDbTests/Domain/Services/WatchProviders/Requests/WatchProvidersForMoviesRequestTests.swift index b4074f3c..f72ad4cb 100644 --- a/Tests/TMDbTests/Domain/Services/WatchProviders/Requests/WatchProvidersForMoviesRequestTests.swift +++ b/Tests/TMDbTests/Domain/Services/WatchProviders/Requests/WatchProvidersForMoviesRequestTests.swift @@ -17,57 +17,67 @@ // limitations under the License. // +import Foundation +import Testing @testable import TMDb -import XCTest -final class WatchProvidersForMoviesRequestTests: XCTestCase { +@Suite(.tags(.requests, .watchProvider)) +struct WatchProvidersForMoviesRequestTests { - func testPath() { + @Test("path is correct") + func path() { let request = WatchProvidersForMoviesRequest() - XCTAssertEqual(request.path, "/watch/providers/movie") + #expect(request.path == "/watch/providers/movie") } - func testQueryItemsIsEmpty() { + @Test("queryItems is empty") + func queryItemsIsEmpty() { let request = WatchProvidersForMoviesRequest() - XCTAssertTrue(request.queryItems.isEmpty) + #expect(request.queryItems.isEmpty) } - func testQueryItemsWithWatchRegion() { + @Test("queryItems with country") + func queryItemsWithCountry() { let request = WatchProvidersForMoviesRequest(country: "GB") - XCTAssertEqual(request.queryItems, ["watch_region": "GB"]) + #expect(request.queryItems == ["watch_region": "GB"]) } - func testQueryItemsWithLanguage() { + @Test("queryItems with language") + func queryItemsWithLanguage() { let request = WatchProvidersForMoviesRequest(language: "en") - XCTAssertEqual(request.queryItems, ["language": "en"]) + #expect(request.queryItems == ["language": "en"]) } - func testQueryItemsWithWatchRegionAndLanguage() { + @Test("queryItems with country and language") + func queryItemsWithCountryAndLanguage() { let request = WatchProvidersForMoviesRequest(country: "GB", language: "en") - XCTAssertEqual(request.queryItems, ["watch_region": "GB", "language": "en"]) + #expect(request.queryItems == ["watch_region": "GB", "language": "en"]) } - func testMethodIsGet() { + @Test("method is GET") + func methodIsGet() { let request = WatchProvidersForMoviesRequest() - XCTAssertEqual(request.method, .get) + #expect(request.method == .get) } - func testHeadersIsEmpty() { + @Test("headers is empty") + func headersIsEmpty() { let request = WatchProvidersForMoviesRequest() - XCTAssertTrue(request.headers.isEmpty) + #expect(request.headers.isEmpty) } - func testBodyIsNil() { + @Test("body is nil") + func bodyIsNil() { let request = WatchProvidersForMoviesRequest() - XCTAssertNil(request.body) + #expect(request.body == nil) } } diff --git a/Tests/TMDbTests/Domain/Services/WatchProviders/Requests/WatchProvidersForTVSeriesRequestTests.swift b/Tests/TMDbTests/Domain/Services/WatchProviders/Requests/WatchProvidersForTVSeriesRequestTests.swift index 552be78a..129b643d 100644 --- a/Tests/TMDbTests/Domain/Services/WatchProviders/Requests/WatchProvidersForTVSeriesRequestTests.swift +++ b/Tests/TMDbTests/Domain/Services/WatchProviders/Requests/WatchProvidersForTVSeriesRequestTests.swift @@ -17,57 +17,67 @@ // limitations under the License. // +import Foundation +import Testing @testable import TMDb -import XCTest -final class WatchProvidersForTVSeriesRequestTests: XCTestCase { +@Suite(.tags(.requests, .watchProvider)) +struct WatchProvidersForTVSeriesRequestTests { - func testPath() { + @Test("path is correct") + func path() { let request = WatchProvidersForTVSeriesRequest() - XCTAssertEqual(request.path, "/watch/providers/tv") + #expect(request.path == "/watch/providers/tv") } - func testQueryItemsIsEmpty() { + @Test("queryItems is empty") + func queryItemsIsEmpty() { let request = WatchProvidersForTVSeriesRequest() - XCTAssertTrue(request.queryItems.isEmpty) + #expect(request.queryItems.isEmpty) } - func testQueryItemsWithWatchRegion() { + @Test("queryItems with country") + func queryItemsWithCountry() { let request = WatchProvidersForTVSeriesRequest(country: "GB") - XCTAssertEqual(request.queryItems, ["watch_region": "GB"]) + #expect(request.queryItems == ["watch_region": "GB"]) } - func testQueryItemsWithLanguage() { + @Test("queryItems with language") + func queryItemsWithLanguage() { let request = WatchProvidersForTVSeriesRequest(language: "en") - XCTAssertEqual(request.queryItems, ["language": "en"]) + #expect(request.queryItems == ["language": "en"]) } - func testQueryItemsWithWatchRegionAndLanguage() { + @Test("queryItems with country and language") + func queryItemsWithCountryAndLanguage() { let request = WatchProvidersForTVSeriesRequest(country: "GB", language: "en") - XCTAssertEqual(request.queryItems, ["watch_region": "GB", "language": "en"]) + #expect(request.queryItems == ["watch_region": "GB", "language": "en"]) } - func testMethodIsGet() { + @Test("method is GET") + func methodIsGet() { let request = WatchProvidersForTVSeriesRequest() - XCTAssertEqual(request.method, .get) + #expect(request.method == .get) } - func testHeadersIsEmpty() { + @Test("headers is empty") + func headersIsEmpty() { let request = WatchProvidersForTVSeriesRequest() - XCTAssertTrue(request.headers.isEmpty) + #expect(request.headers.isEmpty) } - func testBodyIsNil() { + @Test("body is nil") + func bodyIsNil() { let request = WatchProvidersForTVSeriesRequest() - XCTAssertNil(request.body) + #expect(request.body == nil) } } diff --git a/Tests/TMDbTests/Domain/Services/WatchProviders/TMDbWatchProviderServiceTests.swift b/Tests/TMDbTests/Domain/Services/WatchProviders/TMDbWatchProviderServiceTests.swift index a1ecd9ac..9d083a79 100644 --- a/Tests/TMDbTests/Domain/Services/WatchProviders/TMDbWatchProviderServiceTests.swift +++ b/Tests/TMDbTests/Domain/Services/WatchProviders/TMDbWatchProviderServiceTests.swift @@ -17,27 +17,23 @@ // limitations under the License. // +import Foundation +import Testing @testable import TMDb -import XCTest -final class TMDbWatchProviderServiceTests: XCTestCase { +@Suite(.tags(.services, .watchProvider)) +struct TMDbWatchProviderServiceTests { var service: TMDbWatchProviderService! var apiClient: MockAPIClient! - override func setUp() { - super.setUp() - apiClient = MockAPIClient() - service = TMDbWatchProviderService(apiClient: apiClient) + init() { + self.apiClient = MockAPIClient() + self.service = TMDbWatchProviderService(apiClient: apiClient) } - override func tearDown() { - apiClient = nil - service = nil - super.tearDown() - } - - func testCountriesReturnsCountries() async throws { + @Test("countries returns countries") + func countriesReturnsCountries() async throws { let regions = WatchProviderRegions.mock let expectedResult = regions.results apiClient.addResponse(.success(regions)) @@ -45,11 +41,12 @@ final class TMDbWatchProviderServiceTests: XCTestCase { let result = try await service.countries() - XCTAssertEqual(result, expectedResult) - XCTAssertEqual(apiClient.lastRequest as? WatchProviderRegionsRequest, expectedRequest) + #expect(result == expectedResult) + #expect(apiClient.lastRequest as? WatchProviderRegionsRequest == expectedRequest) } - func testCountriesWithLanguageReturnsCountries() async throws { + @Test("countries with language returns countries") + func countriesWithLanguageReturnsCountries() async throws { let regions = WatchProviderRegions.mock let language = "en" let expectedResult = regions.results @@ -58,26 +55,21 @@ final class TMDbWatchProviderServiceTests: XCTestCase { let result = try await service.countries(language: language) - XCTAssertEqual(result, expectedResult) - XCTAssertEqual(apiClient.lastRequest as? WatchProviderRegionsRequest, expectedRequest) + #expect(result == expectedResult) + #expect(apiClient.lastRequest as? WatchProviderRegionsRequest == expectedRequest) } - func testCountriesWhenErrorsThrowsError() async throws { + @Test("countries when errors throws error") + func countriesWhenErrorsThrowsError() async throws { apiClient.addResponse(.failure(.unknown)) - var error: Error? - do { + await #expect(throws: TMDbError.unknown) { _ = try await service.countries() - } catch let err { - error = err } - - let tmdbAPIError = try XCTUnwrap(error as? TMDbError) - - XCTAssertEqual(tmdbAPIError, .unknown) } - func testMovieWatchProvidersReturnsWatchProviders() async throws { + @Test("movingWatchProviders returns watch providers") + func movieWatchProvidersReturnsWatchProviders() async throws { let watchProviderResult = WatchProviderResult.mock let expectedResult = watchProviderResult.results apiClient.addResponse(.success(watchProviderResult)) @@ -85,11 +77,12 @@ final class TMDbWatchProviderServiceTests: XCTestCase { let result = try await service.movieWatchProviders() - XCTAssertEqual(result, expectedResult) - XCTAssertEqual(apiClient.lastRequest as? WatchProvidersForMoviesRequest, expectedRequest) + #expect(result == expectedResult) + #expect(apiClient.lastRequest as? WatchProvidersForMoviesRequest == expectedRequest) } - func testMovieWatchProvidersWithFilterAndLanguageReturnsWatchProviders() async throws { + @Test("movieWatchProviders with filter and language returns watch providers") + func movieWatchProvidersWithFilterAndLanguageReturnsWatchProviders() async throws { let watchProviderResult = WatchProviderResult.mock let country = "GB" let language = "en" @@ -100,26 +93,21 @@ final class TMDbWatchProviderServiceTests: XCTestCase { let filter = WatchProviderFilter(country: country) let result = try await service.movieWatchProviders(filter: filter, language: language) - XCTAssertEqual(result, expectedResult) - XCTAssertEqual(apiClient.lastRequest as? WatchProvidersForMoviesRequest, expectedRequest) + #expect(result == expectedResult) + #expect(apiClient.lastRequest as? WatchProvidersForMoviesRequest == expectedRequest) } - func testMovieWatchProvidersWhenErrorsThrowsError() async throws { + @Test("movieWatchProviders when errors throws error") + func movieWatchProvidersWhenErrorsThrowsError() async throws { apiClient.addResponse(.failure(.unknown)) - var error: Error? - do { + await #expect(throws: TMDbError.unknown) { _ = try await service.movieWatchProviders() - } catch let err { - error = err } - - let tmdbAPIError = try XCTUnwrap(error as? TMDbError) - - XCTAssertEqual(tmdbAPIError, .unknown) } - func testTVSeriesWatchProvidersReturnsWatchProviders() async throws { + @Test("tvSeriesWatchProviders returns watch providers") + func tvSeriesWatchProvidersReturnsWatchProviders() async throws { let watchProviderResult = WatchProviderResult.mock let expectedResult = watchProviderResult.results apiClient.addResponse(.success(watchProviderResult)) @@ -127,11 +115,12 @@ final class TMDbWatchProviderServiceTests: XCTestCase { let result = try await service.tvSeriesWatchProviders() - XCTAssertEqual(result, expectedResult) - XCTAssertEqual(apiClient.lastRequest as? WatchProvidersForTVSeriesRequest, expectedRequest) + #expect(result == expectedResult) + #expect(apiClient.lastRequest as? WatchProvidersForTVSeriesRequest == expectedRequest) } - func testTVSeriesWatchProvidersWithFilterAndLanguageReturnsWatchProviders() async throws { + @Test("tvSeriesWatchProviders with filter and language returns watch providers") + func tvSeriesWatchProvidersWithFilterAndLanguageReturnsWatchProviders() async throws { let watchProviderResult = WatchProviderResult.mock let country = "GB" let language = "en" @@ -142,23 +131,17 @@ final class TMDbWatchProviderServiceTests: XCTestCase { let filter = WatchProviderFilter(country: country) let result = try await service.tvSeriesWatchProviders(filter: filter, language: language) - XCTAssertEqual(result, expectedResult) - XCTAssertEqual(apiClient.lastRequest as? WatchProvidersForTVSeriesRequest, expectedRequest) + #expect(result == expectedResult) + #expect(apiClient.lastRequest as? WatchProvidersForTVSeriesRequest == expectedRequest) } - func testTVSeriesWatchProvidersWhenErrorsThrowsError() async throws { + @Test("tvSeriesWatchProviders when errors throws error") + func tvSeriesWatchProvidersWhenErrorsThrowsError() async throws { apiClient.addResponse(.failure(.unknown)) - var error: Error? - do { + await #expect(throws: TMDbError.unknown) { _ = try await service.tvSeriesWatchProviders() - } catch let err { - error = err } - - let tmdbAPIError = try XCTUnwrap(error as? TMDbError) - - XCTAssertEqual(tmdbAPIError, .unknown) } }