Implemented the "getReviews(_: )" function for the iTunesService service in the iTunes library.
This commit is contained in:
parent
7b9a067713
commit
953f924178
43
Libraries/iTunes/Kit/Services/iTunesService.swift
Normal file
43
Libraries/iTunes/Kit/Services/iTunesService.swift
Normal file
@ -0,0 +1,43 @@
|
||||
//
|
||||
// iTunesService.swift
|
||||
// ReviewsiTunes
|
||||
//
|
||||
// Created by Javier Cicchelli on 17/03/2024.
|
||||
// Copyright © 2024 Röck+Cöde VoF. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import ReviewsFeedKit
|
||||
|
||||
public actor iTunesService: Service {
|
||||
|
||||
// MARK: Constants
|
||||
public let configuration: ServiceConfiguration
|
||||
public let decoder: JSONDecoder
|
||||
public let session: URLSession
|
||||
|
||||
// MARK: Properties
|
||||
private lazy var getReviewsEndpoint: GetReviewsAPIEndpoint = {
|
||||
.init(
|
||||
host: configuration.host,
|
||||
decoder: decoder,
|
||||
session: session
|
||||
)
|
||||
}()
|
||||
|
||||
// MARK: Initialisers
|
||||
public init(configuration: ServiceConfiguration) {
|
||||
self.configuration = configuration
|
||||
self.decoder = configuration.decoder
|
||||
self.session = .init(configuration: configuration.session)
|
||||
}
|
||||
|
||||
// MARK: Functions
|
||||
@discardableResult public func getReviews(
|
||||
_ input: GetReviewsInput
|
||||
) async throws -> GetReviewsOutput {
|
||||
try await getReviewsEndpoint(input)
|
||||
}
|
||||
|
||||
}
|
||||
|
58
Libraries/iTunes/Test/Helpers/Extensions/Array+Reviews.swift
Normal file
58
Libraries/iTunes/Test/Helpers/Extensions/Array+Reviews.swift
Normal file
@ -0,0 +1,58 @@
|
||||
//
|
||||
// Array+Reviews.swift
|
||||
// ReviewsiTunesTest
|
||||
//
|
||||
// Created by Javier Cicchelli on 17/03/2024.
|
||||
// Copyright © 2024 Röck+Cöde VoF. All rights reserved.
|
||||
//
|
||||
|
||||
import ReviewsFeedKit
|
||||
|
||||
extension Array where Element == Review {
|
||||
|
||||
// MARK: Constants
|
||||
static let empty: [Review] = []
|
||||
|
||||
static let many: [Review] = [
|
||||
.init(
|
||||
id: 1,
|
||||
author: "Some author name #1 goes here...",
|
||||
title: "Some title #1 goes here...",
|
||||
content: "Some content #1 goes here...",
|
||||
rating: 1,
|
||||
version: "Some version #1 goes here...",
|
||||
updated: .init()
|
||||
),
|
||||
.init(
|
||||
id: 2,
|
||||
author: "Some author name #2 goes here...",
|
||||
title: "Some title #2 goes here...",
|
||||
content: "Some content #2 goes here...",
|
||||
rating: 5,
|
||||
version: "Some version #2 goes here...",
|
||||
updated: .init()
|
||||
),
|
||||
.init(
|
||||
id: 3,
|
||||
author: "Some author name #3 goes here...",
|
||||
title: "Some title #3 goes here...",
|
||||
content: "Some content #3 goes here...",
|
||||
rating: 3,
|
||||
version: "Some version #3 goes here...",
|
||||
updated: .init()
|
||||
)
|
||||
]
|
||||
|
||||
static let one: [Review] = [
|
||||
.init(
|
||||
id: 1,
|
||||
author: "Some author name goes here...",
|
||||
title: "Some title goes here...",
|
||||
content: "Some content goes here...",
|
||||
rating: 3,
|
||||
version: "Some version goes here...",
|
||||
updated: .init()
|
||||
)
|
||||
]
|
||||
|
||||
}
|
@ -47,7 +47,6 @@ final class GetReviewsAPIEndpointTests: XCTestCase {
|
||||
|
||||
// THEN
|
||||
XCTAssertFalse(output.reviews.isEmpty)
|
||||
XCTAssertEqual(output.reviews.count, reviews.count)
|
||||
XCTAssertEqual(output.reviews, reviews)
|
||||
}
|
||||
|
||||
@ -70,7 +69,6 @@ final class GetReviewsAPIEndpointTests: XCTestCase {
|
||||
|
||||
// THEN
|
||||
XCTAssertFalse(output.reviews.isEmpty)
|
||||
XCTAssertEqual(output.reviews.count, reviews.count)
|
||||
XCTAssertEqual(output.reviews, reviews)
|
||||
}
|
||||
|
||||
@ -93,7 +91,6 @@ final class GetReviewsAPIEndpointTests: XCTestCase {
|
||||
|
||||
// THEN
|
||||
XCTAssertTrue(output.reviews.isEmpty)
|
||||
XCTAssertEqual(output.reviews.count, reviews.count)
|
||||
XCTAssertEqual(output.reviews, reviews)
|
||||
}
|
||||
|
||||
@ -162,53 +159,3 @@ final class GetReviewsAPIEndpointTests: XCTestCase {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Array+Constants
|
||||
private extension Array where Element == Review {
|
||||
|
||||
// MARK: Constants
|
||||
static let empty: [Review] = []
|
||||
|
||||
static let many: [Review] = [
|
||||
.init(
|
||||
id: 1,
|
||||
author: "Some author name #1 goes here...",
|
||||
title: "Some title #1 goes here...",
|
||||
content: "Some content #1 goes here...",
|
||||
rating: 1,
|
||||
version: "Some version #1 goes here...",
|
||||
updated: .init()
|
||||
),
|
||||
.init(
|
||||
id: 2,
|
||||
author: "Some author name #2 goes here...",
|
||||
title: "Some title #2 goes here...",
|
||||
content: "Some content #2 goes here...",
|
||||
rating: 5,
|
||||
version: "Some version #2 goes here...",
|
||||
updated: .init()
|
||||
),
|
||||
.init(
|
||||
id: 3,
|
||||
author: "Some author name #3 goes here...",
|
||||
title: "Some title #3 goes here...",
|
||||
content: "Some content #3 goes here...",
|
||||
rating: 3,
|
||||
version: "Some version #3 goes here...",
|
||||
updated: .init()
|
||||
)
|
||||
]
|
||||
|
||||
static let one: [Review] = [
|
||||
.init(
|
||||
id: 1,
|
||||
author: "Some author name goes here...",
|
||||
title: "Some title goes here...",
|
||||
content: "Some content goes here...",
|
||||
rating: 3,
|
||||
version: "Some version goes here...",
|
||||
updated: .init()
|
||||
)
|
||||
]
|
||||
|
||||
}
|
||||
|
108
Libraries/iTunes/Test/Tests/Services/iTunesServiceTests.swift
Normal file
108
Libraries/iTunes/Test/Tests/Services/iTunesServiceTests.swift
Normal file
@ -0,0 +1,108 @@
|
||||
//
|
||||
// iTunesServiceTests.swift
|
||||
// ReviewsiTunesTest
|
||||
//
|
||||
// Created by Javier Cicchelli on 17/03/2024.
|
||||
// Copyright © 2024 Röck+Cöde VoF. All rights reserved.
|
||||
//
|
||||
|
||||
import ReviewsFeedKit
|
||||
import ReviewsFoundationKit
|
||||
import XCTest
|
||||
|
||||
@testable import ReviewsiTunesKit
|
||||
|
||||
final class iTunesServiceTests: XCTestCase {
|
||||
|
||||
// MARK: Properties
|
||||
private var sut: iTunesService!
|
||||
|
||||
// MARK: Setup
|
||||
override func setUp() async throws {
|
||||
sut = .init(configuration: .init(session: .mock))
|
||||
}
|
||||
|
||||
// MARK: Functions
|
||||
func testGetReviews_whenResponseOK_withOneReview() async throws {
|
||||
// GIVEN
|
||||
let reviews: [Review] = .one
|
||||
|
||||
MockURLProtocol.response = .init(
|
||||
statusCode: 200,
|
||||
object: Feed(entries: reviews)
|
||||
)
|
||||
|
||||
// WHEN
|
||||
let output = try await sut.getReviews(.init(
|
||||
appID: "1234567890",
|
||||
countryCode: "abc"
|
||||
))
|
||||
|
||||
// THEN
|
||||
XCTAssertFalse(output.reviews.isEmpty)
|
||||
XCTAssertEqual(output.reviews.count, reviews.count)
|
||||
XCTAssertEqual(output.reviews, reviews)
|
||||
}
|
||||
|
||||
func testGetReviews_whenResponseOK_withManyReview() async throws {
|
||||
// GIVEN
|
||||
let reviews: [Review] = .many
|
||||
|
||||
MockURLProtocol.response = .init(
|
||||
statusCode: 200,
|
||||
object: Feed(entries: reviews)
|
||||
)
|
||||
|
||||
// WHEN
|
||||
let output = try await sut.getReviews(.init(
|
||||
appID: "1234567890",
|
||||
countryCode: "abc"
|
||||
))
|
||||
|
||||
// THEN
|
||||
XCTAssertFalse(output.reviews.isEmpty)
|
||||
XCTAssertEqual(output.reviews.count, reviews.count)
|
||||
XCTAssertEqual(output.reviews, reviews)
|
||||
}
|
||||
|
||||
func testGetReviews_whenResponseOK_withEmptyReview() async throws {
|
||||
// GIVEN
|
||||
let reviews: [Review] = .empty
|
||||
|
||||
MockURLProtocol.response = .init(
|
||||
statusCode: 200,
|
||||
object: Feed(entries: reviews)
|
||||
)
|
||||
|
||||
// WHEN
|
||||
let output = try await sut.getReviews(.init(
|
||||
appID: "1234567890",
|
||||
countryCode: "abc"
|
||||
))
|
||||
|
||||
// THEN
|
||||
XCTAssertTrue(output.reviews.isEmpty)
|
||||
XCTAssertEqual(output.reviews, reviews)
|
||||
}
|
||||
|
||||
func testGetReviews_whenResponseNotOK() async throws {
|
||||
// GIVEN
|
||||
let statusCode = 404
|
||||
|
||||
MockURLProtocol.response = .init(statusCode: statusCode)
|
||||
|
||||
// WHEN
|
||||
// THEN
|
||||
do {
|
||||
try await sut.getReviews(.init(
|
||||
appID: "1234567890",
|
||||
countryCode: "abc"
|
||||
))
|
||||
} catch EndpointError.requestFailed(statusCode: statusCode) {
|
||||
XCTAssertTrue(true)
|
||||
} catch {
|
||||
XCTAssertTrue(false)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user