GlucoseThresholdTests.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. }
  64. class GlucoseThresholdCodableTests: XCTestCase {
  65. func testCodableMilligramsPerDeciliter() throws {
  66. try assertGlucoseThresholdCodable(GlucoseThreshold(unit: .milligramsPerDeciliter, value: 123),
  67. encodesJSON: """
  68. {
  69. "unit" : "mg/dL",
  70. "value" : 123
  71. }
  72. """
  73. )
  74. }
  75. func testCodableMillimolesPerLiter() throws {
  76. try assertGlucoseThresholdCodable(GlucoseThreshold(unit: .millimolesPerLiter, value: 6.5),
  77. encodesJSON: """
  78. {
  79. "unit" : "mmol<180.1558800000541>/L",
  80. "value" : 6.5
  81. }
  82. """
  83. )
  84. }
  85. private func assertGlucoseThresholdCodable(_ original: GlucoseThreshold, encodesJSON string: String) throws {
  86. let data = try encoder.encode(original)
  87. XCTAssertEqual(String(data: data, encoding: .utf8), string)
  88. let decoded = try decoder.decode(GlucoseThreshold.self, from: data)
  89. XCTAssertEqual(decoded, original)
  90. }
  91. private let encoder: JSONEncoder = {
  92. let encoder = JSONEncoder()
  93. encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
  94. return encoder
  95. }()
  96. private let decoder = JSONDecoder()
  97. }