CachedCarbObjectTests.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. let semaphore = DispatchSemaphore(value: 0)
  26. cacheStore.onReady { error in
  27. semaphore.signal()
  28. }
  29. semaphore.wait()
  30. cacheStore.managedObjectContext.performAndWait {
  31. let cachedCarbObject = CachedCarbObject(context: cacheStore.managedObjectContext)
  32. cachedCarbObject.absorptionTime = 18000
  33. cachedCarbObject.createdByCurrentApp = true
  34. cachedCarbObject.foodType = "Pizza"
  35. cachedCarbObject.grams = 45
  36. cachedCarbObject.startDate = dateFormatter.date(from: "2020-05-14T22:38:14Z")!
  37. cachedCarbObject.uuid = UUID(uuidString: "2A67A303-5203-4CB8-8263-79498265368E")!
  38. cachedCarbObject.provenanceIdentifier = "238E41EA-9576-4981-A1A4-51E10228584F"
  39. cachedCarbObject.syncIdentifier = "7723A0EE-F6D5-46E0-BBFE-1DEEBF8ED6F2"
  40. cachedCarbObject.syncVersion = 2
  41. cachedCarbObject.userCreatedDate = dateFormatter.date(from: "2020-05-14T22:28:12Z")!
  42. cachedCarbObject.userUpdatedDate = dateFormatter.date(from: "2020-05-14T22:33:47Z")!
  43. cachedCarbObject.userDeletedDate = nil
  44. cachedCarbObject.operation = .update
  45. cachedCarbObject.addedDate = dateFormatter.date(from: "2020-05-14T22:33:48Z")!
  46. cachedCarbObject.supercededDate = nil
  47. cachedCarbObject.anchorKey = 123
  48. try! assertCachedCarbObjectEncodable(cachedCarbObject, encodesJSON: """
  49. {
  50. "absorptionTime" : 18000,
  51. "addedDate" : "2020-05-14T22:33:48Z",
  52. "anchorKey" : 123,
  53. "createdByCurrentApp" : true,
  54. "foodType" : "Pizza",
  55. "grams" : 45,
  56. "operation" : 1,
  57. "provenanceIdentifier" : "238E41EA-9576-4981-A1A4-51E10228584F",
  58. "startDate" : "2020-05-14T22:38:14Z",
  59. "syncIdentifier" : "7723A0EE-F6D5-46E0-BBFE-1DEEBF8ED6F2",
  60. "syncVersion" : 2,
  61. "userCreatedDate" : "2020-05-14T22:28:12Z",
  62. "userUpdatedDate" : "2020-05-14T22:33:47Z",
  63. "uuid" : "2A67A303-5203-4CB8-8263-79498265368E"
  64. }
  65. """
  66. )
  67. }
  68. }
  69. func testEncodableOptional() throws {
  70. cacheStore.managedObjectContext.performAndWait {
  71. let cachedCarbObject = CachedCarbObject(context: cacheStore.managedObjectContext)
  72. cachedCarbObject.createdByCurrentApp = false
  73. cachedCarbObject.grams = 34
  74. cachedCarbObject.startDate = dateFormatter.date(from: "2020-05-15T22:38:14Z")!
  75. cachedCarbObject.provenanceIdentifier = "238E41EA-9576-4981-A1A4-51E10228584F"
  76. cachedCarbObject.operation = .create
  77. cachedCarbObject.anchorKey = 234
  78. try! assertCachedCarbObjectEncodable(cachedCarbObject, encodesJSON: """
  79. {
  80. "anchorKey" : 234,
  81. "createdByCurrentApp" : false,
  82. "grams" : 34,
  83. "operation" : 0,
  84. "provenanceIdentifier" : "238E41EA-9576-4981-A1A4-51E10228584F",
  85. "startDate" : "2020-05-15T22:38:14Z"
  86. }
  87. """
  88. )
  89. }
  90. }
  91. private func assertCachedCarbObjectEncodable(_ original: CachedCarbObject, encodesJSON string: String) throws {
  92. let data = try encoder.encode(original)
  93. XCTAssertEqual(String(data: data, encoding: .utf8), string)
  94. }
  95. private let dateFormatter = ISO8601DateFormatter()
  96. private let encoder: JSONEncoder = {
  97. let encoder = JSONEncoder()
  98. encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
  99. encoder.dateEncodingStrategy = .iso8601
  100. return encoder
  101. }()
  102. private let decoder: JSONDecoder = {
  103. let decoder = JSONDecoder()
  104. decoder.dateDecodingStrategy = .iso8601
  105. return decoder
  106. }()
  107. }
  108. extension CachedCarbObject {
  109. fileprivate func setDefaultValues() {
  110. self.absorptionTime = .hours(3)
  111. self.createdByCurrentApp = true
  112. self.foodType = "Breakfast"
  113. self.grams = 45.6
  114. self.startDate = Date()
  115. self.uuid = UUID()
  116. self.provenanceIdentifier = "CachedCarbObjectTests"
  117. self.syncIdentifier = UUID().uuidString
  118. self.syncVersion = 1
  119. self.userCreatedDate = Date()
  120. self.userUpdatedDate = nil
  121. self.userDeletedDate = nil
  122. self.operation = .create
  123. self.addedDate = Date()
  124. self.supercededDate = nil
  125. }
  126. }