Skip to content

Commit

Permalink
Assert location payloads response
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelBCarter committed Nov 22, 2024
1 parent a9662e0 commit 937b820
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ open class LocationPayload: Payload {
override open func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.schema, forKey: .schema)

let coords: CoordinatesStruct = CoordinatesStruct(
accuracy: self.location.horizontalAccuracy,
altitude: self.location.altitude,
Expand All @@ -28,7 +28,8 @@ open class LocationPayload: Payload {
speed: self.location.speed
)
let timestamp = self.location.timestamp
let currentLocation: CurrentLocationStruct = CurrentLocationStruct(coords: coords, timestamp: timestamp)
let currentLocation: CurrentLocationStruct = CurrentLocationStruct(
coords: coords, timestamp: timestamp)
try container.encode(currentLocation, forKey: .currentLocation)
}
}
20 changes: 9 additions & 11 deletions Sources/XyoClient/Witness/Location/LocationWitness.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@ import Foundation

open class LocationWitness: WitnessModuleAsync {
private var _locationService: LocationServiceProtocol?

private var locationService: LocationServiceProtocol {
get {
if let service = _locationService {
return service
} else {
let initialized = LocationService()
self._locationService = initialized
return initialized
}
if let service = _locationService {
return service
} else {
let initialized = LocationService()
self._locationService = initialized
return initialized
}
}

override init(account: AccountInstance? = nil) {
super.init(account: account)
}

convenience init(locationService: LocationServiceProtocol) {
self.init(account: nil)
self._locationService = locationService
Expand Down
19 changes: 13 additions & 6 deletions Tests/XyoClientTests/Witness/LocationWitness.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import XCTest

@testable import XyoClient

fileprivate class MockLocationService: LocationServiceProtocol {
private class MockLocationService: LocationServiceProtocol {
var didRequestAuthorization = false
var simulatedResult: Result<CLLocation, Error>?

func requestAuthorization() {
didRequestAuthorization = true
}
Expand All @@ -18,12 +18,12 @@ fileprivate class MockLocationService: LocationServiceProtocol {
}
}


@available(iOS 13.0, *)
final class LocationWitnessTests: XCTestCase {
static var allTests = [
(
"observe:returnsMultipleLocationPayloads", testLocationWitness_observe_returnsMultipleLocationPayloads
"observe:returnsMultipleLocationPayloads",
testLocationWitness_observe_returnsMultipleLocationPayloads
)
]

Expand All @@ -32,7 +32,14 @@ final class LocationWitnessTests: XCTestCase {
let locationServiceMock = MockLocationService()
locationServiceMock.simulatedResult = .success(CLLocation(latitude: 1, longitude: 2))
let sut = LocationWitness(locationService: locationServiceMock)
let result = try await sut.observe()
XCTAssertEqual(result.count, 2)
let results = try await sut.observe()
XCTAssertEqual(results.count, 2)
let locationPayload = try XCTUnwrap(
results.compactMap { $0 as? LocationPayload }.first, "Missing location payload.")
XCTAssertEqual(locationPayload.schema, "network.xyo.location")
let iosLocationPayload = try XCTUnwrap(
results.compactMap { $0 as? IosLocationPayload }.first, "Missing iOS location payload.")
XCTAssertEqual(iosLocationPayload.schema, "network.xyo.location.ios")

}
}

0 comments on commit 937b820

Please sign in to comment.