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
70 changes: 69 additions & 1 deletion Tests/SharingGRDBTests/CloudKitTests/CloudKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion Tests/SharingGRDBTests/CloudKitTests/RecordTypeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
2 changes: 1 addition & 1 deletion Tests/SharingGRDBTests/CloudKitTests/UserlandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import SharingGRDB
}
}
try await $modelAs.load()
#expect(modelAs == [ModelA(id: 1)])
#expect(modelAs == [ModelA(id: 1, isEven: true)])
}
}
}
5 changes: 4 additions & 1 deletion Tests/SharingGRDBTests/Internal/Schema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Loading