From 6272c9e05c3e9a15e672782020184d12d1049b66 Mon Sep 17 00:00:00 2001 From: Matt Mould <45389812+mattmouldbbc@users.noreply.github.com> Date: Wed, 10 May 2023 15:40:49 +0100 Subject: [PATCH] Remove async/await from the allCandidates code for consistency with PR #66 (#98) * Remove async/await from the allCandidates code for consistency with PR #66 --- .../JSONResultsRepository.swift | 18 +++++++-------- .../ElectionResults/ResultsRepository.swift | 2 +- .../JSONResultsRepositoryTests.swift | 23 ++++++++++++------- .../ResultsServiceTests.swift | 7 +++--- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/election-ui-swift/Modules/ElectionResults/Sources/ElectionResults/JSONResultsRepository.swift b/election-ui-swift/Modules/ElectionResults/Sources/ElectionResults/JSONResultsRepository.swift index 88aec0e..bb5a4e7 100644 --- a/election-ui-swift/Modules/ElectionResults/Sources/ElectionResults/JSONResultsRepository.swift +++ b/election-ui-swift/Modules/ElectionResults/Sources/ElectionResults/JSONResultsRepository.swift @@ -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) -> Void) { do { if let data = resultsJSON[startIndex].data(using: .utf8) { @@ -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) diff --git a/election-ui-swift/Modules/ElectionResults/Sources/ElectionResults/ResultsRepository.swift b/election-ui-swift/Modules/ElectionResults/Sources/ElectionResults/ResultsRepository.swift index 1deee0e..d05ca46 100644 --- a/election-ui-swift/Modules/ElectionResults/Sources/ElectionResults/ResultsRepository.swift +++ b/election-ui-swift/Modules/ElectionResults/Sources/ElectionResults/ResultsRepository.swift @@ -1,5 +1,5 @@ protocol ResultsRepository { func latestResults(completion: (Result) -> Void) - func allCandidates() async throws -> [Candidate] + func allCandidates(completion: (Result<[Candidate], ResultsRepositoryError>) -> Void) } diff --git a/election-ui-swift/Modules/ElectionResults/Tests/ElectionResultsTests/JSONResultsRepositoryTests.swift b/election-ui-swift/Modules/ElectionResults/Tests/ElectionResultsTests/JSONResultsRepositoryTests.swift index 69ce085..9d11a06 100644 --- a/election-ui-swift/Modules/ElectionResults/Tests/ElectionResultsTests/JSONResultsRepositoryTests.swift +++ b/election-ui-swift/Modules/ElectionResults/Tests/ElectionResultsTests/JSONResultsRepositoryTests.swift @@ -3,7 +3,7 @@ import XCTest @testable import ElectionResults class JSONResultsRepositoryTests: XCTestCase { - + var repository: JSONResultsRepository! override func setUpWithError() throws { @@ -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) } } diff --git a/election-ui-swift/Modules/ElectionResults/Tests/ElectionResultsTests/ResultsServiceTests.swift b/election-ui-swift/Modules/ElectionResults/Tests/ElectionResultsTests/ResultsServiceTests.swift index d5260d8..ab5ffcc 100644 --- a/election-ui-swift/Modules/ElectionResults/Tests/ElectionResultsTests/ResultsServiceTests.swift +++ b/election-ui-swift/Modules/ElectionResults/Tests/ElectionResultsTests/ResultsServiceTests.swift @@ -3,7 +3,7 @@ import XCTest @testable import ElectionResults class StubResultsRepository: ResultsRepository { - + var invokedLatestResults = false var invokedLatestResultsCount = 0 var stubbedLatestResultsCompletionResult: (Result, Void)? @@ -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!