From e4b4ec16aac1ccb998b76a83b4b4fef64ca68d14 Mon Sep 17 00:00:00 2001 From: Adam Young Date: Wed, 18 Sep 2024 10:53:28 +0100 Subject: [PATCH] REFACTOR: Migrate Trending tests to Swift Testing (#205) --- .../TrendingTimeWindowFilterTypeTests.swift | 23 +++-- .../Requests/TrendingMoviesRequestTests.swift | 51 ++++++---- .../Requests/TrendingPeopleRequestTests.swift | 51 ++++++---- .../TrendingTVSeriesRequestTests.swift | 51 ++++++---- .../Trending/TMDbTrendingServiceTests.swift | 97 ++++++++----------- Tests/TMDbTests/TestUtils/Tags.swift | 1 + 6 files changed, 145 insertions(+), 129 deletions(-) diff --git a/Tests/TMDbTests/Domain/Services/Trending/Filters/TrendingTimeWindowFilterTypeTests.swift b/Tests/TMDbTests/Domain/Services/Trending/Filters/TrendingTimeWindowFilterTypeTests.swift index 104b8e9a..022728a3 100644 --- a/Tests/TMDbTests/Domain/Services/Trending/Filters/TrendingTimeWindowFilterTypeTests.swift +++ b/Tests/TMDbTests/Domain/Services/Trending/Filters/TrendingTimeWindowFilterTypeTests.swift @@ -17,25 +17,24 @@ // limitations under the License. // +import Foundation +import Testing @testable import TMDb -import XCTest -final class TrendingTimeWindowFilterTypeTests: XCTestCase { +@Suite(.tags(.trending)) +struct TrendingTimeWindowFilterTypeTests { - func testDayReturnsRawValue() { - let expectedResult = "day" + @Test("day raw value is \"day\"") + func dayRawValue() { + let filterType = TrendingTimeWindowFilterType.day - let result = TrendingTimeWindowFilterType.day.rawValue - - XCTAssertEqual(result, expectedResult) + #expect(filterType.rawValue == "day") } - func testWeekReturnsRawValue() { - let expectedResult = "week" - - let result = TrendingTimeWindowFilterType.week.rawValue + func weekRawValue() { + let filterType = TrendingTimeWindowFilterType.week - XCTAssertEqual(result, expectedResult) + #expect(filterType.rawValue == "week") } } diff --git a/Tests/TMDbTests/Domain/Services/Trending/Requests/TrendingMoviesRequestTests.swift b/Tests/TMDbTests/Domain/Services/Trending/Requests/TrendingMoviesRequestTests.swift index fa985daa..7adb8d83 100644 --- a/Tests/TMDbTests/Domain/Services/Trending/Requests/TrendingMoviesRequestTests.swift +++ b/Tests/TMDbTests/Domain/Services/Trending/Requests/TrendingMoviesRequestTests.swift @@ -17,63 +17,74 @@ // limitations under the License. // +import Foundation +import Testing @testable import TMDb -import XCTest -final class TrendingMoviesRequestTests: XCTestCase { +@Suite(.tags(.requests, .trending)) +struct TrendingMoviesRequestTests { - func testPathWithDayTimeWindow() { + @Test("path with day time window") + func pathWithDayTimeWindow() { let request = TrendingMoviesRequest(timeWindow: .day) - XCTAssertEqual(request.path, "/trending/movie/day") + #expect(request.path == "/trending/movie/day") } - func testPathWithWeekTimeWindow() { + @Test("path with week time window") + func pathWithWeekTimeWindow() { let request = TrendingMoviesRequest(timeWindow: .week) - XCTAssertEqual(request.path, "/trending/movie/week") + #expect(request.path == "/trending/movie/week") } - func testQueryItemsIsEmpty() { + @Test("queryItems is empty") + func queryItemsIsEmpty() { let request = TrendingMoviesRequest(timeWindow: .day) - XCTAssertTrue(request.queryItems.isEmpty) + #expect(request.queryItems.isEmpty) } - func testQueryItemsWithPage() { + @Test("queryItems with page") + func queryItemsWithPage() { let request = TrendingMoviesRequest(timeWindow: .day, page: 1) - XCTAssertEqual(request.queryItems, ["page": "1"]) + #expect(request.queryItems == ["page": "1"]) } - func testQueryItemsWithLanguage() { + @Test("queryItems with language") + func queryItemsWithLanguage() { let request = TrendingMoviesRequest(timeWindow: .day, language: "en") - XCTAssertEqual(request.queryItems, ["language": "en"]) + #expect(request.queryItems == ["language": "en"]) } - func testQueryItemsWithPageAndLanguage() { + @Test("queryItems with page and language") + func queryItemsWithPageAndLanguage() { let request = TrendingMoviesRequest(timeWindow: .day, page: 1, language: "en") - XCTAssertEqual(request.queryItems, ["page": "1", "language": "en"]) + #expect(request.queryItems == ["page": "1", "language": "en"]) } - func testMethodIsGet() { + @Test("method is GET") + func methodIsGet() { let request = TrendingMoviesRequest(timeWindow: .day) - XCTAssertEqual(request.method, .get) + #expect(request.method == .get) } - func testHeadersIsEmpty() { + @Test("headers is empty") + func headersIsEmpty() { let request = TrendingMoviesRequest(timeWindow: .day) - XCTAssertTrue(request.headers.isEmpty) + #expect(request.headers.isEmpty) } - func testBodyIsNil() { + @Test("body is nil") + func bodyIsNil() { let request = TrendingMoviesRequest(timeWindow: .day) - XCTAssertNil(request.body) + #expect(request.body == nil) } } diff --git a/Tests/TMDbTests/Domain/Services/Trending/Requests/TrendingPeopleRequestTests.swift b/Tests/TMDbTests/Domain/Services/Trending/Requests/TrendingPeopleRequestTests.swift index 83cd66ef..8a1fd1f6 100644 --- a/Tests/TMDbTests/Domain/Services/Trending/Requests/TrendingPeopleRequestTests.swift +++ b/Tests/TMDbTests/Domain/Services/Trending/Requests/TrendingPeopleRequestTests.swift @@ -17,63 +17,74 @@ // limitations under the License. // +import Foundation +import Testing @testable import TMDb -import XCTest -final class TrendingPeopleRequestTests: XCTestCase { +@Suite(.tags(.requests, .trending)) +struct TrendingPeopleRequestTests { - func testPathWithDayTimeWindow() { + @Test("path with day time window") + func pathWithDayTimeWindow() { let request = TrendingPeopleRequest(timeWindow: .day) - XCTAssertEqual(request.path, "/trending/person/day") + #expect(request.path == "/trending/person/day") } - func testPathWithWeekTimeWindow() { + @Test("path with week time window") + func pathWithWeekTimeWindow() { let request = TrendingPeopleRequest(timeWindow: .week) - XCTAssertEqual(request.path, "/trending/person/week") + #expect(request.path == "/trending/person/week") } - func testQueryItemsIsEmpty() { + @Test("queryItems is empty") + func queryItemsIsEmpty() { let request = TrendingPeopleRequest(timeWindow: .day) - XCTAssertTrue(request.queryItems.isEmpty) + #expect(request.queryItems.isEmpty) } - func testQueryItemsWithPage() { + @Test("queryItems with page") + func queryItemsWithPage() { let request = TrendingPeopleRequest(timeWindow: .day, page: 1) - XCTAssertEqual(request.queryItems, ["page": "1"]) + #expect(request.queryItems == ["page": "1"]) } - func testQueryItemsWithLanguage() { + @Test("queryItems with language") + func queryItemsWithLanguage() { let request = TrendingPeopleRequest(timeWindow: .day, language: "en") - XCTAssertEqual(request.queryItems, ["language": "en"]) + #expect(request.queryItems == ["language": "en"]) } - func testQueryItemsWithPageAndLanguage() { + @Test("queryItems with page and language") + func queryItemsWithPageAndLanguage() { let request = TrendingPeopleRequest(timeWindow: .day, page: 1, language: "en") - XCTAssertEqual(request.queryItems, ["page": "1", "language": "en"]) + #expect(request.queryItems == ["page": "1", "language": "en"]) } - func testMethodIsGet() { + @Test("method is GET") + func methodIsGet() { let request = TrendingPeopleRequest(timeWindow: .day) - XCTAssertEqual(request.method, .get) + #expect(request.method == .get) } - func testHeadersIsEmpty() { + @Test("headers is empty") + func headersIsEmpty() { let request = TrendingPeopleRequest(timeWindow: .day) - XCTAssertTrue(request.headers.isEmpty) + #expect(request.headers.isEmpty) } - func testBodyIsNil() { + @Test("body is nil") + func bodyIsNil() { let request = TrendingPeopleRequest(timeWindow: .day) - XCTAssertNil(request.body) + #expect(request.body == nil) } } diff --git a/Tests/TMDbTests/Domain/Services/Trending/Requests/TrendingTVSeriesRequestTests.swift b/Tests/TMDbTests/Domain/Services/Trending/Requests/TrendingTVSeriesRequestTests.swift index c93cd7f7..af3393bc 100644 --- a/Tests/TMDbTests/Domain/Services/Trending/Requests/TrendingTVSeriesRequestTests.swift +++ b/Tests/TMDbTests/Domain/Services/Trending/Requests/TrendingTVSeriesRequestTests.swift @@ -17,63 +17,74 @@ // limitations under the License. // +import Foundation +import Testing @testable import TMDb -import XCTest -final class TrendingTVSeriesRequestTests: XCTestCase { +@Suite(.tags(.requests, .trending)) +struct TrendingTVSeriesRequestTests { - func testPathWithDayTimeWindow() { + @Test("path with day time window") + func pathWithDayTimeWindow() { let request = TrendingTVSeriesRequest(timeWindow: .day) - XCTAssertEqual(request.path, "/trending/tv/day") + #expect(request.path == "/trending/tv/day") } - func testPathWithWeekTimeWindow() { + @Test("path with week time window") + func pathWithWeekTimeWindow() { let request = TrendingTVSeriesRequest(timeWindow: .week) - XCTAssertEqual(request.path, "/trending/tv/week") + #expect(request.path == "/trending/tv/week") } - func testQueryItemsIsEmpty() { + @Test("queryItems is empty") + func queryItemsIsEmpty() { let request = TrendingTVSeriesRequest(timeWindow: .day) - XCTAssertTrue(request.queryItems.isEmpty) + #expect(request.queryItems.isEmpty) } - func testQueryItemsWithPage() { + @Test("queryItems with page") + func queryItemsWithPage() { let request = TrendingTVSeriesRequest(timeWindow: .day, page: 1) - XCTAssertEqual(request.queryItems, ["page": "1"]) + #expect(request.queryItems == ["page": "1"]) } - func testQueryItemsWithLanguage() { + @Test("queryItems with language") + func queryItemsWithLanguage() { let request = TrendingTVSeriesRequest(timeWindow: .day, language: "en") - XCTAssertEqual(request.queryItems, ["language": "en"]) + #expect(request.queryItems == ["language": "en"]) } - func testQueryItemsWithPageAndLanguage() { + @Test("queryItems with page and language") + func queryItemsWithPageAndLanguage() { let request = TrendingTVSeriesRequest(timeWindow: .day, page: 1, language: "en") - XCTAssertEqual(request.queryItems, ["page": "1", "language": "en"]) + #expect(request.queryItems == ["page": "1", "language": "en"]) } - func testMethodIsGet() { + @Test("method is GET") + func methodIsGet() { let request = TrendingTVSeriesRequest(timeWindow: .day) - XCTAssertEqual(request.method, .get) + #expect(request.method == .get) } - func testHeadersIsEmpty() { + @Test("headers is empty") + func headersIsEmpty() { let request = TrendingTVSeriesRequest(timeWindow: .day) - XCTAssertTrue(request.headers.isEmpty) + #expect(request.headers.isEmpty) } - func testBodyIsNil() { + @Test("body is nil") + func bodyIsNil() { let request = TrendingTVSeriesRequest(timeWindow: .day) - XCTAssertNil(request.body) + #expect(request.body == nil) } } diff --git a/Tests/TMDbTests/Domain/Services/Trending/TMDbTrendingServiceTests.swift b/Tests/TMDbTests/Domain/Services/Trending/TMDbTrendingServiceTests.swift index 9bfb337e..42534814 100644 --- a/Tests/TMDbTests/Domain/Services/Trending/TMDbTrendingServiceTests.swift +++ b/Tests/TMDbTests/Domain/Services/Trending/TMDbTrendingServiceTests.swift @@ -17,27 +17,23 @@ // limitations under the License. // +import Foundation +import Testing @testable import TMDb -import XCTest -final class TMDbTrendingServiceTests: XCTestCase { +@Suite(.tags(.services, .trending)) +struct TMDbTrendingServiceTests { var service: TMDbTrendingService! var apiClient: MockAPIClient! - override func setUp() { - super.setUp() - apiClient = MockAPIClient() - service = TMDbTrendingService(apiClient: apiClient) + init() { + self.apiClient = MockAPIClient() + self.service = TMDbTrendingService(apiClient: apiClient) } - override func tearDown() { - apiClient = nil - service = nil - super.tearDown() - } - - func testMoviesReturnsMovies() async throws { + @Test("movies returns movies") + func moviesReturnsMovies() async throws { let timeWindow = TrendingTimeWindowFilterType.day let expectedResult = MoviePageableList.mock() apiClient.addResponse(.success(expectedResult)) @@ -45,11 +41,12 @@ final class TMDbTrendingServiceTests: XCTestCase { let result = try await service.movies(inTimeWindow: timeWindow) - XCTAssertEqual(result, expectedResult) - XCTAssertEqual(apiClient.lastRequest as? TrendingMoviesRequest, expectedRequest) + #expect(result == expectedResult) + #expect(apiClient.lastRequest as? TrendingMoviesRequest == expectedRequest) } - func testMoviesWithPageAndLanguageReturnsMovies() async throws { + @Test("movies with page and language returns movies") + func moviesWithPageAndLanguageReturnsMovies() async throws { let timeWindow = TrendingTimeWindowFilterType.week let page = 2 let language = "en" @@ -59,27 +56,22 @@ final class TMDbTrendingServiceTests: XCTestCase { let result = try await service.movies(inTimeWindow: timeWindow, page: page, language: language) - XCTAssertEqual(result, expectedResult) - XCTAssertEqual(apiClient.lastRequest as? TrendingMoviesRequest, expectedRequest) + #expect(result == expectedResult) + #expect(apiClient.lastRequest as? TrendingMoviesRequest == expectedRequest) } - func testMoviesWhenErrorsThrowsError() async throws { + @Test("movies when errors throws error") + func moviesWhenErrorsThrowsError() async throws { let timeWindow = TrendingTimeWindowFilterType.day apiClient.addResponse(.failure(.unknown)) - var error: Error? - do { + await #expect(throws: TMDbError.unknown) { _ = try await service.movies(inTimeWindow: timeWindow) - } catch let err { - error = err } - - let tmdbAPIError = try XCTUnwrap(error as? TMDbError) - - XCTAssertEqual(tmdbAPIError, .unknown) } - func testTVSeriesReturnsTVSeries() async throws { + @Test("tvSeries returns tv series") + func tvSeriesReturnsTVSeries() async throws { let timeWindow = TrendingTimeWindowFilterType.day let expectedResult = TVSeriesPageableList.mock() apiClient.addResponse(.success(expectedResult)) @@ -87,11 +79,12 @@ final class TMDbTrendingServiceTests: XCTestCase { let result = try await service.tvSeries(inTimeWindow: timeWindow) - XCTAssertEqual(result, expectedResult) - XCTAssertEqual(apiClient.lastRequest as? TrendingTVSeriesRequest, expectedRequest) + #expect(result == expectedResult) + #expect(apiClient.lastRequest as? TrendingTVSeriesRequest == expectedRequest) } - func testTVSeriesWithPageAndLanguageReturnsTVSeries() async throws { + @Test("tvSeries with page and language returns TV series") + func tvSeriesWithPageAndLanguageReturnsTVSeries() async throws { let timeWindow = TrendingTimeWindowFilterType.week let page = 2 let language = "en" @@ -101,27 +94,22 @@ final class TMDbTrendingServiceTests: XCTestCase { let result = try await service.tvSeries(inTimeWindow: timeWindow, page: page, language: language) - XCTAssertEqual(result, expectedResult) - XCTAssertEqual(apiClient.lastRequest as? TrendingTVSeriesRequest, expectedRequest) + #expect(result == expectedResult) + #expect(apiClient.lastRequest as? TrendingTVSeriesRequest == expectedRequest) } - func testTVSeriesWhenErrorsThrowsError() async throws { + @Test("tvSeries when errors throws error") + func tvSeriesWhenErrorsThrowsError() async throws { let timeWindow = TrendingTimeWindowFilterType.day apiClient.addResponse(.failure(.unknown)) - var error: Error? - do { + await #expect(throws: TMDbError.unknown) { _ = try await service.tvSeries(inTimeWindow: timeWindow) - } catch let err { - error = err } - - let tmdbAPIError = try XCTUnwrap(error as? TMDbError) - - XCTAssertEqual(tmdbAPIError, .unknown) } - func testPeopleReturnsPeople() async throws { + @Test("people returns people") + func peopleReturnsPeople() async throws { let timeWindow = TrendingTimeWindowFilterType.day let expectedResult = PersonPageableList.mock() apiClient.addResponse(.success(expectedResult)) @@ -129,11 +117,12 @@ final class TMDbTrendingServiceTests: XCTestCase { let result = try await service.people(inTimeWindow: timeWindow) - XCTAssertEqual(result, expectedResult) - XCTAssertEqual(apiClient.lastRequest as? TrendingPeopleRequest, expectedRequest) + #expect(result == expectedResult) + #expect(apiClient.lastRequest as? TrendingPeopleRequest == expectedRequest) } - func testPeopleWithPageAndLanguageReturnsPeople() async throws { + @Test("people with page and language returns people") + func peopleWithPageAndLanguageReturnsPeople() async throws { let timeWindow = TrendingTimeWindowFilterType.week let page = 2 let language = "en" @@ -143,24 +132,18 @@ final class TMDbTrendingServiceTests: XCTestCase { let result = try await service.people(inTimeWindow: timeWindow, page: page, language: language) - XCTAssertEqual(result, expectedResult) - XCTAssertEqual(apiClient.lastRequest as? TrendingPeopleRequest, expectedRequest) + #expect(result == expectedResult) + #expect(apiClient.lastRequest as? TrendingPeopleRequest == expectedRequest) } - func testPeopleWhenErrorsThrowsError() async throws { + @Test("people when errors throws error") + func peopleWhenErrorsThrowsError() async throws { let timeWindow = TrendingTimeWindowFilterType.day apiClient.addResponse(.failure(.unknown)) - var error: Error? - do { + await #expect(throws: TMDbError.unknown) { _ = try await service.people(inTimeWindow: timeWindow) - } 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 caaf4255..28fdd209 100644 --- a/Tests/TMDbTests/TestUtils/Tags.swift +++ b/Tests/TMDbTests/TestUtils/Tags.swift @@ -42,5 +42,6 @@ extension Tag { @Tag static var movie: Self @Tag static var people: Self @Tag static var search: Self + @Tag static var trending: Self }