forked from LoopKit/LoopKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPumpManagerStatusTests.swift
321 lines (292 loc) · 10.3 KB
/
PumpManagerStatusTests.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//
// PumpManagerStatusTests.swift
// LoopKitTests
//
// Created by Darin Krauss on 5/4/20.
// Copyright © 2020 LoopKit Authors. All rights reserved.
//
import XCTest
import HealthKit
@testable import LoopKit
class PumpManagerStatusCodableTests: XCTestCase {
func testCodable() throws {
let device = HKDevice(name: "Acme Best Device",
manufacturer: "Acme",
model: "Best",
hardwareVersion: "0.1.2",
firmwareVersion: "1.2.3",
softwareVersion: "2.3.4",
localIdentifier: "Locally Identified",
udiDeviceIdentifier: "U0D1I2")
try assertPumpManagerStatusCodable(PumpManagerStatus(timeZone: TimeZone(identifier: "America/Los_Angeles")!,
device: device,
pumpBatteryChargeRemaining: 0.75,
basalDeliveryState: .active(dateFormatter.date(from: "2020-05-14T15:56:09Z")!),
bolusState: .noBolus,
insulinType: .novolog,
deliveryIsUncertain: true),
encodesJSON: """
{
"basalDeliveryState" : {
"active" : {
"at" : "2020-05-14T15:56:09Z"
}
},
"bolusState" : "noBolus",
"deliveryIsUncertain" : true,
"device" : {
"firmwareVersion" : "1.2.3",
"hardwareVersion" : "0.1.2",
"localIdentifier" : "Locally Identified",
"manufacturer" : "Acme",
"model" : "Best",
"name" : "Acme Best Device",
"softwareVersion" : "2.3.4",
"udiDeviceIdentifier" : "U0D1I2"
},
"insulinType" : 0,
"pumpBatteryChargeRemaining" : 0.75,
"timeZone" : {
"identifier" : "America/Los_Angeles"
}
}
"""
)
}
func testCodableRequiredOnly() throws {
let device = HKDevice(name: nil,
manufacturer: nil,
model: nil,
hardwareVersion: nil,
firmwareVersion: nil,
softwareVersion: nil,
localIdentifier: nil,
udiDeviceIdentifier: "U0D1I2")
try assertPumpManagerStatusCodable(PumpManagerStatus(timeZone: TimeZone(identifier: "America/Los_Angeles")!,
device: device,
pumpBatteryChargeRemaining: nil,
basalDeliveryState: nil,
bolusState: .noBolus,
insulinType: nil,
deliveryIsUncertain: true),
encodesJSON: """
{
"bolusState" : "noBolus",
"deliveryIsUncertain" : true,
"device" : {
"udiDeviceIdentifier" : "U0D1I2"
},
"timeZone" : {
"identifier" : "America/Los_Angeles"
}
}
"""
)
}
private func assertPumpManagerStatusCodable(_ original: PumpManagerStatus, encodesJSON string: String) throws {
let data = try encoder.encode(original)
XCTAssertEqual(String(data: data, encoding: .utf8), string)
let decoded = try decoder.decode(PumpManagerStatus.self, from: data)
XCTAssertEqual(decoded, original)
}
private let dateFormatter = ISO8601DateFormatter()
private let encoder: JSONEncoder = {
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
encoder.dateEncodingStrategy = .iso8601
return encoder
}()
private let decoder: JSONDecoder = {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
return decoder
}()
}
class PumpManagerStatusBasalDeliveryStateCodableTests: XCTestCase {
func testCodableActive() throws {
try assertPumpManagerStatusBasalDeliveryStateCodable(.active(dateFormatter.date(from: "2020-05-14T12:18:09Z")!), encodesJSON: """
{
"basalDeliveryState" : {
"active" : {
"at" : "2020-05-14T12:18:09Z"
}
}
}
"""
)
}
func testCodableInitiatingTempBasal() throws {
try assertPumpManagerStatusBasalDeliveryStateCodable(.initiatingTempBasal, encodesJSON: """
{
"basalDeliveryState" : "initiatingTempBasal"
}
"""
)
}
func testCodableTempBasal() throws {
let dose = DoseEntry(type: .tempBasal,
startDate: dateFormatter.date(from: "2020-05-14T13:13:14Z")!,
endDate: dateFormatter.date(from: "2020-05-14T13:43:14Z")!,
value: 1.25,
unit: .unitsPerHour,
deliveredUnits: 0.5,
description: "Temporary Basal",
syncIdentifier: "238E41EA-9576-4981-A1A4-51E10228584F",
scheduledBasalRate: HKQuantity(unit: DoseEntry.unitsPerHour, doubleValue: 1.0))
try assertPumpManagerStatusBasalDeliveryStateCodable(.tempBasal(dose), encodesJSON: """
{
"basalDeliveryState" : {
"tempBasal" : {
"dose" : {
"deliveredUnits" : 0.5,
"description" : "Temporary Basal",
"endDate" : "2020-05-14T13:43:14Z",
"scheduledBasalRate" : 1,
"scheduledBasalRateUnit" : "IU/hr",
"startDate" : "2020-05-14T13:13:14Z",
"syncIdentifier" : "238E41EA-9576-4981-A1A4-51E10228584F",
"type" : "tempBasal",
"unit" : "U/hour",
"value" : 1.25
}
}
}
}
"""
)
}
func testCodableCancelingTempBasal() throws {
try assertPumpManagerStatusBasalDeliveryStateCodable(.cancelingTempBasal, encodesJSON: """
{
"basalDeliveryState" : "cancelingTempBasal"
}
"""
)
}
func testCodableSuspending() throws {
try assertPumpManagerStatusBasalDeliveryStateCodable(.suspending, encodesJSON: """
{
"basalDeliveryState" : "suspending"
}
"""
)
}
func testCodableSuspended() throws {
try assertPumpManagerStatusBasalDeliveryStateCodable(.suspended(dateFormatter.date(from: "2020-05-14T22:38:19Z")!), encodesJSON: """
{
"basalDeliveryState" : {
"suspended" : {
"at" : "2020-05-14T22:38:19Z"
}
}
}
"""
)
}
func testCodableResuming() throws {
try assertPumpManagerStatusBasalDeliveryStateCodable(.resuming, encodesJSON: """
{
"basalDeliveryState" : "resuming"
}
"""
)
}
private func assertPumpManagerStatusBasalDeliveryStateCodable(_ original: PumpManagerStatus.BasalDeliveryState, encodesJSON string: String) throws {
let data = try encoder.encode(TestContainer(basalDeliveryState: original))
XCTAssertEqual(String(data: data, encoding: .utf8), string)
let decoded = try decoder.decode(TestContainer.self, from: data)
XCTAssertEqual(decoded.basalDeliveryState, original)
}
private let dateFormatter = ISO8601DateFormatter()
private let encoder: JSONEncoder = {
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
encoder.dateEncodingStrategy = .iso8601
return encoder
}()
private let decoder: JSONDecoder = {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
return decoder
}()
private struct TestContainer: Codable, Equatable {
let basalDeliveryState: PumpManagerStatus.BasalDeliveryState
}
}
class PumpManagerStatusBolusStateCodableTests: XCTestCase {
func testCodableNone() throws {
try assertPumpManagerStatusBolusStateCodable(.noBolus, encodesJSON: """
{
"bolusState" : "noBolus"
}
"""
)
}
func testCodableInitiating() throws {
try assertPumpManagerStatusBolusStateCodable(.initiating, encodesJSON: """
{
"bolusState" : "initiating"
}
"""
)
}
func testCodableInProgress() throws {
let dose = DoseEntry(type: .bolus,
startDate: dateFormatter.date(from: "2020-05-14T22:38:16Z")!,
value: 2.5,
unit: .units,
deliveredUnits: 1.0,
description: "Bolus",
syncIdentifier: "2A67A303-5203-4CB8-8123-79498265368E",
isMutable: true)
try assertPumpManagerStatusBolusStateCodable(.inProgress(dose), encodesJSON: """
{
"bolusState" : {
"inProgress" : {
"dose" : {
"deliveredUnits" : 1,
"description" : "Bolus",
"endDate" : "2020-05-14T22:38:16Z",
"isMutable" : true,
"startDate" : "2020-05-14T22:38:16Z",
"syncIdentifier" : "2A67A303-5203-4CB8-8123-79498265368E",
"type" : "bolus",
"unit" : "U",
"value" : 2.5
}
}
}
}
"""
)
}
func testCodableCancelling() throws {
try assertPumpManagerStatusBolusStateCodable(.canceling, encodesJSON: """
{
"bolusState" : "canceling"
}
"""
)
}
private func assertPumpManagerStatusBolusStateCodable(_ original: PumpManagerStatus.BolusState, encodesJSON string: String) throws {
let data = try encoder.encode(TestContainer(bolusState: original))
XCTAssertEqual(String(data: data, encoding: .utf8), string)
let decoded = try decoder.decode(TestContainer.self, from: data)
XCTAssertEqual(decoded.bolusState, original)
}
private let dateFormatter = ISO8601DateFormatter()
private let encoder: JSONEncoder = {
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
encoder.dateEncodingStrategy = .iso8601
return encoder
}()
private let decoder: JSONDecoder = {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
return decoder
}()
private struct TestContainer: Codable, Equatable {
let bolusState: PumpManagerStatus.BolusState
}
}