PumpEventTests.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. let data = try! encoder.encode(pumpEvent)
  32. XCTAssertEqual(String(data: data, encoding: .utf8), """
  33. {
  34. "alarmType" : "An Alarm",
  35. "automatic" : false,
  36. "createdAt" : "2020-05-14T22:33:48Z",
  37. "date" : "2020-05-14T22:38:14Z",
  38. "deliveredUnits" : 0.56,
  39. "doseType" : "tempBasal",
  40. "duration" : 1800,
  41. "insulinType" : 3,
  42. "modificationCounter" : 123,
  43. "mutable" : true,
  44. "raw" : "MTIzNDU2Nzg5MA==",
  45. "title" : "This is the title",
  46. "type" : "TempBasal",
  47. "unit" : "U/hour",
  48. "uploaded" : false,
  49. "value" : 1.23,
  50. "wasProgrammedByPumpUI" : true
  51. }
  52. """
  53. )
  54. }
  55. }
  56. func testEncodableOptional() throws {
  57. cacheStore.managedObjectContext.performAndWait {
  58. let pumpEvent = PumpEvent(context: cacheStore.managedObjectContext)
  59. pumpEvent.createdAt = dateFormatter.date(from: "2020-05-13T22:33:48Z")!
  60. pumpEvent.date = dateFormatter.date(from: "2020-05-13T22:38:14Z")!
  61. pumpEvent.duration = .minutes(60)
  62. pumpEvent.uploaded = true
  63. pumpEvent.mutable = false
  64. pumpEvent.modificationCounter = 234
  65. pumpEvent.wasProgrammedByPumpUI = true
  66. let data = try! encoder.encode(pumpEvent)
  67. XCTAssertEqual(String(data: data, encoding: .utf8), """
  68. {
  69. "createdAt" : "2020-05-13T22:33:48Z",
  70. "date" : "2020-05-13T22:38:14Z",
  71. "duration" : 3600,
  72. "insulinType" : 0,
  73. "modificationCounter" : 234,
  74. "mutable" : false,
  75. "uploaded" : true,
  76. "wasProgrammedByPumpUI" : true
  77. }
  78. """
  79. )
  80. }
  81. }
  82. private let dateFormatter = ISO8601DateFormatter()
  83. private let encoder: JSONEncoder = {
  84. let encoder = JSONEncoder()
  85. encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
  86. encoder.dateEncodingStrategy = .iso8601
  87. return encoder
  88. }()
  89. private let decoder: JSONDecoder = {
  90. let decoder = JSONDecoder()
  91. decoder.dateDecodingStrategy = .iso8601
  92. return decoder
  93. }()
  94. }