Skip to content

Commit

Permalink
REFACTOR: Migrate integration tests to Swift Testing (#192)
Browse files Browse the repository at this point in the history
* WIP

* REFACTOR: Migrate integration tests to Swift Testing

* Update creating sessions

* Fix concurrency issue

* Disabled tests which create a session

* Fix linting errors

* Update make file
  • Loading branch information
adamayoung authored Sep 16, 2024
1 parent 049d827 commit 9ab06cd
Show file tree
Hide file tree
Showing 20 changed files with 636 additions and 478 deletions.
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,18 @@ test-ios:

.PHONY: test-watchos
test-watchos:
set -o pipefail && NSUnbufferedIO=YES xcodebuild build-for-testing -scheme $(TARGET) -only-testing $(TEST_TARGET) -destination $(WATCHOS_DESINTATION) | xcbeautify
set -o pipefail && NSUnbufferedIO=YES xcodebuild test-without-building -scheme $(TARGET) -only-testing $(TEST_TARGET) -destination $(WATCHOS_DESINTATION) | xcbeautify
set -o pipefail && NSUnbufferedIO=YES xcodebuild build-for-testing -scheme $(TARGET) -only-testing $(TEST_TARGET) -destination $(WATCHOS_DESINTATION)
set -o pipefail && NSUnbufferedIO=YES xcodebuild test-without-building -scheme $(TARGET) -only-testing $(TEST_TARGET) -destination $(WATCHOS_DESINTATION)

.PHONY: test-tvos
test-tvos:
set -o pipefail && NSUnbufferedIO=YES xcodebuild build-for-testing -scheme $(TARGET) -only-testing $(TEST_TARGET) -destination $(TVOS_DESTINATION) | xcbeautify
set -o pipefail && NSUnbufferedIO=YES xcodebuild test-without-building -scheme $(TARGET) -only-testing $(TEST_TARGET) -destination $(TVOS_DESTINATION) | xcbeautify
set -o pipefail && NSUnbufferedIO=YES xcodebuild build-for-testing -scheme $(TARGET) -only-testing $(TEST_TARGET) -destination $(TVOS_DESTINATION)
set -o pipefail && NSUnbufferedIO=YES xcodebuild test-without-building -scheme $(TARGET) -only-testing $(TEST_TARGET) -destination $(TVOS_DESTINATION)

.PHONY: test-visionos
test-visionos:
set -o pipefail && NSUnbufferedIO=YES xcodebuild build-for-testing -scheme $(TARGET) -only-testing $(TEST_TARGET) -destination $(VISIONOS_DESTINATION) | xcbeautify
set -o pipefail && NSUnbufferedIO=YES xcodebuild test-without-building -scheme $(TARGET) -only-testing $(TEST_TARGET) -destination $(VISIONOS_DESTINATION) | xcbeautify
set -o pipefail && NSUnbufferedIO=YES xcodebuild build-for-testing -scheme $(TARGET) -only-testing $(TEST_TARGET) -destination $(VISIONOS_DESTINATION)
set -o pipefail && NSUnbufferedIO=YES xcodebuild test-without-building -scheme $(TARGET) -only-testing $(TEST_TARGET) -destination $(VISIONOS_DESTINATION)

.PHONY: test-linux
test-linux:
Expand All @@ -98,7 +98,7 @@ integration-test: .check-env-vars
swift test --skip-build --filter $(INTEGRATION_TEST_TARGET) -Xswiftc -strict-concurrency=complete

.PHONY: ci
ci: .check-env-vars lint lint-markdown test test-ios test-watchos test-tvos test-visionos test-linux integration-test build-release build-docs
ci: .check-env-vars lint lint-markdown test test-ios test-watchos test-tvos test-visionos integration-test build-release build-docs

.check-env-vars:
@test $${TMDB_API_KEY?Please set environment variable TMDB_API_KEY}
Expand Down
73 changes: 41 additions & 32 deletions Tests/TMDbIntegrationTests/AccountIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,48 @@
// limitations under the License.
//

import TMDb
import XCTest

final class AccountIntegrationTests: XCTestCase {
import Foundation
import Testing
@testable import TMDb

@Suite(
.serialized,
.tags(.account),
.enabled(if: CredentialHelper.shared.hasAPIKey && CredentialHelper.shared.hasCredential),
.disabled()
)
final class AccountIntegrationTests {

var accountService: (any AccountService)!
var authenticationService: (any AuthenticationService)!
var session: Session!

override func setUp() async throws {
try await super.setUp()
let apiKey = try tmdbAPIKey()
init() async throws {
let apiKey = CredentialHelper.shared.tmdbAPIKey
let tmdbClient = TMDbClient(apiKey: apiKey)
let credential = try tmdbCredential()

authenticationService = tmdbClient.authentication
accountService = tmdbClient.account

session = try await authenticationService.createSession(withCredential: credential)
self.authenticationService = tmdbClient.authentication
self.accountService = tmdbClient.account
self.session = try await TMDbSessionHelper.shared.createSession()
}

override func tearDown() async throws {
// try await authenticationService.deleteSession(session)
accountService = nil
authenticationService = nil
session = nil
try await super.tearDown()
deinit {
if let thisSession = session {
Task {
try await TMDbSessionHelper.shared.delete(session: thisSession)
}
}
}

func testDetails() async throws {
@Test("details")
func details() async throws {
let details = try await accountService.details(session: session)

XCTAssertGreaterThan(details.id, 0)
#expect(details.id > 0)
}

func testAddingAndRemovingFavouriteMovies() async throws {
@Test("adding and removing favourite movies")
func addingAndRemovingFavouriteMovies() async throws {
let accountDetails = try await accountService.details(session: session)
let movieID = 550

Expand All @@ -63,7 +69,7 @@ final class AccountIntegrationTests: XCTestCase {
session: session
)
let isMovieFavourited = movieListAfterFavorited.results.contains { $0.id == movieID }
XCTAssertTrue(isMovieFavourited)
#expect(isMovieFavourited)

try await accountService.removeFavourite(movie: movieID, accountID: accountDetails.id, session: session)

Expand All @@ -73,10 +79,11 @@ final class AccountIntegrationTests: XCTestCase {
)

let isMovieFavouritedAfterRemoved = movieListAfterFavoriteRemoved.results.contains { $0.id == movieID }
XCTAssertFalse(isMovieFavouritedAfterRemoved)
#expect(!isMovieFavouritedAfterRemoved)
}

func testAddingAndRemovingFavouriteTVSeries() async throws {
@Test("add and removing favourite TV series")
func addingAndRemovingFavouriteTVSeries() async throws {
let accountDetails = try await accountService.details(session: session)
let tvSeriesID = 2261

Expand All @@ -87,7 +94,7 @@ final class AccountIntegrationTests: XCTestCase {
session: session
)
let isTVSeriesFavourited = tvSeriesListAfterFavorited.results.contains { $0.id == tvSeriesID }
XCTAssertTrue(isTVSeriesFavourited)
#expect(isTVSeriesFavourited)

try await accountService.removeFavourite(tvSeries: tvSeriesID, accountID: accountDetails.id, session: session)

Expand All @@ -97,10 +104,11 @@ final class AccountIntegrationTests: XCTestCase {
)

let isTVSeriesFavouritedAfterRemoved = tvSeriesListAfterFavoriteRemoved.results.contains { $0.id == tvSeriesID }
XCTAssertFalse(isTVSeriesFavouritedAfterRemoved)
#expect(!isTVSeriesFavouritedAfterRemoved)
}

func testAddingAndRemovingToMoviesWatchlist() async throws {
@Test("add and removing to movies watchlist")
func addingAndRemovingToMoviesWatchlist() async throws {
let accountDetails = try await accountService.details(session: session)
let movieID = 550

Expand All @@ -111,7 +119,7 @@ final class AccountIntegrationTests: XCTestCase {
session: session
)
let isMovieAddedToWatchlist = movieListAfterAddToWatchlist.results.contains { $0.id == movieID }
XCTAssertTrue(isMovieAddedToWatchlist)
#expect(isMovieAddedToWatchlist)

try await accountService.removeFromWatchlist(movie: movieID, accountID: accountDetails.id, session: session)

Expand All @@ -121,10 +129,11 @@ final class AccountIntegrationTests: XCTestCase {
)

let isMovieInWatchlistAfterRemoved = movieListAfterRemovedFromWatchlist.results.contains { $0.id == movieID }
XCTAssertFalse(isMovieInWatchlistAfterRemoved)
#expect(!isMovieInWatchlistAfterRemoved)
}

func testAddingAndRemovingTVSeriesToWatchlist() async throws {
@Test("add and removing to TV series watchlist")
func addingAndRemovingTVSeriesToWatchlist() async throws {
let accountDetails = try await accountService.details(session: session)
let tvSeriesID = 2261

Expand All @@ -135,7 +144,7 @@ final class AccountIntegrationTests: XCTestCase {
session: session
)
let isTVSeriesAddedToWatchlist = tvSeriesListAfterAddedToWatchlist.results.contains { $0.id == tvSeriesID }
XCTAssertTrue(isTVSeriesAddedToWatchlist)
#expect(isTVSeriesAddedToWatchlist)

try await accountService.removeFromWatchlist(
tvSeries: tvSeriesID,
Expand All @@ -151,7 +160,7 @@ final class AccountIntegrationTests: XCTestCase {
let isTVSeriesInWatchlistAfterRemoved = tvSeriesListAfterRemovedFromWatchlist.results.contains {
$0.id == tvSeriesID
}
XCTAssertFalse(isTVSeriesInWatchlistAfterRemoved)
#expect(!isTVSeriesInWatchlistAfterRemoved)
}

}
57 changes: 32 additions & 25 deletions Tests/TMDbIntegrationTests/AuthenticationIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,58 +17,65 @@
// limitations under the License.
//

import TMDb
import XCTest
import Foundation
import Testing
@testable import TMDb

final class AuthenticationIntegrationTests: XCTestCase {
@Suite(
.tags(.authentication),
.enabled(if: CredentialHelper.shared.hasAPIKey)
)
struct AuthenticationIntegrationTests {

var authenticationService: (any AuthenticationService)!

override func setUpWithError() throws {
try super.setUpWithError()
let apiKey = try tmdbAPIKey()
authenticationService = TMDbClient(apiKey: apiKey).authentication
init() {
let apiKey = CredentialHelper.shared.tmdbAPIKey
self.authenticationService = TMDbClient(apiKey: apiKey).authentication
}

override func tearDown() {
authenticationService = nil
super.tearDown()
}

func testGuestSession() async throws {
@Test("guestSession")
func guestSession() async throws {
let session = try await authenticationService.guestSession()

XCTAssertTrue(session.success)
XCTAssertNotEqual(session.guestSessionID, "")
#expect(session.success)
#expect(session.guestSessionID != "")
}

func testRequestToken() async throws {
@Test("requestToken")
func requestToken() async throws {
let token = try await authenticationService.requestToken()

XCTAssertTrue(token.success)
XCTAssertNotEqual(token.requestToken, "")
#expect(token.success)
#expect(token.requestToken != "")
}

func testCreateAndDeleteSessionWithCredential() async throws {
let credential = try tmdbCredential()
@Test(
"createSession with credential",
.enabled(if: CredentialHelper.shared.hasCredential),
.disabled()
)
func createAndDeleteSessionWithCredential() async throws {
let credential = CredentialHelper.shared.tmdbCredential

let session = try await authenticationService.createSession(withCredential: credential)

XCTAssertTrue(session.success)
XCTAssertNotEqual(session.sessionID, "")
#expect(session.success)
#expect(session.sessionID != "")

do {
let deleteResult = try await authenticationService.deleteSession(session)
XCTAssertTrue(deleteResult)
#expect(deleteResult)
} catch let error {
print(error)
}
}

func testValidateKeyWhenValid() async throws {
@Test("validateKey")
func validateKeyWhenValid() async throws {
let isValid = try await authenticationService.validateKey()

XCTAssertTrue(isValid)
#expect(isValid)
}

}
35 changes: 18 additions & 17 deletions Tests/TMDbIntegrationTests/CertificationIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,39 @@
// limitations under the License.
//

import TMDb
import XCTest
import Foundation
import Testing
@testable import TMDb

final class CertificationIntegrationTests: XCTestCase {
@Suite(
.tags(.certification),
.enabled(if: CredentialHelper.shared.hasAPIKey)
)
struct CertificationIntegrationTests {

var certificationService: (any CertificationService)!

override func setUpWithError() throws {
try super.setUpWithError()
let apiKey = try tmdbAPIKey()
certificationService = TMDbClient(apiKey: apiKey).certifications
}

override func tearDown() {
certificationService = nil
super.tearDown()
init() {
let apiKey = CredentialHelper.shared.tmdbAPIKey
self.certificationService = TMDbClient(apiKey: apiKey).certifications
}

@Test("movieCertifications")
func testMovieCertifications() async throws {
let certifications = try await certificationService.movieCertifications()

let gbCertifications = try XCTUnwrap(certifications["GB"])
let gbCertifications = try #require(certifications["GB"])

XCTAssertEqual(gbCertifications.count, 7)
#expect(gbCertifications.count == 7)
}

func testTVSeriesCertifications() async throws {
@Test("tvSeriesCertifications")
func tvSeriesCertifications() async throws {
let certifications = try await certificationService.tvSeriesCertifications()

let gbCertifications = try XCTUnwrap(certifications["GB"])
let gbCertifications = try #require(certifications["GB"])

XCTAssertEqual(gbCertifications.count, 7)
#expect(gbCertifications.count == 7)
}

}
30 changes: 15 additions & 15 deletions Tests/TMDbIntegrationTests/CompanyIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,31 @@
// limitations under the License.
//

import TMDb
import XCTest
import Foundation
import Testing
@testable import TMDb

final class CompanyIntegrationTests: XCTestCase {
@Suite(
.tags(.company),
.enabled(if: CredentialHelper.shared.hasAPIKey)
)
struct CompanyIntegrationTests {

var companyService: (any CompanyService)!

override func setUpWithError() throws {
try super.setUpWithError()
let apiKey = try tmdbAPIKey()
companyService = TMDbClient(apiKey: apiKey).companies
init() {
let apiKey = CredentialHelper.shared.tmdbAPIKey
self.companyService = TMDbClient(apiKey: apiKey).companies
}

override func tearDown() {
companyService = nil
super.tearDown()
}

func testDetails() async throws {
@Test("details")
func details() async throws {
let companyID = 82968

let company = try await companyService.details(forCompany: companyID)

XCTAssertEqual(company.id, companyID)
XCTAssertEqual(company.name, "LuckyChap Entertainment")
#expect(company.id == companyID)
#expect(company.name == "LuckyChap Entertainment")
}

}
Loading

0 comments on commit 9ab06cd

Please sign in to comment.