DeviceLogEntryTests.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // DeviceLogEntryTests.swift
  3. // LoopKitTests
  4. //
  5. // Created by Darin Krauss on 8/26/20.
  6. // Copyright © 2020 LoopKit Authors. All rights reserved.
  7. //
  8. import XCTest
  9. import CoreData
  10. @testable import LoopKit
  11. class DeviceLogEntryEncodableTests: XCTestCase {
  12. private var persistentContainer: NSPersistentContainer!
  13. private var managedObjectContext: NSManagedObjectContext!
  14. override func setUp() {
  15. super.setUp()
  16. let persistentStoreDescription = NSPersistentStoreDescription()
  17. persistentStoreDescription.type = NSInMemoryStoreType
  18. persistentContainer = PersistentContainer(name: "DeviceLog")
  19. persistentContainer.persistentStoreDescriptions = [persistentStoreDescription]
  20. persistentContainer.loadPersistentStores { (_, _) in }
  21. managedObjectContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  22. managedObjectContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy
  23. managedObjectContext.automaticallyMergesChangesFromParent = true
  24. managedObjectContext.persistentStoreCoordinator = persistentContainer.persistentStoreCoordinator
  25. }
  26. override func tearDown() {
  27. managedObjectContext = nil
  28. persistentContainer = nil
  29. super.tearDown()
  30. }
  31. func testEncodable() throws {
  32. managedObjectContext.performAndWait {
  33. let deviceLogEntry = DeviceLogEntry(context: managedObjectContext)
  34. deviceLogEntry.type = .connection
  35. deviceLogEntry.managerIdentifier = "238E41EA-9576-4981-A1A4-51E10228584F"
  36. deviceLogEntry.deviceIdentifier = "7723A0EE-F6D5-46E0-BBFE-1DEEBF8ED6F2"
  37. deviceLogEntry.message = "This is the message"
  38. deviceLogEntry.timestamp = dateFormatter.date(from: "2020-05-14T22:38:14Z")!
  39. deviceLogEntry.modificationCounter = 123
  40. try! assertDeviceLogEntryEncodable(deviceLogEntry, encodesJSON: """
  41. {
  42. "deviceIdentifier" : "7723A0EE-F6D5-46E0-BBFE-1DEEBF8ED6F2",
  43. "managerIdentifier" : "238E41EA-9576-4981-A1A4-51E10228584F",
  44. "message" : "This is the message",
  45. "modificationCounter" : 123,
  46. "timestamp" : "2020-05-14T22:38:14Z",
  47. "type" : "connection"
  48. }
  49. """
  50. )
  51. }
  52. }
  53. func testEncodableOptional() throws {
  54. managedObjectContext.performAndWait {
  55. let deviceLogEntry = DeviceLogEntry(context: managedObjectContext)
  56. deviceLogEntry.modificationCounter = 234
  57. try! assertDeviceLogEntryEncodable(deviceLogEntry, encodesJSON: """
  58. {
  59. "modificationCounter" : 234
  60. }
  61. """
  62. )
  63. }
  64. }
  65. private func assertDeviceLogEntryEncodable(_ original: DeviceLogEntry, encodesJSON string: String) throws {
  66. let data = try encoder.encode(original)
  67. XCTAssertEqual(String(data: data, encoding: .utf8), string)
  68. }
  69. private let dateFormatter = ISO8601DateFormatter()
  70. private let encoder: JSONEncoder = {
  71. let encoder = JSONEncoder()
  72. encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
  73. encoder.dateEncodingStrategy = .iso8601
  74. return encoder
  75. }()
  76. private let decoder: JSONDecoder = {
  77. let decoder = JSONDecoder()
  78. decoder.dateDecodingStrategy = .iso8601
  79. return decoder
  80. }()
  81. }