8000 FEATURE: Favourite Movies and TV Series by adamayoung · Pull Request #159 · adamayoung/TMDb · GitHub
[go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Favourite Movies and TV Series #159

Merged
merged 11 commits into from
Mar 12, 2024
Prev Previous commit
Next Next commit
Add documentation
  • Loading branch information
adamayoung committed Mar 11, 2024
commit b362905d6a9b81f2cd8fa2ef3ea953f937c3153e
2 changes: 1 addition & 1 deletion Sources/TMDb/Services/Account/AccountEndpoint.swift
8000
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Foundation
enum AccountEndpoint {

case details(sessionID: String)
case favouriteMovies(sortedBy: FavouriteMovieSort? = nil, page: Int? = nil, accountID: Int, sessionID: String)
case favouriteMovies(sortedBy: FavouriteSort? = nil, page: Int? = nil, accountID: Int, sessionID: String)
case addFavourite(accountID: Int, sessionID: String)

}
Expand Down
16 changes: 15 additions & 1 deletion Sources/TMDb/Services/Account/AccountService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,22 @@ public final class AccountService {
return accountDetails
}


///
/// Returns a list of the user's favourited movies.
///
/// - Parameters:
/// - sortedBy: How results should be sorted.
/// - page: The page of results to return.
/// - accountID: The user's account identifier.
/// - session: The user's TMDb session.
///
/// - Throws: TMDb error ``TMDbError``.
///
/// - Returns: A list of the user's favourited movies.
///
public func favouriteMovies(
sortedBy: FavouriteMovieSort? = nil,
sortedBy: FavouriteSort? = nil,
page: Int? = nil,
accountID: Int,
session: Session
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// MovieSort.swift
// FavouriteSort.swift
// TMDb
//
// Copyright © 2024 Adam Young.
Expand All @@ -22,7 +22,7 @@ import Foundation
///
/// A sort specifier when fetching movies.
///
public enum FavouriteMovieSort: CustomStringConvertible {
public enum FavouriteSort: CustomStringConvertible {

///
/// By created at.
Expand All @@ -35,7 +35,7 @@ public enum FavouriteMovieSort: CustomStringConvertible {

}

extension FavouriteMovieSort {
extension FavouriteSort {

private var fieldName: String {
switch self {
Expand All @@ -59,7 +59,7 @@ extension URL {
static let sortBy = "sort_by"
}

func appendingSortBy(_ sortBy: FavouriteMovieSort?) -> Self {
func appendingSortBy(_ sortBy: FavouriteSort?) -> Self {
guard let sortBy else {
return self
}
Expand Down
1 change: 1 addition & 0 deletions Sources/TMDb/TMDb.docc/TMDb.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ For the TMDb API documentation, see
- ``AccountAvatar``
- ``AccountAvatar/Gravatar``
- ``AccountAvatar/TMDb``
- ``FavouriteSort``

### Authentication

Expand Down
24 changes: 24 additions & 0 deletions Tests/TMDbIntegrationTests/AccountIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,28 @@ final class AccountIntegrationTests: XCTestCase {
XCTAssertGreaterThan(details.id, 0)
}

func testAddingAndRemovingFavouriteMovies() async throws {
let accountDetails = try await accountService.details(session: session)
let movieID = 550

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

let movieListAfterFavorited = try await accountService.favouriteMovies(
accountID: accountDetails.id,
session: session
)
let isMovieFavourited = movieListAfterFavorited.results.contains { $0.id == movieID }
XCTAssertTrue(isMovieFavourited)

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

let movieListAfterFavoriteRemoved = try await accountService.favouriteMovies(
accountID: accountDetails.id,
session: session
)

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

}
8 changes: 4 additions & 4 deletions Tests/TMDbTests/Services/Account/AccountEndpointTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ final class AccountEndpointTests: XCTestCase {
func testFavouriteMoviesWhenSortedByIncludedEndpointReturnsURL() throws {
let accountDetails = AccountDetails.mock()
let session = Session.mock()
let sortedBy = FavouriteMovieSort.createdAt()
let sortedBy = FavouriteSort.createdAt()
let expectedURL = try XCTUnwrap(
URL(string: "/account/\(accountDetails.id)/favorite/movies"
+ "?sort_by=\(sortedBy.description)&session_id=\(session.sessionID)")
Expand Down Expand Up @@ -77,17 +77,17 @@ final class AccountEndpointTests: XCTestCase {

XCTAssertEqual(url, expectedURL)
}

func testFavouriteMoviesWhenSortedByAndPageIncludedEndpointReturnsURL() throws {
let accountDetails = AccountDetails.mock()
let session = Session.mock()
let sortedBy = FavouriteMovieSort.createdAt()
let sortedBy = FavouriteSort.createdAt()
let page = 2
let expectedURL = try XCTUnwrap(
URL(string: "/account/\(accountDetails.id)/favorite/movies"
+ "?sort_by=\(sortedBy.description)&page=\(page)&session_id=\(session.sessionID)")
)

let url = AccountEndpoint.favouriteMovies(
sortedBy: sortedBy,
page: page,
Expand Down
4 changes: 2 additions & 2 deletions Tests/TMDbTests/Services/Account/AccountServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ final class AccountServiceTests: XCTestCase {
func testFavouriteMoviesWhenFetchingWithSortedByReturnsMoviesList() async throws {
let accountID = 123
let session = Session.mock()
let sortedBy = FavouriteMovieSort.createdAt()
let sortedBy = FavouriteSort.createdAt()
let expectedResult = MoviePageableList.mock()
apiClient.addResponse(.success(expectedResult))

Expand All @@ -102,7 +102,7 @@ final class AccountServiceTests: XCTestCase {
func testFavouriteMoviesWhenFetchingWithSortedByAndWithPageReturnsMoviesList() async throws {
let accountID = 123
let session = Session.mock()
let sortedBy = FavouriteMovieSort.createdAt()
let sortedBy = FavouriteSort.createdAt()
let page = 2
let expectedResult = MoviePageableList.mock()
apiClient.addResponse(.success(expectedResult))
Expand Down
43 changes: 0 additions & 43 deletions Tests/TMDbTests/Services/Account/FavouriteMovieSortTests.swift

This file was deleted.

55 changes: 55 additions & 0 deletions Tests/TMDbTests/Services/Account/FavouriteSortTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// FavouriteSortTests.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.
//

@testable import TMDb
import XCTest

final class FavouriteSortTests: XCTestCase {

func testCreatedAtWhenDefaultReturnsDescription() {
XCTAssertEqual(FavouriteSort.createdAt().description, "created_at.asc")
}

func testCreatedAtWhenAscendingReturnsDescription() {
XCTAssertEqual(FavouriteSort.createdAt(descending: false).description, "created_at.asc")
}

func testCreatedAtWhenDescendingReturnsDescription() {
XCTAssertEqual(FavouriteSort.createdAt(descending: true).description, "created_at.desc")
}

func testURLAppendingSortByWhenNilReturnsOriginalURL() throws {
let sort: FavouriteSort? = nil
let expectedURL = try XCTUnwrap(URL(string: "/some/path"))

let url = URL(string: "/some/path")?.appendingSortBy(sort)

XCTAssertEqual(url, expectedURL)
}

func testURLAppendingSortByReturnsURL() throws {
let sort: FavouriteSort? = .createdAt()
let expectedURL = try XCTUnwrap(URL(string: "/some/path?sort_by=created_at.asc"))

let url = URL(string: "/some/path")?.appendingSortBy(sort)

XCTAssertEqual(url, expectedURL)
}

}
0