DoseEntryTests.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // DoseEntryTests.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 DoseEntryCodableTests: XCTestCase {
  12. func testCodable() throws {
  13. try assertDoseEntryCodable(DoseEntry(type: .bolus,
  14. startDate: dateFormatter.date(from: "2020-05-14T22:07:19Z")!,
  15. value: 2.5,
  16. unit: .units),
  17. encodesJSON: """
  18. {
  19. "endDate" : "2020-05-14T22:07:19Z",
  20. "isMutable" : false,
  21. "manuallyEntered" : false,
  22. "startDate" : "2020-05-14T22:07:19Z",
  23. "type" : "bolus",
  24. "unit" : "U",
  25. "value" : 2.5,
  26. "wasProgrammedByPumpUI" : false
  27. }
  28. """
  29. )
  30. }
  31. func testCodableOptional() throws {
  32. try assertDoseEntryCodable(DoseEntry(type: .tempBasal,
  33. startDate: dateFormatter.date(from: "2020-05-14T22:07:19Z")!,
  34. endDate: dateFormatter.date(from: "2020-05-14T22:37:19Z")!,
  35. value: 1.25,
  36. unit: .unitsPerHour,
  37. deliveredUnits: 0.5,
  38. description: "Temporary Basal",
  39. syncIdentifier: "238E41EA-9576-4981-A1A4-51E10228584F",
  40. scheduledBasalRate: HKQuantity(unit: DoseEntry.unitsPerHour, doubleValue: 1.5),
  41. insulinType: .fiasp,
  42. automatic: true,
  43. manuallyEntered: true,
  44. isMutable: true,
  45. wasProgrammedByPumpUI: true),
  46. encodesJSON: """
  47. {
  48. "automatic" : true,
  49. "deliveredUnits" : 0.5,
  50. "description" : "Temporary Basal",
  51. "endDate" : "2020-05-14T22:37:19Z",
  52. "insulinType" : 3,
  53. "isMutable" : true,
  54. "manuallyEntered" : true,
  55. "scheduledBasalRate" : 1.5,
  56. "scheduledBasalRateUnit" : "IU/hr",
  57. "startDate" : "2020-05-14T22:07:19Z",
  58. "syncIdentifier" : "238E41EA-9576-4981-A1A4-51E10228584F",
  59. "type" : "tempBasal",
  60. "unit" : "U/hour",
  61. "value" : 1.25,
  62. "wasProgrammedByPumpUI" : true
  63. }
  64. """
  65. )
  66. }
  67. private func assertDoseEntryCodable(_ original: DoseEntry, encodesJSON string: String) throws {
  68. let data = try encoder.encode(original)
  69. XCTAssertEqual(String(data: data, encoding: .utf8), string)
  70. let decoded = try decoder.decode(DoseEntry.self, from: data)
  71. XCTAssertEqual(decoded, original)
  72. }
  73. private let dateFormatter = ISO8601DateFormatter()
  74. private let encoder: JSONEncoder = {
  75. let encoder = JSONEncoder()
  76. encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
  77. encoder.dateEncodingStrategy = .iso8601
  78. return encoder
  79. }()
  80. private let decoder: JSONDecoder = {
  81. let decoder = JSONDecoder()
  82. decoder.dateDecodingStrategy = .iso8601
  83. return decoder
  84. }()
  85. }
  86. class DoseEntryRawRepresentableTests: XCTestCase {
  87. func testDoseEntryRawRepresentable() {
  88. let original = DoseEntry(type: .bolus,
  89. startDate: Date(),
  90. value: 2.5,
  91. unit: .units)
  92. let actual = DoseEntry(rawValue: original.rawValue)
  93. XCTAssertEqual(actual, original)
  94. }
  95. func testDoseEntryRawRepresentableOptional() {
  96. let original = DoseEntry(type: .tempBasal,
  97. startDate: Date(),
  98. endDate: Date().addingTimeInterval(.minutes(30)),
  99. value: 1.25,
  100. unit: .unitsPerHour,
  101. deliveredUnits: 0.5,
  102. description: "Temporary Basal",
  103. syncIdentifier: UUID().uuidString,
  104. scheduledBasalRate: HKQuantity(unit: .internationalUnitsPerHour, doubleValue: 1.5),
  105. insulinType: .fiasp,
  106. automatic: true,
  107. manuallyEntered: true,
  108. isMutable: true,
  109. wasProgrammedByPumpUI: true)
  110. let actual = DoseEntry(rawValue: original.rawValue)
  111. XCTAssertEqual(actual, original)
  112. }
  113. }