|
| 1 | + |
| 2 | +@testable import PowerSync |
| 3 | +import XCTest |
| 4 | + |
| 5 | +final class AttachmentTests: XCTestCase { |
| 6 | + private var database: PowerSyncDatabaseProtocol! |
| 7 | + private var schema: Schema! |
| 8 | + |
| 9 | + override func setUp() async throws { |
| 10 | + try await super.setUp() |
| 11 | + schema = Schema(tables: [ |
| 12 | + Table(name: "users", columns: [ |
| 13 | + .text("name"), |
| 14 | + .text("email"), |
| 15 | + .text("photo_id") |
| 16 | + ]), |
| 17 | + createAttachmentsTable(name: "attachments") |
| 18 | + ]) |
| 19 | + |
| 20 | + database = PowerSyncDatabase( |
| 21 | + schema: schema, |
| 22 | + dbFilename: ":memory:" |
| 23 | + ) |
| 24 | + try await database.disconnectAndClear() |
| 25 | + } |
| 26 | + |
| 27 | + override func tearDown() async throws { |
| 28 | + try await database.disconnectAndClear() |
| 29 | + database = nil |
| 30 | + try await super.tearDown() |
| 31 | + } |
| 32 | + |
| 33 | + func testAttachmentDownload() async throws { |
| 34 | + let queue = AttachmentQueue( |
| 35 | + db: database, |
| 36 | + remoteStorage: { |
| 37 | + struct MockRemoteStorage: RemoteStorageAdapter { |
| 38 | + func uploadFile( |
| 39 | + fileData: Data, |
| 40 | + attachment: Attachment |
| 41 | + ) async throws {} |
| 42 | + |
| 43 | + /** |
| 44 | + * Download a file from remote storage |
| 45 | + */ |
| 46 | + func downloadFile(attachment: Attachment) async throws -> Data { |
| 47 | + return Data([1,2,3]) |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Delete a file from remote storage |
| 52 | + */ |
| 53 | + func deleteFile(attachment: Attachment) async throws {} |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + return MockRemoteStorage() |
| 58 | + }(), |
| 59 | + attachmentDirectory: NSTemporaryDirectory(), |
| 60 | + watchedAttachments: try database.watch(options: WatchOptions( |
| 61 | + sql: "SELECT photo_id FROM users WHERE photo_id IS NOT NULL", |
| 62 | + mapper: { cursor in WatchedAttachmentItem( |
| 63 | + id: try cursor.getString(name: "photo_id"), |
| 64 | + fileExtension: "jpg" |
| 65 | + )} |
| 66 | + )) |
| 67 | + ) |
| 68 | + |
| 69 | + try await queue.startSync() |
| 70 | + |
| 71 | + // Create a user which has a photo_id associated. |
| 72 | + // This will be treated as a download since no attachment record was created. |
| 73 | + // saveFile creates the attachment record before the updates are made. |
| 74 | + _ = try await database.execute( |
| 75 | + sql : "INSERT INTO users (id, name, email, photo_id) VALUES (uuid(), 'steven', '[email protected]', uuid())", |
| 76 | + parameters: [] |
| 77 | + ) |
| 78 | + |
| 79 | + var attachmentsWatch = try database.watch( |
| 80 | + options: WatchOptions( |
| 81 | + sql: "SELECT * FROM attachments", |
| 82 | + mapper: {cursor in try Attachment.fromCursor(cursor)} |
| 83 | + )).makeAsyncIterator() |
| 84 | + |
| 85 | + var attachmentRecord = try await waitForMatch( |
| 86 | + iterator: attachmentsWatch, |
| 87 | + where: {results in results.first?.state == AttachmentState.synced.rawValue}, |
| 88 | + timeout: 5 |
| 89 | + ).first |
| 90 | + |
| 91 | + // The file should exist |
| 92 | + let localData = try await queue.localStorage.readFile(filePath: attachmentRecord!.localUri!) |
| 93 | + XCTAssertEqual(localData.count, 3) |
| 94 | + |
| 95 | + try await queue.clearQueue() |
| 96 | + try await queue.close() |
| 97 | + } |
| 98 | + |
| 99 | + func testAttachmentUpload() async throws { |
| 100 | + |
| 101 | + class MockRemoteStorage: RemoteStorageAdapter { |
| 102 | + public var uploadCalled = false |
| 103 | + |
| 104 | + func uploadFile( |
| 105 | + fileData: Data, |
| 106 | + attachment: Attachment |
| 107 | + ) async throws { |
| 108 | + self.uploadCalled = true |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Download a file from remote storage |
| 113 | + */ |
| 114 | + func downloadFile(attachment: Attachment) async throws -> Data { |
| 115 | + return Data([1,2,3]) |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Delete a file from remote storage |
| 120 | + */ |
| 121 | + func deleteFile(attachment: Attachment) async throws {} |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + let mockedRemote = MockRemoteStorage() |
| 128 | + |
| 129 | + let queue = AttachmentQueue( |
| 130 | + db: database, |
| 131 | + remoteStorage: mockedRemote, |
| 132 | + attachmentDirectory: NSTemporaryDirectory(), |
| 133 | + watchedAttachments: try database.watch(options: WatchOptions( |
| 134 | + sql: "SELECT photo_id FROM users WHERE photo_id IS NOT NULL", |
| 135 | + mapper: { cursor in WatchedAttachmentItem( |
| 136 | + id: try cursor.getString(name: "photo_id"), |
| 137 | + fileExtension: "jpg" |
| 138 | + )} |
| 139 | + )) |
| 140 | + ) |
| 141 | + |
| 142 | + try await queue.startSync() |
| 143 | + |
| 144 | + let attachmentsWatch = try database.watch( |
| 145 | + options: WatchOptions( |
| 146 | + sql: "SELECT * FROM attachments", |
| 147 | + mapper: {cursor in try Attachment.fromCursor(cursor)} |
| 148 | + )).makeAsyncIterator() |
| 149 | + |
| 150 | + _ = try await queue.saveFile( |
| 151 | + data: Data([3,4,5]), |
| 152 | + mediaType: "image/jpg", |
| 153 | + fileExtension: "jpg") {tx, attachment in |
| 154 | + _ = try tx.execute( |
| 155 | + sql : "INSERT INTO users (id, name, email, photo_id) VALUES (uuid(), 'john', '[email protected]', ?)", |
| 156 | + parameters: [attachment.id] |
| 157 | + ) |
| 158 | + } |
| 159 | + |
| 160 | + _ = try await waitForMatch( |
| 161 | + iterator: attachmentsWatch, |
| 162 | + where: {results in results.first?.state == AttachmentState.synced.rawValue}, |
| 163 | + timeout: 5 |
| 164 | + ).first |
| 165 | + |
| 166 | + // Upload should have been called |
| 167 | + XCTAssertTrue(mockedRemote.uploadCalled) |
| 168 | + |
| 169 | + try await queue.clearQueue() |
| 170 | + try await queue.close() |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | + |
| 175 | +enum WaitForMatchError: Error { |
| 176 | + case timeout |
| 177 | +} |
| 178 | + |
| 179 | +func waitForMatch<T, E: Error>( |
| 180 | + iterator: AsyncThrowingStream<T, E>.Iterator, |
| 181 | + where predicate: @escaping (T) -> Bool, |
| 182 | + timeout: TimeInterval |
| 183 | +) async throws -> T { |
| 184 | + var localIterator = iterator |
| 185 | + let timeoutNanoseconds = UInt64(timeout * 1_000_000_000) |
| 186 | + |
| 187 | + return try await withThrowingTaskGroup(of: T.self) { group in |
| 188 | + // Task to wait for a matching value |
| 189 | + group.addTask { |
| 190 | + while let value = try await localIterator.next() { |
| 191 | + if predicate(value) { |
| 192 | + return value |
| 193 | + } |
| 194 | + } |
| 195 | + throw WaitForMatchError.timeout // stream ended before match |
| 196 | + } |
| 197 | + |
| 198 | + // Task to enforce timeout |
| 199 | + group.addTask { |
| 200 | + try await Task.sleep(nanoseconds: timeoutNanoseconds) |
| 201 | + throw WaitForMatchError.timeout |
| 202 | + } |
| 203 | + |
| 204 | + // First one to succeed or fail |
| 205 | + let result = try await group.next() |
| 206 | + group.cancelAll() |
| 207 | + return result! |
| 208 | + } |
| 209 | +} |
0 commit comments