BolusRecommendationTests.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. private func assertBolusRecommendationNoticeCodable(_ original: BolusRecommendationNotice, encodesJSON string: String) throws {
  70. let data = try encoder.encode(TestContainer(bolusRecommendationNotice: original))
  71. XCTAssertEqual(String(data: data, encoding: .utf8), string)
  72. let decoded = try decoder.decode(TestContainer.self, from: data)
  73. XCTAssertEqual(decoded.bolusRecommendationNotice, original)
  74. }
  75. private let dateFormatter = ISO8601DateFormatter()
  76. private let encoder: JSONEncoder = {
  77. let encoder = JSONEncoder()
  78. encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
  79. encoder.dateEncodingStrategy = .iso8601
  80. return encoder
  81. }()
  82. private let decoder: JSONDecoder = {
  83. let decoder = JSONDecoder()
  84. decoder.dateDecodingStrategy = .iso8601
  85. return decoder
  86. }()
  87. private struct TestContainer: Codable, Equatable {
  88. let bolusRecommendationNotice: BolusRecommendationNotice
  89. }
  90. }
  91. extension BolusRecommendationNotice: Equatable {
  92. public static func == (lhs: BolusRecommendationNotice, rhs: BolusRecommendationNotice) -> Bool {
  93. switch (lhs, rhs) {
  94. case (.glucoseBelowSuspendThreshold(let lhsGlucoseValue), .glucoseBelowSuspendThreshold(let rhsGlucoseValue)),
  95. (.currentGlucoseBelowTarget(let lhsGlucoseValue), .currentGlucoseBelowTarget(let rhsGlucoseValue)),
  96. (.predictedGlucoseBelowTarget(let lhsGlucoseValue), .predictedGlucoseBelowTarget(let rhsGlucoseValue)):
  97. return lhsGlucoseValue.startDate == rhsGlucoseValue.startDate &&
  98. lhsGlucoseValue.endDate == rhsGlucoseValue.endDate &&
  99. lhsGlucoseValue.quantity == rhsGlucoseValue.quantity
  100. default:
  101. return false
  102. }
  103. }
  104. }