Skip to content

Commit

Permalink
TECH DEBT: Add tests for error scenarios in services (#177)
Browse files Browse the repository at this point in the history
* AuthenticationService and CertificationsService error tests

* CompanyService error tests

* ConfigurationService error tests

* DiscoverService, GenreService and MovieService error tests

* PersonService error tests

* SearchService error tests

* TrendingService error tests

* TVEpisodesService error tests

* TVSeasonService error tests

* TVSeriesService and WatchProviderService error tests

* Break up MovieServiceTests and TVSeriesTests

* Tidy up

* Fix Linux builds

* Fix Linux builds

* Fix Linux builds
  • Loading branch information
adamayoung authored May 15, 2024
1 parent a143068 commit 27b206d
Show file tree
Hide file tree
Showing 55 changed files with 2,073 additions and 1,024 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// ConfigurationProviding.swift
// TMDb
//
// Copyright © 2024 Adam Young.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an AS IS BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Foundation

///
/// An interface to provide configuration for service.
///
/// Create an API key at [https://www.themoviedb.org/documentation/api](https://www.themoviedb.org/documentation/api).
///
public protocol ConfigurationProviding {

///
/// TMDb API key.
///
var apiKey: String { get }

///
/// The HTTP client adapter for making HTTP requests.
///
var httpClient: any HTTPClient { get }

}
2 changes: 1 addition & 1 deletion Sources/TMDb/Domain/Services/Account/AccountService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public final class AccountService {
///
/// - Parameter configuration: A TMDb configuration object.
///
public convenience init(configuration: TMDbConfiguration) {
public convenience init(configuration: some ConfigurationProviding) {
self.init(
apiClient: TMDbFactory.apiClient(configuration: configuration)
)
Expand Down
16 changes: 0 additions & 16 deletions Sources/TMDb/Domain/Services/Account/FavouriteSort.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,3 @@ extension FavouriteSort {
}

}

extension URL {

private enum QueryItemName {
static let sortBy = "sort_by"
}

func appendingSortBy(_ sortBy: FavouriteSort?) -> Self {
guard let sortBy else {
return self
}

return appendingQueryItem(name: QueryItemName.sortBy, value: sortBy)
}

}
16 changes: 0 additions & 16 deletions Sources/TMDb/Domain/Services/Account/WatchlistSort.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,3 @@ extension WatchlistSort {
}

}

extension URL {

private enum QueryItemName {
static let sortBy = "sort_by"
}

func appendingSortBy(_ sortBy: WatchlistSort?) -> Self {
guard let sortBy else {
return self
}

return appendingQueryItem(name: QueryItemName.sortBy, value: sortBy)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public final class AuthenticationService {
///
/// - Parameter configuration: A TMDb configuration object.
///
public convenience init(configuration: TMDbConfiguration) {
public convenience init(configuration: some ConfigurationProviding) {
self.init(
apiClient: TMDbFactory.authAPIClient(configuration: configuration),
authenticateURLBuilder: TMDbFactory.authenticateURLBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,25 @@ final class AuthenticateURLBuilder: AuthenticateURLBuilding {
}

func authenticateURL(with requestToken: String, redirectURL: URL?) -> URL {
var url = baseURL
let url = baseURL
.appendingPathComponent("authenticate")
.appendingPathComponent(requestToken)

guard var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
return url
}

var queryItems = urlComponents.queryItems ?? []
if let redirectURL {
url = url.appendingQueryItem(name: "redirect_to", value: redirectURL.absoluteString)
let queryItem = URLQueryItem(name: "redirect_to", value: redirectURL.absoluteString)
queryItems.append(queryItem)
}

if !queryItems.isEmpty {
urlComponents.queryItems = queryItems
}

return url
return urlComponents.url ?? url
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public final class CertificationService {
///
/// - Parameter configuration: A TMDb configuration object.
///
public convenience init(configuration: TMDbConfiguration) {
public convenience init(configuration: some ConfigurationProviding) {
self.init(
apiClient: TMDbFactory.apiClient(configuration: configuration)
)
Expand Down
2 changes: 1 addition & 1 deletion Sources/TMDb/Domain/Services/Company/CompanyService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public final class CompanyService {
///
/// - Parameter configuration: A TMDb configuration object.
///
public convenience init(configuration: TMDbConfiguration) {
public convenience init(configuration: some ConfigurationProviding) {
self.init(
apiClient: TMDbFactory.apiClient(configuration: configuration)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public final class ConfigurationService {
///
/// - Parameter configuration: A TMDb configuration object.
///
public convenience init(configuration: TMDbConfiguration) {
public convenience init(configuration: some ConfigurationProviding) {
self.init(
apiClient: TMDbFactory.apiClient(configuration: configuration)
)
Expand Down
44 changes: 44 additions & 0 deletions Sources/TMDb/Domain/Services/Discover/DiscoverMovieFilter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// DiscoverMovieFilter.swift
// TMDb
//
// Copyright © 2024 Adam Young.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an AS IS BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Foundation

///
/// A filter for discovering movies.
///
public struct DiscoverMovieFilter {

///
/// A list of Person identifiers which to return only movies they have
/// appeared in.
///
public let people: [Person.ID]?

///
/// Creates a discover movies filter.
///
/// - Parameters:
/// - people: A list of Person identifiers which to return only
/// movies they have appeared in.
///
public init(people: [Person.ID]? = nil) {
self.people = people
}

}
8 changes: 4 additions & 4 deletions Sources/TMDb/Domain/Services/Discover/DiscoverService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public final class DiscoverService {
///
/// - Parameter configuration: A TMDb configuration object.
///
public convenience init(configuration: TMDbConfiguration) {
public convenience init(configuration: some ConfigurationProviding) {
self.init(
apiClient: TMDbFactory.apiClient(configuration: configuration)
)
Expand All @@ -50,8 +50,8 @@ public final class DiscoverService {
/// - Precondition: `page` can be between `1` and `1000`.
///
/// - Parameters:
/// - filter: Movie filter.
/// - sortedBy: How results should be sorted.
/// - people: A list of Person identifiers which to return only movies they have appeared in.
/// - page: The page of results to return.
/// - language: ISO 639-1 language code to display results in. Defaults to `en`.
///
Expand All @@ -60,14 +60,14 @@ public final class DiscoverService {
/// - Returns: Matching movies as a pageable list.
///
public func movies(
filter: DiscoverMovieFilter? = nil,
sortedBy: MovieSort? = nil,
withPeople people: [Person.ID]? = nil,
page: Int? = nil,
language: String? = nil
) async throws -> MoviePageableList {
let request = DiscoverMoviesRequest(
people: filter?.people,
sortedBy: sortedBy,
people: people,
page: page,
language: language
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ import Foundation
final class DiscoverMoviesRequest: DecodableAPIRequest<MoviePageableList> {

init(
sortedBy: MovieSort? = nil,
people: [Person.ID]? = nil,
sortedBy: MovieSort? = nil,
page: Int? = nil,
language: String? = nil
) {
let path = "/discover/movie"
let queryItems = APIRequestQueryItems(
sortedBy: sortedBy,
people: people,
sortedBy: sortedBy,
page: page,
language: language
)
Expand All @@ -43,21 +43,21 @@ final class DiscoverMoviesRequest: DecodableAPIRequest<MoviePageableList> {
private extension APIRequestQueryItems {

init(
sortedBy: MovieSort?,
people: [Person.ID]?,
sortedBy: MovieSort?,
page: Int?,
language: String?
) {
self.init()

if let sortedBy {
self[.sortBy] = sortedBy
}

if let people {
self[.withPeople] = Self.peopleQueryItemValue(for: people)
}

if let sortedBy {
self[.sortBy] = sortedBy
}

if let page {
self[.page] = page
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/TMDb/Domain/Services/Genres/GenreService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public final class GenreService {
///
/// - Parameter configuration: A TMDb configuration object.
///
public convenience init(configuration: TMDbConfiguration) {
public convenience init(configuration: some ConfigurationProviding) {
self.init(
apiClient: TMDbFactory.apiClient(configuration: configuration)
)
Expand Down
2 changes: 1 addition & 1 deletion Sources/TMDb/Domain/Services/Movies/MovieService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public final class MovieService {
///
/// - Parameter configuration: A TMDb configuration object.
///
public convenience init(configuration: TMDbConfiguration) {
public convenience init(configuration: some ConfigurationProviding) {
self.init(
apiClient: TMDbFactory.apiClient(configuration: configuration)
)
Expand Down
2 changes: 1 addition & 1 deletion Sources/TMDb/Domain/Services/People/PersonService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public final class PersonService {
///
/// - Parameter configuration: A TMDb configuration object.
///
public convenience init(configuration: TMDbConfiguration) {
public convenience init(configuration: some ConfigurationProviding) {
self.init(
apiClient: TMDbFactory.apiClient(configuration: configuration)
)
Expand Down
2 changes: 1 addition & 1 deletion Sources/TMDb/Domain/Services/Search/SearchService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public final class SearchService {
///
/// - Parameter configuration: A TMDb configuration object.
///
public convenience init(configuration: TMDbConfiguration) {
public convenience init(configuration: some ConfigurationProviding) {
self.init(
apiClient: TMDbFactory.apiClient(configuration: configuration)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public final class TVEpisodeService {
///
/// - Parameter configuration: A TMDb configuration object.
///
public convenience init(configuration: TMDbConfiguration) {
public convenience init(configuration: some ConfigurationProviding) {
self.init(
apiClient: TMDbFactory.apiClient(configuration: configuration)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public final class TVSeasonService {
///
/// - Parameter configuration: A TMDb configuration object.
///
public convenience init(configuration: TMDbConfiguration) {
public convenience init(configuration: some ConfigurationProviding) {
self.init(
apiClient: TMDbFactory.apiClient(configuration: configuration)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public final class TVSeriesService {
///
/// - Parameter configuration: A TMDb configuration object.
///
public convenience init(configuration: TMDbConfiguration) {
public convenience init(configuration: some ConfigurationProviding) {
self.init(
apiClient: TMDbFactory.apiClient(configuration: configuration)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public final class TrendingService {
///
/// - Parameter configuration: A TMDb configuration object.
///
public convenience init(configuration: TMDbConfiguration) {
public convenience init(configuration: some ConfigurationProviding) {
self.init(
apiClient: TMDbFactory.apiClient(configuration: configuration)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public final class WatchProviderService {
///
/// - Parameter configuration: A TMDb configuration object.
///
public convenience init(configuration: TMDbConfiguration) {
public convenience init(configuration: some ConfigurationProviding) {
self.init(
apiClient: TMDbFactory.apiClient(configuration: configuration)
)
Expand Down
Loading

0 comments on commit 27b206d

Please sign in to comment.