Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public protocol OCKAnyTaskStore: OCKAnyReadOnlyTaskStore {

public extension OCKAnyReadOnlyTaskStore {
func fetchAnyTask(withID id: String, callbackQueue: DispatchQueue = .main, completion: @escaping OCKResultClosure<OCKAnyTask>) {
var query = OCKTaskQuery(id: id)
var query = OCKTaskQuery(for: Date())

@cbaker6 cbaker6 Mar 3, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched this so it's aligned with the other Entity fetches. It was setting the id twice instead of querying the current version for today. Without the change, it's possible for this fetch to return a future version of the task since it would have a future effectiveDate.

query.sortDescriptors = [.effectiveDate(ascending: false)]
query.ids = [id]
query.limit = 1
Expand Down
24 changes: 24 additions & 0 deletions CareKitStore/CareKitStoreTests/OCKStore/TestStore+CarePlans.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,30 @@ class TestStoreCarePlans: XCTestCase {
XCTAssertEqual(fetched?.previousVersionUUIDs.first, versionA.uuid)
}

func testCarePlanQueryByIdConvenienceMethodReturnsLatestVersionOfACarePlan() throws {
let versionA = try store.addCarePlanAndWait(OCKCarePlan(id: "A", title: "Amy", patientUUID: nil))
let versionB = try store.updateCarePlanAndWait(OCKCarePlan(id: "A", title: "Jared", patientUUID: nil))
let expect = expectation(description: "Fetches versionB")
store.fetchCarePlan(withID: versionA.id) { result in
let fetched = try? result.get()
XCTAssertEqual(fetched?.title, versionB.title)
expect.fulfill()
}
waitForExpectations(timeout: 0.1, handler: nil)
}

func testAnyCarePlanQueryByIdConvenienceMethodReturnsLatestVersionOfACarePlan() throws {
let versionA = try store.addCarePlanAndWait(OCKCarePlan(id: "A", title: "Amy", patientUUID: nil))
let versionB = try store.updateCarePlanAndWait(OCKCarePlan(id: "A", title: "Jared", patientUUID: nil))
let expect = expectation(description: "Fetches versionB")
store.fetchAnyCarePlan(withID: versionA.id) { result in
let fetched = try? result.get()
XCTAssertEqual(fetched?.title, versionB.title)
expect.fulfill()
}
waitForExpectations(timeout: 0.1, handler: nil)
}

func testCarePlanQueryOnPastDateReturnsPastVersionOfACarePlan() throws {
let dateA = Date().addingTimeInterval(-100)
var versionA = OCKCarePlan(id: "A", title: "a", patientUUID: nil)
Expand Down
26 changes: 26 additions & 0 deletions CareKitStore/CareKitStoreTests/OCKStore/TestStore+Contacts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,32 @@ class TestStoreContacts: XCTestCase {
XCTAssertEqual(fetched?.name, versionD.name)
}

func testContactQueryByIdConvenienceMethodReturnsLatestVersionOfAContact() throws {
let versionA = try store.addContactAndWait(OCKContact(id: "contact", givenName: "A", familyName: "", carePlanUUID: nil))
let versionB = try store.updateContactAndWait(OCKContact(id: "contact", givenName: "B", familyName: "", carePlanUUID: nil))

let expect = expectation(description: "Fetches versionB")
store.fetchContact(withID: versionA.id) { result in
let fetched = try? result.get()
XCTAssertEqual(fetched?.name.givenName, versionB.name.givenName)
expect.fulfill()
}
waitForExpectations(timeout: 0.1, handler: nil)
}

func testAnyContactQueryByIdConvenienceMethodReturnsLatestVersionOfAContact() throws {
let versionA = try store.addContactAndWait(OCKContact(id: "contact", givenName: "A", familyName: "", carePlanUUID: nil))
let versionB = try store.updateContactAndWait(OCKContact(id: "contact", givenName: "B", familyName: "", carePlanUUID: nil))

let expect = expectation(description: "Fetches versionB")
store.fetchAnyContact(withID: versionA.id) { result in
let fetched = try? result.get()
XCTAssertEqual(fetched?.name.givenName, versionB.name.givenName)
expect.fulfill()
}
waitForExpectations(timeout: 0.1, handler: nil)
}

func testContactQueryWithDateOnlyReturnsLatestVersionOfAContact() throws {
try store.addContactAndWait(OCKContact(id: "contact", givenName: "A", familyName: "", carePlanUUID: nil))
try store.updateContactAndWait(OCKContact(id: "contact", givenName: "B", familyName: "", carePlanUUID: nil))
Expand Down
24 changes: 24 additions & 0 deletions CareKitStore/CareKitStoreTests/OCKStore/TestStore+Patients.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,30 @@ class TestStorePatients: XCTestCase {
XCTAssertEqual(fetched, versionB)
}

func testPatientQueryByIdConvenienceMethodReturnsLatestVersionOfAPatient() throws {
let versionA = try store.addPatientAndWait(OCKPatient(id: "A", givenName: "Jared", familyName: "Gosler"))
let versionB = try store.updatePatientAndWait(OCKPatient(id: "A", givenName: "John", familyName: "Appleseed"))
let expect = expectation(description: "Fetches versionB")
store.fetchPatient(withID: versionA.id) { result in
let fetched = try? result.get()
XCTAssertEqual(fetched?.name.familyName, versionB.name.familyName)
expect.fulfill()
}
waitForExpectations(timeout: 0.1, handler: nil)
}

func testAnyPatientQueryByIdConvenienceMethodReturnsLatestVersionOfAPatient() throws {
let versionA = try store.addPatientAndWait(OCKPatient(id: "A", givenName: "Jared", familyName: "Gosler"))
let versionB = try store.updatePatientAndWait(OCKPatient(id: "A", givenName: "John", familyName: "Appleseed"))
let expect = expectation(description: "Fetches versionB")
store.fetchAnyPatient(withID: versionA.id) { result in
let fetched = try? result.get()
XCTAssertEqual(fetched?.name.familyName, versionB.name.familyName)
expect.fulfill()
}
waitForExpectations(timeout: 0.1, handler: nil)
}

func testPatientQueryOnPastDateReturnsPastVersionOfAPatient() throws {
let dateA = Date().addingTimeInterval(-100)
var versionA = OCKPatient(id: "A", givenName: "Jared", familyName: "Gosler")
Expand Down
25 changes: 23 additions & 2 deletions CareKitStore/CareKitStoreTests/OCKStore/TestStore+Tasks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,34 @@ class TestStoreTasks: XCTestCase {

var taskV2 = taskV1
taskV2.title = "V2"
taskV2.effectiveDate = Calendar.current.date(byAdding: .year, value: 1, to: Date())!

@cbaker6 cbaker6 Mar 3, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adjusted this test based on #723 (comment). This effectiveDate essentially could have been next year and it would have returned next years task today, which would be unexpected

taskV2 = try store.updateTaskAndWait(taskV2)

let expect = expectation(description: "Fetches V2")
store.fetchTask(withID: "task") { result in
let task = try? result.get()
XCTAssertEqual(task?.title, taskV2.title)
expect.fulfill()
}
waitForExpectations(timeout: 0.1, handler: nil)
}

func testFetchAnyTaskByIdConvenienceMethodReturnsNewestVersionOfTask() throws {
let coordinator = OCKStoreCoordinator()
let store = OCKStore(name: "test", type: .inMemory)
coordinator.attach(store: store)

let schedule = OCKSchedule.dailyAtTime(hour: 0, minutes: 0, start: Date(), end: nil, text: nil)
var taskV1 = OCKTask(id: "task", title: "V1", carePlanUUID: nil, schedule: schedule)
taskV1 = try store.addTaskAndWait(taskV1)

var taskV2 = taskV1
taskV2.title = "V2"
taskV2 = try store.updateTaskAndWait(taskV2)

let expect = expectation(description: "Fetches V2")
store.fetchAnyTask(withID: "task") { result in
let task = try? result.get()
XCTAssertEqual(task?.title, "V2")
XCTAssertEqual(task?.title, taskV2.title)
expect.fulfill()
}
waitForExpectations(timeout: 0.1, handler: nil)
Expand Down