BolusRecommendationTests.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // BolusRecommendationTests.swift
  3. // LoopKitTests
  4. //
  5. // Created by Darin Krauss on 5/4/20.
  6. // Copyright © 2020 LoopKit Authors. All rights reserved.
  7. //
  8. import XCTest
  9. import HealthKit
  10. @testable import LoopKit
  11. class BolusRecommendationNoticeCodableTests: XCTestCase {
  12. func testCodableGlucoseBelowSuspendThreshold() throws {
  13. let glucoseValue = SimpleGlucoseValue(startDate: dateFormatter.date(from: "2020-05-14T22:14:16Z")!,
  14. quantity: HKQuantity(unit: .milligramsPerDeciliter, doubleValue: 65.0))
  15. try assertBolusRecommendationNoticeCodable(.glucoseBelowSuspendThreshold(minGlucose: glucoseValue), encodesJSON: """
  16. {
  17. "bolusRecommendationNotice" : {
  18. "glucoseBelowSuspendThreshold" : {
  19. "minGlucose" : {
  20. "endDate" : "2020-05-14T22:14:16Z",
  21. "quantity" : 65,
  22. "quantityUnit" : "mg/dL",
  23. "startDate" : "2020-05-14T22:14:16Z"
  24. }
  25. }
  26. }
  27. }
  28. """
  29. )
  30. }
  31. func testCodableCurrentGlucoseBelowTarget() throws {
  32. let glucoseValue = SimpleGlucoseValue(startDate: dateFormatter.date(from: "2020-05-14T22:20:16Z")!,
  33. quantity: HKQuantity(unit: .milligramsPerDeciliter, doubleValue: 85.0))
  34. try assertBolusRecommendationNoticeCodable(.currentGlucoseBelowTarget(glucose: glucoseValue), encodesJSON: """
  35. {
  36. "bolusRecommendationNotice" : {
  37. "currentGlucoseBelowTarget" : {
  38. "glucose" : {
  39. "endDate" : "2020-05-14T22:20:16Z",
  40. "quantity" : 85,
  41. "quantityUnit" : "mg/dL",
  42. "startDate" : "2020-05-14T22:20:16Z"
  43. }
  44. }
  45. }
  46. }
  47. """
  48. )
  49. }
  50. func testCodablePredictedGlucoseBelowTarget() throws {
  51. let glucoseValue = SimpleGlucoseValue(startDate: dateFormatter.date(from: "2020-05-14T22:38:16Z")!,
  52. quantity: HKQuantity(unit: .milligramsPerDeciliter, doubleValue: 80.0))
  53. try assertBolusRecommendationNoticeCodable(.predictedGlucoseBelowTarget(minGlucose: glucoseValue), encodesJSON: """
  54. {
  55. "bolusRecommendationNotice" : {
  56. "predictedGlucoseBelowTarget" : {
  57. "minGlucose" : {
  58. "endDate" : "2020-05-14T22:38:16Z",
  59. "quantity" : 80,
  60. "quantityUnit" : "mg/dL",
  61. "startDate" : "2020-05-14T22:38:16Z"
  62. }
  63. }
  64. }
  65. }
  66. """
  67. )
  68. }
  69. func testCodablePredictedGlucoseInRange() throws {
  70. try assertBolusRecommendationNoticeCodable(.predictedGlucoseInRange, encodesJSON: """
  71. {
  72. "bolusRecommendationNotice" : "predictedGlucoseInRange"
  73. }
  74. """
  75. )
  76. }
  77. func testCodableAllGlucoseBelowTarget() throws {
  78. let glucoseValue = SimpleGlucoseValue(startDate: dateFormatter.date(from: "2020-05-14T22:38:16Z")!,
  79. quantity: HKQuantity(unit: .milligramsPerDeciliter, doubleValue: 80.0))
  80. try assertBolusRecommendationNoticeCodable(.allGlucoseBelowTarget(minGlucose: glucoseValue), encodesJSON: """
  81. {
  82. "bolusRecommendationNotice" : {
  83. "allGlucoseBelowTarget" : {
  84. "minGlucose" : {
  85. "endDate" : "2020-05-14T22:38:16Z",
  86. "quantity" : 80,
  87. "quantityUnit" : "mg/dL",
  88. "startDate" : "2020-05-14T22:38:16Z"
  89. }
  90. }
  91. }
  92. }
  93. """
  94. )
  95. }
  96. private func assertBolusRecommendationNoticeCodable(_ original: BolusRecommendationNotice, encodesJSON string: String) throws {
  97. let data = try encoder.encode(TestContainer(bolusRecommendationNotice: original))
  98. XCTAssertEqual(String(data: data, encoding: .utf8), string)
  99. let decoded = try decoder.decode(TestContainer.self, from: data)
  100. XCTAssertEqual(decoded.bolusRecommendationNotice, original)
  101. }
  102. private let dateFormatter = ISO8601DateFormatter()
  103. private let encoder: JSONEncoder = {
  104. let encoder = JSONEncoder()
  105. encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
  106. encoder.dateEncodingStrategy = .iso8601
  107. return encoder
  108. }()
  109. private let decoder: JSONDecoder = {
  110. let decoder = JSONDecoder()
  111. decoder.dateDecodingStrategy = .iso8601
  112. return decoder
  113. }()
  114. private struct TestContainer: Codable, Equatable {
  115. let bolusRecommendationNotice: BolusRecommendationNotice
  116. }
  117. }
  118. extension BolusRecommendationNotice: Equatable {
  119. public static func == (lhs: BolusRecommendationNotice, rhs: BolusRecommendationNotice) -> Bool {
  120. switch (lhs, rhs) {
  121. case (.glucoseBelowSuspendThreshold(let lhsGlucoseValue), .glucoseBelowSuspendThreshold(let rhsGlucoseValue)),
  122. (.currentGlucoseBelowTarget(let lhsGlucoseValue), .currentGlucoseBelowTarget(let rhsGlucoseValue)),
  123. (.predictedGlucoseBelowTarget(let lhsGlucoseValue), .predictedGlucoseBelowTarget(let rhsGlucoseValue)),
  124. (.allGlucoseBelowTarget(let lhsGlucoseValue), .allGlucoseBelowTarget(let rhsGlucoseValue)):
  125. return lhsGlucoseValue.startDate == rhsGlucoseValue.startDate &&
  126. lhsGlucoseValue.endDate == rhsGlucoseValue.endDate &&
  127. lhsGlucoseValue.quantity == rhsGlucoseValue.quantity
  128. case (.predictedGlucoseInRange, .predictedGlucoseInRange):
  129. return true
  130. default:
  131. return false
  132. }
  133. }
  134. }