forked from pointfreeco/sqlite-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssetsTests.swift
More file actions
335 lines (310 loc) · 12.2 KB
/
AssetsTests.swift
File metadata and controls
335 lines (310 loc) · 12.2 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#if canImport(CloudKit)
import CloudKit
import ConcurrencyExtras
import CustomDump
import InlineSnapshotTesting
import OrderedCollections
import SQLiteData
import SQLiteDataTestSupport
import SnapshotTestingCustomDump
import Testing
extension BaseCloudKitTests {
@MainActor
final class AssetsTests: BaseCloudKitTests, @unchecked Sendable {
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func basics() async throws {
try await userDatabase.userWrite { db in
try db.seed {
RemindersList(id: 1, title: "Personal")
RemindersListAsset(remindersListID: 1, coverImage: Data("image".utf8))
}
}
try await syncEngine.processPendingRecordZoneChanges(scope: .private)
assertInlineSnapshot(of: container, as: .customDump) {
"""
MockCloudContainer(
privateCloudDatabase: MockCloudDatabase(
databaseScope: .private,
storage: [
[0]: CKRecord(
recordID: CKRecord.ID(1:remindersListAssets/zone/__defaultOwner__),
recordType: "remindersListAssets",
parent: CKReference(recordID: CKRecord.ID(1:remindersLists/zone/__defaultOwner__)),
share: nil,
coverImage_hash: Data(32 bytes),
remindersListID: 1,
coverImage: CKAsset(
fileURL: URL(file:///6105d6cc76af400325e94d588ce511be5bfdbb73b437dc51eca43917d7a43e3d),
dataString: "image"
)
),
[1]: CKRecord(
recordID: CKRecord.ID(1:remindersLists/zone/__defaultOwner__),
recordType: "remindersLists",
parent: nil,
share: nil,
id: 1,
title: "Personal"
)
]
),
sharedCloudDatabase: MockCloudDatabase(
databaseScope: .shared,
storage: []
)
)
"""
}
inMemoryDataManager.storage.withValue { storage in
let url = URL(
string: "file:///6105d6cc76af400325e94d588ce511be5bfdbb73b437dc51eca43917d7a43e3d"
)!
#expect(storage[url] == Data("image".utf8))
}
try await withDependencies {
$0.currentTime.now += 1
} operation: {
try await userDatabase.userWrite { db in
try RemindersListAsset
.find(1)
.update { $0.coverImage = Data("new-image".utf8) }
.execute(db)
}
}
try await syncEngine.processPendingRecordZoneChanges(scope: .private)
assertInlineSnapshot(of: container, as: .customDump) {
"""
MockCloudContainer(
privateCloudDatabase: MockCloudDatabase(
databaseScope: .private,
storage: [
[0]: CKRecord(
recordID: CKRecord.ID(1:remindersListAssets/zone/__defaultOwner__),
recordType: "remindersListAssets",
parent: CKReference(recordID: CKRecord.ID(1:remindersLists/zone/__defaultOwner__)),
share: nil,
coverImage_hash: Data(32 bytes),
remindersListID: 1,
coverImage: CKAsset(
fileURL: URL(file:///97e67a5645969953f1a4cfe2ea75649864ff99789189cdd3f6db03e59f8a8ebf),
dataString: "new-image"
)
),
[1]: CKRecord(
recordID: CKRecord.ID(1:remindersLists/zone/__defaultOwner__),
recordType: "remindersLists",
parent: nil,
share: nil,
id: 1,
title: "Personal"
)
]
),
sharedCloudDatabase: MockCloudDatabase(
databaseScope: .shared,
storage: []
)
)
"""
}
inMemoryDataManager.storage.withValue { storage in
let url = URL(
string: "file:///97e67a5645969953f1a4cfe2ea75649864ff99789189cdd3f6db03e59f8a8ebf"
)!
#expect(storage[url] == Data("new-image".utf8))
}
}
// * Receive record with CKAsset from CloudKit
// => Stored in database as bytes
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func receiveAsset() async throws {
let remindersListRecord = CKRecord(
recordType: RemindersList.tableName,
recordID: RemindersList.recordID(for: 1)
)
remindersListRecord.setValue("1", forKey: "id", at: now)
remindersListRecord.setValue("Personal", forKey: "title", at: now)
let fileURL = URL(fileURLWithPath: UUID().uuidString)
try inMemoryDataManager.save(Data("image".utf8), to: fileURL)
let remindersListAssetRecord = CKRecord(
recordType: RemindersListAsset.tableName,
recordID: RemindersListAsset.recordID(for: 1)
)
remindersListAssetRecord.setValue("1", forKey: "id", at: now)
remindersListAssetRecord.setAsset(CKAsset(fileURL: fileURL), forKey: "coverImage", at: now)
remindersListAssetRecord.setValue("1", forKey: "remindersListID", at: now)
remindersListAssetRecord.parent = CKRecord.Reference(
record: remindersListRecord,
action: .none
)
try await syncEngine.modifyRecords(
scope: .private,
saving: [remindersListAssetRecord, remindersListRecord]
)
.notify()
try await userDatabase.read { db in
let remindersListAsset = try #require(
try RemindersListAsset.find(1).fetchOne(db)
)
#expect(remindersListAsset.coverImage == Data("image".utf8))
}
}
// * Receive record with CKAsset from CloudKit when local asset exists
// => Stored in database as bytes
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func receiveUpdatedAsset() async throws {
try await userDatabase.userWrite { db in
try db.seed {
RemindersList(id: 1, title: "Personal")
RemindersListAsset(remindersListID: 1, coverImage: Data("image".utf8))
}
}
try await syncEngine.processPendingRecordZoneChanges(scope: .private)
try await withDependencies {
$0.currentTime.now += 1
} operation: {
let fileURL = URL(fileURLWithPath: UUID().uuidString)
try inMemoryDataManager.save(Data("new-image".utf8), to: fileURL)
let remindersListAssetRecord = try syncEngine.private.database.record(
for: RemindersListAsset.recordID(for: 1)
)
remindersListAssetRecord.setAsset(
CKAsset(fileURL: fileURL),
forKey: "coverImage",
at: now
)
try await syncEngine.modifyRecords(
scope: .private,
saving: [remindersListAssetRecord]
)
.notify()
}
try await userDatabase.read { db in
let remindersListAsset = try #require(
try RemindersListAsset.find(1).fetchOne(db)
)
#expect(remindersListAsset.coverImage == Data("new-image".utf8))
}
}
// * Receive record with CKAsset from CloudKit when local asset does not exist
// * Receive updated asset from CloudKit
// => Local database has freshest asset
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func receiveAssetThenReceiveUpdate() async throws {
do {
let remindersListRecord = CKRecord(
recordType: RemindersList.tableName,
recordID: RemindersList.recordID(for: 1)
)
remindersListRecord.setValue("1", forKey: "id", at: now)
remindersListRecord.setValue("Personal", forKey: "title", at: now)
let fileURL = URL(fileURLWithPath: UUID().uuidString)
try inMemoryDataManager.save(Data("image".utf8), to: fileURL)
let remindersListAssetRecord = CKRecord(
recordType: RemindersListAsset.tableName,
recordID: RemindersListAsset.recordID(for: 1)
)
remindersListAssetRecord.setValue("1", forKey: "id", at: now)
remindersListAssetRecord.setAsset(
CKAsset(fileURL: fileURL),
forKey: "coverImage",
at: now
)
remindersListAssetRecord.setValue("1", forKey: "remindersListID", at: now)
remindersListAssetRecord.parent = CKRecord.Reference(
record: remindersListRecord,
action: .none
)
try await syncEngine.modifyRecords(
scope: .private,
saving: [remindersListAssetRecord, remindersListRecord]
)
.notify()
}
try await withDependencies {
$0.currentTime.now += 1
} operation: {
let fileURL = URL(fileURLWithPath: UUID().uuidString)
try inMemoryDataManager.save(Data("new-image".utf8), to: fileURL)
let remindersListAssetRecord = try syncEngine.private.database.record(
for: RemindersListAsset.recordID(for: 1)
)
remindersListAssetRecord.setAsset(
CKAsset(fileURL: fileURL),
forKey: "coverImage",
at: now
)
try await syncEngine.modifyRecords(
scope: .private,
saving: [remindersListAssetRecord]
)
.notify()
}
try await userDatabase.read { db in
let remindersListAsset = try #require(
try RemindersListAsset.find(1).fetchOne(db)
)
#expect(remindersListAsset.coverImage == Data("new-image".utf8))
}
}
// * Client receives RemindersListAsset with image data
// * A moment later client receives the parent RemindersList
// => Both records (and the image data) should be synchronized
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func assetReceivedBeforeParentRecord() async throws {
let remindersListRecord = CKRecord(
recordType: RemindersList.tableName,
recordID: RemindersList.recordID(for: 1)
)
remindersListRecord.setValue("1", forKey: "id", at: now)
remindersListRecord.setValue("Personal", forKey: "title", at: now)
let remindersListAssetRecord = CKRecord(
recordType: RemindersListAsset.tableName,
recordID: RemindersListAsset.recordID(for: 1)
)
remindersListAssetRecord.setValue("1", forKey: "id", at: now)
remindersListAssetRecord.setValue(
Array("image".utf8),
forKey: "coverImage",
at: now
)
remindersListAssetRecord.setValue(
"1",
forKey: "remindersListID",
at: now
)
remindersListAssetRecord.parent = CKRecord.Reference(
record: remindersListRecord,
action: .none
)
let remindersListModification = try syncEngine.modifyRecords(
scope: .private,
saving: [remindersListRecord]
)
try await syncEngine.modifyRecords(scope: .private, saving: [remindersListAssetRecord])
.notify()
await remindersListModification.notify()
assertQuery(RemindersList.all, database: userDatabase.database) {
"""
┌─────────────────────┐
│ RemindersList( │
│ id: 1, │
│ title: "Personal" │
│ ) │
└─────────────────────┘
"""
}
assertQuery(RemindersListAsset.all, database: userDatabase.database) {
"""
┌─────────────────────────────┐
│ RemindersListAsset( │
│ remindersListID: 1, │
│ coverImage: Data(5 bytes) │
│ ) │
└─────────────────────────────┘
"""
}
}
}
}
#endif