Skip to content

Commit

Permalink
REFACTOR: Migrate Genre tests to Swift Testing (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamayoung authored Sep 17, 2024
1 parent 98408cf commit 0922f06
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,53 @@
// limitations under the License.
//

import Foundation
import Testing
@testable import TMDb
import XCTest

final class MovieGenresRequestTests: XCTestCase {
@Suite(.tags(.requests, .genre))
struct MovieGenresRequestTests {

func testPath() {
@Test("path is correct")
func path() {
let request = MovieGenresRequest()

XCTAssertEqual(request.path, "/genre/movie/list")
#expect(request.path == "/genre/movie/list")
}

func testQueryItemsIsEmpty() {
@Test("queryItems is empty")
func queryItemsIsEmpty() {
let request = MovieGenresRequest()

XCTAssertTrue(request.queryItems.isEmpty)
#expect(request.queryItems.isEmpty)
}

func testQueryItemsWithLanguage() {
@Test("queryItems with language")
func queryItemsWithLanguage() {
let request = MovieGenresRequest(language: "en")

XCTAssertEqual(request.queryItems, ["language": "en"])
#expect(request.queryItems == ["language": "en"])
}

func testMethodIsGet() {
@Test("method is GET")
func methodIsGet() {
let request = MovieGenresRequest()

XCTAssertEqual(request.method, .get)
#expect(request.method == .get)
}

func testHeadersIsEmpty() {
@Test("headers is empty")
func headersIsEmpty() {
let request = MovieGenresRequest()

XCTAssertTrue(request.headers.isEmpty)
#expect(request.headers.isEmpty)
}

func testBodyIsNil() {
@Test("body is nil")
func bodyIsNil() {
let request = MovieGenresRequest()

XCTAssertNil(request.body)
#expect(request.body == nil)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,53 @@
// limitations under the License.
//

import Foundation
import Testing
@testable import TMDb
import XCTest

final class TVSeriesGenresRequestTests: XCTestCase {
@Suite(.tags(.requests, .genre))
struct TVSeriesGenresRequestTests {

func testPath() {
@Test("path is correct")
func path() {
let request = TVSeriesGenresRequest()

XCTAssertEqual(request.path, "/genre/tv/list")
#expect(request.path == "/genre/tv/list")
}

func testQueryItemsIsEmpty() {
@Test("queryItems is empty")
func queryItemsIsEmpty() {
let request = TVSeriesGenresRequest()

XCTAssertTrue(request.queryItems.isEmpty)
#expect(request.queryItems.isEmpty)
}

func testQueryItemsWithLanguage() {
@Test("queryItems with language")
func queryItemsWithLanguage() {
let request = TVSeriesGenresRequest(language: "en")

XCTAssertEqual(request.queryItems, ["language": "en"])
#expect(request.queryItems == ["language": "en"])
}

func testMethodIsGet() {
@Test("method is GET")
func methodIsGet() {
let request = TVSeriesGenresRequest()

XCTAssertEqual(request.method, .get)
#expect(request.method == .get)
}

func testHeadersIsEmpty() {
@Test("headers is empty")
func headersIsEmpty() {
let request = TVSeriesGenresRequest()

XCTAssertTrue(request.headers.isEmpty)
#expect(request.headers.isEmpty)
}

func testBodyIsNil() {
@Test("body is nil")
func bodyIsNil() {
let request = TVSeriesGenresRequest()

XCTAssertNil(request.body)
#expect(request.body == nil)
}

}
71 changes: 29 additions & 42 deletions Tests/TMDbTests/Domain/Services/Genres/TMDbGenreServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,36 @@
// limitations under the License.
//

import Foundation
import Testing
@testable import TMDb
import XCTest

final class TMDbGenreServiceTests: XCTestCase {
@Suite(.tags(.services, .genre))
struct TMDbGenreServiceTests {

var service: TMDbGenreService!
var apiClient: MockAPIClient!

override func setUp() {
super.setUp()
apiClient = MockAPIClient()
service = TMDbGenreService(apiClient: apiClient)
init() {
self.apiClient = MockAPIClient()
self.service = TMDbGenreService(apiClient: apiClient)
}

override func tearDown() {
service = nil
apiClient = nil
super.tearDown()
}

func testMovieGenresReturnsGenres() async throws {
@Test("movieGenres returns genres")
func movieGenresReturnsGenres() async throws {
let genreList = GenreList.mock()
let expectedResult = genreList.genres
apiClient.addResponse(.success(genreList))
let expectedRequest = MovieGenresRequest(language: nil)

let result = try await service.movieGenres()

XCTAssertEqual(result, expectedResult)
XCTAssertEqual(apiClient.lastRequest as? MovieGenresRequest, expectedRequest)
#expect(result == expectedResult)
#expect(apiClient.lastRequest as? MovieGenresRequest == expectedRequest)
}

func testMovieGenresWithLanguageReturnsGenres() async throws {
@Test("movieGenres with language returns genres")
func movieGenresWithLanguageReturnsGenres() async throws {
let genreList = GenreList.mock()
let language = "en"
let expectedResult = genreList.genres
Expand All @@ -58,38 +55,34 @@ final class TMDbGenreServiceTests: XCTestCase {

let result = try await service.movieGenres(language: language)

XCTAssertEqual(result, expectedResult)
XCTAssertEqual(apiClient.lastRequest as? MovieGenresRequest, expectedRequest)
#expect(result == expectedResult)
#expect(apiClient.lastRequest as? MovieGenresRequest == expectedRequest)
}

func testMovieGenresWhenErrorsThrowsError() async throws {
@Test("movingGenres when errors throws error")
func movieGenresWhenErrorsThrowsError() async throws {
apiClient.addResponse(.failure(.unknown))

var error: Error?
do {
await #expect(throws: TMDbError.unknown) {
_ = try await service.movieGenres()
} catch let err {
error = err
}

let tmdbAPIError = try XCTUnwrap(error as? TMDbError)

XCTAssertEqual(tmdbAPIError, .unknown)
}

func testTVSeriesGenresReturnsGenres() async throws {
@Test("tvSeriesGenres returns genres")
func tvSeriesGenresReturnsGenres() async throws {
let genreList = GenreList.mock()
let expectedResult = genreList.genres
apiClient.addResponse(.success(genreList))
let expectedRequest = TVSeriesGenresRequest(language: nil)

let result = try await service.tvSeriesGenres()

XCTAssertEqual(result, expectedResult)
XCTAssertEqual(apiClient.lastRequest as? TVSeriesGenresRequest, expectedRequest)
#expect(result == expectedResult)
#expect(apiClient.lastRequest as? TVSeriesGenresRequest == expectedRequest)
}

func testTVSeriesGenresWithLanguageReturnsGenres() async throws {
@Test("tvSeriesGenres with language returns genres")
func tvSeriesGenresWithLanguageReturnsGenres() async throws {
let genreList = GenreList.mock()
let language = "en"
let expectedResult = genreList.genres
Expand All @@ -98,23 +91,17 @@ final class TMDbGenreServiceTests: XCTestCase {

let result = try await service.tvSeriesGenres(language: language)

XCTAssertEqual(result, expectedResult)
XCTAssertEqual(apiClient.lastRequest as? TVSeriesGenresRequest, expectedRequest)
#expect(result == expectedResult)
#expect(apiClient.lastRequest as? TVSeriesGenresRequest == expectedRequest)
}

func testTVSeriesGenresWhenErrorsThrowsError() async throws {
@Test("tvSeriesGenres when errors throws error")
func tvSeriesGenresWhenErrorsThrowsError() async throws {
apiClient.addResponse(.failure(.unknown))

var error: Error?
do {
await #expect(throws: TMDbError.unknown) {
_ = try await service.tvSeriesGenres()
} catch let err {
error = err
}

let tmdbAPIError = try XCTUnwrap(error as? TMDbError)

XCTAssertEqual(tmdbAPIError, .unknown)
}

}
1 change: 1 addition & 0 deletions Tests/TMDbTests/TestUtils/Tags.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ extension Tag {
@Tag static var company: Self
@Tag static var configuration: Self
@Tag static var discover: Self
@Tag static var genre: Self
@Tag static var movie: Self

}

0 comments on commit 0922f06

Please sign in to comment.