|
| 1 | +import { filterSqlDirectives } from "../../../src/infra/engines/sql-filter.js"; |
| 2 | + |
| 3 | +describe("filterSqlDirectives()", () => { |
| 4 | + // --- PostgreSQL directives --- |
| 5 | + |
| 6 | + it("removes \\connect meta-commands", () => { |
| 7 | + const input = |
| 8 | + "SET lock_timeout = 0;\n\\connect sateus\nCREATE TABLE t (id int);"; |
| 9 | + const result = filterSqlDirectives(input); |
| 10 | + expect(result).not.toContain("\\connect"); |
| 11 | + expect(result).toContain("SET lock_timeout = 0;"); |
| 12 | + expect(result).toContain("CREATE TABLE t"); |
| 13 | + }); |
| 14 | + |
| 15 | + it("removes \\c shorthand meta-commands", () => { |
| 16 | + const input = "\\c sateus\nSELECT 1;"; |
| 17 | + const result = filterSqlDirectives(input); |
| 18 | + expect(result).not.toContain("\\c sateus"); |
| 19 | + expect(result).toContain("SELECT 1;"); |
| 20 | + }); |
| 21 | + |
| 22 | + it("removes single-line CREATE DATABASE statement", () => { |
| 23 | + const input = "CREATE DATABASE sateus;\nCREATE TABLE t (id int);"; |
| 24 | + const result = filterSqlDirectives(input); |
| 25 | + expect(result).not.toContain("CREATE DATABASE"); |
| 26 | + expect(result).toContain("CREATE TABLE t"); |
| 27 | + }); |
| 28 | + |
| 29 | + it("removes multi-line CREATE DATABASE block", () => { |
| 30 | + const input = [ |
| 31 | + "CREATE DATABASE sateus", |
| 32 | + " WITH TEMPLATE = template0", |
| 33 | + " ENCODING = 'UTF8';", |
| 34 | + "CREATE TABLE t (id int);", |
| 35 | + ].join("\n"); |
| 36 | + const result = filterSqlDirectives(input); |
| 37 | + expect(result).not.toContain("CREATE DATABASE"); |
| 38 | + expect(result).not.toContain("WITH TEMPLATE"); |
| 39 | + expect(result).toContain("CREATE TABLE t"); |
| 40 | + }); |
| 41 | + |
| 42 | + it("removes DROP DATABASE statement", () => { |
| 43 | + const input = "DROP DATABASE IF EXISTS sateus;\nCREATE TABLE t (id int);"; |
| 44 | + const result = filterSqlDirectives(input); |
| 45 | + expect(result).not.toContain("DROP DATABASE"); |
| 46 | + expect(result).toContain("CREATE TABLE t"); |
| 47 | + }); |
| 48 | + |
| 49 | + // --- MySQL directives --- |
| 50 | + |
| 51 | + it("removes USE statement", () => { |
| 52 | + const input = "USE `sateus`;\nCREATE TABLE t (id int);"; |
| 53 | + const result = filterSqlDirectives(input); |
| 54 | + expect(result).not.toContain("USE `sateus`"); |
| 55 | + expect(result).toContain("CREATE TABLE t"); |
| 56 | + }); |
| 57 | + |
| 58 | + it("removes USE statement without backticks", () => { |
| 59 | + const input = "USE sateus;\nINSERT INTO t VALUES (1);"; |
| 60 | + const result = filterSqlDirectives(input); |
| 61 | + expect(result).not.toContain("USE sateus"); |
| 62 | + expect(result).toContain("INSERT INTO t"); |
| 63 | + }); |
| 64 | + |
| 65 | + // --- Case insensitivity --- |
| 66 | + |
| 67 | + it("is case-insensitive for directives", () => { |
| 68 | + const input = "create database foo;\nuse bar;\n\\Connect baz\nSELECT 1;"; |
| 69 | + const result = filterSqlDirectives(input); |
| 70 | + expect(result).not.toContain("create database"); |
| 71 | + expect(result).not.toContain("use bar"); |
| 72 | + expect(result).not.toContain("\\Connect"); |
| 73 | + expect(result).toContain("SELECT 1;"); |
| 74 | + }); |
| 75 | + |
| 76 | + // --- Passthrough --- |
| 77 | + |
| 78 | + it("does not alter SQL that has no redirect directives", () => { |
| 79 | + const input = |
| 80 | + "CREATE TABLE users (id serial, name text);\nINSERT INTO users VALUES (1, 'Alice');"; |
| 81 | + expect(filterSqlDirectives(input)).toBe(input); |
| 82 | + }); |
| 83 | + |
| 84 | + it("preserves CREATE TABLE that starts with CREATE keyword", () => { |
| 85 | + const input = "CREATE TABLE database_config (key text, val text);"; |
| 86 | + const result = filterSqlDirectives(input); |
| 87 | + expect(result).toContain("CREATE TABLE database_config"); |
| 88 | + }); |
| 89 | +}); |
0 commit comments