-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathManualEntryDoseViewModelTests.swift
More file actions
126 lines (96 loc) · 4.31 KB
/
ManualEntryDoseViewModelTests.swift
File metadata and controls
126 lines (96 loc) · 4.31 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
//
// ManualEntryDoseViewModelTests.swift
// LoopTests
//
// Created by Pete Schwamb on 1/2/21.
// Copyright © 2021 LoopKit Authors. All rights reserved.
//
import HealthKit
import LoopCore
import LoopKit
import XCTest
import LoopAlgorithm
@testable import Loop
@MainActor
class ManualEntryDoseViewModelTests: XCTestCase {
static let now = Date.distantFuture
var now: Date = BolusEntryViewModelTests.now
var manualEntryDoseViewModel: ManualEntryDoseViewModel!
static let exampleBolusQuantity = HKQuantity(unit: .internationalUnit(), doubleValue: 1.0)
static let noBolus = HKQuantity(unit: .internationalUnit(), doubleValue: 0.0)
fileprivate var delegate: MockManualEntryDoseViewModelDelegate!
static let mockUUID = UUID()
let mockUUID = ManualEntryDoseViewModelTests.mockUUID.uuidString
override func setUpWithError() throws {
now = Self.now
delegate = MockManualEntryDoseViewModelDelegate()
setUpViewModel()
}
func setUpViewModel() {
manualEntryDoseViewModel = ManualEntryDoseViewModel(delegate: delegate,
now: { self.now },
debounceIntervalMilliseconds: 0,
uuidProvider: { self.mockUUID },
timeZone: TimeZone(abbreviation: "GMT")!)
manualEntryDoseViewModel.authenticationHandler = { _ in return true }
}
func testDoseLogging() async throws {
XCTAssertEqual(.novolog, manualEntryDoseViewModel.selectedInsulinType)
manualEntryDoseViewModel.enteredBolus = Self.exampleBolusQuantity
try await manualEntryDoseViewModel.saveManualDose()
XCTAssertEqual(delegate.manualEntryBolusUnits, Self.exampleBolusQuantity.doubleValue(for: .internationalUnit()))
XCTAssertEqual(delegate.manuallyEnteredDoseInsulinType, .novolog)
}
func testDoseNotSavedIfNotAuthenticated() async throws {
XCTAssertEqual(.novolog, manualEntryDoseViewModel.selectedInsulinType)
manualEntryDoseViewModel.enteredBolus = Self.exampleBolusQuantity
manualEntryDoseViewModel.authenticationHandler = { _ in return false }
do {
try await manualEntryDoseViewModel.saveManualDose()
XCTFail("Saving should fail if not authenticated.")
} catch { }
XCTAssertNil(delegate.manualEntryBolusUnits)
XCTAssertNil(delegate.manuallyEnteredDoseInsulinType)
}
}
fileprivate class MockManualEntryDoseViewModelDelegate: ManualDoseViewModelDelegate {
func fetchData(for baseTime: Date, disablingPreMeal: Bool, ensureDosingCoverageStart: Date?) async throws -> Loop.StoredDataAlgorithmInput {
return StoredDataAlgorithmInput(
glucoseHistory: [],
doses: [],
carbEntries: [],
predictionStart: baseTime,
basal: [],
sensitivity: [],
carbRatio: [],
target: GlucoseRangeTimeline(),
suspendThreshold: nil,
maxBolus: 10.0,
maxBasalRate: 3.0,
useIntegralRetrospectiveCorrection: false,
includePositiveVelocityAndRC: false,
carbAbsorptionModel: .linear,
recommendationInsulinModel: ExponentialInsulinModelPreset.rapidActingAdult.model,
recommendationType: DoseRecommendationType.manualBolus,
automaticBolusApplicationFactor: nil
)
}
var pumpInsulinType: InsulinType?
var manualEntryBolusUnits: Double?
var manualEntryDoseStartDate: Date?
var manuallyEnteredDoseInsulinType: InsulinType?
func addManuallyEnteredDose(startDate: Date, units: Double, insulinType: InsulinType?) {
manualEntryBolusUnits = units
manualEntryDoseStartDate = startDate
manuallyEnteredDoseInsulinType = insulinType
}
func insulinActivityDuration(for type: InsulinType?) -> TimeInterval {
return InsulinMath.defaultInsulinActivityDuration
}
var algorithmDisplayState = AlgorithmDisplayState()
var settings = StoredSettings()
var scheduleOverride: TemporaryScheduleOverride?
func insulinModel(for type: LoopKit.InsulinType?) -> InsulinModel {
return ExponentialInsulinModelPreset.rapidActingAdult
}
}