|
| 1 | +// |
| 2 | +// RowChangeStatementFactoryUniqueMatchTests.swift |
| 3 | +// TableProTests |
| 4 | +// |
| 5 | + |
| 6 | +import Foundation |
| 7 | +@testable import TablePro |
| 8 | +import TableProPluginKit |
| 9 | +import Testing |
| 10 | + |
| 11 | +@MainActor |
| 12 | +struct RowChangeStatementFactoryUniqueMatchTests { |
| 13 | + private let columns = ["d", "id", "v"] |
| 14 | + private let row: [PluginCellValue] = ["2026-10-22", "1", "a"] |
| 15 | + |
| 16 | + private func factory(_ databaseType: DatabaseType, primaryKeyColumns: [String] = ["d"]) -> RowChangeStatementFactory { |
| 17 | + RowChangeStatementFactory( |
| 18 | + tableName: "t", |
| 19 | + schemaName: nil, |
| 20 | + columns: columns, |
| 21 | + primaryKeyColumns: primaryKeyColumns, |
| 22 | + databaseType: databaseType, |
| 23 | + pluginDriver: nil |
| 24 | + ) |
| 25 | + } |
| 26 | + |
| 27 | + private var edit: RowChange { |
| 28 | + RowChange( |
| 29 | + rowID: .existing(0), |
| 30 | + type: .update, |
| 31 | + cellChanges: [CellChange(columnIndex: 2, columnName: "v", oldValue: "a", newValue: "b")], |
| 32 | + originalRow: row |
| 33 | + ) |
| 34 | + } |
| 35 | + |
| 36 | + private var delete: RowChange { |
| 37 | + RowChange(rowID: .existing(0), type: .delete, originalRow: row) |
| 38 | + } |
| 39 | + |
| 40 | + private func refusal(_ write: () throws -> [ParameterizedStatement]) -> (kind: RowWriteKind?, reason: String)? { |
| 41 | + do { |
| 42 | + let statements = try write() |
| 43 | + Issue.record("Sent \(statements.map(\.sql))") |
| 44 | + return nil |
| 45 | + } catch let DataWriteError.changeRefused(table, kind, reason) { |
| 46 | + #expect(table == "t") |
| 47 | + return (kind, reason) |
| 48 | + } catch { |
| 49 | + Issue.record("Unexpected error: \(error)") |
| 50 | + return nil |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Test(arguments: [DatabaseType.clickhouse, .snowflake, .trino]) |
| 55 | + func aDeleteIsRefusedInsteadOfKeyedOnAKeyTheEngineDoesNotKeepUnique(databaseType: DatabaseType) { |
| 56 | + let refused = refusal { |
| 57 | + try factory(databaseType).statements(for: [delete], deletedRowIDs: [.existing(0)]) |
| 58 | + } |
| 59 | + #expect(refused?.kind == .delete) |
| 60 | + #expect(refused?.reason.contains(databaseType.displayName) == true) |
| 61 | + #expect(refused?.reason.contains("SQL statement") == true) |
| 62 | + } |
| 63 | + |
| 64 | + @Test(arguments: [DatabaseType.clickhouse, .snowflake, .trino]) |
| 65 | + func anEditIsRefusedInsteadOfKeyedOnAKeyTheEngineDoesNotKeepUnique(databaseType: DatabaseType) { |
| 66 | + let refused = refusal { try factory(databaseType).statements(for: [edit]) } |
| 67 | + #expect(refused?.kind == .update) |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + func aNewClickHouseRowIsStillInserted() throws { |
| 72 | + let inserted = RowID.inserted(UUID()) |
| 73 | + let statements = try factory(.clickhouse).statements( |
| 74 | + for: [RowChange(rowID: inserted, type: .insert)], |
| 75 | + insertedRowData: [inserted: row], |
| 76 | + insertedRowIDs: [inserted] |
| 77 | + ) |
| 78 | + #expect(statements.count == 1) |
| 79 | + #expect(statements.first?.sql.hasPrefix("INSERT") == true) |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + func aDamengTableWithAPrimaryKeyIsStillWritten() throws { |
| 84 | + let deleted = RowChange(rowID: .existing(1), type: .delete, originalRow: ["2026-10-22", "2", "a"]) |
| 85 | + let statements = try factory(.dameng, primaryKeyColumns: ["id"]).statements( |
| 86 | + for: [edit, deleted], |
| 87 | + deletedRowIDs: [.existing(1)] |
| 88 | + ) |
| 89 | + #expect(Set(statements.map { String($0.sql.prefix(6)) }) == ["UPDATE", "DELETE"]) |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + func aDamengTableWithoutAPrimaryKeyRefusesADelete() { |
| 94 | + let refused = refusal { |
| 95 | + try factory(.dameng, primaryKeyColumns: []).statements(for: [delete], deletedRowIDs: [.existing(0)]) |
| 96 | + } |
| 97 | + #expect(refused?.kind == .delete) |
| 98 | + #expect(refused?.reason.contains("primary key") == true) |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + func rewindingAClickHouseInsertIsRefused() { |
| 103 | + let target = DataWriteTarget(database: "default", schema: nil, table: "t") |
| 104 | + let record = RewindRecord( |
| 105 | + id: UUID(), |
| 106 | + historyId: nil, |
| 107 | + connectionId: UUID(), |
| 108 | + databaseType: .clickhouse, |
| 109 | + target: target, |
| 110 | + capturedAt: Date(timeIntervalSince1970: 0), |
| 111 | + generatedColumns: [], |
| 112 | + identityColumns: [], |
| 113 | + operations: [ |
| 114 | + RowWriteOperation( |
| 115 | + kind: .insert, |
| 116 | + target: target, |
| 117 | + columns: columns, |
| 118 | + primaryKeyColumns: ["d"], |
| 119 | + preImage: nil, |
| 120 | + postImage: row, |
| 121 | + writtenColumns: columns, |
| 122 | + refusal: nil |
| 123 | + ), |
| 124 | + ] |
| 125 | + ) |
| 126 | + let planner = RewindPlanner( |
| 127 | + record: record, |
| 128 | + factory: factory(.clickhouse), |
| 129 | + queryBuilder: TableQueryBuilder( |
| 130 | + databaseType: .clickhouse, |
| 131 | + dialect: PluginManager.shared.sqlDialect(for: .clickhouse), |
| 132 | + pagination: .offset |
| 133 | + ) |
| 134 | + ) |
| 135 | + |
| 136 | + #expect(throws: DataWriteError.self) { |
| 137 | + try planner.plan(currentRows: [RewindCurrentRow(values: row)]) |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments