Skip to content

Commit

Permalink
Remove async/await from the allCandidates code for consistency with PR
Browse files Browse the repository at this point in the history
…#66 (#98)

* Remove async/await from the allCandidates code for consistency with PR #66
  • Loading branch information
mattmouldbbc authored May 10, 2023
1 parent a537490 commit 6272c9e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import Foundation

class JSONResultsRepository: ResultsRepository {

private var resultsJSON = [JSONData.resultsJSON1, JSONData.resultsJSON2, JSONData.resultsJSON3]
private var startIndex = 0

func latestResults(completion: (Result<ElectionResponse, ResultsRepositoryError>) -> Void) {
do {
if let data = resultsJSON[startIndex].data(using: .utf8) {
Expand All @@ -17,24 +17,24 @@ class JSONResultsRepository: ResultsRepository {
} catch { }
completion(.failure(ResultsRepositoryError.invalidJSON))
}
func allCandidates() async throws -> [Candidate] {

func allCandidates(completion: (Result<[Candidate], ResultsRepositoryError>) -> Void) {
do {
if let data = JSONData.candidateJSON.data(using: .utf8) {
let decoder = JSONDecoder()
let result = try decoder.decode([CandidateDto].self, from: data)
return result.map { Candidate(id: $0.id, name: $0.name) }
completion(.success(result.map { Candidate(id: $0.id, name: $0.name) }))
}
} catch {}
throw ResultsRepositoryError.invalidJSON
} catch { }
completion(.failure(ResultsRepositoryError.invalidJSON))
}

private func incrementIndex() {
if startIndex < resultsJSON.count - 1 {
startIndex = startIndex + 1
}
}

private func adapt(dto: ResultsDto) -> ElectionResponse {
let results = dto.results.map { ElectionResult(candidateId: $0.candidateId, party: $0.party, votes: $0.votes) }
return ElectionResponse(isComplete: dto.metadata.isComplete, electionResults: results)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

protocol ResultsRepository {
func latestResults(completion: (Result<ElectionResponse, ResultsRepositoryError>) -> Void)
func allCandidates() async throws -> [Candidate]
func allCandidates(completion: (Result<[Candidate], ResultsRepositoryError>) -> Void)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import XCTest
@testable import ElectionResults

class JSONResultsRepositoryTests: XCTestCase {

var repository: JSONResultsRepository!

override func setUpWithError() throws {
Expand Down Expand Up @@ -40,12 +40,19 @@ class JSONResultsRepositoryTests: XCTestCase {
}
wait(for: [expectation], timeout: 0.1)
}

func testAllCandidatese() async throws {
let all = try await repository.allCandidates()
let first = all.first!
XCTAssertEqual(all.count, 3)
XCTAssertEqual(first.id, 1)
XCTAssertEqual(first.name, "Baldrick")

func testAllCandidates() async throws {
let expectation = XCTestExpectation()

repository.allCandidates { result in
if case let .success(response) = result {
let first = response.first!
XCTAssertEqual(response.count, 3)
XCTAssertEqual(first.id, 1)
XCTAssertEqual(first.name, "Baldrick")
expectation.fulfill()
}
}
wait(for: [expectation], timeout: 0.1)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import XCTest
@testable import ElectionResults

class StubResultsRepository: ResultsRepository {

var invokedLatestResults = false
var invokedLatestResultsCount = 0
var stubbedLatestResultsCompletionResult: (Result<ElectionResponse, ResultsRepositoryError>, Void)?
Expand All @@ -19,15 +19,14 @@ class StubResultsRepository: ResultsRepository {
var invokedAllCandidates = false
var invokedAllCandidatesCount = 0

func allCandidates() async throws -> [ElectionResults.Candidate] {
func allCandidates(completion: (Result<[Candidate], ResultsRepositoryError>) -> Void) {
invokedAllCandidates = true
invokedAllCandidatesCount += 1
return []
}
}

class ResultsServiceTests: XCTestCase {

var service: ResultsServiceImpl!
var stubResultsRepository: StubResultsRepository!

Expand Down

0 comments on commit 6272c9e

Please sign in to comment.