PersistentDeviceLogTests.swift 4.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // PersistentDeviceLogTests.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 LoopKit
  10. class PersistentDeviceLogCriticalEventLogTests: XCTestCase {
  11. var persistentDeviceLog: PersistentDeviceLog!
  12. var outputStream: MockOutputStream!
  13. var progress: Progress!
  14. override func setUp() {
  15. super.setUp()
  16. let entries = [StoredDeviceLogEntry(type: .delegateResponse, managerIdentifier: "m1", deviceIdentifier: "d1", message: "Message 1", timestamp: dateFormatter.date(from: "2100-01-02T03:08:00Z")!),
  17. StoredDeviceLogEntry(type: .receive, managerIdentifier: "m2", deviceIdentifier: "d2", message: "Message 2", timestamp: dateFormatter.date(from: "2100-01-02T03:10:00Z")!),
  18. StoredDeviceLogEntry(type: .send, managerIdentifier: "m3", deviceIdentifier: "d3", message: "Message 3", timestamp: dateFormatter.date(from: "2100-01-02T03:04:00Z")!),
  19. StoredDeviceLogEntry(type: .delegate, managerIdentifier: "m4", deviceIdentifier: "d4", message: "Message 4", timestamp: dateFormatter.date(from: "2100-01-02T03:06:00Z")!),
  20. StoredDeviceLogEntry(type: .connection, managerIdentifier: "m5", deviceIdentifier: "d5", message: "Message 5", timestamp: dateFormatter.date(from: "2100-01-02T03:02:00Z")!)]
  21. persistentDeviceLog = PersistentDeviceLog(storageFile: URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(UUID().uuidString), maxEntryAge: .hours(1))
  22. XCTAssertNil(persistentDeviceLog.addStoredDeviceLogEntries(entries: entries))
  23. outputStream = MockOutputStream()
  24. progress = Progress()
  25. }
  26. override func tearDown() {
  27. persistentDeviceLog = nil
  28. super.tearDown()
  29. }
  30. func testExportProgressTotalUnitCount() {
  31. switch persistentDeviceLog.exportProgressTotalUnitCount(startDate: dateFormatter.date(from: "2100-01-02T03:03:00Z")!,
  32. endDate: dateFormatter.date(from: "2100-01-02T03:09:00Z")!) {
  33. case .failure(let error):
  34. XCTFail("Unexpected failure: \(error)")
  35. case .success(let progressTotalUnitCount):
  36. XCTAssertEqual(progressTotalUnitCount, 3 * 1)
  37. }
  38. }
  39. func testExportProgressTotalUnitCountEmpty() {
  40. switch persistentDeviceLog.exportProgressTotalUnitCount(startDate: dateFormatter.date(from: "2100-01-02T03:00:00Z")!,
  41. endDate: dateFormatter.date(from: "2100-01-02T03:01:00Z")!) {
  42. case .failure(let error):
  43. XCTFail("Unexpected failure: \(error)")
  44. case .success(let progressTotalUnitCount):
  45. XCTAssertEqual(progressTotalUnitCount, 0)
  46. }
  47. }
  48. func testExport() {
  49. XCTAssertNil(persistentDeviceLog.export(startDate: dateFormatter.date(from: "2100-01-02T03:03:00Z")!,
  50. endDate: dateFormatter.date(from: "2100-01-02T03:09:00Z")!,
  51. to: outputStream,
  52. progress: progress))
  53. XCTAssertEqual(outputStream.string, """
  54. [
  55. {"deviceIdentifier":"d1","managerIdentifier":"m1","message":"Message 1","modificationCounter":1,"timestamp":"2100-01-02T03:08:00.000Z","type":"delegateResponse"},
  56. {"deviceIdentifier":"d3","managerIdentifier":"m3","message":"Message 3","modificationCounter":3,"timestamp":"2100-01-02T03:04:00.000Z","type":"send"},
  57. {"deviceIdentifier":"d4","managerIdentifier":"m4","message":"Message 4","modificationCounter":4,"timestamp":"2100-01-02T03:06:00.000Z","type":"delegate"}
  58. ]
  59. """
  60. )
  61. XCTAssertEqual(progress.completedUnitCount, 3 * 1)
  62. }
  63. func testExportEmpty() {
  64. XCTAssertNil(persistentDeviceLog.export(startDate: dateFormatter.date(from: "2100-01-02T03:00:00Z")!,
  65. endDate: dateFormatter.date(from: "2100-01-02T03:01:00Z")!,
  66. to: outputStream,
  67. progress: progress))
  68. XCTAssertEqual(outputStream.string, "[]")
  69. XCTAssertEqual(progress.completedUnitCount, 0)
  70. }
  71. func testExportCancelled() {
  72. progress.cancel()
  73. XCTAssertEqual(persistentDeviceLog.export(startDate: dateFormatter.date(from: "2100-01-02T03:03:00Z")!,
  74. endDate: dateFormatter.date(from: "2100-01-02T03:09:00Z")!,
  75. to: outputStream,
  76. progress: progress) as? CriticalEventLogError, CriticalEventLogError.cancelled)
  77. }
  78. private let dateFormatter = ISO8601DateFormatter()
  79. }