Skip to content

Commit

Permalink
REFACTOR: Migrate Certification tests to Swift Testing (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamayoung authored Sep 17, 2024
1 parent 62c3fba commit 6900031
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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)
}

}
1 change: 1 addition & 0 deletions Tests/TMDbTests/TestUtils/Tags.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ extension Tag {

@Tag static var account: Self
@Tag static var authentication: Self
@Tag static var certification: Self

}

0 comments on commit 6900031

Please sign in to comment.