StoredCarbEntryTests.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // StoredCarbEntryTests.swift
  3. // LoopKitTests
  4. //
  5. // Created by Darin Krauss on 10/6/20.
  6. // Copyright © 2020 LoopKit Authors. All rights reserved.
  7. //
  8. import XCTest
  9. import HealthKit
  10. @testable import LoopKit
  11. class StoredCarbEntryCodableTests: XCTestCase {
  12. func testCodable() throws {
  13. let storedCarbEntry = StoredCarbEntry(uuid: UUID(uuidString: "18CF3948-0B3D-4B12-8BFE-14986B0E6784")!,
  14. provenanceIdentifier: "com.loopkit.loop",
  15. syncIdentifier: "2B03D96C-6F5D-4140-99CD-80C3E64D6010",
  16. syncVersion: 2,
  17. startDate: dateFormatter.date(from: "2020-01-02T03:00:23Z")!,
  18. quantity: HKQuantity(unit: .gram(), doubleValue: 19),
  19. foodType: "Pizza",
  20. absorptionTime: .hours(5),
  21. createdByCurrentApp: true,
  22. userCreatedDate: dateFormatter.date(from: "2020-01-02T03:03:34Z")!,
  23. userUpdatedDate: dateFormatter.date(from: "2020-01-02T03:05:45Z")!)
  24. try! assertStoredCarbEntryCodable(storedCarbEntry, encodesJSON: """
  25. {
  26. "absorptionTime" : 18000,
  27. "createdByCurrentApp" : true,
  28. "foodType" : "Pizza",
  29. "provenanceIdentifier" : "com.loopkit.loop",
  30. "quantity" : 19,
  31. "startDate" : "2020-01-02T03:00:23Z",
  32. "syncIdentifier" : "2B03D96C-6F5D-4140-99CD-80C3E64D6010",
  33. "syncVersion" : 2,
  34. "userCreatedDate" : "2020-01-02T03:03:34Z",
  35. "userUpdatedDate" : "2020-01-02T03:05:45Z",
  36. "uuid" : "18CF3948-0B3D-4B12-8BFE-14986B0E6784"
  37. }
  38. """
  39. )
  40. }
  41. func testCodableOptional() throws {
  42. let storedCarbEntry = StoredCarbEntry(uuid: nil,
  43. provenanceIdentifier: "com.loopkit.loop",
  44. syncIdentifier: nil,
  45. syncVersion: nil,
  46. startDate: dateFormatter.date(from: "2020-02-03T04:16:18Z")!,
  47. quantity: HKQuantity(unit: .gram(), doubleValue: 19),
  48. foodType: nil,
  49. absorptionTime: nil,
  50. createdByCurrentApp: false,
  51. userCreatedDate: nil,
  52. userUpdatedDate: nil)
  53. try! assertStoredCarbEntryCodable(storedCarbEntry, encodesJSON: """
  54. {
  55. "createdByCurrentApp" : false,
  56. "provenanceIdentifier" : "com.loopkit.loop",
  57. "quantity" : 19,
  58. "startDate" : "2020-02-03T04:16:18Z"
  59. }
  60. """
  61. )
  62. }
  63. private func assertStoredCarbEntryCodable(_ original: StoredCarbEntry, encodesJSON string: String) throws {
  64. let data = try encoder.encode(original)
  65. XCTAssertEqual(String(data: data, encoding: .utf8), string)
  66. let decoded = try decoder.decode(StoredCarbEntry.self, from: data)
  67. XCTAssertEqual(decoded, original)
  68. }
  69. private let encoder: JSONEncoder = {
  70. let encoder = JSONEncoder()
  71. encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
  72. encoder.dateEncodingStrategy = .iso8601
  73. return encoder
  74. }()
  75. private let decoder: JSONDecoder = {
  76. let decoder = JSONDecoder()
  77. decoder.dateDecodingStrategy = .iso8601
  78. return decoder
  79. }()
  80. private let dateFormatter = ISO8601DateFormatter()
  81. }