-
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
572 additions
and
121 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,88 @@ | ||
// | ||
// DBManagerUnitTests.swift | ||
// PokemonTests | ||
// | ||
// Created by Xavier Ramos on 27/5/22. | ||
// | ||
|
||
import XCTest | ||
@testable import Pokemon | ||
|
||
class DBManagerUnitTest: XCTestCase { | ||
|
||
var sut: DBManager! | ||
|
||
override func setUpWithError() throws { | ||
|
||
try super.setUpWithError() | ||
|
||
sut = DBManager(coreDataStack: TestCoreDataStack()) | ||
} | ||
|
||
override func tearDownWithError() throws { | ||
|
||
sut = nil | ||
|
||
try super.tearDownWithError() | ||
} | ||
|
||
// MARK: - Tests | ||
|
||
func testSavePokemon() { | ||
|
||
// Given | ||
let pokemon = getPokemonMock() | ||
|
||
// When | ||
sut.savePokemon(pokemon: pokemon) | ||
let response = sut.getPokemons() | ||
|
||
// Then | ||
XCTAssertEqual(response.count, 1) | ||
} | ||
|
||
func testGetPokemon() { | ||
|
||
// Given | ||
let pokemon = getPokemonMock() | ||
|
||
// When | ||
sut.savePokemon(pokemon: pokemon) | ||
let response = sut.getPokemons() | ||
|
||
// Then | ||
XCTAssertEqual(response.count, 1) | ||
XCTAssertEqual(response.first?.id, pokemon.id) | ||
XCTAssertEqual(response.first?.name, pokemon.name) | ||
XCTAssertEqual(response.first?.weight, pokemon.weight) | ||
XCTAssertEqual(response.first?.height, pokemon.height) | ||
XCTAssertEqual(response.first?.experience, pokemon.experience) | ||
XCTAssertEqual(response.first?.url, pokemon.url) | ||
XCTAssertEqual(response.first?.types, pokemon.types) | ||
} | ||
|
||
func testExistsPokemonTrue() { | ||
|
||
// Given | ||
let pokemon = getPokemonMock() | ||
|
||
// When | ||
sut.savePokemon(pokemon: pokemon) | ||
let response = sut.existsPokemon(pokemon: pokemon) | ||
|
||
// Then | ||
XCTAssertTrue(response) | ||
} | ||
|
||
func testExistsPokemonFalse() { | ||
|
||
// Given | ||
let pokemon = getPokemonMock() | ||
|
||
// When | ||
let response = sut.existsPokemon(pokemon: pokemon) | ||
|
||
// Then | ||
XCTAssertFalse(response) | ||
} | ||
} |
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,35 @@ | ||
// | ||
// TestCoreDataStack.swift | ||
// PokemonTests | ||
// | ||
// Created by Xavier Ramos on 27/5/22. | ||
// | ||
|
||
import Foundation | ||
import CoreData | ||
@testable import Pokemon | ||
|
||
class TestCoreDataStack: CoreDataStack { | ||
|
||
private let modelName = "Model" | ||
|
||
override init() { | ||
|
||
super.init() | ||
|
||
let persistentStoreDescription = NSPersistentStoreDescription() | ||
persistentStoreDescription.type = NSInMemoryStoreType | ||
|
||
let container = NSPersistentContainer(name: modelName) | ||
|
||
container.persistentStoreDescriptions = [persistentStoreDescription] | ||
|
||
container.loadPersistentStores { _, error in | ||
if let error = error as NSError? { | ||
fatalError("Unresolved error \(error), \(error.userInfo)") | ||
} | ||
} | ||
|
||
storeContainer = container | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
Pokemon/PokemonTests/Entities/DBEntitites/DBPokemonUnitTests.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,52 @@ | ||
// | ||
// DBPokemonUnitTests.swift | ||
// PokemonTests | ||
// | ||
// Created by Xavier Ramos on 27/5/22. | ||
// | ||
|
||
import XCTest | ||
@testable import Pokemon | ||
|
||
class DBPokemonUnitTests: XCTestCase { | ||
|
||
override func setUpWithError() throws { | ||
|
||
try super.setUpWithError() | ||
} | ||
|
||
override func tearDownWithError() throws { | ||
|
||
try super.tearDownWithError() | ||
} | ||
|
||
func testConvertToEntity() { | ||
|
||
// Given | ||
let id: Int = 1 | ||
let dbPokemon = DBPokemon(context: TestCoreDataStack().managedContext) | ||
|
||
dbPokemon.id = Int64(id) | ||
dbPokemon.name = "pikachu" | ||
dbPokemon.weight = Int16(1) | ||
dbPokemon.height = Int16(2) | ||
dbPokemon.experience = Int16(5) | ||
dbPokemon.url = "url" | ||
dbPokemon.date = "date" | ||
dbPokemon.types = "electric" | ||
|
||
// When | ||
let pokemon = dbPokemon.convertToEntity() | ||
|
||
// Then | ||
XCTAssertNotNil(pokemon) | ||
XCTAssertEqual(dbPokemon.id, Int64(pokemon.id)) | ||
XCTAssertEqual(dbPokemon.name, pokemon.name) | ||
XCTAssertEqual(dbPokemon.weight, Int16(pokemon.weight)) | ||
XCTAssertEqual(dbPokemon.height, Int16(pokemon.height)) | ||
XCTAssertEqual(dbPokemon.experience, Int16(pokemon.experience)) | ||
XCTAssertEqual(dbPokemon.url, pokemon.url) | ||
XCTAssertEqual(dbPokemon.date, pokemon.date) | ||
XCTAssertEqual(dbPokemon.types, pokemon.types) | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
Pokemon/PokemonTests/Entities/ServerEntities/ServerPokemonUnitTests.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,50 @@ | ||
// | ||
// ServerPokemonUnitTests.swift | ||
// PokemonTests | ||
// | ||
// Created by Xavier Ramos on 27/5/22. | ||
// | ||
|
||
import XCTest | ||
@testable import Pokemon | ||
|
||
class ServerPokemonUnitTests: XCTestCase { | ||
|
||
override func setUpWithError() throws { | ||
|
||
try super.setUpWithError() | ||
} | ||
|
||
override func tearDownWithError() throws { | ||
|
||
try super.tearDownWithError() | ||
} | ||
|
||
func testConvertToEntity() { | ||
|
||
// Given | ||
|
||
let serverSprite = ServerSprite(frontDefault: "url") | ||
let type = ServerType(name: "electric") | ||
let types = ServerTypes(slot: 0, type: type) | ||
let serverPokemon = ServerPokemon(id: 1, | ||
name: "pikachu", | ||
weight: 1, | ||
height: 2, | ||
baseExperience: 5, | ||
sprites: serverSprite, | ||
types: [types]) | ||
|
||
// When | ||
let pokemon = serverPokemon.convertToEntity() | ||
|
||
// Then | ||
XCTAssertEqual(pokemon.id, serverPokemon.id) | ||
XCTAssertEqual(pokemon.name, serverPokemon.name) | ||
XCTAssertEqual(pokemon.weight, serverPokemon.weight) | ||
XCTAssertEqual(pokemon.height, serverPokemon.height) | ||
XCTAssertEqual(pokemon.experience, serverPokemon.baseExperience) | ||
XCTAssertEqual(pokemon.url, serverPokemon.sprites.frontDefault) | ||
XCTAssertEqual(pokemon.types, serverPokemon.types?.first?.type.name) | ||
} | ||
} |
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,25 @@ | ||
// | ||
// Pokemon+Mock.swift | ||
// PokemonTests | ||
// | ||
// Created by Xavier Ramos on 27/5/22. | ||
// | ||
|
||
import Foundation | ||
import XCTest | ||
@testable import Pokemon | ||
|
||
extension XCTestCase { | ||
|
||
func getPokemonMock() -> Pokemon { | ||
|
||
return Pokemon(id: 1, | ||
name: "pikachu", | ||
weight: 1, | ||
height: 2, | ||
experience: 5, | ||
url: "url", | ||
date: "", | ||
types: "electric") | ||
} | ||
} |
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,4 @@ | ||
// | ||
// Use this file to import your target's public headers that you would like to expose to Swift. | ||
// | ||
|
46 changes: 46 additions & 0 deletions
46
Pokemon/PokemonTests/Repositories/MockPokemonRepository.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,46 @@ | ||
// | ||
// MockPokemonRepository.swift | ||
// PokemonTests | ||
// | ||
// Created by Xavier Ramos on 27/5/22. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
@testable import Pokemon | ||
|
||
class MockPokemonRepository: PokemonRepository { | ||
|
||
var isGetPokemonCalled = false | ||
var isSavePokemonCalled = false | ||
var isExistsPokemonCalled = false | ||
var isGetPokemonsCalled = false | ||
|
||
func getPokemon() -> AnyPublisher<Pokemon, Error> { | ||
|
||
self.isGetPokemonCalled = true | ||
|
||
return Just(Pokemon(id: 1, name: "Pikachu", weight: 1, height: 2, experience: 5, url: "url", date: "date", types: "electric")) | ||
.setFailureType(to: Error.self) | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
func savePokemon(pokemon: Pokemon) { | ||
|
||
self.isSavePokemonCalled = true | ||
} | ||
|
||
func existsPokemon(pokemon: Pokemon) -> Bool { | ||
|
||
self.isExistsPokemonCalled = true | ||
|
||
return true | ||
} | ||
|
||
func getPokemons() -> [Pokemon] { | ||
|
||
self.isGetPokemonsCalled = true | ||
|
||
return [] | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
Pokemon/PokemonTests/UseCases/ExistsPokemonUseCaseUnitTests.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,40 @@ | ||
// | ||
// ExistsPokemonUseCaseUnitTests.swift | ||
// PokemonTests | ||
// | ||
// Created by Xavier Ramos on 27/5/22. | ||
// | ||
|
||
import XCTest | ||
@testable import Pokemon | ||
|
||
class ExistsPokemonUseCaseUnitTests: XCTestCase { | ||
|
||
var sut: ExistsPokemonUseCase? | ||
|
||
override func setUpWithError() throws { | ||
|
||
try super.setUpWithError() | ||
} | ||
|
||
override func tearDownWithError() throws { | ||
|
||
sut = nil | ||
|
||
try super.tearDownWithError() | ||
} | ||
|
||
func testIsCalled() { | ||
|
||
// Given | ||
let repository = MockPokemonRepository() | ||
sut = ExistsPokemonUseCase(repository: repository) | ||
let pokemon = getPokemonMock() | ||
|
||
// When | ||
_ = sut!.execute(pokemon: pokemon) | ||
|
||
// Then | ||
XCTAssertTrue(repository.isExistsPokemonCalled) | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
Pokemon/PokemonTests/UseCases/GetPokemonUseCaseUnitTests.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,39 @@ | ||
// | ||
// GetPokemonUseCaseUnitTests.swift | ||
// PokemonTests | ||
// | ||
// Created by Xavier Ramos on 27/5/22. | ||
// | ||
|
||
import XCTest | ||
@testable import Pokemon | ||
|
||
class GetPokemoUseCasenUnitTests: XCTestCase { | ||
|
||
var sut: GetPokemonUseCase? | ||
|
||
override func setUpWithError() throws { | ||
|
||
try super.setUpWithError() | ||
} | ||
|
||
override func tearDownWithError() throws { | ||
|
||
sut = nil | ||
|
||
try super.tearDownWithError() | ||
} | ||
|
||
func testIsCalled() { | ||
|
||
// Given | ||
let repository = MockPokemonRepository() | ||
sut = GetPokemonUseCase(repository: repository) | ||
|
||
// When | ||
_ = sut!.execute() | ||
|
||
// Then | ||
XCTAssertTrue(repository.isGetPokemonCalled) | ||
} | ||
} |
Oops, something went wrong.