CachedCarbObjectTests.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // CachedCarbObjectTests.swift
  3. // LoopKitTests
  4. //
  5. // Copyright © 2018 LoopKit Authors. All rights reserved.
  6. //
  7. import XCTest
  8. @testable import LoopKit
  9. class CachedCarbObjectTests: PersistenceControllerTestCase {
  10. func testSaveWithDefaultValues() {
  11. cacheStore.managedObjectContext.performAndWait {
  12. let object1 = CachedCarbObject(context: cacheStore.managedObjectContext)
  13. object1.setDefaultValues()
  14. try! cacheStore.managedObjectContext.save()
  15. let object2 = CachedCarbObject(context: cacheStore.managedObjectContext)
  16. object2.setDefaultValues()
  17. try! cacheStore.managedObjectContext.save()
  18. let objects: [CachedCarbObject] = cacheStore.managedObjectContext.all()
  19. XCTAssertEqual(2, objects.count)
  20. }
  21. }
  22. }
  23. class CachedCarbObjectEncodableTests: PersistenceControllerTestCase {
  24. func testEncodable() throws {
  25. cacheStore.managedObjectContext.performAndWait {
  26. let cachedCarbObject = CachedCarbObject(context: cacheStore.managedObjectContext)
  27. cachedCarbObject.absorptionTime = 18000
  28. cachedCarbObject.createdByCurrentApp = true
  29. cachedCarbObject.foodType = "Pizza"
  30. cachedCarbObject.grams = 45
  31. cachedCarbObject.startDate = dateFormatter.date(from: "2020-05-14T22:38:14Z")!
  32. cachedCarbObject.uuid = UUID(uuidString: "2A67A303-5203-4CB8-8263-79498265368E")!
  33. cachedCarbObject.provenanceIdentifier = "238E41EA-9576-4981-A1A4-51E10228584F"
  34. cachedCarbObject.syncIdentifier = "7723A0EE-F6D5-46E0-BBFE-1DEEBF8ED6F2"
  35. cachedCarbObject.syncVersion = 2
  36. cachedCarbObject.userCreatedDate = dateFormatter.date(from: "2020-05-14T22:28:12Z")!
  37. cachedCarbObject.userUpdatedDate = dateFormatter.date(from: "2020-05-14T22:33:47Z")!
  38. cachedCarbObject.userDeletedDate = nil
  39. cachedCarbObject.operation = .update
  40. cachedCarbObject.addedDate = dateFormatter.date(from: "2020-05-14T22:33:48Z")!
  41. cachedCarbObject.supercededDate = nil
  42. cachedCarbObject.anchorKey = 123
  43. try! assertCachedCarbObjectEncodable(cachedCarbObject, encodesJSON: """
  44. {
  45. "absorptionTime" : 18000,
  46. "addedDate" : "2020-05-14T22:33:48Z",
  47. "anchorKey" : 123,
  48. "createdByCurrentApp" : true,
  49. "foodType" : "Pizza",
  50. "grams" : 45,
  51. "operation" : 1,
  52. "provenanceIdentifier" : "238E41EA-9576-4981-A1A4-51E10228584F",
  53. "startDate" : "2020-05-14T22:38:14Z",
  54. "syncIdentifier" : "7723A0EE-F6D5-46E0-BBFE-1DEEBF8ED6F2",
  55. "syncVersion" : 2,
  56. "userCreatedDate" : "2020-05-14T22:28:12Z",
  57. "userUpdatedDate" : "2020-05-14T22:33:47Z",
  58. "uuid" : "2A67A303-5203-4CB8-8263-79498265368E"
  59. }
  60. """
  61. )
  62. }
  63. }
  64. func testEncodableOptional() throws {
  65. cacheStore.managedObjectContext.performAndWait {
  66. let cachedCarbObject = CachedCarbObject(context: cacheStore.managedObjectContext)
  67. cachedCarbObject.createdByCurrentApp = false
  68. cachedCarbObject.grams = 34
  69. cachedCarbObject.startDate = dateFormatter.date(from: "2020-05-15T22:38:14Z")!
  70. cachedCarbObject.operation = .create
  71. cachedCarbObject.anchorKey = 234
  72. try! assertCachedCarbObjectEncodable(cachedCarbObject, encodesJSON: """
  73. {
  74. "anchorKey" : 234,
  75. "createdByCurrentApp" : false,
  76. "grams" : 34,
  77. "operation" : 0,
  78. "startDate" : "2020-05-15T22:38:14Z"
  79. }
  80. """
  81. )
  82. }
  83. }
  84. private func assertCachedCarbObjectEncodable(_ original: CachedCarbObject, encodesJSON string: String) throws {
  85. let data = try encoder.encode(original)
  86. XCTAssertEqual(String(data: data, encoding: .utf8), string)
  87. }
  88. private let dateFormatter = ISO8601DateFormatter()
  89. private let encoder: JSONEncoder = {
  90. let encoder = JSONEncoder()
  91. encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
  92. encoder.dateEncodingStrategy = .iso8601
  93. return encoder
  94. }()
  95. private let decoder: JSONDecoder = {
  96. let decoder = JSONDecoder()
  97. decoder.dateDecodingStrategy = .iso8601
  98. return decoder
  99. }()
  100. }
  101. extension CachedCarbObject {
  102. fileprivate func setDefaultValues() {
  103. self.absorptionTime = .hours(3)
  104. self.createdByCurrentApp = true
  105. self.foodType = "Breakfast"
  106. self.grams = 45.6
  107. self.startDate = Date()
  108. self.uuid = UUID()
  109. self.provenanceIdentifier = "CachedCarbObjectTests"
  110. self.syncIdentifier = UUID().uuidString
  111. self.syncVersion = 1
  112. self.userCreatedDate = Date()
  113. self.userUpdatedDate = nil
  114. self.userDeletedDate = nil
  115. self.operation = .create
  116. self.addedDate = Date()
  117. self.supercededDate = nil
  118. }
  119. }