Skip to content

Commit

Permalink
Merge pull request #17 from kinkofer/ResourceServiceFix
Browse files Browse the repository at this point in the history
fix: Replace ResourceService baseURL
  • Loading branch information
kinkofer authored Jul 13, 2020
2 parents b13221e + 50ae716 commit d0ca44f
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 7 deletions.
18 changes: 14 additions & 4 deletions Example/PokemonAPI-Example/CombineExampleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,31 @@ import Combine

@available(iOS 13, *)
class CombineExampleViewController: UIViewController {
let pokemonAPI = PokemonAPI()
var cancellables = Set<AnyCancellable>()


override func viewDidLoad() {
super.viewDidLoad()

PokemonAPI().berryService.fetchBerry(5)
pokemonAPI.berryService.fetchBerry(5)
.tryMap { berry -> PKMNamedAPIResource<PKMBerryFirmness> in
print(berry)
guard let berryFirmness = berry.firmness else {
throw HTTPError.unexpectedResponse
}
return berryFirmness
}
.flatMap { berryFirmness in
return self.pokemonAPI.resourceService.fetch(berryFirmness)
}
.sink(receiveCompletion: { completion in
if case .failure(let error) = completion {
print(error.localizedDescription)
}
}, receiveValue: { berry in
print(berry)
}, receiveValue: { berryFirmness in
print(berryFirmness)
})
.store(in: &cancellables)
}

}
2 changes: 1 addition & 1 deletion PokemonAPI.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = "PokemonAPI"
s.version = "6.0.0"
s.version = "6.0.2"
s.summary = "A wrapper for pokeapi v2"

# This description is used to generate tags and improve search results.
Expand Down
8 changes: 8 additions & 0 deletions PokemonAPI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
objects = {

/* Begin PBXBuildFile section */
5926AD3024BB83D9000CD4BC /* ResourceServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5926AD2F24BB83D9000CD4BC /* ResourceServiceTests.swift */; };
5926AD3124BB83D9000CD4BC /* ResourceServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5926AD2F24BB83D9000CD4BC /* ResourceServiceTests.swift */; };
5926AD3224BB83D9000CD4BC /* ResourceServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5926AD2F24BB83D9000CD4BC /* ResourceServiceTests.swift */; };
593FD62A20E54EBB00CF8862 /* Evolution.swift in Sources */ = {isa = PBXBuildFile; fileRef = 593FD62120E54EBA00CF8862 /* Evolution.swift */; };
593FD62B20E54EBB00CF8862 /* Evolution.swift in Sources */ = {isa = PBXBuildFile; fileRef = 593FD62120E54EBA00CF8862 /* Evolution.swift */; };
593FD62C20E54EBB00CF8862 /* Evolution.swift in Sources */ = {isa = PBXBuildFile; fileRef = 593FD62120E54EBA00CF8862 /* Evolution.swift */; };
Expand Down Expand Up @@ -262,6 +265,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
5926AD2F24BB83D9000CD4BC /* ResourceServiceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResourceServiceTests.swift; sourceTree = "<group>"; };
593FD62120E54EBA00CF8862 /* Evolution.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Evolution.swift; sourceTree = "<group>"; };
593FD62220E54EBB00CF8862 /* Locations.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Locations.swift; sourceTree = "<group>"; };
593FD62320E54EBB00CF8862 /* Pokemon.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Pokemon.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -459,6 +463,7 @@
isa = PBXGroup;
children = (
596476382496FF9F004C7E93 /* BerryServiceTests.swift */,
5926AD2F24BB83D9000CD4BC /* ResourceServiceTests.swift */,
);
path = ServiceTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -939,6 +944,7 @@
buildActionMask = 2147483647;
files = (
596476392496FF9F004C7E93 /* BerryServiceTests.swift in Sources */,
5926AD3024BB83D9000CD4BC /* ResourceServiceTests.swift in Sources */,
597CEC5824A3EA5C00E90A7C /* MockedResponse.swift in Sources */,
597CEC5424A3B78A00E90A7C /* RequestMocking.swift in Sources */,
74B038131D56714D00F48B92 /* PokemonAPI_IntegrationTests.swift in Sources */,
Expand Down Expand Up @@ -1047,6 +1053,7 @@
buildActionMask = 2147483647;
files = (
5964763A2496FFA0004C7E93 /* BerryServiceTests.swift in Sources */,
5926AD3124BB83D9000CD4BC /* ResourceServiceTests.swift in Sources */,
597CEC5924A3EA5C00E90A7C /* MockedResponse.swift in Sources */,
597CEC5524A3B78A00E90A7C /* RequestMocking.swift in Sources */,
74B0388B1D56798100F48B92 /* PokemonAPI_IntegrationTests.swift in Sources */,
Expand Down Expand Up @@ -1107,6 +1114,7 @@
buildActionMask = 2147483647;
files = (
5964763B2496FFA0004C7E93 /* BerryServiceTests.swift in Sources */,
5926AD3224BB83D9000CD4BC /* ResourceServiceTests.swift in Sources */,
597CEC5A24A3EA5C00E90A7C /* MockedResponse.swift in Sources */,
597CEC5624A3B78A00E90A7C /* RequestMocking.swift in Sources */,
74B0388C1D56798300F48B92 /* PokemonAPI_IntegrationTests.swift in Sources */,
Expand Down
3 changes: 2 additions & 1 deletion PokemonAPI/WebServices/ResourceService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public struct ResourceService: PKMResourceService {

public var session: URLSession

public var baseURL: String = "https://pokeapi.co/api/v2"
// Resources do not need a baseURL, the full URL is in the PKMAPIResource itself.
public var baseURL: String = ""


/**
Expand Down
11 changes: 10 additions & 1 deletion Tests/PokemonAPITests/MockData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import PokemonAPI


struct MockBerryData {
Expand Down Expand Up @@ -531,7 +532,15 @@ struct MockPokemonData {


struct MockResourceData {

static var berryResource: PKMNamedAPIResource<PKMBerry> {
let resourceData = """
{
"name": "cherri",
"url": "https://pokeapi.co/api/v2/berry/1/"
}
""".data(using: .utf8)!
return try! PKMNamedAPIResource<PKMBerry>.decode(from: resourceData)
}
}


Expand Down
72 changes: 72 additions & 0 deletions Tests/PokemonAPITests/ServiceTests/ResourceServiceTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// ResourceServiceTests.swift
// PokemonAPI
//
// Created by Christopher Jennewein on 7/12/20.
// Copyright © 2020 Prismatic Games. All rights reserved.
//

import XCTest
import Combine
@testable import PokemonAPI


@available(OSX 10.15, iOS 13, tvOS 13.0, watchOS 6.0, *)
class ResourceServiceTests: XCTestCase {
typealias API = ResourceService.API
typealias Mock = RequestMocking.MockedResponse

var service: ResourceService!
var cancellables = Set<AnyCancellable>()


override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
cancellables = Set<AnyCancellable>()
service = ResourceService(session: .mockedResponsesOnly)
}

override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
RequestMocking.removeAllMocks()
}



// MARK: - Helper

private func mock<T>(_ apiCall: API<T>, result: Result<Data, Swift.Error>, httpCode: Int = 200, paginated: Bool = false) throws {
let mock = try Mock(apiCall: apiCall, baseURL: service.baseURL, result: result, httpCode: httpCode)
RequestMocking.add(mock: mock)
}



// MARK: - Tests

func testFetchResource_success() throws {
let asyncExpectation = expectation(description: "Completion")

try mock(.fetchResource(MockResourceData.berryResource), result: .success(MockBerryData.berry))

service.fetch(MockResourceData.berryResource)
.sinkToResult { result in
switch result {
case .success(let berry):
do {
let berryName = try XCTUnwrap(berry.name, "The berry should have a name")
XCTAssertTrue(berryName == "cheri", "Expected the first berry to be cheri but found \(berryName)")
} catch {
XCTFail("The response was not valid")
}
case .failure(let error):
XCTFail("The service should not fail: \(error.localizedDescription)")
}

asyncExpectation.fulfill();
}
.store(in: &cancellables)

self.wait(for: [asyncExpectation], timeout: 5)
}
}

0 comments on commit d0ca44f

Please sign in to comment.