Skip to content

Commit 66ec0dc

Browse files
committed
add test cases
1 parent 012e91a commit 66ec0dc

File tree

4 files changed

+92
-32
lines changed

4 files changed

+92
-32
lines changed

Sources/ParseCareKit/Models/PCKClock.swift

+19-21
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public struct PCKClock: ParseObject {
2727

2828
public var originalData: Data?
2929

30-
var uuid: UUID?
30+
public var uuid: UUID?
3131

32-
var vector: String?
32+
public var vector: String?
3333

3434
public init() { }
3535

@@ -46,19 +46,22 @@ public struct PCKClock: ParseObject {
4646
return updated
4747
}
4848

49-
func decodeClock(completion: @escaping(OCKRevisionRecord.KnowledgeVector?) -> Void) {
49+
func decodeClock() throws -> OCKRevisionRecord.KnowledgeVector {
5050
guard let data = self.vector?.data(using: .utf8) else {
51+
let errorString = "Could not get data as utf8"
5152
if #available(iOS 14.0, watchOS 7.0, *) {
52-
Logger.clock.error("Error in Clock. Couldn't get data as utf8")
53+
Logger.clock.error("\(errorString)")
5354
} else {
54-
os_log("Error in Clock. Couldn't get data as utf8", log: .clock, type: .error)
55+
os_log("Could not get data as utf8", log: .clock, type: .error)
5556
}
56-
return
57+
throw ParseCareKitError.errorString(errorString)
5758
}
5859

59-
let cloudVector: OCKRevisionRecord.KnowledgeVector?
6060
do {
61-
cloudVector = try JSONDecoder().decode(OCKRevisionRecord.KnowledgeVector.self, from: data)
61+
// swiftlint:disable:next line_length
62+
let cloudVector: OCKRevisionRecord.KnowledgeVector = try JSONDecoder().decode(OCKRevisionRecord.KnowledgeVector.self,
63+
from: data)
64+
return cloudVector
6265
} catch {
6366
if #available(iOS 14.0, watchOS 7.0, *) {
6467
// swiftlint:disable:next line_length
@@ -67,9 +70,8 @@ public struct PCKClock: ParseObject {
6770
os_log("Clock.decodeClock(): %{private}@. Vector %{private}@.",
6871
log: .clock, type: .error, error.localizedDescription, data.debugDescription)
6972
}
70-
cloudVector = nil
73+
throw ParseCareKitError.errorString("Clock.decodeClock(): \(error.localizedDescription)")
7174
}
72-
completion(cloudVector)
7375
}
7476

7577
func encodeClock(_ clock: OCKRevisionRecord.KnowledgeVector) -> Self? {
@@ -154,9 +156,7 @@ public struct PCKClock: ParseObject {
154156
switch result {
155157

156158
case .success(let foundVector):
157-
foundVector.decodeClock { possiblyDecoded in
158-
completion(foundVector, possiblyDecoded, nil)
159-
}
159+
completion(foundVector, try? foundVector.decodeClock(), nil)
160160
case .failure(let error):
161161
if !createNewIfNeeded {
162162
completion(nil, nil, error)
@@ -166,14 +166,12 @@ public struct PCKClock: ParseObject {
166166
Task {
167167
do {
168168
let updatedVector = try await newVector.setupACLWithRoles()
169-
updatedVector.decodeClock { possiblyDecoded in
170-
updatedVector.create(callbackQueue: ParseRemote.queue) { result in
171-
switch result {
172-
case .success(let savedVector):
173-
completion(savedVector, possiblyDecoded, nil)
174-
case .failure(let error):
175-
completion(nil, nil, error)
176-
}
169+
updatedVector.create(callbackQueue: ParseRemote.queue) { result in
170+
switch result {
171+
case .success(let savedVector):
172+
completion(savedVector, try? updatedVector.decodeClock(), nil)
173+
case .failure(let error):
174+
completion(nil, nil, error)
177175
}
178176
}
179177
} catch {

Sources/ParseCareKit/Models/PCKEntity.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ public enum PCKEntity: Equatable, Codable {
9191
var container = encoder.container(keyedBy: Keys.self)
9292
try container.encode(entityType, forKey: .type)
9393
switch self {
94-
case let .patient(patient): try container.encode(patient, forKey: .object)
95-
case let .carePlan(plan): try container.encode(plan, forKey: .object)
96-
case let .contact(contact): try container.encode(contact, forKey: .object)
97-
case let .task(task): try container.encode(task, forKey: .object)
98-
case let .healthKitTask(task): try container.encode(task, forKey: .object)
99-
case let .outcome(outcome): try container.encode(outcome, forKey: .object)
94+
case let .patient(patient): try container.encode(try patient.toPointer(), forKey: .object)
95+
case let .carePlan(plan): try container.encode(try plan.toPointer(), forKey: .object)
96+
case let .contact(contact): try container.encode(try contact.toPointer(), forKey: .object)
97+
case let .task(task): try container.encode(try task.toPointer(), forKey: .object)
98+
case let .healthKitTask(task): try container.encode(try task.toPointer(), forKey: .object)
99+
case let .outcome(outcome): try container.encode(try outcome.toPointer(), forKey: .object)
100100
}
101101
}
102102

Sources/ParseCareKit/Models/PCKRevisionRecord.swift

+3-5
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct PCKRevisionRecord: ParseObject, Equatable, Codable {
4242

4343
/// A knowledge vector indicating the last known state of each other device
4444
/// by the device that authored this revision record.
45-
let knowledgeVector: PCKKnowledgeVector?
45+
var knowledgeVector: PCKKnowledgeVector?
4646

4747
var objects: [any PCKVersionable] {
4848
guard let entities = entities else {
@@ -197,9 +197,6 @@ struct PCKRevisionRecord: ParseObject, Equatable, Codable {
197197
}
198198

199199
extension PCKRevisionRecord {
200-
init() {
201-
self.knowledgeVector = nil
202-
}
203200

204201
init(from decoder: Decoder) throws {
205202
let container = try decoder.container(keyedBy: CodingKeys.self)
@@ -208,7 +205,7 @@ extension PCKRevisionRecord {
208205
self.updatedAt = try container.decodeIfPresent(Date.self, forKey: .updatedAt)
209206
self.ACL = try container.decodeIfPresent(ParseACL.self, forKey: .ACL)
210207
self.knowledgeVector = try container.decodeIfPresent(PCKKnowledgeVector.self, forKey: .knowledgeVector)
211-
self.entities = try container.decodeIfPresent([PCKEntity].self, forKey: .objectId)
208+
self.entities = try container.decodeIfPresent([PCKEntity].self, forKey: .entities)
212209
self.clock = try container.decodeIfPresent(PCKClock.self, forKey: .clock)
213210
self.logicalClock = try container.decodeIfPresent(Int.self, forKey: .logicalClock)
214211
self.clockUUID = try container.decodeIfPresent(UUID.self, forKey: .clockUUID)
@@ -222,6 +219,7 @@ extension PCKRevisionRecord {
222219
try container.encodeIfPresent(updatedAt, forKey: .updatedAt)
223220
try container.encodeIfPresent(ACL, forKey: .ACL)
224221
try container.encodeIfPresent(knowledgeVector, forKey: .knowledgeVector)
222+
try container.encodeIfPresent(entities, forKey: .entities)
225223
try container.encodeIfPresent(clock, forKey: .clock)
226224
try container.encodeIfPresent(logicalClock, forKey: .logicalClock)
227225
try container.encodeIfPresent(clockUUID, forKey: .clockUUID)

Tests/ParseCareKitTests/EncodingCareKitTests.swift

+64
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,70 @@ class ParseCareKitTests: XCTestCase {
12591259
XCTAssertTrue(acl2.getWriteAccess(objectId: objectId))
12601260
}
12611261

1262+
func testRevisionRecord() async throws {
1263+
var careKit = OCKPatient(id: "myId", givenName: "hello", familyName: "world")
1264+
careKit.sex = .female
1265+
careKit = try await store.addPatient(careKit)
1266+
let entity = OCKEntity.patient(careKit)
1267+
let remoteUUID = UUID()
1268+
let clock = PCKClock(uuid: remoteUUID)
1269+
let clockVector = try clock.decodeClock()
1270+
let logicalClockValue = clockVector.clock(for: remoteUUID)
1271+
let careKitRecord = OCKRevisionRecord(entities: [entity], knowledgeVector: clockVector)
1272+
1273+
// Test PCKRevisionRecord <-> OCKRevisionRecord
1274+
let parseRecord = try PCKRevisionRecord(record: careKitRecord,
1275+
remoteClockUUID: remoteUUID,
1276+
remoteClock: clock,
1277+
remoteClockValue: logicalClockValue)
1278+
XCTAssertEqual(parseRecord.clockUUID, remoteUUID)
1279+
XCTAssertEqual(parseRecord.clock, clock)
1280+
XCTAssertEqual(parseRecord.logicalClock, logicalClockValue)
1281+
let decodedParseRecoded = try parseRecord.convertToCareKit()
1282+
XCTAssertEqual(decodedParseRecoded.knowledgeVector, careKitRecord.knowledgeVector)
1283+
XCTAssertEqual(decodedParseRecoded.entities.count, careKitRecord.entities.count)
1284+
guard let decodedPatient = decodedParseRecoded.entities.first else {
1285+
XCTFail("Should have unwrapped")
1286+
return
1287+
}
1288+
switch decodedPatient {
1289+
case .patient(let patient):
1290+
XCTAssertEqual(patient.id, careKit.id)
1291+
XCTAssertEqual(patient.name, careKit.name)
1292+
XCTAssertEqual(patient.sex, careKit.sex)
1293+
default:
1294+
XCTFail("Should have been patient")
1295+
}
1296+
1297+
// Test PCKRevisionRecord encoding/decoding to Parse Server
1298+
let encoded = try ParseCoding.jsonEncoder().encode(parseRecord)
1299+
let decoded = try ParseCoding.jsonDecoder().decode(PCKRevisionRecord.self, from: encoded)
1300+
guard let decodedKnowledgeVector = decoded.knowledgeVector,
1301+
let decodedEntities = decoded.entities else {
1302+
XCTFail("Should have unwrapped")
1303+
return
1304+
}
1305+
XCTAssertEqual(try decodedKnowledgeVector.currentVector(), careKitRecord.knowledgeVector)
1306+
XCTAssertEqual(decodedEntities.count, careKitRecord.entities.count)
1307+
guard let decodedPatient2 = decodedEntities.first else {
1308+
XCTFail("Should have unwrapped")
1309+
return
1310+
}
1311+
switch decodedPatient2 {
1312+
case .patient(let patient):
1313+
// Decoded PCKPatient comes back as a ParsePointer that needs to be fetched to hydrate
1314+
XCTAssertEqual(patient.uuid, careKit.uuid)
1315+
guard let uuidString = patient.objectId,
1316+
let uuid = UUID(uuidString: uuidString) else {
1317+
XCTFail("Should have unwrapped")
1318+
return
1319+
}
1320+
XCTAssertEqual(uuid, careKit.uuid)
1321+
1322+
default:
1323+
XCTFail("Should have been patient")
1324+
}
1325+
}
12621326
/*
12631327
func testAddContact() async throws {
12641328
let contact = OCKContact(id: "test", givenName: "hello", familyName: "world", carePlanUUID: nil)

0 commit comments

Comments
 (0)