From 6900031d668898f330c19aef7c58b3e328982722 Mon Sep 17 00:00:00 2001 From: Adam Young Date: Tue, 17 Sep 2024 13:07:21 +0100 Subject: [PATCH] REFACTOR: Migrate Certification tests to Swift Testing (#197) --- .../MovieCertificationsRequestTests.swift | 41 +++++++------- .../TVSeriesCertificationsRequestTests.swift | 41 +++++++------- .../TMDbCertificationServiceTests.swift | 56 +++++++------------ Tests/TMDbTests/TestUtils/Tags.swift | 1 + 4 files changed, 63 insertions(+), 76 deletions(-) diff --git a/Tests/TMDbTests/Domain/Services/Certifications/Requests/MovieCertificationsRequestTests.swift b/Tests/TMDbTests/Domain/Services/Certifications/Requests/MovieCertificationsRequestTests.swift index 8723f7ce..85ab085f 100644 --- a/Tests/TMDbTests/Domain/Services/Certifications/Requests/MovieCertificationsRequestTests.swift +++ b/Tests/TMDbTests/Domain/Services/Certifications/Requests/MovieCertificationsRequestTests.swift @@ -17,41 +17,42 @@ // limitations under the License. // +import Foundation +import Testing @testable import TMDb -import XCTest -final class MovieCertificationsRequestTests: XCTestCase { +@Suite(.tags(.requests, .certification)) +struct MovieCertificationsRequestTests { var request: MovieCertificationsRequest! - override func setUp() { - super.setUp() - request = MovieCertificationsRequest() + init() { + self.request = MovieCertificationsRequest() } - override func tearDown() { - request = nil - super.tearDown() + @Test("path is correct") + func path() { + #expect(request.path == "/certification/movie/list") } - func testPath() { - XCTAssertEqual(request.path, "/certification/movie/list") + @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/Certifications/Requests/TVSeriesCertificationsRequestTests.swift b/Tests/TMDbTests/Domain/Services/Certifications/Requests/TVSeriesCertificationsRequestTests.swift index 802262d6..7039aebf 100644 --- a/Tests/TMDbTests/Domain/Services/Certifications/Requests/TVSeriesCertificationsRequestTests.swift +++ b/Tests/TMDbTests/Domain/Services/Certifications/Requests/TVSeriesCertificationsRequestTests.swift @@ -17,41 +17,42 @@ // limitations under the License. // +import Foundation +import Testing @testable import TMDb -import XCTest -final class TVSeriesCertificationsRequestTests: XCTestCase { +@Suite(.tags(.requests, .certification)) +struct TVSeriesCertificationsRequestTests { var request: TVSeriesCertificationsRequest! - override func setUp() { - super.setUp() - request = TVSeriesCertificationsRequest() + init() { + self.request = TVSeriesCertificationsRequest() } - override func tearDown() { - request = nil - super.tearDown() + @Test("path is correct") + func path() { + #expect(request.path == "/certification/tv/list") } - func testPath() { - XCTAssertEqual(request.path, "/certification/tv/list") + @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/Certifications/TMDbCertificationServiceTests.swift b/Tests/TMDbTests/Domain/Services/Certifications/TMDbCertificationServiceTests.swift index 649deabf..f24ebd58 100644 --- a/Tests/TMDbTests/Domain/Services/Certifications/TMDbCertificationServiceTests.swift +++ b/Tests/TMDbTests/Domain/Services/Certifications/TMDbCertificationServiceTests.swift @@ -17,27 +17,23 @@ // limitations under the License. // +import Foundation +import Testing @testable import TMDb -import XCTest -final class TMDbCertificationServiceTests: XCTestCase { +@Suite(.tags(.services, .certification)) +struct TMDbCertificationServiceTests { var service: TMDbCertificationService! var apiClient: MockAPIClient! - override func setUp() { - super.setUp() - apiClient = MockAPIClient() - service = TMDbCertificationService(apiClient: apiClient) + init() { + self.apiClient = MockAPIClient() + self.service = TMDbCertificationService(apiClient: apiClient) } - override func tearDown() { - service = nil - apiClient = nil - super.tearDown() - } - - func testMovieCertificationsReturnsMovieCertifications() async throws { + @Test("movieCertifications returns movie certifications") + func movieCertificationsReturnsMovieCertifications() async throws { let certifications = Certifications.gbAndUS let expectedResult = certifications.certifications let expectedRequest = MovieCertificationsRequest() @@ -46,26 +42,21 @@ final class TMDbCertificationServiceTests: XCTestCase { let result = try await service.movieCertifications() - XCTAssertEqual(result, expectedResult) - XCTAssertEqual(apiClient.lastRequest as? MovieCertificationsRequest, expectedRequest) + #expect(result == expectedResult) + #expect(apiClient.lastRequest as? MovieCertificationsRequest == expectedRequest) } - func testMovieCertificationsWhenErrosThrowError() async throws { + @Test("movieCertifications when errors throw error") + func movieCertificationsWhenErrosThrowError() async throws { apiClient.addResponse(.failure(.unknown)) - var error: Error? - do { + await #expect(throws: TMDbError.unknown) { _ = try await service.movieCertifications() - } catch let err { - error = err } - - let tmdbAPIError = try XCTUnwrap(error as? TMDbError) - - XCTAssertEqual(tmdbAPIError, .unknown) } - func testTVSeriesCertificationsReturnsTVSeriesCertifications() async throws { + @Test("tvSeriesCertifications returns TV series certifications") + func tvSeriesCertificationsReturnsTVSeriesCertifications() async throws { let certifications = Certifications.gbAndUS let expectedResult = certifications.certifications let expectedRequest = TVSeriesCertificationsRequest() @@ -74,23 +65,16 @@ final class TMDbCertificationServiceTests: XCTestCase { let result = try await service.tvSeriesCertifications() - XCTAssertEqual(result, expectedResult) - XCTAssertEqual(apiClient.lastRequest as? TVSeriesCertificationsRequest, expectedRequest) + #expect(result == expectedResult) + #expect(apiClient.lastRequest as? TVSeriesCertificationsRequest == expectedRequest) } - func testTVSeriesCertificationsWhenErrorsThrowsError() async throws { + func tvSeriesCertificationsWhenErrorsThrowsError() async throws { apiClient.addResponse(.failure(.unknown)) - var error: Error? - do { + await #expect(throws: TMDbError.unknown) { _ = try await service.tvSeriesCertifications() - } catch let err { - error = err } - - let tmdbAPIError = try XCTUnwrap(error as? TMDbError) - - XCTAssertEqual(tmdbAPIError, .unknown) } } diff --git a/Tests/TMDbTests/TestUtils/Tags.swift b/Tests/TMDbTests/TestUtils/Tags.swift index 4f0da1b7..f1b640e6 100644 --- a/Tests/TMDbTests/TestUtils/Tags.swift +++ b/Tests/TMDbTests/TestUtils/Tags.swift @@ -34,5 +34,6 @@ extension Tag { @Tag static var account: Self @Tag static var authentication: Self + @Tag static var certification: Self }