QuantityScheduleTests.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 therapyTimeZone = TimeZone(secondsFromGMT: -6*60*60)!
  24. let schedule = CarbRatioSchedule(unit: HKUnit.gram(), dailyItems: items, timeZone: therapyTimeZone)!
  25. var calendar = Calendar(identifier: .gregorian)
  26. calendar.timeZone = therapyTimeZone
  27. let midnight = calendar.startOfDay(for: Date())
  28. XCTAssertEqual(HKQuantity(unit: HKUnit.gram(), doubleValue: 10), schedule.quantity(at: midnight))
  29. XCTAssertEqual(9,
  30. schedule.quantity(at: midnight.addingTimeInterval(-1)).doubleValue(for: schedule.unit)
  31. )
  32. XCTAssertEqual(10,
  33. schedule.quantity(at: midnight.addingTimeInterval(TimeInterval(hours: 24))).doubleValue(for: schedule.unit)
  34. )
  35. let midMorning = calendar.nextDate(after: Date(), matching: DateComponents(hour: 10, minute: 29, second: 4), matchingPolicy: .nextTime)!
  36. XCTAssertEqual(10, schedule.quantity(at: midMorning).doubleValue(for: schedule.unit))
  37. let lunch = calendar.nextDate(after: midMorning, matching: DateComponents(hour: 12, minute: 01, second: 01), matchingPolicy: .nextTime)!
  38. XCTAssertEqual(9, schedule.quantity(at: lunch).doubleValue(for: schedule.unit))
  39. let dinner = calendar.nextDate(after: midMorning, matching: DateComponents(hour: 19, minute: 0, second: 0), matchingPolicy: .nextTime)!
  40. XCTAssertEqual(8, schedule.quantity(at: dinner).doubleValue(for: schedule.unit))
  41. }
  42. func testCarbRatioScheduleUTC() {
  43. let schedule = CarbRatioSchedule(unit: HKUnit.gram(), dailyItems: items, timeZone: TimeZone(secondsFromGMT: 0))!
  44. var calendar = Calendar.current
  45. calendar.timeZone = TimeZone(identifier: "America/Los_Angeles")!
  46. let june1 = calendar.nextDate(after: Date(), matching: DateComponents(month: 5), matchingPolicy: .nextTime)!
  47. XCTAssertEqual(-7 * 60 * 60, calendar.timeZone.secondsFromGMT(for: june1))
  48. let midnight = calendar.startOfDay(for: june1)
  49. // This is 7 AM the next day in the Schedule's time zone
  50. XCTAssertEqual(HKQuantity(unit: HKUnit.gram(), doubleValue: 10), schedule.quantity(at: midnight))
  51. XCTAssertEqual(10,
  52. schedule.quantity(at: midnight.addingTimeInterval(-1)).doubleValue(for: schedule.unit)
  53. )
  54. XCTAssertEqual(10,
  55. schedule.quantity(at: midnight.addingTimeInterval(TimeInterval(hours: 24))).doubleValue(for: schedule.unit)
  56. )
  57. // 10:29:04 AM -> 5:29:04 PM
  58. let midMorning = calendar.nextDate(after: june1, matching: DateComponents(hour: 10, minute: 29, second: 4), matchingPolicy: .nextTime)!
  59. XCTAssertEqual(9, schedule.quantity(at: midMorning).doubleValue(for: schedule.unit))
  60. // 12:01:01 PM -> 7:01:01 PM
  61. let lunch = calendar.nextDate(after: midMorning, matching: DateComponents(hour: 12, minute: 01, second: 01), matchingPolicy: .nextTime)!
  62. XCTAssertEqual(8, schedule.quantity(at: lunch).doubleValue(for: schedule.unit))
  63. // 7:00 PM -> 2:00 AM
  64. let dinner = calendar.nextDate(after: midMorning, matching: DateComponents(hour: 19, minute: 0, second: 0), matchingPolicy: .nextTime)!
  65. XCTAssertEqual(10, schedule.quantity(at: dinner).doubleValue(for: schedule.unit))
  66. }
  67. }