StoredCarbEntryTests.swift 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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: nil,
  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. "quantity" : 19,
  57. "startDate" : "2020-02-03T04:16:18Z"
  58. }
  59. """
  60. )
  61. }
  62. private func assertStoredCarbEntryCodable(_ original: StoredCarbEntry, encodesJSON string: String) throws {
  63. let data = try encoder.encode(original)
  64. XCTAssertEqual(String(data: data, encoding: .utf8), string)
  65. let decoded = try decoder.decode(StoredCarbEntry.self, from: data)
  66. XCTAssertEqual(decoded, original)
  67. }
  68. private let encoder: JSONEncoder = {
  69. let encoder = JSONEncoder()
  70. encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
  71. encoder.dateEncodingStrategy = .iso8601
  72. return encoder
  73. }()
  74. private let decoder: JSONDecoder = {
  75. let decoder = JSONDecoder()
  76. decoder.dateDecodingStrategy = .iso8601
  77. return decoder
  78. }()
  79. private let dateFormatter = ISO8601DateFormatter()
  80. }