|
| 1 | +// |
| 2 | +// SQLRowToStatementConverterTests.swift |
| 3 | +// TableProTests |
| 4 | +// |
| 5 | + |
| 6 | +import Foundation |
| 7 | +import Testing |
| 8 | +@testable import TablePro |
| 9 | + |
| 10 | +@Suite("SQL Row To Statement Converter") |
| 11 | +struct SQLRowToStatementConverterTests { |
| 12 | + // MARK: - Factory |
| 13 | + |
| 14 | + private func makeConverter( |
| 15 | + tableName: String = "users", |
| 16 | + columns: [String] = ["id", "name", "email"], |
| 17 | + primaryKeyColumn: String? = "id", |
| 18 | + databaseType: DatabaseType = .mysql |
| 19 | + ) -> SQLRowToStatementConverter { |
| 20 | + SQLRowToStatementConverter( |
| 21 | + tableName: tableName, |
| 22 | + columns: columns, |
| 23 | + primaryKeyColumn: primaryKeyColumn, |
| 24 | + databaseType: databaseType |
| 25 | + ) |
| 26 | + } |
| 27 | + |
| 28 | + // MARK: - INSERT Generation |
| 29 | + |
| 30 | + @Test("Single row produces one INSERT statement") |
| 31 | + func insertSingleRow() { |
| 32 | + let converter = makeConverter() |
| 33 | + let result = converter.generateInserts(rows: [["1", "Alice", "alice@example.com"]]) |
| 34 | + #expect(result == "INSERT INTO `users` (`id`, `name`, `email`) VALUES ('1', 'Alice', 'alice@example.com');") |
| 35 | + } |
| 36 | + |
| 37 | + @Test("Multiple rows are joined by newlines") |
| 38 | + func insertMultipleRows() { |
| 39 | + let converter = makeConverter() |
| 40 | + let rows: [[String?]] = [ |
| 41 | + ["1", "Alice", "alice@example.com"], |
| 42 | + ["2", "Bob", "bob@example.com"] |
| 43 | + ] |
| 44 | + let result = converter.generateInserts(rows: rows) |
| 45 | + let lines = result.components(separatedBy: "\n") |
| 46 | + #expect(lines.count == 2) |
| 47 | + #expect(lines[0] == "INSERT INTO `users` (`id`, `name`, `email`) VALUES ('1', 'Alice', 'alice@example.com');") |
| 48 | + #expect(lines[1] == "INSERT INTO `users` (`id`, `name`, `email`) VALUES ('2', 'Bob', 'bob@example.com');") |
| 49 | + } |
| 50 | + |
| 51 | + @Test("NULL values render as unquoted NULL") |
| 52 | + func insertNullValues() { |
| 53 | + let converter = makeConverter() |
| 54 | + let result = converter.generateInserts(rows: [["1", nil, nil]]) |
| 55 | + #expect(result == "INSERT INTO `users` (`id`, `name`, `email`) VALUES ('1', NULL, NULL);") |
| 56 | + } |
| 57 | + |
| 58 | + @Test("Empty strings render as empty quoted string") |
| 59 | + func insertEmptyStrings() { |
| 60 | + let converter = makeConverter() |
| 61 | + let result = converter.generateInserts(rows: [["1", "", ""]]) |
| 62 | + #expect(result == "INSERT INTO `users` (`id`, `name`, `email`) VALUES ('1', '', '');") |
| 63 | + } |
| 64 | + |
| 65 | + @Test("Single quotes in data are escaped as double single-quotes") |
| 66 | + func insertSpecialCharactersSingleQuotes() { |
| 67 | + let converter = makeConverter() |
| 68 | + let result = converter.generateInserts(rows: [["1", "O'Brien", "o'brien@example.com"]]) |
| 69 | + #expect(result == "INSERT INTO `users` (`id`, `name`, `email`) VALUES ('1', 'O''Brien', 'o''brien@example.com');") |
| 70 | + } |
| 71 | + |
| 72 | + // MARK: - UPDATE Generation |
| 73 | + |
| 74 | + @Test("UPDATE with primary key excludes PK from SET and uses PK in WHERE") |
| 75 | + func updateWithPrimaryKey() { |
| 76 | + let converter = makeConverter() |
| 77 | + let result = converter.generateUpdates(rows: [["1", "Alice", "alice@example.com"]]) |
| 78 | + #expect(result == "UPDATE `users` SET `name` = 'Alice', `email` = 'alice@example.com' WHERE `id` = '1';") |
| 79 | + } |
| 80 | + |
| 81 | + @Test("UPDATE without primary key uses all columns in SET and WHERE") |
| 82 | + func updateWithoutPrimaryKey() { |
| 83 | + let converter = makeConverter(primaryKeyColumn: nil) |
| 84 | + let result = converter.generateUpdates(rows: [["1", "Alice", "alice@example.com"]]) |
| 85 | + #expect(result == "UPDATE `users` SET `id` = '1', `name` = 'Alice', `email` = 'alice@example.com' WHERE `id` = '1' AND `name` = 'Alice' AND `email` = 'alice@example.com';") |
| 86 | + } |
| 87 | + |
| 88 | + @Test("UPDATE without PK uses IS NULL in WHERE clause for NULL values") |
| 89 | + func updateNullValuesInWhereClauseNoPK() { |
| 90 | + let converter = makeConverter(primaryKeyColumn: nil) |
| 91 | + let result = converter.generateUpdates(rows: [["1", nil, "alice@example.com"]]) |
| 92 | + #expect(result == "UPDATE `users` SET `id` = '1', `name` = NULL, `email` = 'alice@example.com' WHERE `id` = '1' AND `name` IS NULL AND `email` = 'alice@example.com';") |
| 93 | + } |
| 94 | + |
| 95 | + // MARK: - Database-Specific Quoting |
| 96 | + |
| 97 | + @Test("ClickHouse uses ALTER TABLE ... UPDATE syntax") |
| 98 | + func clickhouseUsesAlterTableUpdate() { |
| 99 | + let converter = makeConverter(databaseType: .clickhouse) |
| 100 | + let result = converter.generateUpdates(rows: [["1", "Alice", "alice@example.com"]]) |
| 101 | + #expect(result == "ALTER TABLE `users` UPDATE `name` = 'Alice', `email` = 'alice@example.com' WHERE `id` = '1';") |
| 102 | + } |
| 103 | + |
| 104 | + @Test("MSSQL uses bracket quoting") |
| 105 | + func mssqlUsesBracketQuoting() { |
| 106 | + let converter = makeConverter(databaseType: .mssql) |
| 107 | + let result = converter.generateInserts(rows: [["1", "Alice", "alice@example.com"]]) |
| 108 | + #expect(result == "INSERT INTO [users] ([id], [name], [email]) VALUES ('1', 'Alice', 'alice@example.com');") |
| 109 | + } |
| 110 | + |
| 111 | + @Test("PostgreSQL uses double-quote quoting") |
| 112 | + func postgresqlUsesDoubleQuoteQuoting() { |
| 113 | + let converter = makeConverter(databaseType: .postgresql) |
| 114 | + let result = converter.generateInserts(rows: [["1", "Alice", "alice@example.com"]]) |
| 115 | + #expect(result == "INSERT INTO \"users\" (\"id\", \"name\", \"email\") VALUES ('1', 'Alice', 'alice@example.com');") |
| 116 | + } |
| 117 | + |
| 118 | + @Test("MySQL uses backtick quoting") |
| 119 | + func mysqlUsesBacktickQuoting() { |
| 120 | + let converter = makeConverter(databaseType: .mysql) |
| 121 | + let result = converter.generateInserts(rows: [["1", "Alice", "alice@example.com"]]) |
| 122 | + #expect(result == "INSERT INTO `users` (`id`, `name`, `email`) VALUES ('1', 'Alice', 'alice@example.com');") |
| 123 | + } |
| 124 | + |
| 125 | + // MARK: - Edge Cases |
| 126 | + |
| 127 | + @Test("Empty rows input returns empty string") |
| 128 | + func emptyRowsReturnsEmptyString() { |
| 129 | + let converter = makeConverter() |
| 130 | + #expect(converter.generateInserts(rows: []) == "") |
| 131 | + #expect(converter.generateUpdates(rows: []) == "") |
| 132 | + } |
| 133 | + |
| 134 | + @Test("Row cap at 50,000 — 50,001 rows produces exactly 50,000 lines") |
| 135 | + func rowCapAt50k() { |
| 136 | + let converter = makeConverter( |
| 137 | + columns: ["id", "name"], |
| 138 | + primaryKeyColumn: "id" |
| 139 | + ) |
| 140 | + let rows: [[String?]] = (1...50_001).map { i in ["\(i)", "name\(i)"] } |
| 141 | + let result = converter.generateInserts(rows: rows) |
| 142 | + let lines = result.components(separatedBy: "\n") |
| 143 | + #expect(lines.count == 50_000) |
| 144 | + } |
| 145 | +} |
0 commit comments