From 461f96f3a267981cd7ccb25b9e87017b9606ea88 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Sat, 6 Sep 2025 21:27:32 -0500 Subject: [PATCH] Fix tests for iOS. --- .../CloudKit/Internal/CloudKitFunctions.swift | 3 +- Sources/SQLiteData/CloudKit/SyncEngine.swift | 8 ++--- .../SQLiteData/Internal/UserDatabase.swift | 8 ++--- Tests/SQLiteDataTests/AssertQueryTests.swift | 11 +++++++ .../CloudKitTests/AccountLifecycleTests.swift | 2 +- .../CloudKitTests/CloudKitTests.swift | 4 +-- .../CloudKitTests/SchemaChangeTests.swift | 10 +++---- .../SharingPermissionsTests.swift | 4 +-- .../SQLiteDataTests/CustomFunctionTests.swift | 1 + .../Internal/UserDatabaseHelpers.swift | 29 ++----------------- 10 files changed, 31 insertions(+), 49 deletions(-) diff --git a/Sources/SQLiteData/CloudKit/Internal/CloudKitFunctions.swift b/Sources/SQLiteData/CloudKit/Internal/CloudKitFunctions.swift index 667987cc..fb1fa47f 100644 --- a/Sources/SQLiteData/CloudKit/Internal/CloudKitFunctions.swift +++ b/Sources/SQLiteData/CloudKit/Internal/CloudKitFunctions.swift @@ -18,9 +18,8 @@ || share.currentUserParticipant?.permission == .readWrite } - @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) @DatabaseFunction("sqlitedata_icloud_syncEngineIsSynchronizingChanges") func syncEngineIsSynchronizingChanges() -> Bool { - SyncEngine._isSynchronizingChanges + _isSynchronizingChanges } #endif diff --git a/Sources/SQLiteData/CloudKit/SyncEngine.swift b/Sources/SQLiteData/CloudKit/SyncEngine.swift index 06450fc1..fbb1c8df 100644 --- a/Sources/SQLiteData/CloudKit/SyncEngine.swift +++ b/Sources/SQLiteData/CloudKit/SyncEngine.swift @@ -251,8 +251,6 @@ try validateSchema() } - @TaskLocal package static var _isSynchronizingChanges = false - nonisolated package func setUpSyncEngine() throws { let migrator = metadatabaseMigrator() #if DEBUG @@ -464,7 +462,7 @@ previousRecordTypeByTableName[tableName] == nil } - try Self.$_isSynchronizingChanges.withValue(false) { + try $_isSynchronizingChanges.withValue(false) { for tableName in newTableNames { try self.uploadRecordsToCloudKit(tableName: tableName, db: db) } @@ -1304,7 +1302,7 @@ else { continue } func open(_: T.Type) async throws { try await userDatabase.write { db in - try Self.$_isSynchronizingChanges.withValue(false) { + try $_isSynchronizingChanges.withValue(false) { switch foreignKey.onDelete { case .cascade: try T @@ -2006,4 +2004,6 @@ } return query } + + @TaskLocal package var _isSynchronizingChanges = false #endif diff --git a/Sources/SQLiteData/Internal/UserDatabase.swift b/Sources/SQLiteData/Internal/UserDatabase.swift index 96ca2a2a..520e8601 100644 --- a/Sources/SQLiteData/Internal/UserDatabase.swift +++ b/Sources/SQLiteData/Internal/UserDatabase.swift @@ -15,18 +15,16 @@ package struct UserDatabase { database.configuration } - @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) package func write( _ updates: @Sendable (Database) throws -> T ) async throws -> T { try await database.write { db in - try SyncEngine.$_isSynchronizingChanges.withValue(true) { + try $_isSynchronizingChanges.withValue(true) { try updates(db) } } } - @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) package func read( _ updates: @Sendable (Database) throws -> T ) async throws -> T { @@ -36,19 +34,17 @@ package struct UserDatabase { } @_disfavoredOverload - @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) package func write( _ updates: (Database) throws -> T ) throws -> T { try database.write { db in - try SyncEngine.$_isSynchronizingChanges.withValue(true) { + try $_isSynchronizingChanges.withValue(true) { try updates(db) } } } @_disfavoredOverload - @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) package func read( _ updates: (Database) throws -> T ) throws -> T { diff --git a/Tests/SQLiteDataTests/AssertQueryTests.swift b/Tests/SQLiteDataTests/AssertQueryTests.swift index 17179471..4878a923 100644 --- a/Tests/SQLiteDataTests/AssertQueryTests.swift +++ b/Tests/SQLiteDataTests/AssertQueryTests.swift @@ -10,6 +10,7 @@ import Testing .snapshots(record: .failed), ) struct AssertQueryTests { + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) @Test func assertQueryBasic() throws { assertQuery( Record.all.select(\.id) @@ -23,6 +24,8 @@ struct AssertQueryTests { """ } } + + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) @Test func assertQueryRecord() throws { assertQuery( Record.where { $0.id == 1 } @@ -37,6 +40,8 @@ struct AssertQueryTests { """ } } + + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) @Test func assertQueryBasicUpdate() throws { assertQuery( Record.all @@ -52,6 +57,8 @@ struct AssertQueryTests { """ } } + + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) @Test func assertQueryRecordUpdate() throws { assertQuery( Record @@ -69,7 +76,9 @@ struct AssertQueryTests { """ } } + #if DEBUG + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) @Test func assertQueryBasicIncludeSQL() throws { assertQuery( includeSQL: true, @@ -90,7 +99,9 @@ struct AssertQueryTests { } } #endif + #if DEBUG + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) @Test func assertQueryRecordIncludeSQL() throws { assertQuery( includeSQL: true, diff --git a/Tests/SQLiteDataTests/CloudKitTests/AccountLifecycleTests.swift b/Tests/SQLiteDataTests/CloudKitTests/AccountLifecycleTests.swift index e547a132..ba1e4bdc 100644 --- a/Tests/SQLiteDataTests/CloudKitTests/AccountLifecycleTests.swift +++ b/Tests/SQLiteDataTests/CloudKitTests/AccountLifecycleTests.swift @@ -25,7 +25,7 @@ await signOut() - try await userDatabase.userRead { db in + try await userDatabase.read { db in try #expect(RemindersList.count().fetchOne(db) == 0) try #expect(Reminder.count().fetchOne(db) == 0) try #expect(RemindersListPrivate.count().fetchOne(db) == 0) diff --git a/Tests/SQLiteDataTests/CloudKitTests/CloudKitTests.swift b/Tests/SQLiteDataTests/CloudKitTests/CloudKitTests.swift index b2a2d4bd..8fec608f 100644 --- a/Tests/SQLiteDataTests/CloudKitTests/CloudKitTests.swift +++ b/Tests/SQLiteDataTests/CloudKitTests/CloudKitTests.swift @@ -442,7 +442,7 @@ as: String.self ) assertInlineSnapshot( - of: try { try userDatabase.userRead { try query.fetchAll($0) } }(), + of: try { try userDatabase.write { try query.fetchAll($0) } }(), as: .customDump ) { """ @@ -458,7 +458,7 @@ try syncEngine.tearDownSyncEngine() assertInlineSnapshot( - of: try { try userDatabase.userRead { try query.fetchAll($0) } }(), + of: try { try userDatabase.read { try query.fetchAll($0) } }(), as: .customDump ) { """ diff --git a/Tests/SQLiteDataTests/CloudKitTests/SchemaChangeTests.swift b/Tests/SQLiteDataTests/CloudKitTests/SchemaChangeTests.swift index ebd7d64f..9292a102 100644 --- a/Tests/SQLiteDataTests/CloudKitTests/SchemaChangeTests.swift +++ b/Tests/SQLiteDataTests/CloudKitTests/SchemaChangeTests.swift @@ -81,10 +81,10 @@ ) defer { _ = relaunchedSyncEngine } - let remindersLists = try await userDatabase.userRead { db in + let remindersLists = try await userDatabase.read { db in try RemindersListWithPosition.order(by: \.id).fetchAll(db) } - let reminders = try await userDatabase.userRead { db in + let reminders = try await userDatabase.read { db in try ReminderWithPosition.order(by: \.id).fetchAll(db) } @@ -154,7 +154,7 @@ ) defer { _ = relaunchedSyncEngine } - let remindersLists = try await userDatabase.userRead { db in + let remindersLists = try await userDatabase.read { db in try RemindersListWithData.order(by: \.id).fetchAll(db) } @@ -223,7 +223,7 @@ ) defer { _ = relaunchedSyncEngine } - let remindersLists = try await userDatabase.userRead { db in + let remindersLists = try await userDatabase.read { db in try RemindersListWithData.order(by: \.id).fetchAll(db) } @@ -280,7 +280,7 @@ ) defer { _ = relaunchedSyncEngine } - let images = try await userDatabase.userRead { db in + let images = try await userDatabase.read { db in try Image.order(by: \.id).fetchAll(db) } diff --git a/Tests/SQLiteDataTests/CloudKitTests/SharingPermissionsTests.swift b/Tests/SQLiteDataTests/CloudKitTests/SharingPermissionsTests.swift index 4457e490..aa3e3957 100644 --- a/Tests/SQLiteDataTests/CloudKitTests/SharingPermissionsTests.swift +++ b/Tests/SQLiteDataTests/CloudKitTests/SharingPermissionsTests.swift @@ -346,7 +346,7 @@ } try await syncEngine.processPendingRecordZoneChanges(scope: .shared) - try await self.userDatabase.userRead { db in + try await self.userDatabase.read { db in try #expect(Reminder.all.fetchCount(db) == 0) } assertInlineSnapshot(of: container, as: .customDump) { @@ -432,7 +432,7 @@ } try await syncEngine.processPendingRecordZoneChanges(scope: .shared) - try await self.userDatabase.userRead { db in + try await self.userDatabase.read { db in try #expect(RemindersList.find(1).fetchOne(db) == RemindersList(id: 1, title: "Personal")) } assertInlineSnapshot(of: container, as: .customDump) { diff --git a/Tests/SQLiteDataTests/CustomFunctionTests.swift b/Tests/SQLiteDataTests/CustomFunctionTests.swift index 60eda159..3c9436e8 100644 --- a/Tests/SQLiteDataTests/CustomFunctionTests.swift +++ b/Tests/SQLiteDataTests/CustomFunctionTests.swift @@ -7,6 +7,7 @@ import Testing Date(timeIntervalSinceReferenceDate: 0) } + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) @Test func basics() throws { var configuration = Configuration() configuration.prepareDatabase { db in diff --git a/Tests/SQLiteDataTests/Internal/UserDatabaseHelpers.swift b/Tests/SQLiteDataTests/Internal/UserDatabaseHelpers.swift index 74c93a9d..710c6b07 100644 --- a/Tests/SQLiteDataTests/Internal/UserDatabaseHelpers.swift +++ b/Tests/SQLiteDataTests/Internal/UserDatabaseHelpers.swift @@ -2,47 +2,22 @@ import GRDB import SQLiteData extension UserDatabase { - @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) func userWrite( _ updates: @Sendable (Database) throws -> T ) async throws -> T { try await write { db in - try SyncEngine.$_isSynchronizingChanges.withValue(false) { - try updates(db) - } - } - } - - @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) - func userRead( - _ updates: @Sendable (Database) throws -> T - ) async throws -> T { - try await read { db in - try SyncEngine.$_isSynchronizingChanges.withValue(false) { + try $_isSynchronizingChanges.withValue(false) { try updates(db) } } } @_disfavoredOverload - @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) func userWrite( _ updates: (Database) throws -> T ) throws -> T { try write { db in - try SyncEngine.$_isSynchronizingChanges.withValue(false) { - try updates(db) - } - } - } - - @_disfavoredOverload - @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) - func userRead( - _ updates: (Database) throws -> T - ) throws -> T { - try write { db in - try SyncEngine.$_isSynchronizingChanges.withValue(false) { + try $_isSynchronizingChanges.withValue(false) { try updates(db) } }