forked from LoopKit/Loop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoopDataManagerTests.swift
More file actions
218 lines (188 loc) · 7.95 KB
/
LoopDataManagerTests.swift
File metadata and controls
218 lines (188 loc) · 7.95 KB
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
//
// LoopDataManagerTests.swift
// LoopTests
//
// Created by Anna Quinlan on 8/4/20.
// Copyright © 2020 LoopKit Authors. All rights reserved.
//
import XCTest
import HealthKit
import LoopKit
@testable import LoopCore
@testable import Loop
public typealias JSONDictionary = [String: Any]
enum DosingTestScenario {
case liveCapture // Includes actual dosing history, bg history, etc.
case flatAndStable
case highAndStable
case highAndRisingWithCOB
case lowAndFallingWithCOB
case lowWithLowTreatment
case highAndFalling
var fixturePrefix: String {
switch self {
case .liveCapture:
return "live_capture_"
case .flatAndStable:
return "flat_and_stable_"
case .highAndStable:
return "high_and_stable_"
case .highAndRisingWithCOB:
return "high_rising_with_cob_"
case .lowAndFallingWithCOB:
return "low_and_falling_with_cob_"
case .lowWithLowTreatment:
return "low_with_low_treatment_"
case .highAndFalling:
return "high_and_falling_"
}
}
static let localDateFormatter = ISO8601DateFormatter.localTimeDate()
static var dateFormatter: ISO8601DateFormatter = {
let dateFormatter = ISO8601DateFormatter()
dateFormatter.formatOptions = [.withInternetDateTime]
return dateFormatter
}()
var currentDate: Date {
switch self {
case .liveCapture:
return Self.dateFormatter.date(from: "2023-07-29T19:21:00Z")!
case .flatAndStable:
return Self.localDateFormatter.date(from: "2020-08-11T20:45:02")!
case .highAndStable:
return Self.localDateFormatter.date(from: "2020-08-12T12:39:22")!
case .highAndRisingWithCOB:
return Self.localDateFormatter.date(from: "2020-08-11T21:48:17")!
case .lowAndFallingWithCOB:
return Self.localDateFormatter.date(from: "2020-08-11T22:06:06")!
case .lowWithLowTreatment:
return Self.localDateFormatter.date(from: "2020-08-11T22:23:55")!
case .highAndFalling:
return Self.localDateFormatter.date(from: "2020-08-11T22:59:45")!
}
}
}
extension TimeZone {
static var fixtureTimeZone: TimeZone {
return TimeZone(secondsFromGMT: 25200)!
}
static var utcTimeZone: TimeZone {
return TimeZone(secondsFromGMT: 0)!
}
}
extension ISO8601DateFormatter {
static func localTimeDate(timeZone: TimeZone = .fixtureTimeZone) -> Self {
let formatter = self.init()
formatter.formatOptions = .withInternetDateTime
formatter.formatOptions.subtract(.withTimeZone)
formatter.timeZone = timeZone
return formatter
}
}
class LoopDataManagerTests: XCTestCase {
// MARK: Constants for testing
let retrospectiveCorrectionEffectDuration = TimeInterval(hours: 1)
let retrospectiveCorrectionGroupingInterval = 1.01
let retrospectiveCorrectionGroupingIntervalMultiplier = 1.01
let inputDataRecencyInterval = TimeInterval(minutes: 15)
let dateFormatter = ISO8601DateFormatter.localTimeDate()
let defaultAccuracy = 1.0 / 40.0
var suspendThreshold: GlucoseThreshold {
return GlucoseThreshold(unit: HKUnit.milligramsPerDeciliter, value: 75)
}
var adultExponentialInsulinModel: InsulinModel = ExponentialInsulinModel(actionDuration: 21600.0, peakActivityTime: 4500.0)
var glucoseTargetRangeSchedule: GlucoseRangeSchedule {
return GlucoseRangeSchedule(unit: HKUnit.milligramsPerDeciliter, dailyItems: [
RepeatingScheduleValue(startTime: TimeInterval(0), value: DoubleRange(minValue: 100, maxValue: 110)),
RepeatingScheduleValue(startTime: TimeInterval(28800), value: DoubleRange(minValue: 90, maxValue: 100)),
RepeatingScheduleValue(startTime: TimeInterval(75600), value: DoubleRange(minValue: 100, maxValue: 110))
], timeZone: .utcTimeZone)!
}
// MARK: Mock stores
var now: Date!
var dosingDecisionStore: MockDosingDecisionStore!
var automaticDosingStatus: AutomaticDosingStatus!
var loopDataManager: LoopDataManager!
func setUp(for test: DosingTestScenario,
basalDeliveryState: PumpManagerStatus.BasalDeliveryState? = nil,
maxBolus: Double = 10,
maxBasalRate: Double = 5.0,
dosingStrategy: AutomaticDosingStrategy = .tempBasalOnly)
{
let basalRateSchedule = loadBasalRateScheduleFixture("basal_profile")
let insulinSensitivitySchedule = InsulinSensitivitySchedule(
unit: .milligramsPerDeciliter,
dailyItems: [
RepeatingScheduleValue(startTime: 0, value: 45),
RepeatingScheduleValue(startTime: 32400, value: 55)
],
timeZone: .utcTimeZone
)!
let carbRatioSchedule = CarbRatioSchedule(
unit: .gram(),
dailyItems: [
RepeatingScheduleValue(startTime: 0.0, value: 10.0),
],
timeZone: .utcTimeZone
)!
let settings = LoopSettings(
dosingEnabled: false,
glucoseTargetRangeSchedule: glucoseTargetRangeSchedule,
insulinSensitivitySchedule: insulinSensitivitySchedule,
basalRateSchedule: basalRateSchedule,
carbRatioSchedule: carbRatioSchedule,
maximumBasalRatePerHour: maxBasalRate,
maximumBolus: maxBolus,
suspendThreshold: suspendThreshold,
automaticDosingStrategy: dosingStrategy
)
let doseStore = MockDoseStore(for: test)
doseStore.basalProfile = basalRateSchedule
doseStore.basalProfileApplyingOverrideHistory = doseStore.basalProfile
doseStore.sensitivitySchedule = insulinSensitivitySchedule
let glucoseStore = MockGlucoseStore(for: test)
let carbStore = MockCarbStore(for: test)
carbStore.insulinSensitivityScheduleApplyingOverrideHistory = insulinSensitivitySchedule
carbStore.carbRatioSchedule = carbRatioSchedule
let currentDate = glucoseStore.latestGlucose!.startDate
now = currentDate
dosingDecisionStore = MockDosingDecisionStore()
automaticDosingStatus = AutomaticDosingStatus(automaticDosingEnabled: true, isAutomaticDosingAllowed: true)
loopDataManager = LoopDataManager(
lastLoopCompleted: currentDate,
basalDeliveryState: basalDeliveryState ?? .active(currentDate),
settings: settings,
overrideHistory: TemporaryScheduleOverrideHistory(),
analyticsServicesManager: AnalyticsServicesManager(),
localCacheDuration: .days(1),
doseStore: doseStore,
glucoseStore: glucoseStore,
carbStore: carbStore,
dosingDecisionStore: dosingDecisionStore,
latestStoredSettingsProvider: MockLatestStoredSettingsProvider(),
now: { currentDate },
pumpInsulinType: .novolog,
automaticDosingStatus: automaticDosingStatus,
trustedTimeOffset: { 0 }
)
}
override func tearDownWithError() throws {
loopDataManager = nil
}
}
extension LoopDataManagerTests {
public var bundle: Bundle {
return Bundle(for: type(of: self))
}
public func loadFixture<T>(_ resourceName: String) -> T {
let path = bundle.path(forResource: resourceName, ofType: "json")!
return try! JSONSerialization.jsonObject(with: Data(contentsOf: URL(fileURLWithPath: path)), options: []) as! T
}
func loadBasalRateScheduleFixture(_ resourceName: String) -> BasalRateSchedule {
let fixture: [JSONDictionary] = loadFixture(resourceName)
let items = fixture.map {
return RepeatingScheduleValue(startTime: TimeInterval(minutes: $0["minutes"] as! Double), value: $0["rate"] as! Double)
}
return BasalRateSchedule(dailyItems: items, timeZone: .utcTimeZone)!
}
}