-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from XYOracleNetwork/feature/location-payload-…
…serialization Location payload serialization
- Loading branch information
Showing
6 changed files
with
127 additions
and
18 deletions.
There are no files selected for viewing
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
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
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
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,13 @@ | ||
extension KeyedEncodingContainer { | ||
mutating func encodeIfValidNumeric<T>(_ value: T?, forKey key: KeyedEncodingContainer<K>.Key) throws | ||
where T: BinaryFloatingPoint & Encodable { | ||
if let value = value, !value.isNaN { | ||
try encode(value, forKey: key) | ||
} | ||
} | ||
mutating func encodeIfNotNil<T>(_ value: T?, forKey key: KeyedEncodingContainer<K>.Key) throws where T: Encodable { | ||
if let value = value { | ||
try encode(value, forKey: key) | ||
} | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
Tests/XyoClientTests/Witness/Location/Generic/LocationPayloadTests.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,96 @@ | ||
import XCTest | ||
import CoreLocation | ||
|
||
@testable import XyoClient | ||
|
||
class LocationPayloadTests: XCTestCase { | ||
struct CoordinatesStruct: Encodable { | ||
let accuracy: CLLocationAccuracy | ||
let altitude: CLLocationDistance | ||
let altitudeAccuracy: CLLocationDistance | ||
let heading: CLLocationDirection | ||
let latitude: CLLocationDegrees | ||
let longitude: CLLocationDegrees | ||
let speed: CLLocationSpeed | ||
} | ||
|
||
struct CurrentLocationStruct: Encodable { | ||
let coords: CoordinatesStruct | ||
let timestamp: Date | ||
} | ||
|
||
func testLocationPayloadEncoding() throws { | ||
// Arrange: Create a CLLocation instance and the corresponding LocationPayload | ||
let location = CLLocation( | ||
coordinate: CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194), | ||
altitude: 15.0, | ||
horizontalAccuracy: 5.0, | ||
verticalAccuracy: 3.0, | ||
course: 90.0, | ||
speed: 2.5, | ||
timestamp: Date(timeIntervalSince1970: 1609459200) // Jan 1, 2021 | ||
) | ||
let payload = LocationPayload(location) | ||
|
||
// Act: Encode the LocationPayload instance into JSON | ||
let encoder = JSONEncoder() | ||
encoder.outputFormatting = [.sortedKeys, .prettyPrinted] // Consistent output for tests | ||
let jsonData = try encoder.encode(payload) | ||
|
||
// Assert: Verify the serialized JSON matches expectations | ||
let jsonString = String(data: jsonData, encoding: .utf8)! | ||
let expectedJSON = """ | ||
{ | ||
"currentLocation" : { | ||
"coords" : { | ||
"accuracy" : 5, | ||
"altitude" : 15, | ||
"altitudeAccuracy" : 15, | ||
"heading" : 90, | ||
"latitude" : 37.7749, | ||
"longitude" : -122.4194, | ||
"speed" : 2.5 | ||
}, | ||
"timestamp" : 1609459200000 | ||
}, | ||
"schema" : "network.xyo.location" | ||
} | ||
""" | ||
XCTAssertEqual(jsonString, expectedJSON) | ||
} | ||
|
||
func testLocationPayloadEncodingHandlesNilValues() throws { | ||
// Arrange: Create a CLLocation instance with some properties unset | ||
let location = CLLocation( | ||
coordinate: CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0), | ||
altitude: CLLocationDistance.nan, | ||
horizontalAccuracy: CLLocationAccuracy.nan, | ||
verticalAccuracy: CLLocationAccuracy.nan, | ||
course: CLLocationDirection.nan, | ||
speed: CLLocationSpeed.nan, | ||
timestamp: Date(timeIntervalSince1970: 0) | ||
) | ||
let payload = LocationPayload(location) | ||
|
||
// Act: Encode the LocationPayload instance into JSON | ||
let encoder = JSONEncoder() | ||
encoder.outputFormatting = [.sortedKeys, .prettyPrinted] | ||
let jsonData = try encoder.encode(payload) | ||
|
||
// Assert: Verify the serialized JSON handles NaN values gracefully (e.g., omitted or replaced) | ||
let jsonString = String(data: jsonData, encoding: .utf8)! | ||
let expectedJSON = """ | ||
{ | ||
"currentLocation" : { | ||
"coords" : { | ||
"latitude" : 0, | ||
"longitude" : 0 | ||
}, | ||
"timestamp" : 0 | ||
}, | ||
"schema" : "network.xyo.location" | ||
} | ||
""" | ||
XCTAssertEqual(jsonString, expectedJSON) | ||
} | ||
} |
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