|
| 1 | +/** |
| 2 | + * Unit tests for ClickHouse driver logic: |
| 3 | + * - DDL vs SELECT routing (command vs query) |
| 4 | + * - LIMIT injection and bypass prevention |
| 5 | + * - Truncation detection |
| 6 | + * - Nullable detection from type string |
| 7 | + * - Connection guard (execute before connect) |
| 8 | + * - Binds parameter is silently ignored |
| 9 | + */ |
| 10 | +import { describe, test, expect, mock, beforeEach } from "bun:test" |
| 11 | + |
| 12 | +// --- Mock @clickhouse/client --- |
| 13 | + |
| 14 | +let mockCommandCalls: any[] = [] |
| 15 | +let mockQueryCalls: any[] = [] |
| 16 | +let mockQueryResult: any[] = [] |
| 17 | +let mockCloseCalls = 0 |
| 18 | + |
| 19 | +function resetMocks() { |
| 20 | + mockCommandCalls = [] |
| 21 | + mockQueryCalls = [] |
| 22 | + mockQueryResult = [] |
| 23 | + mockCloseCalls = 0 |
| 24 | +} |
| 25 | + |
| 26 | +mock.module("@clickhouse/client", () => ({ |
| 27 | + createClient: (_config: any) => ({ |
| 28 | + command: async (opts: any) => { |
| 29 | + mockCommandCalls.push(opts) |
| 30 | + }, |
| 31 | + query: async (opts: any) => { |
| 32 | + mockQueryCalls.push(opts) |
| 33 | + return { json: async () => mockQueryResult } |
| 34 | + }, |
| 35 | + close: async () => { |
| 36 | + mockCloseCalls++ |
| 37 | + }, |
| 38 | + }), |
| 39 | +})) |
| 40 | + |
| 41 | +// Import after mocking |
| 42 | +const { connect } = await import("../src/clickhouse") |
| 43 | + |
| 44 | +describe("ClickHouse driver unit tests", () => { |
| 45 | + let connector: Awaited<ReturnType<typeof connect>> |
| 46 | + |
| 47 | + beforeEach(async () => { |
| 48 | + resetMocks() |
| 49 | + connector = await connect({ host: "localhost", port: 8123 }) |
| 50 | + await connector.connect() |
| 51 | + }) |
| 52 | + |
| 53 | + // --- DDL vs SELECT routing --- |
| 54 | + |
| 55 | + describe("DDL routing via client.command()", () => { |
| 56 | + const ddlStatements = [ |
| 57 | + "INSERT INTO t VALUES (1, 'a')", |
| 58 | + "CREATE TABLE t (id UInt32) ENGINE = MergeTree()", |
| 59 | + "DROP TABLE t", |
| 60 | + "ALTER TABLE t ADD COLUMN x String", |
| 61 | + "TRUNCATE TABLE t", |
| 62 | + "OPTIMIZE TABLE t FINAL", |
| 63 | + "SYSTEM RELOAD DICTIONARY", |
| 64 | + "SET max_memory_usage = 1000000", |
| 65 | + ] |
| 66 | + |
| 67 | + for (const sql of ddlStatements) { |
| 68 | + test(`routes "${sql.slice(0, 40)}..." to client.command()`, async () => { |
| 69 | + const result = await connector.execute(sql) |
| 70 | + expect(mockCommandCalls.length).toBe(1) |
| 71 | + expect(mockQueryCalls.length).toBe(0) |
| 72 | + expect(result.row_count).toBe(0) |
| 73 | + }) |
| 74 | + } |
| 75 | + |
| 76 | + test("strips trailing semicolons from DDL", async () => { |
| 77 | + await connector.execute("DROP TABLE t; ") |
| 78 | + expect(mockCommandCalls[0].query).toBe("DROP TABLE t") |
| 79 | + }) |
| 80 | + }) |
| 81 | + |
| 82 | + describe("SELECT routing via client.query()", () => { |
| 83 | + test("routes SELECT to client.query()", async () => { |
| 84 | + mockQueryResult = [{ id: 1, name: "test" }] |
| 85 | + await connector.execute("SELECT id, name FROM t") |
| 86 | + expect(mockQueryCalls.length).toBe(1) |
| 87 | + expect(mockCommandCalls.length).toBe(0) |
| 88 | + }) |
| 89 | + |
| 90 | + test("routes SHOW to client.query()", async () => { |
| 91 | + mockQueryResult = [{ name: "db1" }] |
| 92 | + await connector.execute("SHOW DATABASES") |
| 93 | + expect(mockQueryCalls.length).toBe(1) |
| 94 | + }) |
| 95 | + |
| 96 | + test("routes DESCRIBE to client.query()", async () => { |
| 97 | + mockQueryResult = [{ name: "col1", type: "String" }] |
| 98 | + await connector.execute("DESCRIBE TABLE t") |
| 99 | + expect(mockQueryCalls.length).toBe(1) |
| 100 | + }) |
| 101 | + |
| 102 | + test("routes EXPLAIN to client.query()", async () => { |
| 103 | + mockQueryResult = [{ explain: "ReadFromMergeTree" }] |
| 104 | + await connector.execute("EXPLAIN SELECT 1") |
| 105 | + expect(mockQueryCalls.length).toBe(1) |
| 106 | + }) |
| 107 | + }) |
| 108 | + |
| 109 | + // --- LIMIT injection --- |
| 110 | + |
| 111 | + describe("LIMIT injection", () => { |
| 112 | + test("appends LIMIT to SELECT without one", async () => { |
| 113 | + mockQueryResult = [{ id: 1 }] |
| 114 | + await connector.execute("SELECT * FROM t", 10) |
| 115 | + expect(mockQueryCalls[0].query).toContain("LIMIT 11") |
| 116 | + }) |
| 117 | + |
| 118 | + test("does NOT append LIMIT to SELECT that already has one", async () => { |
| 119 | + mockQueryResult = [{ id: 1 }] |
| 120 | + await connector.execute("SELECT * FROM t LIMIT 5", 10) |
| 121 | + expect(mockQueryCalls[0].query).not.toContain("LIMIT 11") |
| 122 | + }) |
| 123 | + |
| 124 | + test("does NOT append LIMIT to SHOW/DESCRIBE/EXPLAIN/EXISTS", async () => { |
| 125 | + mockQueryResult = [{ name: "t" }] |
| 126 | + |
| 127 | + await connector.execute("SHOW TABLES", 10) |
| 128 | + expect(mockQueryCalls[0].query).not.toContain("LIMIT") |
| 129 | + |
| 130 | + mockQueryCalls = [] |
| 131 | + await connector.execute("DESCRIBE TABLE t", 10) |
| 132 | + expect(mockQueryCalls[0].query).not.toContain("LIMIT") |
| 133 | + |
| 134 | + mockQueryCalls = [] |
| 135 | + await connector.execute("EXISTS TABLE t", 10) |
| 136 | + expect(mockQueryCalls[0].query).not.toContain("LIMIT") |
| 137 | + }) |
| 138 | + |
| 139 | + test("does NOT append LIMIT when limit=0 (unlimited)", async () => { |
| 140 | + mockQueryResult = [{ id: 1 }, { id: 2 }] |
| 141 | + await connector.execute("SELECT * FROM t", 0) |
| 142 | + expect(mockQueryCalls[0].query).not.toContain("LIMIT") |
| 143 | + }) |
| 144 | + |
| 145 | + test("uses default limit=1000 when limit is undefined", async () => { |
| 146 | + mockQueryResult = [{ id: 1 }] |
| 147 | + await connector.execute("SELECT * FROM t") |
| 148 | + expect(mockQueryCalls[0].query).toContain("LIMIT 1001") |
| 149 | + }) |
| 150 | + |
| 151 | + test("LIMIT in SQL comment does NOT prevent LIMIT injection", async () => { |
| 152 | + mockQueryResult = [{ id: 1 }] |
| 153 | + await connector.execute("SELECT * FROM t -- LIMIT 100", 10) |
| 154 | + // Should still append LIMIT because the comment-stripped SQL has no LIMIT |
| 155 | + expect(mockQueryCalls[0].query).toContain("LIMIT 11") |
| 156 | + }) |
| 157 | + |
| 158 | + test("LIMIT in block comment does NOT prevent LIMIT injection", async () => { |
| 159 | + mockQueryResult = [{ id: 1 }] |
| 160 | + await connector.execute("SELECT * FROM t /* LIMIT 50 */", 10) |
| 161 | + expect(mockQueryCalls[0].query).toContain("LIMIT 11") |
| 162 | + }) |
| 163 | + |
| 164 | + test("real LIMIT in SQL still prevents double LIMIT", async () => { |
| 165 | + mockQueryResult = [{ id: 1 }] |
| 166 | + await connector.execute("SELECT * FROM t LIMIT 5 -- max rows", 10) |
| 167 | + expect(mockQueryCalls[0].query).not.toContain("LIMIT 11") |
| 168 | + }) |
| 169 | + }) |
| 170 | + |
| 171 | + // --- Truncation detection --- |
| 172 | + |
| 173 | + describe("truncation detection", () => { |
| 174 | + test("detects truncation when rows exceed limit", async () => { |
| 175 | + mockQueryResult = Array.from({ length: 6 }, (_, i) => ({ id: i })) |
| 176 | + const result = await connector.execute("SELECT * FROM t", 5) |
| 177 | + expect(result.truncated).toBe(true) |
| 178 | + expect(result.row_count).toBe(5) |
| 179 | + expect(result.rows.length).toBe(5) |
| 180 | + }) |
| 181 | + |
| 182 | + test("no truncation when rows equal limit", async () => { |
| 183 | + mockQueryResult = Array.from({ length: 5 }, (_, i) => ({ id: i })) |
| 184 | + const result = await connector.execute("SELECT * FROM t", 5) |
| 185 | + expect(result.truncated).toBe(false) |
| 186 | + expect(result.row_count).toBe(5) |
| 187 | + }) |
| 188 | + |
| 189 | + test("no truncation when rows below limit", async () => { |
| 190 | + mockQueryResult = [{ id: 1 }] |
| 191 | + const result = await connector.execute("SELECT * FROM t", 10) |
| 192 | + expect(result.truncated).toBe(false) |
| 193 | + expect(result.row_count).toBe(1) |
| 194 | + }) |
| 195 | + |
| 196 | + test("limit=0 returns all rows without truncation", async () => { |
| 197 | + mockQueryResult = Array.from({ length: 100 }, (_, i) => ({ id: i })) |
| 198 | + const result = await connector.execute("SELECT * FROM t", 0) |
| 199 | + expect(result.truncated).toBe(false) |
| 200 | + expect(result.row_count).toBe(100) |
| 201 | + }) |
| 202 | + |
| 203 | + test("empty result returns correctly", async () => { |
| 204 | + mockQueryResult = [] |
| 205 | + const result = await connector.execute("SELECT * FROM t", 10) |
| 206 | + expect(result.row_count).toBe(0) |
| 207 | + expect(result.columns).toEqual([]) |
| 208 | + expect(result.truncated).toBe(false) |
| 209 | + }) |
| 210 | + }) |
| 211 | + |
| 212 | + // --- Nullable detection --- |
| 213 | + |
| 214 | + describe("describeTable nullable detection", () => { |
| 215 | + test("detects Nullable(String) as nullable", async () => { |
| 216 | + mockQueryResult = [{ name: "col1", type: "Nullable(String)" }] |
| 217 | + const cols = await connector.describeTable("default", "t") |
| 218 | + expect(cols[0].nullable).toBe(true) |
| 219 | + }) |
| 220 | + |
| 221 | + test("detects String as non-nullable", async () => { |
| 222 | + mockQueryResult = [{ name: "col1", type: "String" }] |
| 223 | + const cols = await connector.describeTable("default", "t") |
| 224 | + expect(cols[0].nullable).toBe(false) |
| 225 | + }) |
| 226 | + |
| 227 | + test("detects Nullable(UInt32) as nullable", async () => { |
| 228 | + mockQueryResult = [{ name: "col1", type: "Nullable(UInt32)" }] |
| 229 | + const cols = await connector.describeTable("default", "t") |
| 230 | + expect(cols[0].nullable).toBe(true) |
| 231 | + }) |
| 232 | + |
| 233 | + test("Array(Nullable(String)) is NOT nullable at column level", async () => { |
| 234 | + // The column itself isn't Nullable — the array elements are |
| 235 | + mockQueryResult = [{ name: "col1", type: "Array(Nullable(String))" }] |
| 236 | + const cols = await connector.describeTable("default", "t") |
| 237 | + expect(cols[0].nullable).toBe(false) |
| 238 | + }) |
| 239 | + |
| 240 | + test("LowCardinality(Nullable(String)) is NOT nullable at column level", async () => { |
| 241 | + // Nullable is nested inside LowCardinality — column-level is LowCardinality |
| 242 | + mockQueryResult = [{ name: "col1", type: "LowCardinality(Nullable(String))" }] |
| 243 | + const cols = await connector.describeTable("default", "t") |
| 244 | + expect(cols[0].nullable).toBe(false) |
| 245 | + }) |
| 246 | + }) |
| 247 | + |
| 248 | + // --- Connection guard --- |
| 249 | + |
| 250 | + describe("connection lifecycle", () => { |
| 251 | + test("execute before connect throws clear error", async () => { |
| 252 | + const freshConnector = await connect({ host: "localhost" }) |
| 253 | + // Don't call connect() |
| 254 | + expect(freshConnector.execute("SELECT 1")).rejects.toThrow("not connected") |
| 255 | + }) |
| 256 | + |
| 257 | + test("close is idempotent", async () => { |
| 258 | + await connector.close() |
| 259 | + await connector.close() // should not throw |
| 260 | + expect(mockCloseCalls).toBe(1) // only called once |
| 261 | + }) |
| 262 | + }) |
| 263 | + |
| 264 | + // --- Binds parameter --- |
| 265 | + |
| 266 | + describe("binds parameter", () => { |
| 267 | + test("binds parameter is silently ignored", async () => { |
| 268 | + mockQueryResult = [{ id: 1 }] |
| 269 | + // Should not throw — binds are ignored |
| 270 | + const result = await connector.execute("SELECT 1", 10, ["unused", "binds"]) |
| 271 | + expect(result.row_count).toBe(1) |
| 272 | + }) |
| 273 | + |
| 274 | + test("empty binds array works fine", async () => { |
| 275 | + mockQueryResult = [{ id: 1 }] |
| 276 | + const result = await connector.execute("SELECT 1", 10, []) |
| 277 | + expect(result.row_count).toBe(1) |
| 278 | + }) |
| 279 | + }) |
| 280 | + |
| 281 | + // --- Column mapping --- |
| 282 | + |
| 283 | + describe("result format", () => { |
| 284 | + test("maps rows to column-ordered arrays", async () => { |
| 285 | + mockQueryResult = [ |
| 286 | + { id: 1, name: "alice", age: 30 }, |
| 287 | + { id: 2, name: "bob", age: 25 }, |
| 288 | + ] |
| 289 | + const result = await connector.execute("SELECT * FROM t", 10) |
| 290 | + expect(result.columns).toEqual(["id", "name", "age"]) |
| 291 | + expect(result.rows).toEqual([ |
| 292 | + [1, "alice", 30], |
| 293 | + [2, "bob", 25], |
| 294 | + ]) |
| 295 | + }) |
| 296 | + }) |
| 297 | + |
| 298 | + // --- listTables type detection --- |
| 299 | + |
| 300 | + describe("listTables engine-to-type mapping", () => { |
| 301 | + test("MergeTree engines map to table", async () => { |
| 302 | + mockQueryResult = [{ name: "t1", engine: "MergeTree" }] |
| 303 | + const tables = await connector.listTables("default") |
| 304 | + expect(tables[0].type).toBe("table") |
| 305 | + }) |
| 306 | + |
| 307 | + test("MaterializedView maps to view", async () => { |
| 308 | + mockQueryResult = [{ name: "v1", engine: "MaterializedView" }] |
| 309 | + const tables = await connector.listTables("default") |
| 310 | + expect(tables[0].type).toBe("view") |
| 311 | + }) |
| 312 | + |
| 313 | + test("View maps to view", async () => { |
| 314 | + mockQueryResult = [{ name: "v2", engine: "View" }] |
| 315 | + const tables = await connector.listTables("default") |
| 316 | + expect(tables[0].type).toBe("view") |
| 317 | + }) |
| 318 | + }) |
| 319 | +}) |
0 commit comments