-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
335 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
Pokemon/PokemonTests/Repositories/LocalPokemonDataSourceUnitTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// | ||
// LocalPokemonDataSourceUnitTests.swift | ||
// PokemonTests | ||
// | ||
// Created by Xavier Ramos on 30/5/22. | ||
// | ||
|
||
import XCTest | ||
@testable import Pokemon | ||
|
||
class LocalPokemonDataSourceUnitTests: XCTestCase { | ||
|
||
var sut: LocalPokemonDataSource! | ||
|
||
override func setUpWithError() throws { | ||
|
||
try super.setUpWithError() | ||
|
||
sut = LocalPokemonDataSource(dbManager: DBManager(coreDataStack: TestCoreDataStack())) | ||
} | ||
|
||
override func tearDownWithError() throws { | ||
|
||
sut = nil | ||
|
||
try super.tearDownWithError() | ||
} | ||
|
||
func testSavePokemon() { | ||
|
||
// Given | ||
let pokemon = getPokemonMock() | ||
|
||
// When | ||
sut.savePokemon(pokemon: pokemon) | ||
let pokemons = sut.getPokemons() | ||
|
||
// Then | ||
XCTAssertEqual(pokemons.count, 1) | ||
} | ||
|
||
func testExistsPokemon() { | ||
|
||
// Given | ||
let pokemon = getPokemonMock() | ||
|
||
// When | ||
sut.savePokemon(pokemon: pokemon) | ||
let response = sut.existsPokemon(pokemon: pokemon) | ||
|
||
// Then | ||
XCTAssertTrue(response) | ||
} | ||
|
||
func testGetPokemonsEmpty() { | ||
|
||
// When | ||
let pokemons = sut.getPokemons() | ||
|
||
// Then | ||
XCTAssertEqual(pokemons.count, 0) | ||
} | ||
|
||
func testGetPokemons() { | ||
|
||
// Given | ||
let pokemon1 = getPokemonMock() | ||
let pokemon2 = getPokemonMock2() | ||
|
||
// When | ||
sut.savePokemon(pokemon: pokemon1) | ||
sut.savePokemon(pokemon: pokemon2) | ||
let pokemons = sut.getPokemons() | ||
|
||
// Then | ||
XCTAssertEqual(pokemons.count, 2) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
Pokemon/PokemonTests/Repositories/PokemonRepositoryImplementationUnitTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
// | ||
// PokemonRepositoryImplementationUnitTests.swift | ||
// PokemonTests | ||
// | ||
// Created by Xavier Ramos on 30/5/22. | ||
// | ||
|
||
import XCTest | ||
import Combine | ||
@testable import Pokemon | ||
|
||
class PokemonRepositoryImplementationUnitTests: XCTestCase { | ||
|
||
var sut: PokemonRepositoryImplementation! | ||
|
||
var cancellable: AnyCancellable? | ||
|
||
let baseUrlString = "http://jsonplaceholder.typicode.com/" | ||
|
||
let successStatusCode = 200 | ||
let failureStatusCode = 401 | ||
let timeoutTime: TimeInterval = 2 | ||
|
||
override func setUpWithError() throws { | ||
|
||
try super.setUpWithError() | ||
|
||
sut = PokemonRepositoryImplementation() | ||
} | ||
|
||
override func tearDownWithError() throws { | ||
|
||
sut = nil | ||
|
||
try super.tearDownWithError() | ||
} | ||
|
||
func testGetPokemonOK() { | ||
|
||
// Given | ||
let id = 1 | ||
let session = getPokemonSession(statusCode: successStatusCode, id: id) | ||
let remote = RemotePokemonDataSource(baseURL: baseUrlString, session: session) | ||
sut = PokemonRepositoryImplementation(remoteDataSource: remote) | ||
|
||
let exp = expectation(description: "expected values") | ||
|
||
// When | ||
cancellable = sut.getPokemon(id: id) | ||
.sink(receiveCompletion: { completion in | ||
|
||
switch completion { | ||
case .finished: | ||
exp.fulfill() | ||
case .failure: | ||
break | ||
} | ||
|
||
}, receiveValue: { pokemon in | ||
XCTAssertEqual(pokemon.id, 2) | ||
}) | ||
|
||
wait(for: [exp], timeout: timeoutTime) | ||
|
||
// Then | ||
XCTAssertNotNil(cancellable) | ||
} | ||
|
||
func testGetPokemonKO() { | ||
|
||
// Given | ||
let id = 1 | ||
let session = getPokemonSession(statusCode: failureStatusCode, id: id) | ||
let remote = RemotePokemonDataSource(baseURL: baseUrlString, session: session) | ||
sut = PokemonRepositoryImplementation(remoteDataSource: remote) | ||
|
||
let exp = expectation(description: "expected values") | ||
|
||
// When | ||
cancellable = sut.getPokemon(id: id) | ||
.sink(receiveCompletion: { completion in | ||
|
||
switch completion { | ||
case .finished: | ||
break | ||
case .failure: | ||
exp.fulfill() | ||
} | ||
}, receiveValue: { _ in | ||
|
||
// Nothing to recieve | ||
}) | ||
|
||
wait(for: [exp], timeout: timeoutTime) | ||
|
||
// Then | ||
XCTAssertNotNil(cancellable) | ||
} | ||
} | ||
|
||
// MARK: - Session | ||
|
||
extension PokemonRepositoryImplementationUnitTests { | ||
|
||
func getPokemonSession(statusCode: Int, id: Int) -> URLSession { | ||
|
||
// URL we expect to call | ||
let url = URL(string: "http://jsonplaceholder.typicode.com/pokemon/\(id)") | ||
|
||
// data we expect to receive | ||
let data = getPokemonData() | ||
|
||
// attach that to some fixed data in our protocol handler | ||
URLProtocolMock.testURLs = [url: data] | ||
URLProtocolMock.response = HTTPURLResponse(url: URL(string: "http://jsonplaceholder.typicode.com:8080")!, | ||
statusCode: statusCode, | ||
httpVersion: nil, | ||
headerFields: nil) | ||
|
||
// now setup a configuration to use our mock | ||
let config = URLSessionConfiguration.ephemeral | ||
config.protocolClasses = [URLProtocolMock.self] | ||
|
||
// and create the URLSession from that | ||
let session = URLSession(configuration: config) | ||
|
||
return session | ||
} | ||
|
||
func getPokemonData() -> Data { | ||
|
||
let dataString = """ | ||
{ | ||
"base_experience": 142, | ||
"height": 10, | ||
"id": 2, | ||
"name": "ivysaur", | ||
"weight": 130, | ||
"sprites": { | ||
"front_default": "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/2.png" | ||
}, | ||
"types": [ | ||
{ | ||
"slot": 1, | ||
"type": { | ||
"name": "grass" | ||
} | ||
}, | ||
{ | ||
"slot": 2, | ||
"type": { | ||
"name: "poison" | ||
} | ||
} | ||
] | ||
""" | ||
return Data(dataString.utf8) | ||
} | ||
} |
Oops, something went wrong.