From 8c0317582ccd10159f334086694fd28fbb0b5f0d Mon Sep 17 00:00:00 2001 From: Adam Young Date: Tue, 17 Sep 2024 15:55:04 +0100 Subject: [PATCH] REFACTOR: Migrate Configuration tests to Swift Testing --- .../APIConfigurationRequestTests.swift | 41 +++---- .../CountriesConfigurationRequestTests.swift | 36 ++++--- .../JobsConfigurationRequestTests.swift | 41 +++---- .../LanguaguesConfigurationRequestTests.swift | 41 +++---- .../TMDbConfigurationServiceTests.swift | 102 +++++++----------- .../HTTPClient/HTTPMockClient.swift | 2 +- .../JSONDecoder+DecodeFromFile.swift | 8 +- Tests/TMDbTests/TestUtils/Tags.swift | 1 + 8 files changed, 130 insertions(+), 142 deletions(-) diff --git a/Tests/TMDbTests/Domain/Services/Configuration/Requests/APIConfigurationRequestTests.swift b/Tests/TMDbTests/Domain/Services/Configuration/Requests/APIConfigurationRequestTests.swift index 9649a19e..60c913fc 100644 --- a/Tests/TMDbTests/Domain/Services/Configuration/Requests/APIConfigurationRequestTests.swift +++ b/Tests/TMDbTests/Domain/Services/Configuration/Requests/APIConfigurationRequestTests.swift @@ -17,41 +17,42 @@ // limitations under the License. // +import Foundation +import Testing @testable import TMDb -import XCTest -final class APIConfigurationRequestTests: XCTestCase { +@Suite(.tags(.requests, .configuration)) +struct APIConfigurationRequestTests { var request: APIConfigurationRequest! - override func setUp() { - super.setUp() - request = APIConfigurationRequest() + init() { + self.request = APIConfigurationRequest() } - override func tearDown() { - request = nil - super.tearDown() + @Test("path is correct") + func path() { + #expect(request.path == "/configuration") } - func testPath() { - XCTAssertEqual(request.path, "/configuration") + @Test("queryItems is empty") + func queryItemsAreEmpty() { + #expect(request.queryItems.isEmpty) } - func testQueryItemsAreEmpty() { - XCTAssertTrue(request.queryItems.isEmpty) + @Test("method is GET") + func methodIsGet() { + #expect(request.method == .get) } - func testMethodIsGet() { - XCTAssertEqual(request.method, .get) + @Test("headers is empty") + func headersIsEmpty() { + #expect(request.headers.isEmpty) } - func testHeadersIsEmpty() { - XCTAssertTrue(request.headers.isEmpty) - } - - func testBodyIsNil() { - XCTAssertNil(request.body) + @Test("body is nil") + func bodyIsNil() { + #expect(request.body == nil) } } diff --git a/Tests/TMDbTests/Domain/Services/Configuration/Requests/CountriesConfigurationRequestTests.swift b/Tests/TMDbTests/Domain/Services/Configuration/Requests/CountriesConfigurationRequestTests.swift index 18d3a0c1..57339da2 100644 --- a/Tests/TMDbTests/Domain/Services/Configuration/Requests/CountriesConfigurationRequestTests.swift +++ b/Tests/TMDbTests/Domain/Services/Configuration/Requests/CountriesConfigurationRequestTests.swift @@ -17,45 +17,53 @@ // limitations under the License. // +import Foundation +import Testing @testable import TMDb -import XCTest -final class CountriesConfigurationRequestTests: XCTestCase { +@Suite(.tags(.requests, .configuration)) +struct CountriesConfigurationRequestTests { - func testPath() { + @Test("path is correct") + func path() { let request = CountriesConfigurationRequest() - XCTAssertEqual(request.path, "/configuration/countries") + #expect(request.path == "/configuration/countries") } - func testQueryItems() { + @Test("queryItems is empty") + func queryItemsAreEmpty() { let request = CountriesConfigurationRequest() - XCTAssertTrue(request.queryItems.isEmpty) + #expect(request.queryItems.isEmpty) } - func testQueryItemsWithLanguage() { + @Test("queryItems with language") + func queryItemsWithLanguage() { let request = CountriesConfigurationRequest(language: "en") - XCTAssertEqual(request.queryItems, ["language": "en"]) + #expect(request.queryItems == ["language": "en"]) } - func testMethodIsGet() { + @Test("method is GET") + func methodIsGet() { let request = CountriesConfigurationRequest() - XCTAssertEqual(request.method, .get) + #expect(request.method == .get) } - func testHeadersIsEmpty() { + @Test("headers is empty") + func headersIsEmpty() { let request = CountriesConfigurationRequest() - XCTAssertTrue(request.headers.isEmpty) + #expect(request.headers.isEmpty) } - func testBodyIsNil() { + @Test("body is nil") + func bodyIsNil() { let request = CountriesConfigurationRequest() - XCTAssertNil(request.body) + #expect(request.body == nil) } } diff --git a/Tests/TMDbTests/Domain/Services/Configuration/Requests/JobsConfigurationRequestTests.swift b/Tests/TMDbTests/Domain/Services/Configuration/Requests/JobsConfigurationRequestTests.swift index e0779029..f1e65880 100644 --- a/Tests/TMDbTests/Domain/Services/Configuration/Requests/JobsConfigurationRequestTests.swift +++ b/Tests/TMDbTests/Domain/Services/Configuration/Requests/JobsConfigurationRequestTests.swift @@ -17,41 +17,42 @@ // limitations under the License. // +import Foundation +import Testing @testable import TMDb -import XCTest -final class JobsConfigurationRequestTests: XCTestCase { +@Suite(.tags(.requests, .configuration)) +struct JobsConfigurationRequestTests { var request: JobsConfigurationRequest! - override func setUp() { - super.setUp() - request = JobsConfigurationRequest() + init() { + self.request = JobsConfigurationRequest() } - override func tearDown() { - request = nil - super.tearDown() + @Test("path is correct") + func path() { + #expect(request.path == "/configuration/jobs") } - func testPath() { - XCTAssertEqual(request.path, "/configuration/jobs") + @Test("queryItems is empty") + func queryItemsAreEmpty() { + #expect(request.queryItems.isEmpty) } - func testQueryItemsAreEmpty() { - XCTAssertTrue(request.queryItems.isEmpty) + @Test("method is GET") + func methodIsGet() { + #expect(request.method == .get) } - func testMethodIsGet() { - XCTAssertEqual(request.method, .get) + @Test("headers is empty") + func headersIsEmpty() { + #expect(request.headers.isEmpty) } - func testHeadersIsEmpty() { - XCTAssertTrue(request.headers.isEmpty) - } - - func testBodyIsNil() { - XCTAssertNil(request.body) + @Test("body is nil") + func bodyIsNil() { + #expect(request.body == nil) } } diff --git a/Tests/TMDbTests/Domain/Services/Configuration/Requests/LanguaguesConfigurationRequestTests.swift b/Tests/TMDbTests/Domain/Services/Configuration/Requests/LanguaguesConfigurationRequestTests.swift index 33738a81..6a008632 100644 --- a/Tests/TMDbTests/Domain/Services/Configuration/Requests/LanguaguesConfigurationRequestTests.swift +++ b/Tests/TMDbTests/Domain/Services/Configuration/Requests/LanguaguesConfigurationRequestTests.swift @@ -17,41 +17,42 @@ // limitations under the License. // +import Foundation +import Testing @testable import TMDb -import XCTest -final class LanguaguesConfigurationRequestTests: XCTestCase { +@Suite(.tags(.requests, .configuration)) +struct LanguaguesConfigurationRequestTests { var request: LanguaguesConfigurationRequest! - override func setUp() { - super.setUp() - request = LanguaguesConfigurationRequest() + init() { + self.request = LanguaguesConfigurationRequest() } - override func tearDown() { - request = nil - super.tearDown() + @Test("path is correct") + func path() { + #expect(request.path == "/configuration/languages") } - func testPath() { - XCTAssertEqual(request.path, "/configuration/languages") + @Test("queryItems is empty") + func queryItemsAreEmpty() { + #expect(request.queryItems.isEmpty) } - func testQueryItemsAreEmpty() { - XCTAssertTrue(request.queryItems.isEmpty) + @Test("method is GET") + func methodIsGet() { + #expect(request.method == .get) } - func testMethodIsGet() { - XCTAssertEqual(request.method, .get) + @Test("headers is empty") + func headersIsEmpty() { + #expect(request.headers.isEmpty) } - func testHeadersIsEmpty() { - XCTAssertTrue(request.headers.isEmpty) - } - - func testBodyIsNil() { - XCTAssertNil(request.body) + @Test("body is nil") + func bodyIsNil() { + #expect(request.body == nil) } } diff --git a/Tests/TMDbTests/Domain/Services/Configuration/TMDbConfigurationServiceTests.swift b/Tests/TMDbTests/Domain/Services/Configuration/TMDbConfigurationServiceTests.swift index fcf911bc..31bcae74 100644 --- a/Tests/TMDbTests/Domain/Services/Configuration/TMDbConfigurationServiceTests.swift +++ b/Tests/TMDbTests/Domain/Services/Configuration/TMDbConfigurationServiceTests.swift @@ -17,64 +17,56 @@ // limitations under the License. // +import Foundation +import Testing @testable import TMDb -import XCTest -final class TMDbConfigurationServiceTests: XCTestCase { +@Suite(.tags(.services, .configuration)) +struct TMDbConfigurationServiceTests { var service: TMDbConfigurationService! var apiClient: MockAPIClient! - override func setUp() { - super.setUp() - apiClient = MockAPIClient() - service = TMDbConfigurationService(apiClient: apiClient) + init() { + self.apiClient = MockAPIClient() + self.service = TMDbConfigurationService(apiClient: apiClient) } - override func tearDown() { - apiClient = nil - service = nil - super.tearDown() - } - - func testAPIConfigurationReturnsAPIConfiguration() async throws { + @Test("apiConfiguration returns api configuration") + func apiConfigurationReturnsAPIConfiguration() async throws { let expectedResult = APIConfiguration.mock() apiClient.addResponse(.success(expectedResult)) let expectedRequest = APIConfigurationRequest() let result = try await service.apiConfiguration() - XCTAssertEqual(result, expectedResult) - XCTAssertEqual(apiClient.lastRequest as? APIConfigurationRequest, expectedRequest) + #expect(result == expectedResult) + #expect(apiClient.lastRequest as? APIConfigurationRequest == expectedRequest) } - func testAPIConfigurationWhenErrorsThrowsError() async throws { + @Test("apiConfiguration when errors throws error") + func apiConfigurationWhenErrorsThrowsError() async throws { apiClient.addResponse(.failure(.unknown)) - var error: Error? - do { + await #expect(throws: TMDbError.unknown) { _ = try await service.apiConfiguration() - } catch let err { - error = err } - - let tmdbAPIError = try XCTUnwrap(error as? TMDbError) - - XCTAssertEqual(tmdbAPIError, .unknown) } - func testCountriesReturnsCountries() async throws { + @Test("countries returns countries") + func countriesReturnsCountries() async throws { let expectedResult = [Country].mocks apiClient.addResponse(.success(expectedResult)) let expectedRequest = CountriesConfigurationRequest(language: nil) let result = try await service.countries() - XCTAssertEqual(result, expectedResult) - XCTAssertEqual(apiClient.lastRequest as? CountriesConfigurationRequest, expectedRequest) + #expect(result == expectedResult) + #expect(apiClient.lastRequest as? CountriesConfigurationRequest == expectedRequest) } - func testCountriesWithLanguageReturnsCountries() async throws { + @Test("countries with language returns countries") + func countriesWithLanguageReturnsCountries() async throws { let expectedResult = [Country].mocks let language = "en" apiClient.addResponse(.success(expectedResult)) @@ -82,75 +74,59 @@ final class TMDbConfigurationServiceTests: XCTestCase { let result = try await service.countries(language: language) - XCTAssertEqual(result, expectedResult) - XCTAssertEqual(apiClient.lastRequest as? CountriesConfigurationRequest, expectedRequest) + #expect(result == expectedResult) + #expect(apiClient.lastRequest as? CountriesConfigurationRequest == 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 testJobsByDepartmentReturnsDepartments() async throws { + @Test("jobsByDepartment returns departments") + func jobsByDepartmentReturnsDepartments() async throws { let expectedResult = [Department].mocks apiClient.addResponse(.success(expectedResult)) let expectedRequest = JobsConfigurationRequest() let result = try await service.jobsByDepartment() - XCTAssertEqual(result, expectedResult) - XCTAssertEqual(apiClient.lastRequest as? JobsConfigurationRequest, expectedRequest) + #expect(result == expectedResult) + #expect(apiClient.lastRequest as? JobsConfigurationRequest == expectedRequest) } - func testJobsByDepartmentWhenErrorsThrows() async throws { + @Test("jobByDepartment when errors throws error") + func jobsByDepartmentWhenErrorsThrows() async throws { apiClient.addResponse(.failure(.unknown)) - var error: Error? - do { + await #expect(throws: TMDbError.unknown) { _ = try await service.jobsByDepartment() - } catch let err { - error = err } - - let tmdbAPIError = try XCTUnwrap(error as? TMDbError) - - XCTAssertEqual(tmdbAPIError, .unknown) } - func testLanguagesReturnsLanguages() async throws { + @Test("language returns languages") + func languagesReturnsLanguages() async throws { let expectedResult = [Language].mocks apiClient.addResponse(.success(expectedResult)) let expectedRequest = LanguaguesConfigurationRequest() let result = try await service.languages() - XCTAssertEqual(result, expectedResult) - XCTAssertEqual(apiClient.lastRequest as? LanguaguesConfigurationRequest, expectedRequest) + #expect(result == expectedResult) + #expect(apiClient.lastRequest as? LanguaguesConfigurationRequest == expectedRequest) } - func testLanguagesWhenErrorsThrowsError() async throws { + @Test("language when errors throws error") + func languagesWhenErrorsThrowsError() async throws { apiClient.addResponse(.failure(.unknown)) - var error: Error? - do { + await #expect(throws: TMDbError.unknown) { _ = try await service.languages() - } catch let err { - error = err } - - let tmdbAPIError = try XCTUnwrap(error as? TMDbError) - - XCTAssertEqual(tmdbAPIError, .unknown) } } diff --git a/Tests/TMDbTests/Networking/HTTPClient/HTTPMockClient.swift b/Tests/TMDbTests/Networking/HTTPClient/HTTPMockClient.swift index 266d741c..e6358ff6 100644 --- a/Tests/TMDbTests/Networking/HTTPClient/HTTPMockClient.swift +++ b/Tests/TMDbTests/Networking/HTTPClient/HTTPMockClient.swift @@ -17,8 +17,8 @@ // limitations under the License. // +import Foundation @testable import TMDb -import XCTest @MainActor final class HTTPMockClient: HTTPClient { diff --git a/Tests/TMDbTests/TestUtils/JSONDecoder+DecodeFromFile.swift b/Tests/TMDbTests/TestUtils/JSONDecoder+DecodeFromFile.swift index a434936c..c22dfe35 100644 --- a/Tests/TMDbTests/TestUtils/JSONDecoder+DecodeFromFile.swift +++ b/Tests/TMDbTests/TestUtils/JSONDecoder+DecodeFromFile.swift @@ -18,7 +18,7 @@ // import Foundation -import XCTest +import Testing extension JSONDecoder { @@ -26,14 +26,14 @@ extension JSONDecoder { _: T.Type, fromResource fileName: String, withExtension fileType: String = "json", - file: StaticString = #filePath, - line: UInt = #line + file _: StaticString = #filePath, + line _: UInt = #line ) throws -> T where T: Decodable { do { let data = try Data(fromResource: fileName, withExtension: fileType) return try decode(T.self, from: data) } catch let error { - XCTFail("Decode error: \(error.localizedDescription)", file: file, line: line) + Issue.record(error, "Decode error") throw error } } diff --git a/Tests/TMDbTests/TestUtils/Tags.swift b/Tests/TMDbTests/TestUtils/Tags.swift index 73d4ba38..cb2bdc04 100644 --- a/Tests/TMDbTests/TestUtils/Tags.swift +++ b/Tests/TMDbTests/TestUtils/Tags.swift @@ -36,5 +36,6 @@ extension Tag { @Tag static var authentication: Self @Tag static var certification: Self @Tag static var company: Self + @Tag static var configuration: Self }