Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/apple/swift-collections", from: "1.0.0"),
.package(url: "https://github.com/groue/GRDB.swift", from: "7.6.0"),
.package(url: "https://github.com/pointfreeco/swift-concurrency-extras", from: "1.0.0"),
.package(url: "https://github.com/pointfreeco/swift-concurrency-extras", from: "1.4.0"),
.package(url: "https://github.com/pointfreeco/swift-custom-dump", from: "1.3.3"),
.package(url: "https://github.com/pointfreeco/swift-dependencies", from: "1.9.0"),
.package(url: "https://github.com/pointfreeco/swift-perception", from: "2.0.0"),
Expand Down Expand Up @@ -69,6 +69,7 @@ let package = Package(
dependencies: [
"SQLiteData",
.product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"),
.product(name: "ConcurrencyExtrasTestSupport", package: "swift-concurrency-extras"),
.product(name: "CustomDump", package: "swift-custom-dump"),
.product(name: "Dependencies", package: "swift-dependencies"),
.product(name: "InlineSnapshotTesting", package: "swift-snapshot-testing"),
Expand All @@ -80,12 +81,17 @@ let package = Package(
dependencies: [
"SQLiteData",
"SQLiteDataTestSupport",
"TestLocals",
.product(name: "DependenciesTestSupport", package: "swift-dependencies"),
.product(name: "InlineSnapshotTesting", package: "swift-snapshot-testing"),
.product(name: "SnapshotTestingCustomDump", package: "swift-snapshot-testing"),
.product(name: "StructuredQueries", package: "swift-structured-queries"),
]
),
.target(
name: "TestLocals",
dependencies: ["SQLiteData"]
)
],
swiftLanguageModes: [.v6]
)
Expand Down
12 changes: 12 additions & 0 deletions Sources/TestLocals/TestLocals.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#if canImport(CloudKit)
import CloudKit
import Testing
import SQLiteData

@TaskLocal package var prepareDatabase: @Sendable (UserDatabase) async throws -> Void = { _ in }
@TaskLocal package var startImmediately = true
@TaskLocal package var attachMetadatabase = false
@TaskLocal package var accountStatus = CKAccountStatus.available
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@TaskLocal package var syncEngineDelegate: (any SyncEngineDelegate)? = nil
#endif
12 changes: 8 additions & 4 deletions Tests/SQLiteDataTests/CloudKitTests/AccountLifecycleTests.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#if canImport(CloudKit)
import ConcurrencyExtrasTestSupport
import CloudKit
import CustomDump
import Foundation
import InlineSnapshotTesting
import SQLiteData
import SnapshotTestingCustomDump
import Testing
import TestLocals
import SQLiteDataTestSupport

extension BaseCloudKitTests {
Expand Down Expand Up @@ -38,7 +40,8 @@
}

@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test(.accountStatus(.noAccount)) func signInUploadsLocalRecordsToCloudKit() async throws {
@Test($accountStatus.set(.noAccount))
func signInUploadsLocalRecordsToCloudKit() async throws {
try await userDatabase.userWrite { db in
try db.seed {
RemindersList(id: 1, title: "Personal")
Expand Down Expand Up @@ -367,7 +370,8 @@
_ = try syncEngine.modifyRecords(scope: .shared, saving: [share, remindersListRecord])
let freshShare = try syncEngine.shared.database.record(for: share.recordID) as! CKShare
let freshRemindersListRecord = try syncEngine.shared.database.record(
for: remindersListRecord.recordID)
for: remindersListRecord.recordID
)

try await syncEngine
.acceptShare(
Expand Down Expand Up @@ -604,8 +608,8 @@

@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test(
.accountStatus(.noAccount),
.prepareDatabase { userDatabase in
$accountStatus.set(.noAccount),
$prepareDatabase.set { userDatabase in
try await userDatabase.write { db in
try db.seed {
RemindersList(id: 1, title: "Personal")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#if canImport(CloudKit)
import CloudKit
import ConcurrencyExtras
import ConcurrencyExtrasTestSupport
import CustomDump
import InlineSnapshotTesting
import OrderedCollections
import SQLiteData
import SQLiteDataTestSupport
import SnapshotTestingCustomDump
import Testing
import TestLocals

extension BaseCloudKitTests {
@MainActor
@Suite(.attachMetadatabase(true))
@Suite($attachMetadatabase.set(false))
final class AttachedMetadatabaseTests: BaseCloudKitTests, @unchecked Sendable {
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func basics() async throws {
Expand Down
5 changes: 3 additions & 2 deletions Tests/SQLiteDataTests/CloudKitTests/MergeConflictTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#if canImport(CloudKit)
import CloudKit
import ConcurrencyExtrasTestSupport
import CustomDump
import Foundation
import InlineSnapshotTesting
Expand All @@ -11,8 +12,8 @@

extension BaseCloudKitTests {
@MainActor
@Suite(.printTimestamps) final class MergeConflictTests: BaseCloudKitTests, @unchecked Sendable
{
@Suite(.taskLocal(CKRecord._$printTimestamps, true))
final class MergeConflictTests: BaseCloudKitTests, @unchecked Sendable {
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func merge_clientRecordUpdatedBeforeServerRecord() async throws {
try await userDatabase.userWrite { db in
Expand Down
6 changes: 4 additions & 2 deletions Tests/SQLiteDataTests/CloudKitTests/MetadataTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#if canImport(CloudKit)
import CloudKit
import ConcurrencyExtrasTestSupport
import CustomDump
import SQLiteDataTestSupport
import Foundation
Expand All @@ -8,6 +9,7 @@
import SQLiteData
import SnapshotTestingCustomDump
import Testing
import TestLocals

extension BaseCloudKitTests {
@MainActor
Expand Down Expand Up @@ -623,7 +625,7 @@
}

@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test(.attachMetadatabase(true))
@Test($attachMetadatabase.set(true))
func observation() async throws {
let remindersList = RemindersList(id: 1, title: "Personal")
try await userDatabase.userWrite { db in
Expand Down Expand Up @@ -652,7 +654,7 @@
}

@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test(.attachMetadatabase(true))
@Test($attachMetadatabase.set(true))
func observation_GeneratedColumn() async throws {
let remindersList = RemindersList(id: 1, title: "Personal")
try await userDatabase.userWrite { db in
Expand Down
4 changes: 3 additions & 1 deletion Tests/SQLiteDataTests/CloudKitTests/NewTableSyncTests.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#if canImport(CloudKit)
import CloudKit
import ConcurrencyExtrasTestSupport
import CustomDump
import SQLiteDataTestSupport
import Foundation
import InlineSnapshotTesting
import SQLiteData
import SnapshotTestingCustomDump
import Testing
import TestLocals

extension BaseCloudKitTests {
@MainActor
@Suite(
.prepareDatabase { userDatabase in
$prepareDatabase.set { userDatabase in
try await userDatabase.userWrite { db in
try db.seed {
RemindersList(id: 1, title: "Personal")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#if canImport(CloudKit)
import CloudKit
import ConcurrencyExtrasTestSupport
import CustomDump
import Foundation
import InlineSnapshotTesting
Expand Down Expand Up @@ -264,7 +265,10 @@
}

@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test(.printTimestamps(true), .printRecordChangeTag)
@Test(
.taskLocal(CKRecord._$printRecordChangeTag, true),
.taskLocal(CKRecord._$printTimestamps, true)
)
func editBetweenBatchAndSentRecordZoneChanges() async throws {
try await userDatabase.userWrite { db in
try db.seed {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#if canImport(CloudKit)
import CloudKit
import ConcurrencyExtrasTestSupport
import CustomDump
import DependenciesTestSupport
import Foundation
Expand All @@ -8,13 +9,14 @@
import SQLiteDataTestSupport
import SnapshotTestingCustomDump
import Testing
import TestLocals

extension BaseCloudKitTests {
@MainActor
final class SyncEngineDelegateTests: BaseCloudKitTests, @unchecked Sendable {
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)

@Test(.syncEngineDelegate(MyDelegate()))
@Test($syncEngineDelegate.set(MyDelegate()))
func accountChanged() async throws {
try await userDatabase.userWrite { db in
try db.seed {
Expand Down Expand Up @@ -173,7 +175,7 @@
try await syncEngine.processPendingDatabaseChanges(scope: .private)
}

@Test(.syncEngineDelegate(DefaultImplementationDelegate()))
@Test($syncEngineDelegate.set(DefaultImplementationDelegate()))
func accountChanged_DefaultImplementation() async throws {
try await userDatabase.userWrite { db in
try db.seed {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#if canImport(CloudKit)
import CloudKit
import ConcurrencyExtrasTestSupport
import DependenciesTestSupport
import InlineSnapshotTesting
import SQLiteDataTestSupport
Expand All @@ -8,6 +9,7 @@
import SnapshotTesting
import SnapshotTestingCustomDump
import Testing
import TestLocals
import os

extension BaseCloudKitTests {
Expand All @@ -16,8 +18,7 @@
@MainActor
@Suite
final class SyncEngineLifecycleTests_ImmediatelyStarted: BaseCloudKitTests,
@unchecked
Sendable
@unchecked Sendable
{
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func stopAndReStart() async throws {
Expand Down Expand Up @@ -581,7 +582,8 @@
// * Start sync engine
// * Verify that data is sent to CloudKit database and cached locally.
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test(.startImmediately(false)) func writeAndThenStart() async throws {
@Test($startImmediately.set(false))
func writeAndThenStart() async throws {
try await userDatabase.userWrite { db in
try db.seed {
RemindersList(id: 1, title: "Personal")
Expand Down
18 changes: 9 additions & 9 deletions Tests/SQLiteDataTests/Internal/BaseCloudKitTests.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#if canImport(CloudKit)
import Clocks
import CloudKit
import ConcurrencyExtrasTestSupport
import DependenciesTestSupport
import OrderedCollections
import SQLiteData
import SnapshotTesting
import Testing
import TestLocals
import os

@Suite(
Expand All @@ -15,7 +17,7 @@
$0.continuousClock = TestClock<Duration>()
$0.dataManager = InMemoryDataManager()
},
.attachMetadatabase(false)
$attachMetadatabase.set(false)
)
class BaseCloudKitTests: @unchecked Sendable {
let userDatabase: UserDatabase
Expand Down Expand Up @@ -49,14 +51,14 @@
self.userDatabase = UserDatabase(
database: try SQLiteDataTests.database(
containerIdentifier: testContainerIdentifier,
attachMetadatabase: _AttachMetadatabaseTrait.attachMetadatabase
attachMetadatabase: attachMetadatabase
)
)
try await _PrepareDatabaseTrait.prepareDatabase(userDatabase)
try await prepareDatabase(userDatabase)
let privateDatabase = MockCloudDatabase(databaseScope: .private)
let sharedDatabase = MockCloudDatabase(databaseScope: .shared)
let container = MockCloudContainer(
accountStatus: _AccountStatusScope.accountStatus,
accountStatus: accountStatus,
containerIdentifier: testContainerIdentifier,
privateCloudDatabase: privateDatabase,
sharedCloudDatabase: sharedDatabase
Expand All @@ -67,7 +69,7 @@
_syncEngine = try await SyncEngine(
container: container,
userDatabase: self.userDatabase,
delegate: _SyncEngineDelegateTrait.syncEngineDelegate,
delegate: syncEngineDelegate,
tables: Reminder.self,
RemindersList.self,
RemindersListAsset.self,
Expand All @@ -81,11 +83,9 @@
ModelC.self,
ScopedModel.self,
privateTables: RemindersListPrivate.self,
startImmediately: _StartImmediatelyTrait.startImmediately
startImmediately: startImmediately
)
if _StartImmediatelyTrait.startImmediately,
_AccountStatusScope.accountStatus == .available
{
if startImmediately, accountStatus == .available {
await syncEngine.handleEvent(
.accountChange(changeType: .signIn(currentUser: currentUserRecordID)),
syncEngine: syncEngine.private
Expand Down
10 changes: 8 additions & 2 deletions Tests/SQLiteDataTests/Internal/CloudKit+CustomDump.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@
}

extension CKRecord {
@TaskLocal static var printTimestamps = false
@TaskLocal static var printRecordChangeTag = false
static let _$printTimestamps = TaskLocal(wrappedValue: false)
static let _$printRecordChangeTag = TaskLocal(wrappedValue: false)
static var printTimestamps: Bool {
_$printTimestamps.get()
}
static var printRecordChangeTag: Bool {
_$printRecordChangeTag.get()
}
}

@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
Expand Down
Loading