PumpEventTests.swift 3.0 KB

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