Skip to content

Commit dfd2359

Browse files
authored
Add a test for what happens with outside records. (#384)
* Add a test for what happens with outside records. * wip * fix tests * Skip records that don't have a userModificationTime.
1 parent f3f7f5f commit dfd2359

5 files changed

Lines changed: 78 additions & 16 deletions

File tree

Sources/SQLiteData/CloudKit/SyncEngine.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
private let notificationsObserver = LockIsolated<(any NSObjectProtocol)?>(nil)
3939
private let activityCounts = LockIsolated(ActivityCounts())
4040
private let startTask = LockIsolated<Task<Void, Never>?>(nil)
41-
#if canImport(DeveloperToolsSupport)
41+
#if DEBUG && canImport(DeveloperToolsSupport)
4242
private let previewTimerTask = LockIsolated<Task<Void, Never>?>(nil)
4343
#endif
4444

@@ -423,7 +423,7 @@
423423
/// You must start the sync engine again using ``start()`` to synchronize the changes.
424424
public func stop() {
425425
guard isRunning else { return }
426-
#if canImport(DeveloperToolsSupport)
426+
#if DEBUG && canImport(DeveloperToolsSupport)
427427
previewTimerTask.withValue {
428428
$0?.cancel()
429429
$0 = nil
@@ -503,7 +503,7 @@
503503
}
504504
)
505505

506-
#if canImport(DeveloperToolsSupport)
506+
#if DEBUG && canImport(DeveloperToolsSupport)
507507
@Dependency(\.context) var context
508508
@Dependency(\.continuousClock) var clock
509509
if context == .preview {
@@ -1892,8 +1892,12 @@
18921892
db: Database
18931893
) {
18941894
withErrorReporting(.sqliteDataCloudKitFailure) {
1895-
guard let recordPrimaryKey = serverRecord.recordID.recordPrimaryKey
1896-
else { return }
1895+
guard
1896+
let recordPrimaryKey = serverRecord.recordID.recordPrimaryKey,
1897+
serverRecord.encryptedValues[CKRecord.userModificationTimeKey] != nil
1898+
else {
1899+
return
1900+
}
18971901

18981902
try SyncMetadata.insert {
18991903
SyncMetadata(

Tests/SQLiteDataTests/CloudKitTests/CloudKitTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@
860860
}
861861

862862
let record = try syncEngine.private.database.record(for: ModelA.recordID(for: 1))
863-
record.encryptedValues["isEven"] = false
863+
record.setValue(false, forKey: "isEven", at: 0)
864864
try await syncEngine.modifyRecords(scope: .private, saving: [record]).notify()
865865

866866
assertInlineSnapshot(of: container, as: .customDump) {

Tests/SQLiteDataTests/CloudKitTests/FetchRecordZoneChangesTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@
508508
recordType: Tag.tableName,
509509
recordID: Tag.recordID(for: "tag")
510510
)
511-
tagRecord.encryptedValues["title"] = "tag"
511+
tagRecord.setValue("tag", forKey: "title", at: 0)
512512
try await syncEngine.modifyRecords(scope: .private, saving: [tagRecord]).notify()
513513

514514
assertQuery(Tag.all, database: userDatabase.database) {
@@ -583,7 +583,7 @@
583583
recordType: Tag.tableName,
584584
recordID: Tag.recordID(for: "tag")
585585
)
586-
tagRecord.encryptedValues["title"] = "tag"
586+
tagRecord.setValue("tag", forKey: "title", at: 0)
587587
let modifications = try syncEngine.modifyRecords(scope: .private, saving: [tagRecord])
588588

589589
try await userDatabase.userWrite { db in

Tests/SQLiteDataTests/CloudKitTests/PreviewTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if canImport(CloudKit)
1+
#if DEBUG && canImport(DeveloperToolsSupport) && canImport(CloudKit)
22
import DependenciesTestSupport
33
import InlineSnapshotTesting
44
import SnapshotTestingCustomDump

Tests/SQLiteDataTests/CloudKitTests/SchemaChangeTests.swift

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,7 @@
846846
saving: [imageRecord]
847847
)
848848
.notify()
849+
syncEngine.stop()
849850

850851
inMemoryDataManager.storage.withValue { $0.removeAll() }
851852

@@ -870,16 +871,73 @@
870871
)
871872
defer { _ = relaunchedSyncEngine }
872873

873-
let images = try await userDatabase.read { db in
874-
try Image.order(by: \.id).fetchAll(db)
874+
try await userDatabase.read { db in
875+
expectNoDifference(
876+
try Image.order(by: \.id).fetchAll(db),
877+
[
878+
Image(id: 1, image: Data("image".utf8), caption: "A good image")
879+
]
880+
)
875881
}
876882

877-
expectNoDifference(
878-
images,
879-
[
880-
Image(id: 1, image: Data("image".utf8), caption: "A good image")
881-
]
883+
assertInlineSnapshot(of: relaunchedSyncEngine.container, as: .customDump) {
884+
"""
885+
MockCloudContainer(
886+
privateCloudDatabase: MockCloudDatabase(
887+
databaseScope: .private,
888+
storage: [
889+
[0]: CKRecord(
890+
recordID: CKRecord.ID(1:images/zone/__defaultOwner__),
891+
recordType: "images",
892+
parent: nil,
893+
share: nil,
894+
caption: "A good image",
895+
id: "1",
896+
image: Data(5 bytes)
897+
)
898+
]
899+
),
900+
sharedCloudDatabase: MockCloudDatabase(
901+
databaseScope: .shared,
902+
storage: []
903+
)
904+
)
905+
"""
906+
}
907+
}
908+
}
909+
910+
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
911+
@Test func outsideRecord() async throws {
912+
let customRecord = CKRecord(
913+
recordType: "customRecord",
914+
recordID: CKRecord.ID(
915+
recordName: "customRecord",
916+
zoneID: SyncEngine.defaultTestZone.zoneID
882917
)
918+
)
919+
try await syncEngine.modifyRecords(scope: .private, saving: [customRecord]).notify()
920+
assertQuery(SyncMetadata.all, database: syncEngine.metadatabase) {
921+
"""
922+
(No results)
923+
"""
924+
}
925+
}
926+
927+
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
928+
@Test func outsideRecordWithColon() async throws {
929+
let customRecord = CKRecord(
930+
recordType: "customRecord",
931+
recordID: CKRecord.ID(
932+
recordName: "1:customRecord",
933+
zoneID: SyncEngine.defaultTestZone.zoneID
934+
)
935+
)
936+
try await syncEngine.modifyRecords(scope: .private, saving: [customRecord]).notify()
937+
assertQuery(SyncMetadata.all, database: syncEngine.metadatabase) {
938+
"""
939+
(No results)
940+
"""
883941
}
884942
}
885943
}

0 commit comments

Comments
 (0)