-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathLoopAlgorithmTests.swift
More file actions
76 lines (57 loc) · 3 KB
/
LoopAlgorithmTests.swift
File metadata and controls
76 lines (57 loc) · 3 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
//
// LoopAlgorithmTests.swift
// LoopTests
//
// Created by Pete Schwamb on 8/17/23.
// Copyright © 2023 LoopKit Authors. All rights reserved.
//
import XCTest
import LoopKit
import LoopCore
import HealthKit
final class LoopAlgorithmTests: XCTestCase {
override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
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)!
}
func loadPredictedGlucoseFixture(_ name: String) -> [PredictedGlucoseValue] {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
let url = bundle.url(forResource: name, withExtension: "json")!
return try! decoder.decode([PredictedGlucoseValue].self, from: try! Data(contentsOf: url))
}
// SKIPPED: ISF end dates do not capture the entire range, so this test fails when using LoopKit accurate insulin effects
func skip_testLiveCaptureWithFunctionalAlgorithm() throws {
// This matches the "testForecastFromLiveCaptureInputData" test of LoopDataManagerDosingTests,
// Using the same input data, but generating the forecast using the LoopAlgorithm.generatePrediction()
// function.
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
let url = bundle.url(forResource: "live_capture_input", withExtension: "json")!
let predictionInput = try! decoder.decode(LoopPredictionInput.self, from: try! Data(contentsOf: url))
let prediction = try LoopAlgorithm.generatePrediction(input: predictionInput)
let expectedPredictedGlucose = loadPredictedGlucoseFixture("live_capture_predicted_glucose")
XCTAssertEqual(expectedPredictedGlucose.count, prediction.glucose.count)
let defaultAccuracy = 1.0 / 40.0
for (expected, calculated) in zip(expectedPredictedGlucose, prediction.glucose) {
XCTAssertEqual(expected.startDate, calculated.startDate)
XCTAssertEqual(expected.quantity.doubleValue(for: .milligramsPerDeciliter), calculated.quantity.doubleValue(for: .milligramsPerDeciliter), accuracy: defaultAccuracy)
}
}
}