-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathSyncEngineTests.swift
More file actions
136 lines (126 loc) · 4.55 KB
/
SyncEngineTests.swift
File metadata and controls
136 lines (126 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#if canImport(CloudKit)
import CloudKit
import CustomDump
import DependenciesTestSupport
import Foundation
import InlineSnapshotTesting
import SQLiteData
import SnapshotTestingCustomDump
import Testing
extension BaseCloudKitTests {
@MainActor
final class SyncEngineTests {
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func inMemory() throws {
#expect(URL(string: "")?.isInMemory == nil)
#expect(URL(string: ":memory:")?.isInMemory == true)
#expect(URL(string: ":memory:?cache=shared")?.isInMemory == true)
#expect(URL(string: "file::memory:")?.isInMemory == true)
#expect(URL(string: "file:memdb1?mode=memory&cache=shared")?.isInMemory == true)
}
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func inMemoryUserDatabase() async throws {
let syncEngine = try await SyncEngine(
container: MockCloudContainer(
containerIdentifier: "test",
privateCloudDatabase: MockCloudDatabase(databaseScope: .private),
sharedCloudDatabase: MockCloudDatabase(databaseScope: .shared)
),
userDatabase: UserDatabase(database: DatabaseQueue()),
tables: []
)
try await syncEngine.userDatabase.read { db in
try #sql(
"""
SELECT 1 FROM "sqlitedata_icloud_metadata"
"""
)
.execute(db)
}
}
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test(.dependency(\.context, .live))
func inMemoryUserDatabase_LiveContext() async throws {
let error = await #expect(throws: (any Error).self) {
try await SyncEngine(
container: MockCloudContainer(
containerIdentifier: "test",
privateCloudDatabase: MockCloudDatabase(databaseScope: .private),
sharedCloudDatabase: MockCloudDatabase(databaseScope: .shared)
),
userDatabase: UserDatabase(database: DatabaseQueue()),
tables: []
)
}
assertInlineSnapshot(of: error, as: .customDump) {
"""
InMemoryDatabase()
"""
}
}
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func metadatabaseMismatch() async throws {
let error = await #expect(throws: SyncEngine.SchemaError.self) {
var configuration = Configuration()
configuration.prepareDatabase { db in
try db.attachMetadatabase(containerIdentifier: "iCloud.co.pointfree")
}
let path = "/tmp/\(UUID()).sqlite"
let database = try DatabasePool(
path: path,
configuration: configuration
)
_ = try await SyncEngine(
container: MockCloudContainer(
containerIdentifier: "iCloud.co.point-free",
privateCloudDatabase: MockCloudDatabase(databaseScope: .private),
sharedCloudDatabase: MockCloudDatabase(databaseScope: .shared)
),
userDatabase: UserDatabase(database: database),
tables: []
)
}
#expect(
error?.debugDescription == """
Metadatabase attached in 'prepareDatabase' does not match metadatabase prepared in \
'SyncEngine.init'. Are different CloudKit container identifiers being provided?
"""
)
}
@Test func isSynchronizingTriggerWarning() {
withKnownIssue {
_ = Reminder.createTemporaryTrigger(
after: .insert { new in
Values(SyncEngine.isSynchronizing)
}
)
} matching: { issue in
issue.description.hasSuffix(
"""
Invoked 'SyncEngine.isSynchronizing' at trigger creation, which is unexpected. Use \
'SyncEngine.$isSynchronizing' to invoke at trigger execution, instead.
"""
)
}
_ = Reminder.createTemporaryTrigger(
after: .insert { new in
Values(SyncEngine.$isSynchronizing)
}
)
}
}
}
private func databaseWithForeignKeys() throws -> any DatabaseWriter {
try DatabaseQueue()
}
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func testSyncEngine() throws {
@Dependency(\.defaultSyncEngine) var syncEngine
_ = syncEngine
}
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test(.dependency(\.context, .preview)) func previewSyncEngine() throws {
@Dependency(\.defaultSyncEngine) var syncEngine
_ = syncEngine
}
#endif