forked from carekit-apple/CareKit
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTestStore+BuildRevisions.swift
233 lines (176 loc) · 9.05 KB
/
TestStore+BuildRevisions.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
Copyright (c) 2020, Apple Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
3. Neither the name of the copyright holder(s) nor the names of any contributors
may be used to endorse or promote products derived from this software without
specific prior written permission. No license is granted to the trademarks of
the copyright holders even if such marks are included in this software.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
@testable import CareKitStore
import XCTest
class TestStoreBuildRevisions: XCTestCase {
private var store: OCKStore!
override func setUp() {
super.setUp()
let remote = DummyEndpoint()
remote.automaticallySynchronizes = false
store = OCKStore(
name: "TestDatabase",
type: .inMemory,
remote: remote)
}
override func tearDown() {
super.tearDown()
store = nil
}
func testEmptyStoreProducesEmptyRevision() {
let revision = store.computeRevision(since: store.context.clockTime)
XCTAssert(revision.entities.isEmpty)
}
// MARK: Tasks
func testAddingTaskCreatesRevisionRecord() throws {
let schedule = OCKSchedule.mealTimesEachDay(start: Date(), end: nil)
let task = OCKTask(id: "a", title: nil, carePlanUUID: nil, schedule: schedule)
try store.addTaskAndWait(task)
let revision = store.computeRevision(since: 0)
XCTAssert(revision.entities.count == 1)
XCTAssert(revision.entities.first?.entityType == .task)
}
func testUpdatingTaskCreatesRevisionRecord() throws {
let schedule = OCKSchedule.mealTimesEachDay(start: Date(), end: nil)
var task = OCKTask(id: "a", title: nil, carePlanUUID: nil, schedule: schedule)
try store.addTaskAndWait(task)
task.title = "Updated"
try store.updateTaskAndWait(task)
let revision = store.computeRevision(since: 0)
XCTAssert(revision.entities.count == 2)
XCTAssert(revision.entities.first?.entityType == .task)
}
func testRevisionForDeletingTask() throws {
let schedule = OCKSchedule.dailyAtTime(hour: 12, minutes: 0, start: Date(), end: nil, text: nil)
let taskA1 = OCKTask(id: "A", title: "A1", carePlanUUID: nil, schedule: schedule)
try store.addTaskAndWait(taskA1)
let taskA2 = OCKTask(id: "A", title: "A2", carePlanUUID: nil, schedule: schedule)
try store.updateTaskAndWait(taskA2)
store.context.knowledgeVector.increment(clockFor: store.context.clockID)
try store.deleteTasksAndWait([taskA2])
let revision = store.computeRevision(since: store.context.clockTime)
XCTAssert(revision.entities.count == 1)
XCTAssert(revision.entities.first?.deletedDate != nil)
}
// MARK: Outcomes
func testAddingOutcomeCreatesRevisionRecord() throws {
let schedule = OCKSchedule.mealTimesEachDay(start: Date(), end: nil)
var task = OCKTask(id: "a", title: nil, carePlanUUID: nil, schedule: schedule)
task = try store.addTaskAndWait(task)
let outcome = OCKOutcome(taskUUID: try task.getUUID(), taskOccurrenceIndex: 0, values: [])
try store.addOutcomeAndWait(outcome)
let revision = store.computeRevision(since: 0)
XCTAssert(revision.entities.count == 2)
XCTAssert(revision.entities.last?.entityType == .outcome)
}
func testDeletingOutcomeCreatesRevisionRecords() throws {
let schedule = OCKSchedule.mealTimesEachDay(start: Date(), end: nil)
var task = OCKTask(id: "a", title: nil, carePlanUUID: nil, schedule: schedule)
task = try store.addTaskAndWait(task)
let outcome = OCKOutcome(taskUUID: try task.getUUID(), taskOccurrenceIndex: 0, values: [])
try store.addOutcomeAndWait(outcome)
try store.deleteOutcomeAndWait(outcome)
let revision = store.computeRevision(since: 0)
// Tombstones are sorted first in the revision record, which is
// why we expect the first entity to be the deleted outcome.
XCTAssert(revision.entities.first?.entityType == .outcome)
XCTAssert(revision.entities.count == 3)
}
// MARK: Patients
func testAddingPatientCreatesRevisionRecord() throws {
let patient = OCKPatient(id: "id1", givenName: "Amy", familyName: "Frost")
try store.addPatientAndWait(patient)
let revision = store.computeRevision(since: 0)
XCTAssert(revision.entities.count == 1)
XCTAssert(revision.entities.first?.entityType == .patient)
}
func testUpdatingPatientCreatesRevisionRecord() throws {
var patient = OCKPatient(id: "id1", givenName: "Amy", familyName: "Frost")
try store.addPatientAndWait(patient)
patient.asset = "Updated"
try store.updatePatientAndWait(patient)
let revision = store.computeRevision(since: 0)
XCTAssert(revision.entities.count == 2)
XCTAssert(revision.entities.first?.entityType == .patient)
}
func testRevisionForDeletingPatient() throws {
let patient = OCKPatient(id: "id1", givenName: "Amy", familyName: "Frost")
try store.addPatientAndWait(patient)
try store.deletePatientsAndWait([patient])
let revision = store.computeRevision(since: 0)
XCTAssert(revision.entities.count == 2)
XCTAssert(revision.entities.first?.deletedDate != nil)
}
// MARK: CarePlans
func testAddingCarePlanCreatesRevisionRecord() throws {
let plan = OCKCarePlan(id: "diabetes_type_1", title: "Diabetes Care Plan", patientUUID: nil)
try store.addCarePlanAndWait(plan)
let revision = store.computeRevision(since: 0)
XCTAssert(revision.entities.count == 1)
XCTAssert(revision.entities.first?.entityType == .carePlan)
}
func testUpdatingCarePlanCreatesRevisionRecord() throws {
var plan = OCKCarePlan(id: "diabetes_type_1", title: "Diabetes Care Plan", patientUUID: nil)
try store.addCarePlanAndWait(plan)
plan.title = "Updated"
try store.updateCarePlanAndWait(plan)
let revision = store.computeRevision(since: 0)
XCTAssert(revision.entities.count == 2)
XCTAssert(revision.entities.first?.entityType == .carePlan)
}
func testRevisionForDeletingCarePlan() throws {
let plan = OCKCarePlan(id: "diabetes_type_1", title: "Diabetes Care Plan", patientUUID: nil)
try store.addCarePlanAndWait(plan)
try store.deleteCarePlansAndWait([plan])
let revision = store.computeRevision(since: 0)
XCTAssert(revision.entities.count == 2)
XCTAssert(revision.entities.first?.deletedDate != nil)
}
// MARK: Contact
func testAddingContactCreatesRevisionRecord() throws {
let contact = OCKContact(id: "contact", givenName: "Amy", familyName: "Frost", carePlanUUID: nil)
try store.addContactAndWait(contact)
let revision = store.computeRevision(since: 0)
XCTAssert(revision.entities.count == 1)
XCTAssert(revision.entities.first?.entityType == .contact)
}
func testUpdatingContactCreatesRevisionRecord() throws {
var contact = OCKContact(id: "contact", givenName: "Amy", familyName: "Frost", carePlanUUID: nil)
try store.addContactAndWait(contact)
contact.organization = "Updated"
try store.updateContactAndWait(contact)
let revision = store.computeRevision(since: 0)
XCTAssert(revision.entities.count == 2)
XCTAssert(revision.entities.first?.entityType == .contact)
}
func testRevisionForDeletingContact() throws {
let contact = OCKContact(id: "contact", givenName: "Amy", familyName: "Frost", carePlanUUID: nil)
try store.addContactAndWait(contact)
try store.deleteContactsAndWait([contact])
let revision = store.computeRevision(since: 0)
XCTAssert(revision.entities.count == 2)
XCTAssert(revision.entities.first?.deletedDate != nil)
}
}