DoseEntryTests.swift 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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: .tempBasal,
  14. startDate: dateFormatter.date(from: "2020-05-14T22:07:19Z")!,
  15. endDate: dateFormatter.date(from: "2020-05-14T22:37:19Z")!,
  16. value: 1.25,
  17. unit: .unitsPerHour,
  18. deliveredUnits: 0.5,
  19. description: "Temporary Basal",
  20. syncIdentifier: "238E41EA-9576-4981-A1A4-51E10228584F",
  21. scheduledBasalRate: HKQuantity(unit: DoseEntry.unitsPerHour, doubleValue: 1.5)),
  22. encodesJSON: """
  23. {
  24. "automatic" : null,
  25. "deliveredUnits" : 0.5,
  26. "description" : "Temporary Basal",
  27. "endDate" : "2020-05-14T22:37:19Z",
  28. "scheduledBasalRate" : 1.5,
  29. "scheduledBasalRateUnit" : "IU/hr",
  30. "startDate" : "2020-05-14T22:07:19Z",
  31. "syncIdentifier" : "238E41EA-9576-4981-A1A4-51E10228584F",
  32. "type" : "tempBasal",
  33. "unit" : "U/hour",
  34. "value" : 1.25
  35. }
  36. """
  37. )
  38. }
  39. private func assertDoseEntryCodable(_ original: DoseEntry, encodesJSON string: String) throws {
  40. let data = try encoder.encode(original)
  41. XCTAssertEqual(String(data: data, encoding: .utf8), string)
  42. let decoded = try decoder.decode(DoseEntry.self, from: data)
  43. XCTAssertEqual(decoded, original)
  44. }
  45. private let dateFormatter = ISO8601DateFormatter()
  46. private let encoder: JSONEncoder = {
  47. let encoder = JSONEncoder()
  48. encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
  49. encoder.dateEncodingStrategy = .iso8601
  50. return encoder
  51. }()
  52. private let decoder: JSONDecoder = {
  53. let decoder = JSONDecoder()
  54. decoder.dateDecodingStrategy = .iso8601
  55. return decoder
  56. }()
  57. }