Преглед изворни кода

Look for pump event duplicates using only the timestamp

Sam King пре 1 година
родитељ
комит
116894c4b0

+ 2 - 3
Model/Helper/PumpEvent+helper.swift

@@ -108,9 +108,8 @@ extension NSPredicate {
         return NSPredicate(format: "timestamp >= %@ AND bolus.isExternal == %@", date as NSDate, false as NSNumber)
     }
 
-    static func duplicateInLastHour(_ date: Date) -> NSPredicate {
-        let date60m = Date.oneHourAgo
-        return NSPredicate(format: "timestamp >= %@ && timestamp == %@", date60m as NSDate, date as NSDate)
+    static func duplicates(_ date: Date) -> NSPredicate {
+        NSPredicate(format: "timestamp == %@", date as NSDate)
     }
 
     static var pumpEventsNotYetUploadedToNightscout: NSPredicate {

+ 1 - 1
Trio/Sources/APS/Storage/PumpHistoryStorage.swift

@@ -52,7 +52,7 @@ final class BasePumpHistoryStorage: PumpHistoryStorage, Injectable {
                 let existingEvents: [PumpEventStored] = try CoreDataStack.shared.fetchEntities(
                     ofType: PumpEventStored.self,
                     onContext: self.context,
-                    predicate: NSPredicate.duplicateInLastHour(event.date),
+                    predicate: NSPredicate.duplicates(event.date),
                     key: "timestamp",
                     ascending: false,
                     batchSize: 50

+ 2 - 1
TrioTests/BolusCalculatorTests/BolusCalculatorTests.swift

@@ -338,7 +338,8 @@ import Testing
         let result = await calculator.handleBolusCalculation(
             carbs: carbs,
             useFattyMealCorrection: false,
-            useSuperBolus: false
+            useSuperBolus: false,
+            minPredBG: nil
         )
 
         // Then

+ 64 - 0
TrioTests/CoreDataTests/PumpHistoryStorageTests.swift

@@ -286,4 +286,68 @@ import Testing
         #expect(fetchedEvent?.isUploadedToHealth == false, "Should not be uploaded to Health")
         #expect(fetchedEvent?.isUploadedToTidepool == false, "Should not be uploaded to Tidepool")
     }
+
+    @Test("Test duplicates in PumpHistoryStorage") func testDuplicatePumpEvents() async throws {
+        // Given
+        let date = Date()
+        let twoHoursAgo = date - 2.hours.timeInterval
+        let oneMinuteAgo = date - 1.minutes.timeInterval
+
+        // Get initial entries to compare to final entries later
+        let initialEntries = try await testContext.perform {
+            try testContext.fetch(PumpEventStored.fetchRequest())
+        }
+
+        // Create two suspend events and two resume events
+        let events: [LoopKit.NewPumpEvent] = [
+            LoopKit.NewPumpEvent(
+                date: twoHoursAgo,
+                dose: nil,
+                raw: Data(),
+                title: "Test Suspend",
+                type: .suspend
+            ),
+            LoopKit.NewPumpEvent(
+                date: twoHoursAgo,
+                dose: nil,
+                raw: Data(),
+                title: "Test Suspend",
+                type: .suspend
+            ),
+            LoopKit.NewPumpEvent(
+                date: oneMinuteAgo,
+                dose: nil,
+                raw: Data(),
+                title: "Test Resume",
+                type: .resume
+            ),
+            LoopKit.NewPumpEvent(
+                date: oneMinuteAgo,
+                dose: nil,
+                raw: Data(),
+                title: "Test Resume",
+                type: .resume
+            )
+        ]
+
+        // When
+        // Store in our in-memory PumphistoryStorage
+        try await storage.storePumpEvents(events)
+
+        // Then
+        // Fetch all events after storing
+        let finalEntriesUnsorted = try await testContext.perform {
+            try testContext.fetch(PumpEventStored.fetchRequest())
+        }
+        let finalEntries = finalEntriesUnsorted.sorted { $0.timestamp! < $1.timestamp! }
+
+        // Verify there were no initial entries
+        #expect(initialEntries.isEmpty, "There should be no initial entries")
+
+        // Verify count increased by 2
+        #expect(finalEntries.count == initialEntries.count + 2, "Should have added 2 new events")
+
+        #expect(finalEntries.first?.type == PumpEvent.pumpSuspend.rawValue)
+        #expect(finalEntries.last?.type == PumpEvent.pumpResume.rawValue)
+    }
 }