Просмотр исходного кода

Get unit tests working in Swift

These unit tests match JS equivalents that you can find here:
  - https://github.com/kingst/trio-oref/blob/dev-fixes-for-swift-comparison/tests/determine-basal-low-eventual-glucose.test.js
Sam King 9 месяцев назад
Родитель
Сommit
f1e5b12577

+ 3 - 3
Trio/Sources/APS/OpenAPSSwift/DetermineBasal/DosingEngine.swift

@@ -412,7 +412,7 @@ enum DosingEngine {
             "Eventual BG \(convertGlucose(profile: profile, glucose: eventualGlucose)) < \(convertGlucose(profile: profile, glucose: minGlucose))"
 
         // if 5m or 30m avg BG is rising faster than expected delta
-        if minDelta > expectedDelta, minDelta > 0, carbsRequired != 0 {
+        if minDelta > expectedDelta, minDelta > 0, carbsRequired == 0 {
             if naiveEventualGlucose < 40 {
                 newDetermination.reason += ", naive_eventualBG < 40. "
                 let finalDetermination = try TempBasalFunctions.setTempBasal(
@@ -461,8 +461,8 @@ enum DosingEngine {
         let naiveInsulinRequired = min(0, (naiveEventualGlucose - targetGlucose) / adjustedSensitivity).jsRounded(scale: 2)
 
         if minDelta < 0, minDelta > expectedDelta {
-            let newInsulinReq = (insulinRequired * (minDelta / expectedDelta)).jsRounded(scale: 2)
-            insulinRequired = newInsulinReq
+            let newInsulinRequired = (insulinRequired * (minDelta / expectedDelta)).jsRounded(scale: 2)
+            insulinRequired = newInsulinRequired
         }
 
         var rate = basal + (2 * insulinRequired)

+ 32 - 41
TrioTests/OpenAPSSwiftTests/DetermineBasalLowEventualGlucoseTests.swift

@@ -250,7 +250,7 @@ import Testing
         let (shouldSet, determination) = try callHandleLowEventualGlucose(
             minDelta: 1,
             expectedDelta: 0,
-            carbsRequired: 10,
+            carbsRequired: 0,
             naiveEventualGlucose: 39
         )
         #expect(shouldSet == true)
@@ -264,13 +264,19 @@ import Testing
         #expect(shouldSet == true)
     }
 
+    @Test("Min delta < 0") func testMinDeltaLessThanZero() throws {
+        let (shouldSet, determination) = try callHandleLowEventualGlucose(minDelta: -1, expectedDelta: -2, carbsRequired: 0)
+        #expect(shouldSet == true)
+        #expect(determination.rate == 0.6)
+    }
+
     @Test("Current temp rate matches basal") func testCurrentTempRateMatchesBasal() throws {
         let profile = defaultProfile()
         let currentTemp = TempBasal(duration: 20, rate: profile.currentBasal!, temp: .absolute, timestamp: Date())
         let (shouldSet, determination) = try callHandleLowEventualGlucose(
             minDelta: 1,
             expectedDelta: 0,
-            carbsRequired: 10,
+            carbsRequired: 0,
             currentTemp: currentTemp,
             profile: profile
         )
@@ -284,7 +290,7 @@ import Testing
         let (shouldSet, determination) = try callHandleLowEventualGlucose(
             minDelta: 1,
             expectedDelta: 0,
-            carbsRequired: 10,
+            carbsRequired: 0,
             profile: profile
         )
         #expect(shouldSet == true)
@@ -293,37 +299,31 @@ import Testing
         #expect(determination.reason.contains("setting current basal of \(profile.currentBasal!) as temp."))
     }
 
-    /*
-     @Test("Insulin scheduled less than required") func testInsulinScheduledLessThanRequired() throws {
-         let (shouldSet, determination) = try callHandleLowEventualGlucose(
-             eventualGlucose: 80,
-             naiveEventualGlucose: 70,
-             currentTemp: TempBasal(duration: 10, rate: 0, temp: .absolute, timestamp: Date())
-         )
-         #expect(shouldSet == true)
-         #expect(determination.rate != nil)
-         #expect(determination.duration == 30)
-         #expect(determination.reason.contains("is a lot less than needed"))
-     }*/
-
-    /*
-     @Test("Rate similar to current temp") func testRateSimilarToCurrentTemp() throws {
-         let currentTemp = TempBasal(duration: 10, rate: 0.1, temp: .absolute, timestamp: Date())
-         let (shouldSet, determination) = try callHandleLowEventualGlucose(
-             eventualGlucose: 99,
-             targetGlucose: 110,
-             currentTemp: currentTemp,
-             adjustedSensitivity: 50
-         )
+    @Test("Insulin scheduled less than required") func testInsulinScheduledLessThanRequired() throws {
+        let (shouldSet, determination) = try callHandleLowEventualGlucose(
+            eventualGlucose: 80,
+            naiveEventualGlucose: 70,
+            currentTemp: TempBasal(duration: 120, rate: 0, temp: .absolute, timestamp: Date())
+        )
+        #expect(shouldSet == true)
+        #expect(determination.rate == nil)
+        #expect(determination.duration == nil)
+        #expect(determination.reason.contains("is a lot less than needed"))
+    }
 
-         let insulinRequired = 2 * min(0, (Decimal(99) - Decimal(110)) / Decimal(50))
-         let expectedRate = (1.0 + (2 * insulinRequired)).rounded(toPlaces: 2)
+    @Test("Rate similar to current temp") func testRateSimilarToCurrentTemp() throws {
+        let currentTemp = TempBasal(duration: 10, rate: 0.1, temp: .absolute, timestamp: Date())
+        let (shouldSet, determination) = try callHandleLowEventualGlucose(
+            eventualGlucose: 99,
+            targetGlucose: 110,
+            currentTemp: currentTemp,
+            adjustedSensitivity: 50
+        )
 
-         #expect(shouldSet == true)
-         #expect(determination.rate == nil) // No change
-         #expect(determination.reason.contains("temp \(currentTemp.rate) ~< req \(expectedRate)U/hr."))
-     }
-      */
+        #expect(shouldSet == true)
+        #expect(determination.rate == nil) // No change
+        #expect(determination.reason.contains("temp \(currentTemp.rate) ~< req"))
+    }
 
     @Test("Set zero temp") func testSetZeroTemp() throws {
         let (shouldSet, determination) = try callHandleLowEventualGlucose(eventualGlucose: 70, naiveEventualGlucose: 60)
@@ -332,13 +332,4 @@ import Testing
         #expect(determination.duration! > 0)
         #expect(determination.reason.contains("setting \(determination.duration!)m zero temp."))
     }
-
-    /*
-     @Test("Set calculated rate") func testSetCalculatedRate() throws {
-         let (shouldSet, determination) = try callHandleLowEventualGlucose(eventualGlucose: 85)
-         #expect(shouldSet == true)
-         #expect(determination.rate! > 0)
-         #expect(determination.duration == 30)
-         #expect(determination.reason.contains("setting \(determination.rate!)U/hr."))
-     }*/
 }