PumpEventTests.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // PumpEventTests.swift
  3. // LoopKitTests
  4. //
  5. // Created by Darin Krauss on 8/26/20.
  6. // Copyright © 2020 LoopKit Authors. All rights reserved.
  7. //
  8. import XCTest
  9. @testable import LoopKit
  10. class PumpEventEncodableTests: PersistenceControllerTestCase {
  11. func testEncodable() throws {
  12. cacheStore.managedObjectContext.performAndWait {
  13. let pumpEvent = PumpEvent(context: cacheStore.managedObjectContext)
  14. pumpEvent.createdAt = dateFormatter.date(from: "2020-05-14T22:33:48Z")!
  15. pumpEvent.date = dateFormatter.date(from: "2020-05-14T22:38:14Z")!
  16. pumpEvent.doseType = .tempBasal
  17. pumpEvent.duration = .minutes(30)
  18. pumpEvent.type = .tempBasal
  19. pumpEvent.unit = .unitsPerHour
  20. pumpEvent.uploaded = false
  21. pumpEvent.value = 1.23
  22. pumpEvent.deliveredUnits = 0.56
  23. pumpEvent.mutable = true
  24. pumpEvent.raw = Data(base64Encoded: "MTIzNDU2Nzg5MA==")!
  25. pumpEvent.title = "This is the title"
  26. pumpEvent.insulinType = .fiasp
  27. pumpEvent.automatic = false
  28. pumpEvent.alarmType = .other("An Alarm")
  29. pumpEvent.modificationCounter = 123
  30. pumpEvent.wasProgrammedByPumpUI = true
  31. try! assertPumpEventEncodable(pumpEvent, encodesJSON: """
  32. {
  33. "alarmType" : "An Alarm",
  34. "automatic" : false,
  35. "createdAt" : "2020-05-14T22:33:48Z",
  36. "date" : "2020-05-14T22:38:14Z",
  37. "deliveredUnits" : 0.56000000000000005,
  38. "doseType" : "tempBasal",
  39. "duration" : 1800,
  40. "insulinType" : 3,
  41. "modificationCounter" : 123,
  42. "mutable" : true,
  43. "raw" : "MTIzNDU2Nzg5MA==",
  44. "title" : "This is the title",
  45. "type" : "TempBasal",
  46. "unit" : "U/hour",
  47. "uploaded" : false,
  48. "value" : 1.23,
  49. "wasProgrammedByPumpUI" : true
  50. }
  51. """
  52. )
  53. }
  54. }
  55. func testEncodableOptional() throws {
  56. cacheStore.managedObjectContext.performAndWait {
  57. let pumpEvent = PumpEvent(context: cacheStore.managedObjectContext)
  58. pumpEvent.createdAt = dateFormatter.date(from: "2020-05-13T22:33:48Z")!
  59. pumpEvent.date = dateFormatter.date(from: "2020-05-13T22:38:14Z")!
  60. pumpEvent.duration = .minutes(60)
  61. pumpEvent.uploaded = true
  62. pumpEvent.mutable = false
  63. pumpEvent.modificationCounter = 234
  64. pumpEvent.wasProgrammedByPumpUI = true
  65. try! assertPumpEventEncodable(pumpEvent, encodesJSON: """
  66. {
  67. "createdAt" : "2020-05-13T22:33:48Z",
  68. "date" : "2020-05-13T22:38:14Z",
  69. "duration" : 3600,
  70. "insulinType" : 0,
  71. "modificationCounter" : 234,
  72. "mutable" : false,
  73. "uploaded" : true,
  74. "wasProgrammedByPumpUI" : true
  75. }
  76. """
  77. )
  78. }
  79. }
  80. private func assertPumpEventEncodable(_ original: PumpEvent, encodesJSON string: String) throws {
  81. let data = try encoder.encode(original)
  82. XCTAssertEqual(String(data: data, encoding: .utf8), string)
  83. }
  84. private let dateFormatter = ISO8601DateFormatter()
  85. private let encoder: JSONEncoder = {
  86. let encoder = JSONEncoder()
  87. encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
  88. encoder.dateEncodingStrategy = .iso8601
  89. return encoder
  90. }()
  91. private let decoder: JSONDecoder = {
  92. let decoder = JSONDecoder()
  93. decoder.dateDecodingStrategy = .iso8601
  94. return decoder
  95. }()
  96. }