diff --git a/Tests/SharingGRDBTests/CloudKitTests/CloudKitTests.swift b/Tests/SharingGRDBTests/CloudKitTests/CloudKitTests.swift index 13559069..06757dc4 100644 --- a/Tests/SharingGRDBTests/CloudKitTests/CloudKitTests.swift +++ b/Tests/SharingGRDBTests/CloudKitTests/CloudKitTests.swift @@ -344,7 +344,8 @@ extension BaseCloudKitTests { schema: """ CREATE TABLE "modelAs" ( "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, - "count" INTEGER NOT NULL ON CONFLICT REPLACE DEFAULT 0 + "count" INTEGER NOT NULL ON CONFLICT REPLACE DEFAULT 0, + "isEven" INTEGER GENERATED ALWAYS AS ("count" % 2 == 0) VIRTUAL ) """, tableInfo: [ @@ -1009,5 +1010,72 @@ extension BaseCloudKitTests { } } + @Test func generatedColumns() async throws { + try await userDatabase.userWrite { db in + try db.seed { + ModelA(id: 1, count: 42, isEven: true) + } + } + try await syncEngine.processPendingRecordZoneChanges(scope: .private) + assertInlineSnapshot(of: container, as: .customDump) { + """ + MockCloudContainer( + privateCloudDatabase: MockCloudDatabase( + databaseScope: .private, + storage: [ + [0]: CKRecord( + recordID: CKRecord.ID(1:modelAs/co.pointfree.SQLiteData.defaultZone/__defaultOwner__), + recordType: "modelAs", + parent: nil, + share: nil, + count: 42, + id: 1 + ) + ] + ), + sharedCloudDatabase: MockCloudDatabase( + databaseScope: .shared, + storage: [] + ) + ) + """ + } + + + let record = try syncEngine.private.database.record(for: ModelA.recordID(for: 1)) + record.encryptedValues["isEven"] = false + try await syncEngine.modifyRecords(scope: .private, saving: [record]).notify() + + assertInlineSnapshot(of: container, as: .customDump) { + """ + MockCloudContainer( + privateCloudDatabase: MockCloudDatabase( + databaseScope: .private, + storage: [ + [0]: CKRecord( + recordID: CKRecord.ID(1:modelAs/co.pointfree.SQLiteData.defaultZone/__defaultOwner__), + recordType: "modelAs", + parent: nil, + share: nil, + count: 42, + id: 1, + isEven: 0 + ) + ] + ), + sharedCloudDatabase: MockCloudDatabase( + databaseScope: .shared, + storage: [] + ) + ) + """ + } + + try await userDatabase.read { db in + let modelA = try #require(try ModelA.find(1).fetchOne(db)) + #expect(modelA.isEven == true) + } + } + // TODO: Test what happens when we delete locally and then an edit comes in from the server } diff --git a/Tests/SharingGRDBTests/CloudKitTests/RecordTypeTests.swift b/Tests/SharingGRDBTests/CloudKitTests/RecordTypeTests.swift index dabfa903..44272b58 100644 --- a/Tests/SharingGRDBTests/CloudKitTests/RecordTypeTests.swift +++ b/Tests/SharingGRDBTests/CloudKitTests/RecordTypeTests.swift @@ -343,7 +343,8 @@ extension BaseCloudKitTests { schema: """ CREATE TABLE "modelAs" ( "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, - "count" INTEGER NOT NULL ON CONFLICT REPLACE DEFAULT 0 + "count" INTEGER NOT NULL ON CONFLICT REPLACE DEFAULT 0, + "isEven" INTEGER GENERATED ALWAYS AS ("count" % 2 == 0) VIRTUAL ) """, tableInfo: [ diff --git a/Tests/SharingGRDBTests/CloudKitTests/UserlandTests.swift b/Tests/SharingGRDBTests/CloudKitTests/UserlandTests.swift index 988cb476..7a1ba9cd 100644 --- a/Tests/SharingGRDBTests/CloudKitTests/UserlandTests.swift +++ b/Tests/SharingGRDBTests/CloudKitTests/UserlandTests.swift @@ -25,7 +25,7 @@ import SharingGRDB } } try await $modelAs.load() - #expect(modelAs == [ModelA(id: 1)]) + #expect(modelAs == [ModelA(id: 1, isEven: true)]) } } } diff --git a/Tests/SharingGRDBTests/Internal/Schema.swift b/Tests/SharingGRDBTests/Internal/Schema.swift index 093c7aa9..2022a0e2 100644 --- a/Tests/SharingGRDBTests/Internal/Schema.swift +++ b/Tests/SharingGRDBTests/Internal/Schema.swift @@ -55,6 +55,8 @@ import SharingGRDB @Table struct ModelA: Equatable, Identifiable { let id: Int var count = 0 + @Column(generated: .virtual) + let isEven: Bool } @Table struct ModelB: Equatable, Identifiable { let id: Int @@ -175,7 +177,8 @@ func database(containerIdentifier: String) throws -> DatabasePool { try #sql(""" CREATE TABLE "modelAs" ( "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, - "count" INTEGER NOT NULL ON CONFLICT REPLACE DEFAULT 0 + "count" INTEGER NOT NULL ON CONFLICT REPLACE DEFAULT 0, + "isEven" INTEGER GENERATED ALWAYS AS ("count" % 2 == 0) VIRTUAL ) """) .execute(db)