GlucoseThresholdTests.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // GlucoseThresholdTests.swift
  3. // LoopKitTests
  4. //
  5. // Created by Darin Krauss on 8/24/19.
  6. // Copyright © 2019 LoopKit Authors. All rights reserved.
  7. //
  8. import XCTest
  9. import HealthKit
  10. @testable import LoopKit
  11. class GlucoseThresholdTests: XCTestCase {
  12. func testInitializer() {
  13. let glucoseThreshold = GlucoseThreshold(unit: HKUnit.gram(), value: 1.23)
  14. XCTAssertEqual(glucoseThreshold.value, 1.23)
  15. XCTAssertEqual(glucoseThreshold.unit, HKUnit.gram())
  16. }
  17. func testInitializerWithRawValueValid() {
  18. let rawValue: GlucoseThreshold.RawValue = [
  19. "units": "g",
  20. "value": 1.23
  21. ]
  22. let glucoseThreshold = GlucoseThreshold(rawValue: rawValue)
  23. XCTAssertNotNil(glucoseThreshold)
  24. XCTAssertEqual(glucoseThreshold!.value, 1.23)
  25. XCTAssertEqual(glucoseThreshold!.unit, HKUnit.gram())
  26. }
  27. func testInitializerWithRawValueWithValueMissing() {
  28. let rawValue: GlucoseThreshold.RawValue = [
  29. "units": "g"
  30. ]
  31. XCTAssertNil(GlucoseThreshold(rawValue: rawValue))
  32. }
  33. func testInitializerWithRawValueWithValueNotDouble() {
  34. let rawValue: GlucoseThreshold.RawValue = [
  35. "units": "g",
  36. "value": "g"
  37. ]
  38. XCTAssertNil(GlucoseThreshold(rawValue: rawValue))
  39. }
  40. func testInitializerWithRawValueWithUnitMissing() {
  41. let rawValue: GlucoseThreshold.RawValue = [
  42. "value": 1.23
  43. ]
  44. XCTAssertNil(GlucoseThreshold(rawValue: rawValue))
  45. }
  46. func testInitializerWithRawValueWithUnitNotString() {
  47. let rawValue: GlucoseThreshold.RawValue = [
  48. "units": 1.23,
  49. "value": 1.23
  50. ]
  51. XCTAssertNil(GlucoseThreshold(rawValue: rawValue))
  52. }
  53. func testQuantity() {
  54. let glucoseThreshold = GlucoseThreshold(unit: HKUnit.gram(), value: 1.23)
  55. XCTAssertEqual(glucoseThreshold.quantity, HKQuantity(unit: HKUnit.gram(), doubleValue: 1.23))
  56. }
  57. func testRawValue() {
  58. let glucoseThreshold = GlucoseThreshold(unit: HKUnit.gram(), value: 1.23)
  59. XCTAssertEqual(glucoseThreshold.rawValue.count, 2)
  60. XCTAssertEqual(glucoseThreshold.rawValue["units"] as! String, "g")
  61. XCTAssertEqual(glucoseThreshold.rawValue["value"] as! Double, 1.23)
  62. }
  63. func testConvertTo() {
  64. let glucoseThresholdMMOLL = GlucoseThreshold(unit: .millimolesPerLiter, value: 4.4)
  65. let glucoseThresholdMGDL = glucoseThresholdMMOLL.convertTo(unit: .milligramsPerDeciliter)
  66. XCTAssertEqual(glucoseThresholdMGDL.unit, .milligramsPerDeciliter)
  67. XCTAssertEqual(glucoseThresholdMGDL.value, HKQuantity(unit: .millimolesPerLiter, doubleValue: glucoseThresholdMMOLL.value).doubleValue(for: .milligramsPerDeciliter))
  68. }
  69. }
  70. class GlucoseThresholdCodableTests: XCTestCase {
  71. func testCodableMilligramsPerDeciliter() throws {
  72. try assertGlucoseThresholdCodable(GlucoseThreshold(unit: .milligramsPerDeciliter, value: 123),
  73. encodesJSON: """
  74. {
  75. "unit" : "mg/dL",
  76. "value" : 123
  77. }
  78. """
  79. )
  80. }
  81. func testCodableMillimolesPerLiter() throws {
  82. try assertGlucoseThresholdCodable(GlucoseThreshold(unit: .millimolesPerLiter, value: 6.5),
  83. encodesJSON: """
  84. {
  85. "unit" : "mmol<180.1558800000541>/L",
  86. "value" : 6.5
  87. }
  88. """
  89. )
  90. }
  91. private func assertGlucoseThresholdCodable(_ original: GlucoseThreshold, encodesJSON string: String) throws {
  92. let data = try encoder.encode(original)
  93. XCTAssertEqual(String(data: data, encoding: .utf8), string)
  94. let decoded = try decoder.decode(GlucoseThreshold.self, from: data)
  95. XCTAssertEqual(decoded, original)
  96. }
  97. private let encoder: JSONEncoder = {
  98. let encoder = JSONEncoder()
  99. encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
  100. return encoder
  101. }()
  102. private let decoder = JSONDecoder()
  103. }