Skip to content

Commit

Permalink
add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cbaker6 committed Mar 3, 2025
1 parent ada038c commit 4257a8b
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 3 deletions.
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())
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())!
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

0 comments on commit 4257a8b

Please sign in to comment.