Skip to content

Commit

Permalink
TEST: Improve code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
adamayoung committed Nov 25, 2024
1 parent 809024b commit 6e62acf
Show file tree
Hide file tree
Showing 35 changed files with 529 additions and 193 deletions.
4 changes: 2 additions & 2 deletions Sources/TMDb/Domain/Models/Media.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public enum Media: Identifiable, Codable, Equatable, Hashable, Sendable {
///
/// Person.
///
case person(Person)
case person(PersonListItem)

}

Expand Down Expand Up @@ -93,7 +93,7 @@ extension Media {
self = try .tvSeries(TVSeriesListItem(from: decoder))

case .person:
self = try .person(Person(from: decoder))
self = try .person(PersonListItem(from: decoder))
}
}

Expand Down
38 changes: 23 additions & 15 deletions Sources/TMDb/Domain/Services/TVSeries/TVSeriesService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,11 @@ public protocol TVSeriesService: Sendable {
///
/// - Returns: Reviews for the matching TV series as a pageable list.
///
func reviews(forTVSeries tvSeriesID: TVSeries.ID, page: Int?, language: String?) async throws
-> ReviewPageableList
func reviews(
forTVSeries tvSeriesID: TVSeries.ID,
page: Int?,
language: String?
) async throws -> ReviewPageableList

///
/// Returns the images that belong to a TV series.
Expand All @@ -109,8 +112,10 @@ public protocol TVSeriesService: Sendable {
///
/// - Returns: A collection of images for the matching TV series.
///
func images(forTVSeries tvSeriesID: TVSeries.ID, filter: TVSeriesImageFilter?) async throws
-> ImageCollection
func images(
forTVSeries tvSeriesID: TVSeries.ID,
filter: TVSeriesImageFilter?
) async throws -> ImageCollection

///
/// Returns the videos that belong to a TV series.
Expand All @@ -125,8 +130,10 @@ public protocol TVSeriesService: Sendable {
///
/// - Returns: A collection of videos for the matching TV series.
///
func videos(forTVSeries tvSeriesID: TVSeries.ID, filter: TVSeriesVideoFilter?) async throws
-> VideoCollection
func videos(
forTVSeries tvSeriesID: TVSeries.ID,
filter: TVSeriesVideoFilter?
) async throws -> VideoCollection

///
/// Returns a list of recommended TV series for a TV series.
Expand Down Expand Up @@ -209,8 +216,7 @@ public protocol TVSeriesService: Sendable {
func watchProviders(
forTVSeries tvSeriesID: TVSeries.ID,
country: String
) async throws
-> ShowWatchProvider?
) async throws -> ShowWatchProvider?

///
/// Returns a collection of media databases and social links for a TV series.
Expand All @@ -225,8 +231,7 @@ public protocol TVSeriesService: Sendable {
///
func externalLinks(
forTVSeries tvSeriesID: TVSeries.ID
) async throws
-> TVSeriesExternalLinksCollection
) async throws -> TVSeriesExternalLinksCollection

///
/// Returns the content rating of a TV series.
Expand All @@ -241,8 +246,10 @@ public protocol TVSeriesService: Sendable {
///
/// - Returns: A content rating for the specificed TV series.
///
func contentRatings(forTVSeries tvSeriesID: TVSeries.ID, country: String) async throws
-> ContentRating?
func contentRatings(
forTVSeries tvSeriesID: TVSeries.ID,
country: String
) async throws -> ContentRating?
}

extension TVSeriesService {
Expand All @@ -260,9 +267,10 @@ extension TVSeriesService {
///
/// - Returns: The matching TV series.
///
public func details(forTVSeries id: TVSeries.ID, language: String? = nil) async throws
-> TVSeries
{
public func details(
forTVSeries id: TVSeries.ID,
language: String? = nil
) async throws -> TVSeries {
try await details(forTVSeries: id, language: language)
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/TMDbTests/Domain/Models/MediaPageableListTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct MediaPageableListTests {
results: [
.movie(.theFirstOmen),
.tvSeries(.bigBrother),
.person(Person(id: 51329, name: "Bradley Cooper", gender: .unknown))
.person(.bradPitt)
],
totalResults: 3,
totalPages: 1
Expand Down
60 changes: 51 additions & 9 deletions Tests/TMDbTests/Domain/Models/MediaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,72 @@ struct MediaTests {

@Test("id when movie returns movieID")
func idWhenMovieReturnsMovieID() {
#expect(media[0].id == 437_342)
let media = Media.movie(.theFirstOmen)

#expect(media.id == 437_342)
}

@Test("id when TV series returns tvSeriesID")
func idWhenTVSeriesReturnsTVSeriesID() {
#expect(media[1].id == 11366)
let media = Media.tvSeries(.bigBrother)

#expect(media.id == 11366)
}

@Test("id when person returns personID")
func idWhenPersonReturnsPersonID() {
#expect(media[2].id == 51329)
let media = Media.person(.bradPitt)

#expect(media.id == 287)
}

@Test("JSON decoding of Media", .tags(.decoding))
func testDecodeReturnsMedia() throws {
func decodeReturnsMedia() throws {
let media: [Media] = [
.movie(.theFirstOmen),
.tvSeries(.bigBrother),
.person(.bradPitt)
]

let result = try JSONDecoder.theMovieDatabase.decode([Media].self, fromResource: "media")

#expect(result == media)
}

private let media: [Media] = [
.movie(.theFirstOmen),
.tvSeries(.bigBrother),
.person(Person(id: 51329, name: "Bradley Cooper", gender: .unknown))
]
@Test("JSON encoding of movie media", .tags(.encoding))
func encodeMovieMedia() throws {
let movie = MovieListItem.theFirstOmen
let media = Media.movie(movie)

let data = try JSONEncoder.theMovieDatabase.encode(media)
let decodedMedia = try JSONDecoder.theMovieDatabase.decode(MovieListItem.self, from: data)

#expect(decodedMedia == movie)
}

@Test("JSON encoding of TV series media", .tags(.encoding))
func encodeTVSeriesMedia() throws {
let tvSeries = TVSeriesListItem.bigBrother
let media = Media.tvSeries(tvSeries)

let data = try JSONEncoder.theMovieDatabase.encode(media)
let decodedMedia = try JSONDecoder.theMovieDatabase.decode(
TVSeriesListItem.self,
from: data
)

#expect(decodedMedia == tvSeries)
}

@Test("JSON encoding of person media", .tags(.encoding))
func encodePersonMedia() throws {
let person = PersonListItem.bradPitt
let media = Media.person(person)

let data = try JSONEncoder.theMovieDatabase.encode(media)
let decodedMedia = try JSONDecoder.theMovieDatabase.decode(PersonListItem.self, from: data)

#expect(decodedMedia == person)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ struct TMDbAccountFavouriteServiceTests {

extension TMDbAccountFavouriteServiceTests {

@Test("favouriteMovies returns movies list")
func favouriteMoviesReturnsMoviesList() async throws {
@Test("favouriteMovies with default parameter values returns movies list")
func favouriteMoviesWithDefaultParameterValuesReturnsMoviesList() async throws {
let accountID = 123
let session = Session.mock()
let expectedResult = MoviePageableList.mock()
Expand All @@ -52,7 +52,10 @@ extension TMDbAccountFavouriteServiceTests {
sessionID: session.sessionID
)

let result = try await service.favouriteMovies(accountID: accountID, session: session)
let result = try await (service as AccountService).favouriteMovies(
accountID: accountID,
session: session
)

#expect(result == expectedResult)
#expect(apiClient.lastRequest as? FavouriteMoviesRequest == expectedRequest)
Expand Down Expand Up @@ -212,8 +215,8 @@ extension TMDbAccountFavouriteServiceTests {

extension TMDbAccountFavouriteServiceTests {

@Test("favouriteTVSeries returns TV series list")
func favouriteTVSeriesReturnsTVSeriesList() async throws {
@Test("favouriteTVSeries with default parameter values returns TV series list")
func favouriteTVSeriesWithDefaultParameterValuesReturnsTVSeriesList() async throws {
let accountID = 123
let session = Session.mock()
let expectedResult = TVSeriesPageableList.mock()
Expand All @@ -225,7 +228,10 @@ extension TMDbAccountFavouriteServiceTests {
sessionID: session.sessionID
)

let result = try await service.favouriteTVSeries(accountID: accountID, session: session)
let result = try await (service as AccountService).favouriteTVSeries(
accountID: accountID,
session: session
)

#expect(result == expectedResult)
#expect(apiClient.lastRequest as? FavouriteTVSeriesRequest == expectedRequest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ struct TMDbAccountServiceWatchlistTests {

extension TMDbAccountServiceWatchlistTests {

@Test("movieWatchlist returns movies list")
func movieWatchlistReturnsMoviesList() async throws {
@Test("movieWatchlist with default parameter values returns movies list")
func movieWatchlistWithDefaultParameterValuesReturnsMoviesList() async throws {
let accountID = 123
let session = Session.mock()
let expectedResult = MoviePageableList.mock()
Expand All @@ -52,7 +52,10 @@ extension TMDbAccountServiceWatchlistTests {
sessionID: session.sessionID
)

let result = try await service.movieWatchlist(accountID: accountID, session: session)
let result = try await (service as AccountService).movieWatchlist(
accountID: accountID,
session: session
)

#expect(result == expectedResult)
#expect(apiClient.lastRequest as? MovieWatchlistRequest == expectedRequest)
Expand Down Expand Up @@ -207,8 +210,8 @@ extension TMDbAccountServiceWatchlistTests {

extension TMDbAccountServiceWatchlistTests {

@Test("TV series watchlist returns TV series list")
func tvSeriesWatchlistReturnsTVSeriesList() async throws {
@Test("TV series watchlist with default parameter values returns TV series list")
func tvSeriesWatchlistWithDefaultParameterValuesReturnsTVSeriesList() async throws {
let accountID = 123
let session = Session.mock()
let expectedResult = TVSeriesPageableList.mock()
Expand All @@ -220,7 +223,10 @@ extension TMDbAccountServiceWatchlistTests {
sessionID: session.sessionID
)

let result = try await service.tvSeriesWatchlist(accountID: accountID, session: session)
let result = try await (service as AccountService).tvSeriesWatchlist(
accountID: accountID,
session: session
)

#expect(result == expectedResult)
#expect(apiClient.lastRequest as? TVSeriesWatchlistRequest == expectedRequest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ struct TMDbAuthenticationServiceTests {
}
}

@Test("authenticateURL returns URL")
func authenticateURLReturnsURL() throws {
@Test("authenticateURL with default parameter values returns URL")
func authenticateURLWithDefaultParameterValuesReturnsURL() throws {
let expiresAt = Date(timeIntervalSince1970: 1_705_956_596)
let token = Token(success: true, requestToken: "abc123", expiresAt: expiresAt)
let expectedURL = try #require(URL(string: "https://some.domain.com/authenticate/abc123"))
authenticateURLBuilder.authenticateURLResult = expectedURL

let url = service.authenticateURL(for: token)
let url = (service as AuthenticationService).authenticateURL(for: token)

#expect(url == expectedURL)
#expect(authenticateURLBuilder.lastRequestToken == token.requestToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ struct TMDbCertificationServiceTests {
#expect(apiClient.lastRequest as? TVSeriesCertificationsRequest == expectedRequest)
}

@Test("tvSeriesCertifications when errors throw error")
func tvSeriesCertificationsWhenErrorsThrowsError() async throws {
apiClient.addResponse(.failure(.unknown))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ struct TMDbConfigurationServiceTests {
}
}

@Test("countries returns countries")
func countriesReturnsCountries() async throws {
@Test("countries with default parameter values returns countries")
func countriesWithDefaultParameterValuesReturnsCountries() async throws {
let expectedResult = [Country].mocks
apiClient.addResponse(.success(expectedResult))
let expectedRequest = CountriesConfigurationRequest(language: nil)

let result = try await service.countries()
let result = try await (service as ConfigurationService).countries()

#expect(result == expectedResult)
#expect(apiClient.lastRequest as? CountriesConfigurationRequest == expectedRequest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ struct TMDbDiscoverServiceTests {
self.service = TMDbDiscoverService(apiClient: apiClient)
}

@Test("movies returns movies")
func moviesReturnsMovies() async throws {
@Test("movies with default parameter values returns movies")
func moviesWithDefaultParameterValuesReturnsMovies() async throws {
let expectedResult = MoviePageableList.mock()
apiClient.addResponse(.success(expectedResult))
let expectedRequest = DiscoverMoviesRequest(
people: nil, sortedBy: nil, page: nil, language: nil)

let result = try await service.movies()
let result = try await (service as DiscoverService).movies()

#expect(result == expectedResult)
#expect(apiClient.lastRequest as? DiscoverMoviesRequest == expectedRequest)
Expand Down Expand Up @@ -74,13 +74,13 @@ struct TMDbDiscoverServiceTests {
}
}

@Test("tvSeries returns TV series")
func tvSeriesReturnsTVSeries() async throws {
@Test("tvSeries with default parameter values returns TV series")
func tvSeriesWithDefaultParameterValuesReturnsTVSeries() async throws {
let expectedResult = TVSeriesPageableList.mock()
apiClient.addResponse(.success(expectedResult))
let expectedRequest = DiscoverTVSeriesRequest(sortedBy: nil, page: nil, language: nil)

let result = try await service.tvSeries()
let result = try await (service as DiscoverService).tvSeries()

#expect(result == expectedResult)
#expect(apiClient.lastRequest as? DiscoverTVSeriesRequest == expectedRequest)
Expand Down
Loading

0 comments on commit 6e62acf

Please sign in to comment.