DailyQuantityScheduleTests.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // DailyQuantityScheduleTests.swift
  3. // LoopKitTests
  4. //
  5. // Created by Darin Krauss on 5/1/20.
  6. // Copyright © 2020 LoopKit Authors. All rights reserved.
  7. //
  8. import XCTest
  9. import LoopKit
  10. class DailyQuantityScheduleCodableTests: XCTestCase {
  11. func testCodableDouble() throws {
  12. try assertCodable(DailyQuantitySchedule(unit: .milligramsPerDeciliter,
  13. dailyItems: [RepeatingScheduleValue(startTime: .hours(0), value: 110.3),
  14. RepeatingScheduleValue(startTime: .hours(5), value: 100.5),
  15. RepeatingScheduleValue(startTime: .hours(18), value: 120.5)],
  16. timeZone: TimeZone(identifier: "America/New_York")!)!,
  17. encodesJSON: """
  18. {
  19. "unit" : "mg/dL",
  20. "valueSchedule" : {
  21. "items" : [
  22. {
  23. "startTime" : 0,
  24. "value" : 110.3
  25. },
  26. {
  27. "startTime" : 18000,
  28. "value" : 100.5
  29. },
  30. {
  31. "startTime" : 64800,
  32. "value" : 120.5
  33. }
  34. ],
  35. "referenceTimeInterval" : 0,
  36. "repeatInterval" : 86400,
  37. "timeZone" : {
  38. "identifier" : "America/New_York"
  39. }
  40. }
  41. }
  42. """
  43. )
  44. }
  45. func testCodableDoubleRange() throws {
  46. try assertCodable(DailyQuantitySchedule(unit: .milligramsPerDeciliter,
  47. dailyItems: [RepeatingScheduleValue(startTime: .hours(0), value: DoubleRange(minValue: 100.2, maxValue: 111.2)),
  48. RepeatingScheduleValue(startTime: .hours(6), value: DoubleRange(minValue: 90.5, maxValue: 101.2)),
  49. RepeatingScheduleValue(startTime: .hours(19), value: DoubleRange(minValue: 110.2, maxValue: 121.2))],
  50. timeZone: TimeZone(identifier: "America/Chicago")!)!,
  51. encodesJSON: """
  52. {
  53. "unit" : "mg/dL",
  54. "valueSchedule" : {
  55. "items" : [
  56. {
  57. "startTime" : 0,
  58. "value" : {
  59. "maxValue" : 111.2,
  60. "minValue" : 100.2
  61. }
  62. },
  63. {
  64. "startTime" : 21600,
  65. "value" : {
  66. "maxValue" : 101.2,
  67. "minValue" : 90.5
  68. }
  69. },
  70. {
  71. "startTime" : 68400,
  72. "value" : {
  73. "maxValue" : 121.2,
  74. "minValue" : 110.2
  75. }
  76. }
  77. ],
  78. "referenceTimeInterval" : 0,
  79. "repeatInterval" : 86400,
  80. "timeZone" : {
  81. "identifier" : "America/Chicago"
  82. }
  83. }
  84. }
  85. """
  86. )
  87. }
  88. func testCodableInt() throws {
  89. try assertCodable(DailyQuantitySchedule(unit: .milligramsPerDeciliter,
  90. dailyItems: [RepeatingScheduleValue(startTime: .hours(0), value: 112),
  91. RepeatingScheduleValue(startTime: .hours(7), value: 102),
  92. RepeatingScheduleValue(startTime: .hours(20), value: 122)],
  93. timeZone: TimeZone(identifier: "America/Los_Angeles")!)!,
  94. encodesJSON: """
  95. {
  96. "unit" : "mg/dL",
  97. "valueSchedule" : {
  98. "items" : [
  99. {
  100. "startTime" : 0,
  101. "value" : 112
  102. },
  103. {
  104. "startTime" : 25200,
  105. "value" : 102
  106. },
  107. {
  108. "startTime" : 72000,
  109. "value" : 122
  110. }
  111. ],
  112. "referenceTimeInterval" : 0,
  113. "repeatInterval" : 86400,
  114. "timeZone" : {
  115. "identifier" : "America/Los_Angeles"
  116. }
  117. }
  118. }
  119. """
  120. )
  121. }
  122. func assertCodable<T>(_ original: DailyQuantitySchedule<T>, encodesJSON string: String) throws where T: RawRepresentable & Codable & Equatable {
  123. let data = try encoder.encode(original)
  124. XCTAssertEqual(String(data: data, encoding: .utf8), string)
  125. let decoded = try decoder.decode(DailyQuantitySchedule<T>.self, from: data)
  126. XCTAssertEqual(decoded, original)
  127. }
  128. private let encoder: JSONEncoder = {
  129. let encoder = JSONEncoder()
  130. encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
  131. return encoder
  132. }()
  133. private let decoder = JSONDecoder()
  134. }
  135. extension Int: RawRepresentable {
  136. public typealias RawValue = Int
  137. public init?(rawValue: RawValue) {
  138. self = rawValue
  139. }
  140. public var rawValue: RawValue {
  141. return self
  142. }
  143. }