|
Bogdan Timofte
authored
2 weeks ago
|
1
|
import CoreData
|
|
|
2
|
import HealthKit
|
|
|
3
|
import XCTest
|
|
|
4
|
@testable import HealthProbe
|
|
|
5
|
|
|
|
6
|
final class CoreDataArchiveCacheStoreTests: XCTestCase {
|
|
|
7
|
private var temporaryDirectory: URL!
|
|
|
8
|
|
|
|
9
|
override func setUpWithError() throws {
|
|
|
10
|
temporaryDirectory = FileManager.default.temporaryDirectory
|
|
|
11
|
.appending(path: "HealthProbeCacheTests-\(UUID().uuidString)", directoryHint: .isDirectory)
|
|
|
12
|
try FileManager.default.createDirectory(at: temporaryDirectory, withIntermediateDirectories: true)
|
|
|
13
|
}
|
|
|
14
|
|
|
|
15
|
override func tearDownWithError() throws {
|
|
|
16
|
if let temporaryDirectory {
|
|
|
17
|
try? FileManager.default.removeItem(at: temporaryDirectory)
|
|
|
18
|
}
|
|
|
19
|
temporaryDirectory = nil
|
|
|
20
|
}
|
|
|
21
|
|
|
|
22
|
func testRebuildCreatesCoreDataRowsFromSQLiteArchive() async throws {
|
|
|
23
|
let archiveURL = temporaryDirectory.appending(path: "Archive.sqlite")
|
|
|
24
|
let archive = SQLiteHealthArchiveStore(databaseURL: archiveURL)
|
|
|
25
|
let firstSample = makeStepCountSample(value: 42, start: 1_000)
|
|
|
26
|
let secondSample = makeStepCountSample(value: 7, start: 2_000)
|
|
|
27
|
|
|
|
28
|
_ = try await archive.upsertSamples([firstSample], observedAt: Date(timeIntervalSince1970: 3_000))
|
|
|
29
|
_ = try await archive.upsertSamples([firstSample, secondSample], observedAt: Date(timeIntervalSince1970: 3_060))
|
|
|
30
|
try await archive.recordDisappearance(
|
|
|
31
|
sampleUUIDHash: HashService.sampleUUIDHash(firstSample.uuid.uuidString),
|
|
|
32
|
sampleTypeIdentifier: HKQuantityTypeIdentifier.stepCount.rawValue,
|
|
|
33
|
observedMissingAt: Date(timeIntervalSince1970: 3_120)
|
|
|
34
|
)
|
|
|
35
|
try await archive.markVerification(
|
|
|
36
|
sampleType: firstSample.sampleType,
|
|
|
37
|
verifiedAt: Date(timeIntervalSince1970: 3_180)
|
|
|
38
|
)
|
|
|
39
|
|
|
|
40
|
let cache = try CoreDataArchiveCacheStore(inMemory: true)
|
|
|
41
|
let summary = try cache.rebuild(fromArchiveAt: archiveURL)
|
|
|
42
|
let context = cache.container.viewContext
|
|
|
43
|
|
|
|
44
|
XCTAssertEqual(summary.observationRows, 4)
|
|
Bogdan Timofte
authored
2 weeks ago
|
45
|
XCTAssertEqual(try cache.observationCount(), 4)
|
|
Bogdan Timofte
authored
2 weeks ago
|
46
|
XCTAssertEqual(summary.typeSummaryRows, 4)
|
|
|
47
|
XCTAssertGreaterThanOrEqual(summary.dailyAggregateRows, 1)
|
|
|
48
|
XCTAssertEqual(summary.archiveHealthRows, 1)
|
|
|
49
|
XCTAssertEqual(try count("CachedObservationRow", in: context), 4)
|
|
|
50
|
XCTAssertEqual(try count("CachedTypeSummary", in: context), 4)
|
|
|
51
|
XCTAssertEqual(try count("CachedArchiveHealth", in: context), 1)
|
|
|
52
|
|
|
|
53
|
let latestObservation = try fetchFirst(
|
|
|
54
|
"CachedObservationRow",
|
|
|
55
|
predicate: NSPredicate(format: "observationID == %d", 4),
|
|
|
56
|
in: context
|
|
|
57
|
)
|
|
|
58
|
XCTAssertEqual(latestObservation?.value(forKey: "visibleRecordCount") as? Int64, 1)
|
|
|
59
|
XCTAssertEqual(latestObservation?.value(forKey: "cacheSchemaVersion") as? Int64, Int64(CoreDataArchiveCacheStore.cacheSchemaVersion))
|
|
Bogdan Timofte
authored
2 weeks ago
|
60
|
|
|
|
61
|
let latestRow = try XCTUnwrap(cache.latestObservationRow())
|
|
|
62
|
XCTAssertEqual(latestRow.observationID, 4)
|
|
|
63
|
XCTAssertEqual(latestRow.visibleRecordCount, 1)
|
|
|
64
|
XCTAssertEqual(latestRow.cacheSchemaVersion, CoreDataArchiveCacheStore.cacheSchemaVersion)
|
|
|
65
|
|
|
|
66
|
let summaries = try cache.typeSummaries(observationID: latestRow.observationID)
|
|
|
67
|
XCTAssertEqual(summaries.count, 1)
|
|
|
68
|
XCTAssertEqual(summaries.first?.sampleTypeIdentifier, HKQuantityTypeIdentifier.stepCount.rawValue)
|
|
|
69
|
|
|
Bogdan Timofte
authored
2 weeks ago
|
70
|
let diff = try XCTUnwrap(cache.diffSummary(
|
|
|
71
|
fromObservationID: 1,
|
|
|
72
|
toObservationID: 2,
|
|
|
73
|
sampleTypeIdentifier: HKQuantityTypeIdentifier.stepCount.rawValue
|
|
|
74
|
))
|
|
|
75
|
XCTAssertEqual(diff.appearedCount, 1)
|
|
|
76
|
XCTAssertEqual(diff.disappearedCount, 0)
|
|
|
77
|
XCTAssertEqual(diff.representationChangedCount, 0)
|
|
|
78
|
|
|
Bogdan Timofte
authored
2 weeks ago
|
79
|
let health = try XCTUnwrap(cache.latestArchiveHealthStatus())
|
|
|
80
|
XCTAssertEqual(health.archiveSchemaVersion, 2)
|
|
|
81
|
XCTAssertEqual(health.lastIntegrityStatus, "ok")
|
|
Bogdan Timofte
authored
2 weeks ago
|
82
|
}
|
|
|
83
|
|
|
|
84
|
func testDeletingCacheDoesNotDeleteSQLiteArchiveAndRebuildRestoresRows() async throws {
|
|
|
85
|
let archiveURL = temporaryDirectory.appending(path: "Archive.sqlite")
|
|
|
86
|
let archive = SQLiteHealthArchiveStore(databaseURL: archiveURL)
|
|
|
87
|
let sample = makeStepCountSample(value: 10, start: 1_000)
|
|
|
88
|
_ = try await archive.upsertSamples([sample], observedAt: Date(timeIntervalSince1970: 2_000))
|
|
|
89
|
|
|
|
90
|
let cache = try CoreDataArchiveCacheStore(inMemory: true)
|
|
|
91
|
_ = try cache.rebuild(fromArchiveAt: archiveURL)
|
|
|
92
|
try cache.deleteCache()
|
|
|
93
|
|
|
Bogdan Timofte
authored
2 weeks ago
|
94
|
XCTAssertEqual(try cache.observationCount(), 0)
|
|
Bogdan Timofte
authored
2 weeks ago
|
95
|
XCTAssertEqual(try count("CachedObservationRow", in: cache.container.viewContext), 0)
|
|
|
96
|
let integrityReport = try await archive.checkIntegrity()
|
|
|
97
|
XCTAssertTrue(integrityReport.passed)
|
|
|
98
|
|
|
|
99
|
let rebuilt = try cache.rebuild(fromArchiveAt: archiveURL)
|
|
|
100
|
XCTAssertEqual(rebuilt.observationRows, 1)
|
|
|
101
|
XCTAssertEqual(try count("CachedObservationRow", in: cache.container.viewContext), 1)
|
|
|
102
|
}
|
|
|
103
|
|
|
|
104
|
private func makeStepCountSample(value: Double, start: TimeInterval, end: TimeInterval? = nil) -> HKQuantitySample {
|
|
|
105
|
let quantityType = HKQuantityType.quantityType(forIdentifier: .stepCount)!
|
|
|
106
|
let quantity = HKQuantity(unit: .count(), doubleValue: value)
|
|
|
107
|
let startDate = Date(timeIntervalSince1970: start)
|
|
|
108
|
let endDate = Date(timeIntervalSince1970: end ?? (start + 300))
|
|
|
109
|
return HKQuantitySample(type: quantityType, quantity: quantity, start: startDate, end: endDate)
|
|
|
110
|
}
|
|
|
111
|
|
|
|
112
|
private func count(_ entityName: String, in context: NSManagedObjectContext) throws -> Int {
|
|
|
113
|
let request = NSFetchRequest<NSManagedObject>(entityName: entityName)
|
|
|
114
|
return try context.count(for: request)
|
|
|
115
|
}
|
|
|
116
|
|
|
|
117
|
private func fetchFirst(
|
|
|
118
|
_ entityName: String,
|
|
|
119
|
predicate: NSPredicate,
|
|
|
120
|
in context: NSManagedObjectContext
|
|
|
121
|
) throws -> NSManagedObject? {
|
|
|
122
|
let request = NSFetchRequest<NSManagedObject>(entityName: entityName)
|
|
|
123
|
request.predicate = predicate
|
|
|
124
|
request.fetchLimit = 1
|
|
|
125
|
return try context.fetch(request).first
|
|
|
126
|
}
|
|
|
127
|
}
|