MealHistoryTests.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import Foundation
  2. import Testing
  3. @testable import Trio
  4. @Suite("MealHistory Tests") struct MealHistoryTests {
  5. @Test("should process carbs from carbHistory") func processCarbsFromCarbHistory() async {
  6. let carbHistory = [
  7. CarbsEntry.forTest(
  8. createdAt: Date.from(isoString: "2016-06-19T12:00:00-04:00"),
  9. carbs: 20
  10. )
  11. ]
  12. let output = MealHistory.findMealInputs(
  13. pumpHistory: [],
  14. carbHistory: carbHistory
  15. )
  16. #expect(output.count == 1)
  17. #expect(output[0].carbs == 20)
  18. #expect(output[0].timestamp == Date.from(isoString: "2016-06-19T12:00:00-04:00"))
  19. }
  20. @Test("should process bolus events from pumpHistory") func processBolusEventsFromPumpHistory() async {
  21. let pumpHistory = [
  22. PumpHistoryEvent(
  23. id: UUID().uuidString,
  24. type: .bolus,
  25. timestamp: Date.from(isoString: "2016-06-19T12:00:00-04:00"),
  26. amount: 2.5
  27. )
  28. ]
  29. let output = MealHistory.findMealInputs(
  30. pumpHistory: pumpHistory,
  31. carbHistory: []
  32. )
  33. #expect(output.count == 1)
  34. #expect(output[0].bolus == 2.5)
  35. #expect(output[0].timestamp == Date.from(isoString: "2016-06-19T12:00:00-04:00"))
  36. }
  37. @Test("should handle both carbs and bolus entries") func handleBothCarbsAndBolusEntries() async {
  38. let pumpHistory = [
  39. PumpHistoryEvent(
  40. id: UUID().uuidString,
  41. type: .bolus,
  42. timestamp: Date.from(isoString: "2016-06-19T12:00:00-04:00"),
  43. amount: 2.5
  44. )
  45. ]
  46. let carbHistory = [
  47. CarbsEntry.forTest(
  48. createdAt: Date.from(isoString: "2016-06-19T12:30:00-04:00"),
  49. carbs: 20
  50. )
  51. ]
  52. let output = MealHistory.findMealInputs(
  53. pumpHistory: pumpHistory,
  54. carbHistory: carbHistory
  55. )
  56. #expect(output.count == 2)
  57. // Find the carb entry
  58. let carbEntry = output.first { $0.carbs != nil }
  59. #expect(carbEntry != nil)
  60. #expect(carbEntry?.carbs == 20)
  61. // Find the bolus entry
  62. let bolusEntry = output.first { $0.bolus != nil }
  63. #expect(bolusEntry != nil)
  64. #expect(bolusEntry?.bolus == 2.5)
  65. }
  66. @Test("should dedupe carb entries with same timestamp") func dedupeCarbs() async {
  67. let carbHistory = [
  68. CarbsEntry.forTest(
  69. createdAt: Date.from(isoString: "2016-06-19T12:00:00-04:00"),
  70. carbs: 20
  71. ),
  72. CarbsEntry.forTest(
  73. createdAt: Date.from(isoString: "2016-06-19T12:00:00-04:00"),
  74. carbs: 30
  75. )
  76. ]
  77. let output = MealHistory.findMealInputs(
  78. pumpHistory: [],
  79. carbHistory: carbHistory
  80. )
  81. #expect(output.count == 1)
  82. #expect(output[0].carbs == 20)
  83. }
  84. @Test("should dedupe bolus entries with same timestamp") func dedupeBolusEntries() async {
  85. let pumpHistory = [
  86. PumpHistoryEvent(
  87. id: UUID().uuidString,
  88. type: .bolus,
  89. timestamp: Date.from(isoString: "2016-06-19T12:00:00-04:00"),
  90. amount: 2.5
  91. ),
  92. PumpHistoryEvent(
  93. id: UUID().uuidString,
  94. type: .bolus,
  95. timestamp: Date.from(isoString: "2016-06-19T12:00:00-04:00"),
  96. amount: 3.0
  97. )
  98. ]
  99. let output = MealHistory.findMealInputs(
  100. pumpHistory: pumpHistory,
  101. carbHistory: []
  102. )
  103. #expect(output.count == 1)
  104. #expect(output[0].bolus == 2.5)
  105. }
  106. @Test("should consider timestamps within 2 seconds as duplicates") func timestampNearlyDuplicates() async {
  107. let pumpHistory = [
  108. PumpHistoryEvent(
  109. id: UUID().uuidString,
  110. type: .bolus,
  111. timestamp: Date.from(isoString: "2016-06-19T12:00:00-04:00"),
  112. amount: 2.5
  113. ),
  114. PumpHistoryEvent(
  115. id: UUID().uuidString,
  116. type: .bolus,
  117. timestamp: Date.from(isoString: "2016-06-19T12:00:01-04:00"),
  118. amount: 3.0
  119. )
  120. ]
  121. let output = MealHistory.findMealInputs(
  122. pumpHistory: pumpHistory,
  123. carbHistory: []
  124. )
  125. #expect(output.count == 1)
  126. #expect(output[0].bolus == 2.5)
  127. }
  128. }