QuantityScheduleTests.swift 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // QuantityScheduleTests.swift
  3. // Naterade
  4. //
  5. // Created by Nathan Racklyeft on 1/18/16.
  6. // Copyright © 2016 Nathan Racklyeft. All rights reserved.
  7. //
  8. import XCTest
  9. import HealthKit
  10. @testable import LoopKit
  11. class QuantityScheduleTests: XCTestCase {
  12. var items: [RepeatingScheduleValue<Double>]!
  13. override func setUp() {
  14. super.setUp()
  15. let path = Bundle(for: type(of: self)).path(forResource: "read_carb_ratios", ofType: "json")!
  16. let fixture = try! JSONSerialization.jsonObject(with: Data(contentsOf: URL(fileURLWithPath: path)), options: []) as! JSONDictionary
  17. let schedule = fixture["schedule"] as! [JSONDictionary]
  18. items = schedule.map {
  19. return RepeatingScheduleValue(startTime: TimeInterval(minutes: $0["offset"] as! Double), value: $0["ratio"] as! Double)
  20. }
  21. }
  22. func testCarbRatioScheduleLocalTimeZone() {
  23. let schedule = CarbRatioSchedule(unit: HKUnit.gram(), dailyItems: items, timeZone: nil)!
  24. let calendar = Calendar.current
  25. let midnight = calendar.startOfDay(for: Date())
  26. XCTAssertEqual(HKQuantity(unit: HKUnit.gram(), doubleValue: 10), schedule.quantity(at: midnight))
  27. XCTAssertEqual(9,
  28. schedule.quantity(at: midnight.addingTimeInterval(-1)).doubleValue(for: schedule.unit)
  29. )
  30. XCTAssertEqual(10,
  31. schedule.quantity(at: midnight.addingTimeInterval(TimeInterval(hours: 24))).doubleValue(for: schedule.unit)
  32. )
  33. let midMorning = calendar.nextDate(after: Date(), matching: DateComponents(hour: 10, minute: 29, second: 4), matchingPolicy: .nextTime)!
  34. XCTAssertEqual(10, schedule.quantity(at: midMorning).doubleValue(for: schedule.unit))
  35. let lunch = calendar.nextDate(after: midMorning, matching: DateComponents(hour: 12, minute: 01, second: 01), matchingPolicy: .nextTime)!
  36. XCTAssertEqual(9, schedule.quantity(at: lunch).doubleValue(for: schedule.unit))
  37. let dinner = calendar.nextDate(after: midMorning, matching: DateComponents(hour: 19, minute: 0, second: 0), matchingPolicy: .nextTime)!
  38. XCTAssertEqual(8, schedule.quantity(at: dinner).doubleValue(for: schedule.unit))
  39. }
  40. func testCarbRatioScheduleUTC() {
  41. let schedule = CarbRatioSchedule(unit: HKUnit.gram(), dailyItems: items, timeZone: TimeZone(secondsFromGMT: 0))!
  42. var calendar = Calendar.current
  43. calendar.timeZone = TimeZone(identifier: "America/Los_Angeles")!
  44. let june1 = calendar.nextDate(after: Date(), matching: DateComponents(month: 5), matchingPolicy: .nextTime)!
  45. XCTAssertEqual(-7 * 60 * 60, calendar.timeZone.secondsFromGMT(for: june1))
  46. let midnight = calendar.startOfDay(for: june1)
  47. // This is 7 AM the next day in the Schedule's time zone
  48. XCTAssertEqual(HKQuantity(unit: HKUnit.gram(), doubleValue: 10), schedule.quantity(at: midnight))
  49. XCTAssertEqual(10,
  50. schedule.quantity(at: midnight.addingTimeInterval(-1)).doubleValue(for: schedule.unit)
  51. )
  52. XCTAssertEqual(10,
  53. schedule.quantity(at: midnight.addingTimeInterval(TimeInterval(hours: 24))).doubleValue(for: schedule.unit)
  54. )
  55. // 10:29:04 AM -> 5:29:04 PM
  56. let midMorning = calendar.nextDate(after: june1, matching: DateComponents(hour: 10, minute: 29, second: 4), matchingPolicy: .nextTime)!
  57. XCTAssertEqual(9, schedule.quantity(at: midMorning).doubleValue(for: schedule.unit))
  58. // 12:01:01 PM -> 7:01:01 PM
  59. let lunch = calendar.nextDate(after: midMorning, matching: DateComponents(hour: 12, minute: 01, second: 01), matchingPolicy: .nextTime)!
  60. XCTAssertEqual(8, schedule.quantity(at: lunch).doubleValue(for: schedule.unit))
  61. // 7:00 PM -> 2:00 AM
  62. let dinner = calendar.nextDate(after: midMorning, matching: DateComponents(hour: 19, minute: 0, second: 0), matchingPolicy: .nextTime)!
  63. XCTAssertEqual(10, schedule.quantity(at: dinner).doubleValue(for: schedule.unit))
  64. }
  65. }