From 3a568b376e9179f2694b3fbbe2b39442d4b67244 Mon Sep 17 00:00:00 2001 From: chickdan Date: Mon, 24 Jun 2024 16:33:33 -0500 Subject: [PATCH 01/13] Add models and reqeust --- .../Domain/Models/ContentRatingResult.swift | 18 +++++++++++ .../Requests/ContentRatingRequest.swift | 30 +++++++++++++++++++ .../TVSeries/TMDbTVSeriesService.swift | 18 +++++++++++ .../Services/TVSeries/TVSeriesService.swift | 17 +++++++++++ 4 files changed, 83 insertions(+) create mode 100644 Sources/TMDb/Domain/Models/ContentRatingResult.swift create mode 100644 Sources/TMDb/Domain/Services/TVSeries/Requests/ContentRatingRequest.swift diff --git a/Sources/TMDb/Domain/Models/ContentRatingResult.swift b/Sources/TMDb/Domain/Models/ContentRatingResult.swift new file mode 100644 index 00000000..a7361fb4 --- /dev/null +++ b/Sources/TMDb/Domain/Models/ContentRatingResult.swift @@ -0,0 +1,18 @@ +// +// File.swift +// +// +// Created by Daniel Chick on 6/24/24. +// + +import Foundation + +struct ContentRatingResult: Codable, Equatable, Hashable, Sendable { + let results: [ContentRating] +} + +public struct ContentRating: Codable, Equatable, Hashable, Sendable { + public let descriptors: [String] + public let country: String + public let rating: String +} diff --git a/Sources/TMDb/Domain/Services/TVSeries/Requests/ContentRatingRequest.swift b/Sources/TMDb/Domain/Services/TVSeries/Requests/ContentRatingRequest.swift new file mode 100644 index 00000000..6c36bd52 --- /dev/null +++ b/Sources/TMDb/Domain/Services/TVSeries/Requests/ContentRatingRequest.swift @@ -0,0 +1,30 @@ +// +// TVSeriesWatchProvidersRequest.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 + +final class ContentRatingRequest: DecodableAPIRequest { + + init(id: TVSeries.ID) { + let path = "/tv/\(id)/content_ratings" + + super.init(path: path) + } + +} diff --git a/Sources/TMDb/Domain/Services/TVSeries/TMDbTVSeriesService.swift b/Sources/TMDb/Domain/Services/TVSeries/TMDbTVSeriesService.swift index c6fe1ecf..d5304c4c 100644 --- a/Sources/TMDb/Domain/Services/TVSeries/TMDbTVSeriesService.swift +++ b/Sources/TMDb/Domain/Services/TVSeries/TMDbTVSeriesService.swift @@ -181,6 +181,24 @@ final class TMDbTVSeriesService: TVSeriesService { return result.results[country] } + + func contentRatings( + forTVSeries tvSeriesID: TVSeries.ID, + country: String = "US" + ) async throws -> ContentRating? { + let request = ContentRatingRequest(id: tvSeriesID) + + let result: ContentRatingResult + do { + result = try await apiClient.perform(request) + } catch let error { + throw TMDbError(error: error) + } + + return result.results.first { rating in + rating.country == country + } + } func externalLinks(forTVSeries tvSeriesID: TVSeries.ID) async throws -> TVSeriesExternalLinksCollection { let request = TVSeriesExternalLinksRequest(id: tvSeriesID) diff --git a/Sources/TMDb/Domain/Services/TVSeries/TVSeriesService.swift b/Sources/TMDb/Domain/Services/TVSeries/TVSeriesService.swift index 67e65852..a23049be 100644 --- a/Sources/TMDb/Domain/Services/TVSeries/TVSeriesService.swift +++ b/Sources/TMDb/Domain/Services/TVSeries/TVSeriesService.swift @@ -217,6 +217,17 @@ public protocol TVSeriesService: Sendable { /// func externalLinks(forTVSeries tvSeriesID: TVSeries.ID) async throws -> TVSeriesExternalLinksCollection + /// + /// Returns a collection of media databases and social links for a TV series. + /// + /// [TMDb API - TVSeries: External IDs](https://developer.themoviedb.org/reference/tv-series-external-ids) + /// + /// - Parameters: + /// - tvSeriesID: The identifier of the TV series. + /// + /// - Returns: A collection of external links for the specificed TV series. + /// + func contentRatings(forTVSeries tvSeriesID: TVSeries.ID, country: String) async throws -> ContentRating? } public extension TVSeriesService { @@ -285,4 +296,10 @@ public extension TVSeriesService { try await watchProviders(forTVSeries: tvSeriesID, country: country) } + func contentRatings( + forTVSeries tvSeriesID: TVSeries.ID, + country: String = "US" + ) async throws -> ContentRating? { + try await contentRatings(forTVSeries: tvSeriesID, country: country) + } } From f344b05443288d323367bead15898966de8df2a0 Mon Sep 17 00:00:00 2001 From: chickdan Date: Mon, 24 Jun 2024 17:28:15 -0500 Subject: [PATCH 02/13] Add coding key --- Sources/TMDb/Domain/Models/ContentRatingResult.swift | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sources/TMDb/Domain/Models/ContentRatingResult.swift b/Sources/TMDb/Domain/Models/ContentRatingResult.swift index a7361fb4..0d08f1c4 100644 --- a/Sources/TMDb/Domain/Models/ContentRatingResult.swift +++ b/Sources/TMDb/Domain/Models/ContentRatingResult.swift @@ -15,4 +15,10 @@ public struct ContentRating: Codable, Equatable, Hashable, Sendable { public let descriptors: [String] public let country: String public let rating: String + + enum CodingKeys: String, CodingKey { + case rating + case descriptors + case country = "iso31661" + } } From 4eee213d2837c1b6fbd22ac284877bcef576b2ee Mon Sep 17 00:00:00 2001 From: chickdan Date: Tue, 25 Jun 2024 15:05:39 -0500 Subject: [PATCH 03/13] Finalize --- .../TMDb/Domain/Models/ContentRating.swift | 49 +++++++++++++++++++ .../Domain/Models/ContentRatingResult.swift | 31 ++++++------ Sources/TMDb/Domain/Models/CrewMember.swift | 2 +- .../TVSeries/TMDbTVSeriesService.swift | 2 +- .../Services/TVSeries/TVSeriesService.swift | 6 +-- 5 files changed, 70 insertions(+), 20 deletions(-) create mode 100644 Sources/TMDb/Domain/Models/ContentRating.swift diff --git a/Sources/TMDb/Domain/Models/ContentRating.swift b/Sources/TMDb/Domain/Models/ContentRating.swift new file mode 100644 index 00000000..78f85f89 --- /dev/null +++ b/Sources/TMDb/Domain/Models/ContentRating.swift @@ -0,0 +1,49 @@ +// +// Company.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 model representing the content rating. +/// +public struct ContentRating: Codable, Equatable, Hashable, Sendable { + + /// + /// ? + /// + public let descriptors: [String] + + /// + /// The ISO 3166-1 country code. + /// + public let countryCode: String + + /// + /// The content rating of the tv show + /// + public let rating: String +} + +extension ContentRating { + private enum CodingKeys: String, CodingKey { + case rating + case descriptors + case countryCode = "iso31661" + } +} diff --git a/Sources/TMDb/Domain/Models/ContentRatingResult.swift b/Sources/TMDb/Domain/Models/ContentRatingResult.swift index 0d08f1c4..5cad05e3 100644 --- a/Sources/TMDb/Domain/Models/ContentRatingResult.swift +++ b/Sources/TMDb/Domain/Models/ContentRatingResult.swift @@ -1,24 +1,25 @@ // -// File.swift -// +// Company.swift +// TMDb // -// Created by Daniel Chick on 6/24/24. +// 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 + struct ContentRatingResult: Codable, Equatable, Hashable, Sendable { let results: [ContentRating] } - -public struct ContentRating: Codable, Equatable, Hashable, Sendable { - public let descriptors: [String] - public let country: String - public let rating: String - - enum CodingKeys: String, CodingKey { - case rating - case descriptors - case country = "iso31661" - } -} diff --git a/Sources/TMDb/Domain/Models/CrewMember.swift b/Sources/TMDb/Domain/Models/CrewMember.swift index 7dd1a12e..63ba3f1f 100644 --- a/Sources/TMDb/Domain/Models/CrewMember.swift +++ b/Sources/TMDb/Domain/Models/CrewMember.swift @@ -20,7 +20,7 @@ import Foundation /// -/// A model representing a crew member.. +/// A model representing a crew member. /// public struct CrewMember: Identifiable, Codable, Equatable, Hashable, Sendable { diff --git a/Sources/TMDb/Domain/Services/TVSeries/TMDbTVSeriesService.swift b/Sources/TMDb/Domain/Services/TVSeries/TMDbTVSeriesService.swift index d5304c4c..a4f7a7f4 100644 --- a/Sources/TMDb/Domain/Services/TVSeries/TMDbTVSeriesService.swift +++ b/Sources/TMDb/Domain/Services/TVSeries/TMDbTVSeriesService.swift @@ -196,7 +196,7 @@ final class TMDbTVSeriesService: TVSeriesService { } return result.results.first { rating in - rating.country == country + rating.countryCode == country } } diff --git a/Sources/TMDb/Domain/Services/TVSeries/TVSeriesService.swift b/Sources/TMDb/Domain/Services/TVSeries/TVSeriesService.swift index a23049be..567cbf00 100644 --- a/Sources/TMDb/Domain/Services/TVSeries/TVSeriesService.swift +++ b/Sources/TMDb/Domain/Services/TVSeries/TVSeriesService.swift @@ -218,14 +218,14 @@ public protocol TVSeriesService: Sendable { func externalLinks(forTVSeries tvSeriesID: TVSeries.ID) async throws -> TVSeriesExternalLinksCollection /// - /// Returns a collection of media databases and social links for a TV series. + /// Returns the content rating of a TV series. /// - /// [TMDb API - TVSeries: External IDs](https://developer.themoviedb.org/reference/tv-series-external-ids) + /// [TMDb API - TVSeries: Content ratings](https://developer.themoviedb.org/reference/tv-series-content-ratings) /// /// - Parameters: /// - tvSeriesID: The identifier of the TV series. /// - /// - Returns: A collection of external links for the specificed TV series. + /// - Returns: A content rating for the specificed TV series. /// func contentRatings(forTVSeries tvSeriesID: TVSeries.ID, country: String) async throws -> ContentRating? } From 1e0dd6743d8c6f8f10ab7188bfdab5546e8c86a8 Mon Sep 17 00:00:00 2001 From: chickdan Date: Tue, 25 Jun 2024 15:17:44 -0500 Subject: [PATCH 04/13] Add id and initializer to Content Rating --- .../TMDb/Domain/Models/ContentRating.swift | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Sources/TMDb/Domain/Models/ContentRating.swift b/Sources/TMDb/Domain/Models/ContentRating.swift index 78f85f89..545a9e53 100644 --- a/Sources/TMDb/Domain/Models/ContentRating.swift +++ b/Sources/TMDb/Domain/Models/ContentRating.swift @@ -24,6 +24,11 @@ import Foundation /// public struct ContentRating: Codable, Equatable, Hashable, Sendable { + /// + /// The tv show's identifier + /// + public let id: Int + /// /// ? /// @@ -38,10 +43,26 @@ public struct ContentRating: Codable, Equatable, Hashable, Sendable { /// The content rating of the tv show /// public let rating: String + + /// Creates a content rating object. + /// + /// - Parameters: + /// - id: the ID of the show. + /// - descriptors: Array of.... + /// - countryCode: ISO 3166-1 country code. + /// - rating: The content rating of the tv show + /// + public init(id: Int, descriptors: [String], countryCode: String, rating: String) { + self.id = id + self.descriptors = descriptors + self.countryCode = countryCode + self.rating = rating + } } extension ContentRating { private enum CodingKeys: String, CodingKey { + case id case rating case descriptors case countryCode = "iso31661" From 2a9d6694dc5b09052ce5edb7d60ee5fe96f4bc6b Mon Sep 17 00:00:00 2001 From: chickdan Date: Tue, 25 Jun 2024 17:29:34 -0500 Subject: [PATCH 05/13] Update models --- Sources/TMDb/Domain/Models/ContentRating.swift | 10 +--------- Sources/TMDb/Domain/Models/ContentRatingResult.swift | 1 + 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/Sources/TMDb/Domain/Models/ContentRating.swift b/Sources/TMDb/Domain/Models/ContentRating.swift index 545a9e53..61c5cb3c 100644 --- a/Sources/TMDb/Domain/Models/ContentRating.swift +++ b/Sources/TMDb/Domain/Models/ContentRating.swift @@ -24,11 +24,6 @@ import Foundation /// public struct ContentRating: Codable, Equatable, Hashable, Sendable { - /// - /// The tv show's identifier - /// - public let id: Int - /// /// ? /// @@ -47,13 +42,11 @@ public struct ContentRating: Codable, Equatable, Hashable, Sendable { /// Creates a content rating object. /// /// - Parameters: - /// - id: the ID of the show. /// - descriptors: Array of.... /// - countryCode: ISO 3166-1 country code. /// - rating: The content rating of the tv show /// - public init(id: Int, descriptors: [String], countryCode: String, rating: String) { - self.id = id + public init(descriptors: [String], countryCode: String, rating: String) { self.descriptors = descriptors self.countryCode = countryCode self.rating = rating @@ -62,7 +55,6 @@ public struct ContentRating: Codable, Equatable, Hashable, Sendable { extension ContentRating { private enum CodingKeys: String, CodingKey { - case id case rating case descriptors case countryCode = "iso31661" diff --git a/Sources/TMDb/Domain/Models/ContentRatingResult.swift b/Sources/TMDb/Domain/Models/ContentRatingResult.swift index 5cad05e3..dbe8aaca 100644 --- a/Sources/TMDb/Domain/Models/ContentRatingResult.swift +++ b/Sources/TMDb/Domain/Models/ContentRatingResult.swift @@ -22,4 +22,5 @@ import Foundation struct ContentRatingResult: Codable, Equatable, Hashable, Sendable { let results: [ContentRating] + let id: Int } From 621cc679ecf8dfc658f8311c2abd62adc0c82442 Mon Sep 17 00:00:00 2001 From: chickdan Date: Tue, 25 Jun 2024 17:29:56 -0500 Subject: [PATCH 06/13] Add mocks and tests --- .../Domain/Models/ContentRatingTests.swift | 38 +++++ .../Domain/Models/ContentRatingsTests.swift | 153 ++++++++++++++++++ .../Mocks/Models/ContentRating+Mocks.swift | 43 +++++ 3 files changed, 234 insertions(+) create mode 100644 Tests/TMDbTests/Domain/Models/ContentRatingTests.swift create mode 100644 Tests/TMDbTests/Domain/Models/ContentRatingsTests.swift create mode 100644 Tests/TMDbTests/Mocks/Models/ContentRating+Mocks.swift diff --git a/Tests/TMDbTests/Domain/Models/ContentRatingTests.swift b/Tests/TMDbTests/Domain/Models/ContentRatingTests.swift new file mode 100644 index 00000000..e1eb060d --- /dev/null +++ b/Tests/TMDbTests/Domain/Models/ContentRatingTests.swift @@ -0,0 +1,38 @@ +// +// CertificationTests.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 ContentRatingTests: XCTestCase { + + func testDecodeReturnsContentRating() throws { + let result = try JSONDecoder.theMovieDatabase.decode(ContentRating.self, fromResource: "content-rating") + + XCTAssertEqual(result.descriptors, contentRating.descriptors) + XCTAssertEqual(result.countryCode, contentRating.countryCode) + XCTAssertEqual(result.rating, contentRating.rating) + } + + private let contentRating = ContentRating( + descriptors: [], + countryCode: "US", + rating: "TV-14" + ) +} diff --git a/Tests/TMDbTests/Domain/Models/ContentRatingsTests.swift b/Tests/TMDbTests/Domain/Models/ContentRatingsTests.swift new file mode 100644 index 00000000..d6cf4539 --- /dev/null +++ b/Tests/TMDbTests/Domain/Models/ContentRatingsTests.swift @@ -0,0 +1,153 @@ +// +// CertificationTests.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 ContentRatingsTests: XCTestCase { + + func testDecodeReturnsContentRatingResult() throws { + let result = try JSONDecoder.theMovieDatabase.decode(ContentRatingResult.self, fromResource: "content-ratings") + + XCTAssertEqual(result, contentRatings) + } + + // swiftlint:disable line_length + private let contentRatings = ContentRatingResult( + results: [ + ContentRating( + descriptors: [], + countryCode: "US", + rating: "TV-14" + ), + ContentRating( + descriptors: [], + countryCode: "AU", + rating: "M" + ), + ContentRating( + descriptors: [], + countryCode: "RU", + rating: "12+" + ), + ContentRating( + descriptors: [], + countryCode: "GB", + rating: "15" + ), + ContentRating( + descriptors: [], + countryCode: "BR", + rating: "12" + ), + ContentRating( + descriptors: [], + countryCode: "HU", + rating: "12" + ), + ContentRating( + descriptors: [], + countryCode: "PH", + rating: "G" + ), + ContentRating( + descriptors: [], + countryCode: "MX", + rating: "A" + ), + ContentRating( + descriptors: [], + countryCode: "PT", + rating: "NR" + ), + ContentRating( + descriptors: [], + countryCode: "ES", + rating: "16" + ), + ContentRating( + descriptors: [], + countryCode: "FR", + rating: "16" + ), + ContentRating( + descriptors: [], + countryCode: "CA", + rating: "PG" + ), + ContentRating( + descriptors: [], + countryCode: "NL", + rating: "6" + ), + ContentRating( + descriptors: [], + countryCode: "DE", + rating: "12" + ), + ContentRating( + descriptors: [], + countryCode: "DE", + rating: "16" + ), + ContentRating( + descriptors: [], + countryCode: "AU", + rating: "PG" + ), + ContentRating( + descriptors: [], + countryCode: "KR", + rating: "15" + ), + ContentRating( + descriptors: [], + countryCode: "AT", + rating: "12" + ), + ContentRating( + descriptors: [], + countryCode: "CH", + rating: "12" + ), + ContentRating( + descriptors: [], + countryCode: "PL", + rating: "16" + ), + ContentRating( + descriptors: [], + countryCode: "HU", + rating: "16" + ), + ContentRating( + descriptors: [], + countryCode: "CZ", + rating: "15+" + ), + ContentRating( + descriptors: [], + countryCode: "RO", + rating: "15" + ), + ], + id: 8592 + ) + // swiftlint:enable line_length +} diff --git a/Tests/TMDbTests/Mocks/Models/ContentRating+Mocks.swift b/Tests/TMDbTests/Mocks/Models/ContentRating+Mocks.swift new file mode 100644 index 00000000..1109e773 --- /dev/null +++ b/Tests/TMDbTests/Mocks/Models/ContentRating+Mocks.swift @@ -0,0 +1,43 @@ +// +// Country+Mocks.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 +import TMDb + +extension ContentRating { + static func mock( + descriptors: [String], + countryCode: String, + rating: String + ) -> Self { + .init( + descriptors: descriptors, + countryCode: countryCode, + rating: rating + ) + } + + static var parksAndRecreationGB: Self { + .mock(descriptors: [], countryCode: "GB", rating: "15") + } + + static var parksAndRecreationUS: Self { + .mock(descriptors: [], countryCode: "GB", rating: "TV-14") + } +} From e0dec8d0b87cad134b515cf3b6c52bef877d25cd Mon Sep 17 00:00:00 2001 From: chickdan Date: Tue, 25 Jun 2024 17:30:30 -0500 Subject: [PATCH 07/13] Add json --- .../Resources/json/content-rating.json | 5 + .../Resources/json/content-ratings.json | 120 ++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 Tests/TMDbTests/Resources/json/content-rating.json create mode 100644 Tests/TMDbTests/Resources/json/content-ratings.json diff --git a/Tests/TMDbTests/Resources/json/content-rating.json b/Tests/TMDbTests/Resources/json/content-rating.json new file mode 100644 index 00000000..e7a201ea --- /dev/null +++ b/Tests/TMDbTests/Resources/json/content-rating.json @@ -0,0 +1,5 @@ +{ + "descriptors": [], + "iso_3166_1": "US", + "rating": "TV-14" +} diff --git a/Tests/TMDbTests/Resources/json/content-ratings.json b/Tests/TMDbTests/Resources/json/content-ratings.json new file mode 100644 index 00000000..48c2617c --- /dev/null +++ b/Tests/TMDbTests/Resources/json/content-ratings.json @@ -0,0 +1,120 @@ +{ + "results": [ + { + "descriptors": [], + "iso_3166_1": "US", + "rating": "TV-14" + }, + { + "descriptors": [], + "iso_3166_1": "AU", + "rating": "M" + }, + { + "descriptors": [], + "iso_3166_1": "RU", + "rating": "12+" + }, + { + "descriptors": [], + "iso_3166_1": "GB", + "rating": "15" + }, + { + "descriptors": [], + "iso_3166_1": "BR", + "rating": "12" + }, + { + "descriptors": [], + "iso_3166_1": "HU", + "rating": "12" + }, + { + "descriptors": [], + "iso_3166_1": "PH", + "rating": "G" + }, + { + "descriptors": [], + "iso_3166_1": "MX", + "rating": "A" + }, + { + "descriptors": [], + "iso_3166_1": "PT", + "rating": "NR" + }, + { + "descriptors": [], + "iso_3166_1": "ES", + "rating": "16" + }, + { + "descriptors": [], + "iso_3166_1": "FR", + "rating": "16" + }, + { + "descriptors": [], + "iso_3166_1": "CA", + "rating": "PG" + }, + { + "descriptors": [], + "iso_3166_1": "NL", + "rating": "6" + }, + { + "descriptors": [], + "iso_3166_1": "DE", + "rating": "12" + }, + { + "descriptors": [], + "iso_3166_1": "DE", + "rating": "16" + }, + { + "descriptors": [], + "iso_3166_1": "AU", + "rating": "PG" + }, + { + "descriptors": [], + "iso_3166_1": "KR", + "rating": "15" + }, + { + "descriptors": [], + "iso_3166_1": "AT", + "rating": "12" + }, + { + "descriptors": [], + "iso_3166_1": "CH", + "rating": "12" + }, + { + "descriptors": [], + "iso_3166_1": "PL", + "rating": "16" + }, + { + "descriptors": [], + "iso_3166_1": "HU", + "rating": "16" + }, + { + "descriptors": [], + "iso_3166_1": "CZ", + "rating": "15+" + }, + { + "descriptors": [], + "iso_3166_1": "RO", + "rating": "15" + } + ], + "id": 8592 +} From 3b4ff7e827269ea3b41026ef5c8d620e7755dbba Mon Sep 17 00:00:00 2001 From: chickdan Date: Tue, 25 Jun 2024 18:35:37 -0500 Subject: [PATCH 08/13] Add service test --- .../TMDbIntegrationTests/TVSeriesServiceTests.swift | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Tests/TMDbIntegrationTests/TVSeriesServiceTests.swift b/Tests/TMDbIntegrationTests/TVSeriesServiceTests.swift index 9c37897b..9044522f 100644 --- a/Tests/TMDbIntegrationTests/TVSeriesServiceTests.swift +++ b/Tests/TMDbIntegrationTests/TVSeriesServiceTests.swift @@ -125,4 +125,17 @@ final class TVSeriesServiceTests: XCTestCase { XCTAssertNotNil(linksCollection.twitter) } + func testContentRating() async throws { + let tvSeriesID = 8592 + + let contentRatings = try await tvSeriesService.contentRatings(forTVSeries: tvSeriesID, country: "US") + + XCTAssertNotNil(contentRatings) + + if let contentRating = contentRatings { + XCTAssertEqual(contentRating.rating, "TV-14") + XCTAssertEqual(contentRating.countryCode, "US") + } + + } } From 1e6dda1d314b5cbb9272f03c7e1a726168fe8b20 Mon Sep 17 00:00:00 2001 From: chickdan Date: Tue, 25 Jun 2024 18:42:41 -0500 Subject: [PATCH 09/13] Format --- Sources/TMDb/Domain/Models/ContentRating.swift | 2 +- Sources/TMDb/Domain/Models/ContentRatingResult.swift | 3 +-- .../Services/TVSeries/Requests/ContentRatingRequest.swift | 2 +- .../TMDb/Domain/Services/TVSeries/TMDbTVSeriesService.swift | 2 +- Tests/TMDbIntegrationTests/TVSeriesServiceTests.swift | 1 - Tests/TMDbTests/Domain/Models/ContentRatingTests.swift | 2 +- Tests/TMDbTests/Domain/Models/ContentRatingsTests.swift | 4 ++-- Tests/TMDbTests/Mocks/Models/ContentRating+Mocks.swift | 2 +- 8 files changed, 8 insertions(+), 10 deletions(-) diff --git a/Sources/TMDb/Domain/Models/ContentRating.swift b/Sources/TMDb/Domain/Models/ContentRating.swift index 61c5cb3c..c2db7df3 100644 --- a/Sources/TMDb/Domain/Models/ContentRating.swift +++ b/Sources/TMDb/Domain/Models/ContentRating.swift @@ -1,5 +1,5 @@ // -// Company.swift +// ContentRating.swift // TMDb // // Copyright © 2024 Adam Young. diff --git a/Sources/TMDb/Domain/Models/ContentRatingResult.swift b/Sources/TMDb/Domain/Models/ContentRatingResult.swift index dbe8aaca..c9f0f1a7 100644 --- a/Sources/TMDb/Domain/Models/ContentRatingResult.swift +++ b/Sources/TMDb/Domain/Models/ContentRatingResult.swift @@ -1,5 +1,5 @@ // -// Company.swift +// ContentRatingResult.swift // TMDb // // Copyright © 2024 Adam Young. @@ -19,7 +19,6 @@ import Foundation - struct ContentRatingResult: Codable, Equatable, Hashable, Sendable { let results: [ContentRating] let id: Int diff --git a/Sources/TMDb/Domain/Services/TVSeries/Requests/ContentRatingRequest.swift b/Sources/TMDb/Domain/Services/TVSeries/Requests/ContentRatingRequest.swift index 6c36bd52..d575817f 100644 --- a/Sources/TMDb/Domain/Services/TVSeries/Requests/ContentRatingRequest.swift +++ b/Sources/TMDb/Domain/Services/TVSeries/Requests/ContentRatingRequest.swift @@ -1,5 +1,5 @@ // -// TVSeriesWatchProvidersRequest.swift +// ContentRatingRequest.swift // TMDb // // Copyright © 2024 Adam Young. diff --git a/Sources/TMDb/Domain/Services/TVSeries/TMDbTVSeriesService.swift b/Sources/TMDb/Domain/Services/TVSeries/TMDbTVSeriesService.swift index a4f7a7f4..1c958e72 100644 --- a/Sources/TMDb/Domain/Services/TVSeries/TMDbTVSeriesService.swift +++ b/Sources/TMDb/Domain/Services/TVSeries/TMDbTVSeriesService.swift @@ -181,7 +181,7 @@ final class TMDbTVSeriesService: TVSeriesService { return result.results[country] } - + func contentRatings( forTVSeries tvSeriesID: TVSeries.ID, country: String = "US" diff --git a/Tests/TMDbIntegrationTests/TVSeriesServiceTests.swift b/Tests/TMDbIntegrationTests/TVSeriesServiceTests.swift index 9044522f..ec7ea0fa 100644 --- a/Tests/TMDbIntegrationTests/TVSeriesServiceTests.swift +++ b/Tests/TMDbIntegrationTests/TVSeriesServiceTests.swift @@ -136,6 +136,5 @@ final class TVSeriesServiceTests: XCTestCase { XCTAssertEqual(contentRating.rating, "TV-14") XCTAssertEqual(contentRating.countryCode, "US") } - } } diff --git a/Tests/TMDbTests/Domain/Models/ContentRatingTests.swift b/Tests/TMDbTests/Domain/Models/ContentRatingTests.swift index e1eb060d..55e64138 100644 --- a/Tests/TMDbTests/Domain/Models/ContentRatingTests.swift +++ b/Tests/TMDbTests/Domain/Models/ContentRatingTests.swift @@ -1,5 +1,5 @@ // -// CertificationTests.swift +// ContentRatingTests.swift // TMDb // // Copyright © 2024 Adam Young. diff --git a/Tests/TMDbTests/Domain/Models/ContentRatingsTests.swift b/Tests/TMDbTests/Domain/Models/ContentRatingsTests.swift index d6cf4539..756a1600 100644 --- a/Tests/TMDbTests/Domain/Models/ContentRatingsTests.swift +++ b/Tests/TMDbTests/Domain/Models/ContentRatingsTests.swift @@ -1,5 +1,5 @@ // -// CertificationTests.swift +// ContentRatingsTests.swift // TMDb // // Copyright © 2024 Adam Young. @@ -145,7 +145,7 @@ final class ContentRatingsTests: XCTestCase { descriptors: [], countryCode: "RO", rating: "15" - ), + ) ], id: 8592 ) diff --git a/Tests/TMDbTests/Mocks/Models/ContentRating+Mocks.swift b/Tests/TMDbTests/Mocks/Models/ContentRating+Mocks.swift index 1109e773..d44d4c63 100644 --- a/Tests/TMDbTests/Mocks/Models/ContentRating+Mocks.swift +++ b/Tests/TMDbTests/Mocks/Models/ContentRating+Mocks.swift @@ -1,5 +1,5 @@ // -// Country+Mocks.swift +// ContentRating+Mocks.swift // TMDb // // Copyright © 2024 Adam Young. From 4dc4e2edf5c31b50bbcd7b04b85b0da80619cde9 Mon Sep 17 00:00:00 2001 From: chickdan Date: Tue, 25 Jun 2024 18:44:23 -0500 Subject: [PATCH 10/13] Remove swiftlint disable comment --- Tests/TMDbTests/Domain/Models/ContentRatingsTests.swift | 2 -- 1 file changed, 2 deletions(-) diff --git a/Tests/TMDbTests/Domain/Models/ContentRatingsTests.swift b/Tests/TMDbTests/Domain/Models/ContentRatingsTests.swift index 756a1600..8fbf3a93 100644 --- a/Tests/TMDbTests/Domain/Models/ContentRatingsTests.swift +++ b/Tests/TMDbTests/Domain/Models/ContentRatingsTests.swift @@ -28,7 +28,6 @@ final class ContentRatingsTests: XCTestCase { XCTAssertEqual(result, contentRatings) } - // swiftlint:disable line_length private let contentRatings = ContentRatingResult( results: [ ContentRating( @@ -149,5 +148,4 @@ final class ContentRatingsTests: XCTestCase { ], id: 8592 ) - // swiftlint:enable line_length } From c392416098e0f793711542b0c5c8341879ba3fc9 Mon Sep 17 00:00:00 2001 From: chickdan Date: Tue, 16 Jul 2024 12:53:47 -0500 Subject: [PATCH 11/13] Request tests --- .../TVSeriesServiceTests.swift | 2 +- .../TVSeriesContentRatingsRequestTests.swift | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 Tests/TMDbTests/Domain/Services/TVSeries/Requests/TVSeriesContentRatingsRequestTests.swift diff --git a/Tests/TMDbIntegrationTests/TVSeriesServiceTests.swift b/Tests/TMDbIntegrationTests/TVSeriesServiceTests.swift index ec7ea0fa..88a3c043 100644 --- a/Tests/TMDbIntegrationTests/TVSeriesServiceTests.swift +++ b/Tests/TMDbIntegrationTests/TVSeriesServiceTests.swift @@ -125,7 +125,7 @@ final class TVSeriesServiceTests: XCTestCase { XCTAssertNotNil(linksCollection.twitter) } - func testContentRating() async throws { + func testContentRatings() async throws { let tvSeriesID = 8592 let contentRatings = try await tvSeriesService.contentRatings(forTVSeries: tvSeriesID, country: "US") diff --git a/Tests/TMDbTests/Domain/Services/TVSeries/Requests/TVSeriesContentRatingsRequestTests.swift b/Tests/TMDbTests/Domain/Services/TVSeries/Requests/TVSeriesContentRatingsRequestTests.swift new file mode 100644 index 00000000..05609426 --- /dev/null +++ b/Tests/TMDbTests/Domain/Services/TVSeries/Requests/TVSeriesContentRatingsRequestTests.swift @@ -0,0 +1,56 @@ +// +// TVSeriesContentRatingsRequestTests.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 TVSeriesContentRatingsRequestTests: XCTestCase { + + func testPath() { + let request = ContentRatingRequest(id: 1) + + XCTAssertEqual(request.path, "/tv/1/content_ratings") + } + + func testQueryItemsIsEmpty() { + let request = ContentRatingRequest(id: 1) + + XCTAssertTrue(request.queryItems.isEmpty) + } + + func testMethodIsGet() { + let request = ContentRatingRequest(id: 1) + + XCTAssertEqual(request.method, .get) + } + + func testHeadersIsEmpty() { + let request = ContentRatingRequest(id: 1) + + XCTAssertTrue(request.headers.isEmpty) + } + + func testBodyIsNil() { + let request = ContentRatingRequest(id: 1) + + XCTAssertNil(request.body) + } + +} + From e8f41b049d380d4c6e1eab37a3b96deb01f149a2 Mon Sep 17 00:00:00 2001 From: chickdan Date: Tue, 16 Jul 2024 12:56:49 -0500 Subject: [PATCH 12/13] formatting --- .../TVSeries/Requests/TVSeriesContentRatingsRequestTests.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/Tests/TMDbTests/Domain/Services/TVSeries/Requests/TVSeriesContentRatingsRequestTests.swift b/Tests/TMDbTests/Domain/Services/TVSeries/Requests/TVSeriesContentRatingsRequestTests.swift index 05609426..ffd465c2 100644 --- a/Tests/TMDbTests/Domain/Services/TVSeries/Requests/TVSeriesContentRatingsRequestTests.swift +++ b/Tests/TMDbTests/Domain/Services/TVSeries/Requests/TVSeriesContentRatingsRequestTests.swift @@ -53,4 +53,3 @@ final class TVSeriesContentRatingsRequestTests: XCTestCase { } } - From 68c3e1d5e1e2b326a4a2a97264df650018e561f3 Mon Sep 17 00:00:00 2001 From: chickdan Date: Tue, 16 Jul 2024 13:44:20 -0500 Subject: [PATCH 13/13] More mocks and tests --- ...MDbTVSeriesServiceContentRatingTests.swift | 73 +++++++++++++++++++ .../Mocks/Models/ContentRating+Mocks.swift | 11 +++ .../Models/ContentRatingResult+Mocks.swift | 32 ++++++++ 3 files changed, 116 insertions(+) create mode 100644 Tests/TMDbTests/Domain/Services/TVSeries/TMDbTVSeriesServiceContentRatingTests.swift create mode 100644 Tests/TMDbTests/Mocks/Models/ContentRatingResult+Mocks.swift diff --git a/Tests/TMDbTests/Domain/Services/TVSeries/TMDbTVSeriesServiceContentRatingTests.swift b/Tests/TMDbTests/Domain/Services/TVSeries/TMDbTVSeriesServiceContentRatingTests.swift new file mode 100644 index 00000000..0fccdf5b --- /dev/null +++ b/Tests/TMDbTests/Domain/Services/TVSeries/TMDbTVSeriesServiceContentRatingTests.swift @@ -0,0 +1,73 @@ +// +// TMDbTVSeriesServiceContentRatingTests.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 TMDbTVSeriesServiceContentRatingTests: XCTestCase { + + var service: TMDbTVSeriesService! + var apiClient: MockAPIClient! + + override func setUp() { + super.setUp() + apiClient = MockAPIClient() + service = TMDbTVSeriesService(apiClient: apiClient) + } + + override func tearDown() { + apiClient = nil + service = nil + super.tearDown() + } + + func testContentRatingsWithCountryReturnsShowRatings() async throws { + let expectedResult = ContentRatingResult.mock + let tvSeriesID = expectedResult.id + let country = "US" + + let expectedRating = expectedResult.results.first { rating in + rating.countryCode == country + } + + apiClient.addResponse(.success(expectedResult)) + let expectedRequest = ContentRatingRequest(id: tvSeriesID) + + let result = try await service.contentRatings(forTVSeries: tvSeriesID, country: country) + + XCTAssertEqual(result, expectedRating) + XCTAssertEqual(apiClient.lastRequest as? ContentRatingRequest, expectedRequest) + } + + func testContentRatingsWhenErrorsThrowsError() async throws { + let tvSeriesID = 1 + apiClient.addResponse(.failure(.unknown)) + + var error: Error? + do { + _ = try await service.contentRatings(forTVSeries: tvSeriesID) + } catch let err { + error = err + } + + let tmdbAPIError = try XCTUnwrap(error as? TMDbError) + + XCTAssertEqual(tmdbAPIError, .unknown) + } +} diff --git a/Tests/TMDbTests/Mocks/Models/ContentRating+Mocks.swift b/Tests/TMDbTests/Mocks/Models/ContentRating+Mocks.swift index d44d4c63..0e9871a6 100644 --- a/Tests/TMDbTests/Mocks/Models/ContentRating+Mocks.swift +++ b/Tests/TMDbTests/Mocks/Models/ContentRating+Mocks.swift @@ -41,3 +41,14 @@ extension ContentRating { .mock(descriptors: [], countryCode: "GB", rating: "TV-14") } } + +extension [ContentRating] { + + static var mocks: [Element] { + [ + .parksAndRecreationGB, + .parksAndRecreationUS + ] + } + +} diff --git a/Tests/TMDbTests/Mocks/Models/ContentRatingResult+Mocks.swift b/Tests/TMDbTests/Mocks/Models/ContentRatingResult+Mocks.swift new file mode 100644 index 00000000..322e244d --- /dev/null +++ b/Tests/TMDbTests/Mocks/Models/ContentRatingResult+Mocks.swift @@ -0,0 +1,32 @@ +// +// ContentRatingResult+Mocks.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 +@testable import TMDb + +extension ContentRatingResult { + + static var mock: Self { + .init( + results: .mocks, + id: 8592 + ) + } + +}