Skip to content

Commit

Permalink
Adding UnitTests
Browse files Browse the repository at this point in the history
  • Loading branch information
xramos committed May 27, 2022
1 parent 5d6508b commit f7a74c7
Show file tree
Hide file tree
Showing 12 changed files with 572 additions and 121 deletions.
235 changes: 114 additions & 121 deletions Pokemon/Pokemon.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

88 changes: 88 additions & 0 deletions Pokemon/PokemonTests/DataBase/DBManagerUnitTests.swift
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)
}
}
35 changes: 35 additions & 0 deletions Pokemon/PokemonTests/DataBase/TestCoreDataStack.swift
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 Pokemon/PokemonTests/Entities/DBEntitites/DBPokemonUnitTests.swift
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)
}
}
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)
}
}
25 changes: 25 additions & 0 deletions Pokemon/PokemonTests/Extensions/Pokemon+Mock.swift
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")
}
}
4 changes: 4 additions & 0 deletions Pokemon/PokemonTests/PokemonTests-Bridging-Header.h
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 Pokemon/PokemonTests/Repositories/MockPokemonRepository.swift
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 Pokemon/PokemonTests/UseCases/ExistsPokemonUseCaseUnitTests.swift
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 Pokemon/PokemonTests/UseCases/GetPokemonUseCaseUnitTests.swift
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)
}
}
Loading

0 comments on commit f7a74c7

Please sign in to comment.