DoseEntryTests.swift 4.5 KB

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