modify tests for new formatter

This commit is contained in:
Levy A.
2025-07-05 02:15:24 -03:00
parent 373a4a26c4
commit e81c7b07fb
9 changed files with 180 additions and 178 deletions

View File

@@ -207,71 +207,71 @@ mod tests {
to_sql_string_test!(
test_alter_table_rename,
"ALTER TABLE t RENAME TO new_table_name;"
"ALTER TABLE t RENAME TO new_table_name"
);
to_sql_string_test!(
test_alter_table_add_column,
"ALTER TABLE t ADD COLUMN c INTEGER;"
"ALTER TABLE t ADD COLUMN c INTEGER"
);
to_sql_string_test!(
test_alter_table_add_column_with_default,
"ALTER TABLE t ADD COLUMN c TEXT DEFAULT 'value';"
"ALTER TABLE t ADD COLUMN c TEXT DEFAULT 'value'"
);
to_sql_string_test!(
test_alter_table_add_column_not_null_default,
"ALTER TABLE t ADD COLUMN c REAL NOT NULL DEFAULT 0.0;"
"ALTER TABLE t ADD COLUMN c REAL NOT NULL DEFAULT 0.0"
);
to_sql_string_test!(
test_alter_table_add_column_unique,
"ALTER TABLE t ADD COLUMN c TEXT UNIQUE",
ignore = "ParserError = Cannot add a UNIQUE column;"
ignore = "ParserError = Cannot add a UNIQUE column"
);
to_sql_string_test!(
test_alter_table_rename_column,
"ALTER TABLE t RENAME COLUMN old_name TO new_name;"
"ALTER TABLE t RENAME COLUMN old_name TO new_name"
);
to_sql_string_test!(test_alter_table_drop_column, "ALTER TABLE t DROP COLUMN c;");
to_sql_string_test!(test_alter_table_drop_column, "ALTER TABLE t DROP COLUMN c");
to_sql_string_test!(
test_alter_table_add_column_check,
"ALTER TABLE t ADD COLUMN c INTEGER CHECK (c > 0);"
"ALTER TABLE t ADD COLUMN c INTEGER CHECK (c > 0)"
);
to_sql_string_test!(
test_alter_table_add_column_foreign_key,
"ALTER TABLE t ADD COLUMN c INTEGER REFERENCES t2(id) ON DELETE CASCADE;"
"ALTER TABLE t ADD COLUMN c INTEGER REFERENCES t2 (id) ON DELETE CASCADE"
);
to_sql_string_test!(
test_alter_table_add_column_collate,
"ALTER TABLE t ADD COLUMN c TEXT COLLATE NOCASE;"
"ALTER TABLE t ADD COLUMN c TEXT COLLATE NOCASE"
);
to_sql_string_test!(
test_alter_table_add_column_primary_key,
"ALTER TABLE t ADD COLUMN c INTEGER PRIMARY KEY;",
"ALTER TABLE t ADD COLUMN c INTEGER PRIMARY KEY",
ignore = "ParserError = Cannot add a PRIMARY KEY column"
);
to_sql_string_test!(
test_alter_table_add_column_primary_key_autoincrement,
"ALTER TABLE t ADD COLUMN c INTEGER PRIMARY KEY AUTOINCREMENT;",
"ALTER TABLE t ADD COLUMN c INTEGER PRIMARY KEY AUTOINCREMENT",
ignore = "ParserError = Cannot add a PRIMARY KEY column"
);
to_sql_string_test!(
test_alter_table_add_generated_column,
"ALTER TABLE t ADD COLUMN c_generated AS (a + b) STORED;"
"ALTER TABLE t ADD COLUMN c_generated AS (a + b) STORED"
);
to_sql_string_test!(
test_alter_table_add_column_schema,
"ALTER TABLE schema_name.t ADD COLUMN c INTEGER;"
"ALTER TABLE schema_name.t ADD COLUMN c INTEGER"
);
}

View File

@@ -123,118 +123,118 @@ mod tests {
to_sql_string_test!(
test_create_table_simple,
"CREATE TABLE t (a INTEGER, b TEXT);"
"CREATE TABLE t (a INTEGER, b TEXT)"
);
to_sql_string_test!(
test_create_table_primary_key,
"CREATE TABLE t (id INTEGER PRIMARY KEY, name TEXT);"
"CREATE TABLE t (id INTEGER PRIMARY KEY, name TEXT)"
);
to_sql_string_test!(
test_create_table_multi_primary_key,
"CREATE TABLE t (a INTEGER, b TEXT, PRIMARY KEY (a, b));"
"CREATE TABLE t (a INTEGER, b TEXT, PRIMARY KEY (a, b))"
);
to_sql_string_test!(
test_create_table_data_types,
"CREATE TABLE t (a INTEGER, b TEXT, c REAL, d BLOB, e NUMERIC);"
"CREATE TABLE t (a INTEGER, b TEXT, c REAL, d BLOB, e NUMERIC)"
);
to_sql_string_test!(
test_create_table_foreign_key,
"CREATE TABLE t2 (id INTEGER PRIMARY KEY, t_id INTEGER, FOREIGN KEY (t_id) REFERENCES t(id));"
"CREATE TABLE t2 (id INTEGER PRIMARY KEY, t_id INTEGER, FOREIGN KEY (t_id) REFERENCES t (id))"
);
to_sql_string_test!(
test_create_table_foreign_key_cascade,
"CREATE TABLE t2 (id INTEGER PRIMARY KEY, t_id INTEGER, FOREIGN KEY (t_id) REFERENCES t(id) ON DELETE CASCADE);"
"CREATE TABLE t2 (id INTEGER PRIMARY KEY, t_id INTEGER, FOREIGN KEY (t_id) REFERENCES t (id) ON DELETE CASCADE)"
);
to_sql_string_test!(
test_create_table_unique,
"CREATE TABLE t (a INTEGER UNIQUE, b TEXT);"
"CREATE TABLE t (a INTEGER UNIQUE, b TEXT)"
);
to_sql_string_test!(
test_create_table_not_null,
"CREATE TABLE t (a INTEGER NOT NULL, b TEXT);"
"CREATE TABLE t (a INTEGER NOT NULL, b TEXT)"
);
to_sql_string_test!(
test_create_table_check,
"CREATE TABLE t (a INTEGER CHECK (a > 0), b TEXT);"
"CREATE TABLE t (a INTEGER CHECK (a > 0), b TEXT)"
);
to_sql_string_test!(
test_create_table_default,
"CREATE TABLE t (a INTEGER DEFAULT 0, b TEXT);"
"CREATE TABLE t (a INTEGER DEFAULT 0, b TEXT)"
);
to_sql_string_test!(
test_create_table_multiple_constraints,
"CREATE TABLE t (a INTEGER NOT NULL UNIQUE, b TEXT DEFAULT 'default');"
"CREATE TABLE t (a INTEGER NOT NULL UNIQUE, b TEXT DEFAULT 'default')"
);
to_sql_string_test!(
test_create_table_generated_column,
"CREATE TABLE t (a INTEGER, b INTEGER, c INTEGER AS (a + b));"
"CREATE TABLE t (a INTEGER, b INTEGER, c INTEGER AS (a + b))"
);
to_sql_string_test!(
test_create_table_generated_stored,
"CREATE TABLE t (a INTEGER, b INTEGER, c INTEGER AS (a + b) STORED);"
"CREATE TABLE t (a INTEGER, b INTEGER, c INTEGER AS (a + b) STORED)"
);
to_sql_string_test!(
test_create_table_generated_virtual,
"CREATE TABLE t (a INTEGER, b INTEGER, c INTEGER AS (a + b) VIRTUAL);"
"CREATE TABLE t (a INTEGER, b INTEGER, c INTEGER AS (a + b) VIRTUAL)"
);
to_sql_string_test!(
test_create_table_quoted_columns,
"CREATE TABLE t (\"select\" INTEGER, \"from\" TEXT);"
"CREATE TABLE t (\"select\" INTEGER, \"from\" TEXT)"
);
to_sql_string_test!(
test_create_table_quoted_table,
"CREATE TABLE \"my table\" (a INTEGER);"
"CREATE TABLE \"my table\" (a INTEGER)"
);
to_sql_string_test!(
test_create_table_if_not_exists,
"CREATE TABLE IF NOT EXISTS t (a INTEGER);"
"CREATE TABLE IF NOT EXISTS t (a INTEGER)"
);
to_sql_string_test!(test_create_temp_table, "CREATE TEMP TABLE t (a INTEGER);");
to_sql_string_test!(test_create_temp_table, "CREATE TEMP TABLE t (a INTEGER)");
to_sql_string_test!(
test_create_table_without_rowid,
"CREATE TABLE t (a INTEGER PRIMARY KEY, b TEXT) WITHOUT ROWID;"
"CREATE TABLE t (a INTEGER PRIMARY KEY, b TEXT) WITHOUT ROWID"
);
to_sql_string_test!(
test_create_table_named_primary_key,
"CREATE TABLE t (a INTEGER CONSTRAINT pk_a PRIMARY KEY);"
"CREATE TABLE t (a INTEGER CONSTRAINT pk_a PRIMARY KEY)"
);
to_sql_string_test!(
test_create_table_named_unique,
"CREATE TABLE t (a INTEGER, CONSTRAINT unique_a UNIQUE (a));"
"CREATE TABLE t (a INTEGER, CONSTRAINT unique_a UNIQUE (a))"
);
to_sql_string_test!(
test_create_table_named_foreign_key,
"CREATE TABLE t2 (id INTEGER, t_id INTEGER, CONSTRAINT fk_t FOREIGN KEY (t_id) REFERENCES t(id));"
"CREATE TABLE t2 (id INTEGER, t_id INTEGER, CONSTRAINT fk_t FOREIGN KEY (t_id) REFERENCES t (id))"
);
to_sql_string_test!(
test_create_table_complex,
"CREATE TABLE t (id INTEGER PRIMARY KEY, a INTEGER NOT NULL, b TEXT DEFAULT 'default', c INTEGER AS (a * 2), CONSTRAINT unique_a UNIQUE (a));"
"CREATE TABLE t (id INTEGER PRIMARY KEY, a INTEGER NOT NULL, b TEXT DEFAULT 'default', c INTEGER AS (a * 2), CONSTRAINT unique_a UNIQUE (a))"
);
to_sql_string_test!(
test_create_table_multiple_foreign_keys,
"CREATE TABLE t3 (id INTEGER PRIMARY KEY, t1_id INTEGER, t2_id INTEGER, FOREIGN KEY (t1_id) REFERENCES t1(id), FOREIGN KEY (t2_id) REFERENCES t2(id));"
"CREATE TABLE t3 (id INTEGER PRIMARY KEY, t1_id INTEGER, t2_id INTEGER, FOREIGN KEY (t1_id) REFERENCES t1 (id), FOREIGN KEY (t2_id) REFERENCES t2 (id))"
);
}

View File

@@ -255,7 +255,7 @@ mod tests {
BEGIN
INSERT INTO employee_log (action, employee_id, timestamp)
VALUES ('INSERT', NEW.id, CURRENT_TIMESTAMP);
END;"
END"
);
to_sql_string_test!(
@@ -266,7 +266,7 @@ mod tests {
BEGIN
INSERT INTO employee_log (action, employee_id, old_value, new_value, timestamp)
VALUES ('UPDATE', OLD.id, OLD.salary, NEW.salary, CURRENT_TIMESTAMP);
END;"
END"
);
to_sql_string_test!(
@@ -277,7 +277,7 @@ mod tests {
BEGIN
INSERT INTO employee_log (action, employee_id, timestamp)
VALUES ('DELETE', OLD.id, CURRENT_TIMESTAMP);
END;"
END"
);
to_sql_string_test!(
@@ -287,8 +287,8 @@ mod tests {
FOR EACH ROW
WHEN NEW.salary < 0
BEGIN
SELECT RAISE(FAIL, 'Salary cannot be negative');
END;"
SELECT RAISE (FAIL, 'Salary cannot be negative');
END"
);
to_sql_string_test!(
@@ -299,6 +299,6 @@ mod tests {
BEGIN
INSERT INTO departments (name) SELECT NEW.department WHERE NOT EXISTS (SELECT 1 FROM departments WHERE name = NEW.department);
INSERT INTO employees (name, department_id) VALUES (NEW.name, (SELECT id FROM departments WHERE name = NEW.department));
END;"
END"
);
}

View File

@@ -24,61 +24,61 @@ mod tests {
to_sql_string_test!(
test_create_virtual_table_fts5_basic,
"CREATE VIRTUAL TABLE docs USING fts5(title, content);"
"CREATE VIRTUAL TABLE docs USING fts5 (title, content)"
);
to_sql_string_test!(
test_create_virtual_table_fts5_tokenizer,
"CREATE VIRTUAL TABLE docs USING fts5(title, content, tokenize = 'porter');"
"CREATE VIRTUAL TABLE docs USING fts5 (title, content, tokenize = 'porter')"
);
to_sql_string_test!(
test_create_virtual_table_fts5_unindexed,
"CREATE VIRTUAL TABLE docs USING fts5(title, content, metadata UNINDEXED);"
"CREATE VIRTUAL TABLE docs USING fts5 (title, content, metadata UNINDEXED)"
);
to_sql_string_test!(
test_create_virtual_table_fts5_prefix,
"CREATE VIRTUAL TABLE docs USING fts5(title, content, tokenize = 'unicode61', prefix = '2 4');"
"CREATE VIRTUAL TABLE docs USING fts5 (title, content, tokenize = 'unicode61', prefix = '2 4')"
);
to_sql_string_test!(
test_create_virtual_table_fts5_contentless,
"CREATE VIRTUAL TABLE docs USING fts5(title, content, content = '');"
"CREATE VIRTUAL TABLE docs USING fts5 (title, content, content = '')"
);
to_sql_string_test!(
test_create_virtual_table_fts5_external_content,
"CREATE VIRTUAL TABLE docs_fts USING fts5(title, content, content = 'documents');"
"CREATE VIRTUAL TABLE docs_fts USING fts5 (title, content, content = 'documents')"
);
to_sql_string_test!(
test_create_virtual_table_rtree,
"CREATE VIRTUAL TABLE geo USING rtree(id, min_x, max_x, min_y, max_y);"
"CREATE VIRTUAL TABLE geo USING rtree (id, min_x, max_x, min_y, max_y)"
);
to_sql_string_test!(
test_create_virtual_table_rtree_aux,
"CREATE VIRTUAL TABLE geo USING rtree(id, min_x, max_x, min_y, max_y, +name TEXT, +category INTEGER);"
"CREATE VIRTUAL TABLE geo USING rtree (id, min_x, max_x, min_y, max_y, +name TEXT, +category INTEGER)"
);
to_sql_string_test!(
test_create_virtual_table_if_not_exists,
"CREATE VIRTUAL TABLE IF NOT EXISTS docs USING fts5(title, content);"
"CREATE VIRTUAL TABLE IF NOT EXISTS docs USING fts5 (title, content)"
);
to_sql_string_test!(
test_create_virtual_table_fts4,
"CREATE VIRTUAL TABLE docs USING fts4(title, content, matchinfo = 'fts3');"
"CREATE VIRTUAL TABLE docs USING fts4 (title, content, matchinfo = 'fts3')"
);
to_sql_string_test!(
test_create_virtual_table_fts5_detail,
"CREATE VIRTUAL TABLE docs USING fts5(title, body TEXT, detail = 'none');"
"CREATE VIRTUAL TABLE docs USING fts5 (title, body TEXT, detail = 'none')"
);
to_sql_string_test!(
test_create_virtual_table_schema,
"CREATE VIRTUAL TABLE main.docs USING fts5(title, content);"
"CREATE VIRTUAL TABLE main.docs USING fts5 (title, content)"
);
}

View File

@@ -51,83 +51,83 @@ mod tests {
use crate::to_sql_string_test;
// Basic DELETE from a single table
to_sql_string_test!(test_delete_all, "DELETE FROM employees;");
to_sql_string_test!(test_delete_all, "DELETE FROM employees");
// DELETE with a simple WHERE clause
to_sql_string_test!(
test_delete_with_where,
"DELETE FROM employees WHERE id = 1;"
"DELETE FROM employees WHERE id = 1"
);
// DELETE with multiple WHERE conditions
to_sql_string_test!(
test_delete_with_multi_where,
"DELETE FROM employees WHERE salary < 50000 AND department_id = 3;"
"DELETE FROM employees WHERE salary < 50000 AND department_id = 3"
);
// DELETE with IN clause
to_sql_string_test!(
test_delete_with_in,
"DELETE FROM employees WHERE id IN (1, 2, 3);"
"DELETE FROM employees WHERE id IN (1, 2, 3)"
);
// DELETE with subquery in WHERE
to_sql_string_test!(
test_delete_with_subquery,
"DELETE FROM employees WHERE department_id IN (SELECT id FROM departments WHERE name = 'Sales');"
"DELETE FROM employees WHERE department_id IN (SELECT id FROM departments WHERE name = 'Sales')"
);
// DELETE with EXISTS clause
to_sql_string_test!(
test_delete_with_exists,
"DELETE FROM employees WHERE EXISTS (SELECT 1 FROM orders WHERE orders.employee_id = employees.id AND orders.status = 'pending');"
"DELETE FROM employees WHERE EXISTS (SELECT 1 FROM orders WHERE orders.employee_id = employees.id AND orders.status = 'pending')"
);
// DELETE with RETURNING clause
to_sql_string_test!(
test_delete_with_returning,
"DELETE FROM employees WHERE salary < 30000 RETURNING id, name;"
"DELETE FROM employees WHERE salary < 30000 RETURNING id, name"
);
// DELETE with LIMIT clause
to_sql_string_test!(
test_delete_with_limit,
"DELETE FROM employees WHERE salary < 40000 LIMIT 5;"
"DELETE FROM employees WHERE salary < 40000 LIMIT 5"
);
// DELETE with ORDER BY and LIMIT
to_sql_string_test!(
test_delete_with_order_by_limit,
"DELETE FROM employees WHERE salary < 40000 ORDER BY id DESC LIMIT 5;"
"DELETE FROM employees WHERE salary < 40000 ORDER BY id DESC LIMIT 5"
);
// DELETE from schema-qualified table
to_sql_string_test!(
test_delete_schema_qualified,
"DELETE FROM main.employees WHERE id = 1;"
"DELETE FROM main.employees WHERE id = 1"
);
// DELETE with BETWEEN clause
to_sql_string_test!(
test_delete_with_between,
"DELETE FROM employees WHERE salary BETWEEN 30000 AND 50000;"
"DELETE FROM employees WHERE salary BETWEEN 30000 AND 50000"
);
// DELETE with NULL check
to_sql_string_test!(
test_delete_with_null,
"DELETE FROM employees WHERE department_id IS NULL;"
"DELETE FROM employees WHERE department_id IS NULL"
);
// DELETE with LIKE clause
to_sql_string_test!(
test_delete_with_like,
"DELETE FROM employees WHERE name LIKE 'J%';"
"DELETE FROM employees WHERE name LIKE 'J%'"
);
// DELETE with complex expression in WHERE
to_sql_string_test!(
test_delete_with_complex_expression,
"DELETE FROM employees WHERE (salary * 1.1) > 60000 AND department_id != 1;"
"DELETE FROM employees WHERE (salary * 1.1) > 60000 AND department_id != 1"
);
}

View File

@@ -59,90 +59,90 @@ mod tests {
// Basic INSERT with all columns
to_sql_string_test!(
test_insert_basic,
"INSERT INTO employees (id, name, salary) VALUES (1, 'John Doe', 50000);"
"INSERT INTO employees (id, name, salary) VALUES (1, 'John Doe', 50000)"
);
// INSERT with multiple rows
to_sql_string_test!(
test_insert_multiple_rows,
"INSERT INTO employees (id, name, salary) VALUES (1, 'John Doe', 50000), (2, 'Jane Smith', 60000);"
"INSERT INTO employees (id, name, salary) VALUES (1, 'John Doe', 50000), (2, 'Jane Smith', 60000)"
);
// INSERT with specific columns
to_sql_string_test!(
test_insert_specific_columns,
"INSERT INTO employees (name, salary) VALUES ('Alice Brown', 55000);"
"INSERT INTO employees (name, salary) VALUES ('Alice Brown', 55000)"
);
// INSERT with DEFAULT VALUES
to_sql_string_test!(
test_insert_default_values,
"INSERT INTO employees DEFAULT VALUES;"
"INSERT INTO employees DEFAULT VALUES"
);
// INSERT with SELECT subquery
to_sql_string_test!(
test_insert_select_subquery,
"INSERT INTO employees (id, name, salary) SELECT id, name, salary FROM temp_employees WHERE salary > 40000;"
"INSERT INTO employees (id, name, salary) SELECT id, name, salary FROM temp_employees WHERE salary > 40000"
);
// INSERT with ON CONFLICT IGNORE
to_sql_string_test!(
test_insert_on_conflict_ignore,
"INSERT INTO employees (id, name, salary) VALUES (1, 'John Doe', 50000) ON CONFLICT(id) DO NOTHING;"
"INSERT INTO employees (id, name, salary) VALUES (1, 'John Doe', 50000) ON CONFLICT (id) DO NOTHING"
);
// INSERT with ON CONFLICT REPLACE
to_sql_string_test!(
test_insert_on_conflict_replace,
"INSERT INTO employees (id, name, salary) VALUES (1, 'John Doe', 50000) ON CONFLICT(id) DO UPDATE SET name = excluded.name, salary = excluded.salary;"
"INSERT INTO employees (id, name, salary) VALUES (1, 'John Doe', 50000) ON CONFLICT (id) DO UPDATE SET name = excluded.name, salary = excluded.salary"
);
// INSERT with RETURNING clause
to_sql_string_test!(
test_insert_with_returning,
"INSERT INTO employees (id, name, salary) VALUES (1, 'John Doe', 50000) RETURNING id, name;"
"INSERT INTO employees (id, name, salary) VALUES (1, 'John Doe', 50000) RETURNING id, name"
);
// INSERT with NULL values
to_sql_string_test!(
test_insert_with_null,
"INSERT INTO employees (id, name, salary, department_id) VALUES (1, 'John Doe', NULL, NULL);"
"INSERT INTO employees (id, name, salary, department_id) VALUES (1, 'John Doe', NULL, NULL)"
);
// INSERT with expression in VALUES
to_sql_string_test!(
test_insert_with_expression,
"INSERT INTO employees (id, name, salary) VALUES (1, 'John Doe', 50000 * 1.1);"
"INSERT INTO employees (id, name, salary) VALUES (1, 'John Doe', 50000 * 1.1)"
);
// INSERT into schema-qualified table
to_sql_string_test!(
test_insert_schema_qualified,
"INSERT INTO main.employees (id, name, salary) VALUES (1, 'John Doe', 50000);"
"INSERT INTO main.employees (id, name, salary) VALUES (1, 'John Doe', 50000)"
);
// INSERT with subquery and JOIN
to_sql_string_test!(
test_insert_subquery_join,
"INSERT INTO employees (id, name, department_id) SELECT e.id, e.name, d.id FROM temp_employees e JOIN departments d ON e.dept_name = d.name;"
"INSERT INTO employees (id, name, department_id) SELECT e.id, e.name, d.id FROM temp_employees e JOIN departments d ON e.dept_name = d.name"
);
// INSERT with all columns from SELECT
to_sql_string_test!(
test_insert_all_columns_select,
"INSERT INTO employees SELECT * FROM temp_employees;"
"INSERT INTO employees SELECT * FROM temp_employees"
);
// INSERT with ON CONFLICT and WHERE clause
to_sql_string_test!(
test_insert_on_conflict_where,
"INSERT INTO employees (id, name, salary) VALUES (1, 'John Doe', 50000) ON CONFLICT(id) DO UPDATE SET salary = excluded.salary WHERE excluded.salary > employees.salary;"
"INSERT INTO employees (id, name, salary) VALUES (1, 'John Doe', 50000) ON CONFLICT (id) DO UPDATE SET salary = excluded.salary WHERE excluded.salary > employees.salary"
);
// INSERT with quoted column names (reserved words)
to_sql_string_test!(
test_insert_quoted_columns,
"INSERT INTO employees (\"select\", \"from\") VALUES (1, 'data');"
"INSERT INTO employees (\"select\", \"from\") VALUES (1, 'data')"
);
}

View File

@@ -211,6 +211,7 @@ mod tests {
($test_name:ident, $input:expr) => {
#[test]
fn $test_name() {
use crate::parser::ast::fmt::ToTokens;
let context = $crate::to_sql_string::stmt::tests::TestContext;
let input = $input.split_whitespace().collect::<Vec<&str>>().join(" ");
let mut parser = $crate::lexer::sql::Parser::new(input.as_bytes());
@@ -219,7 +220,7 @@ mod tests {
.unwrap();
assert_eq!(
input,
$crate::to_sql_string::ToSqlString::to_sql_string(cmd.stmt(), &context)
cmd.stmt().format_with_context(&context).unwrap().replace('\n', " "),
);
}
};
@@ -227,6 +228,7 @@ mod tests {
#[test]
$(#[$attribute])*
fn $test_name() {
use crate::parser::ast::fmt::ToTokens;
let context = $crate::to_sql_string::stmt::tests::TestContext;
let input = $input.split_whitespace().collect::<Vec<&str>>().join(" ");
let mut parser = $crate::lexer::sql::Parser::new(input.as_bytes());
@@ -235,7 +237,7 @@ mod tests {
.unwrap();
assert_eq!(
input,
$crate::to_sql_string::ToSqlString::to_sql_string(cmd.stmt(), &context)
cmd.stmt().format_with_context(&context).unwrap().replace('\n', " "),
);
}
}
@@ -255,163 +257,163 @@ mod tests {
}
}
to_sql_string_test!(test_analyze, "ANALYZE;");
to_sql_string_test!(test_analyze, "ANALYZE");
to_sql_string_test!(
test_analyze_table,
"ANALYZE table;",
"ANALYZE table",
ignore = "parser can't parse table name"
);
to_sql_string_test!(
test_analyze_schema_table,
"ANALYZE schema.table;",
"ANALYZE schema.table",
ignore = "parser can't parse schema.table name"
);
to_sql_string_test!(test_attach, "ATTACH './test.db' AS test_db;");
to_sql_string_test!(test_attach, "ATTACH './test.db' AS test_db");
to_sql_string_test!(test_transaction, "BEGIN;");
to_sql_string_test!(test_transaction, "BEGIN");
to_sql_string_test!(test_transaction_deferred, "BEGIN DEFERRED;");
to_sql_string_test!(test_transaction_deferred, "BEGIN DEFERRED");
to_sql_string_test!(test_transaction_immediate, "BEGIN IMMEDIATE;");
to_sql_string_test!(test_transaction_immediate, "BEGIN IMMEDIATE");
to_sql_string_test!(test_transaction_exclusive, "BEGIN EXCLUSIVE;");
to_sql_string_test!(test_transaction_exclusive, "BEGIN EXCLUSIVE");
to_sql_string_test!(test_commit, "COMMIT;");
to_sql_string_test!(test_commit, "COMMIT");
// Test a simple index on a single column
to_sql_string_test!(
test_create_index_simple,
"CREATE INDEX idx_name ON employees (last_name);"
"CREATE INDEX idx_name ON employees (last_name)"
);
// Test a unique index to enforce uniqueness on a column
to_sql_string_test!(
test_create_unique_index,
"CREATE UNIQUE INDEX idx_unique_email ON users (email);"
"CREATE UNIQUE INDEX idx_unique_email ON users (email)"
);
// Test a multi-column index
to_sql_string_test!(
test_create_index_multi_column,
"CREATE INDEX idx_name_salary ON employees (last_name, salary);"
"CREATE INDEX idx_name_salary ON employees (last_name, salary)"
);
// Test a partial index with a WHERE clause
to_sql_string_test!(
test_create_partial_index,
"CREATE INDEX idx_active_users ON users (username) WHERE active = true;"
"CREATE INDEX idx_active_users ON users (username) WHERE active = true"
);
// Test an index on an expression
to_sql_string_test!(
test_create_index_on_expression,
"CREATE INDEX idx_upper_name ON employees (UPPER(last_name));"
"CREATE INDEX idx_upper_name ON employees (UPPER (last_name))"
);
// Test an index with descending order
to_sql_string_test!(
test_create_index_descending,
"CREATE INDEX idx_salary_desc ON employees (salary DESC);"
"CREATE INDEX idx_salary_desc ON employees (salary DESC)"
);
// Test an index with mixed ascending and descending orders on multiple columns
to_sql_string_test!(
test_create_index_mixed_order,
"CREATE INDEX idx_name_asc_salary_desc ON employees (last_name ASC, salary DESC);"
"CREATE INDEX idx_name_asc_salary_desc ON employees (last_name ASC, salary DESC)"
);
// Test 1: View with DISTINCT keyword
to_sql_string_test!(
test_create_view_distinct,
"CREATE VIEW view_distinct AS SELECT DISTINCT name FROM employees;"
"CREATE VIEW view_distinct AS SELECT DISTINCT name FROM employees"
);
// Test 2: View with LIMIT clause
to_sql_string_test!(
test_create_view_limit,
"CREATE VIEW view_limit AS SELECT id, name FROM employees LIMIT 10;"
"CREATE VIEW view_limit AS SELECT id, name FROM employees LIMIT 10"
);
// Test 3: View with CASE expression
to_sql_string_test!(
test_create_view_case,
"CREATE VIEW view_case AS SELECT name, CASE WHEN salary > 70000 THEN 'High' ELSE 'Low' END AS salary_level FROM employees;"
"CREATE VIEW view_case AS SELECT name, CASE WHEN salary > 70000 THEN 'High' ELSE 'Low' END AS salary_level FROM employees"
);
// Test 4: View with LEFT JOIN
to_sql_string_test!(
test_create_view_left_join,
"CREATE VIEW view_left_join AS SELECT e.name, d.name AS department FROM employees e LEFT JOIN departments d ON e.department_id = d.id;"
"CREATE VIEW view_left_join AS SELECT e.name, d.name AS department FROM employees e LEFT OUTER JOIN departments d ON e.department_id = d.id"
);
// Test 5: View with HAVING clause
to_sql_string_test!(
test_create_view_having,
"CREATE VIEW view_having AS SELECT department_id, AVG(salary) AS avg_salary FROM employees GROUP BY department_id HAVING AVG(salary) > 55000;"
"CREATE VIEW view_having AS SELECT department_id, AVG (salary) AS avg_salary FROM employees GROUP BY department_id HAVING AVG (salary) > 55000"
);
// Test 6: View with CTE (Common Table Expression)
to_sql_string_test!(
test_create_view_cte,
"CREATE VIEW view_cte AS WITH high_earners AS (SELECT * FROM employees WHERE salary > 80000) SELECT id, name FROM high_earners;"
"CREATE VIEW view_cte AS WITH high_earners AS (SELECT * FROM employees WHERE salary > 80000) SELECT id, name FROM high_earners"
);
// Test 7: View with multiple conditions in WHERE
to_sql_string_test!(
test_create_view_multi_where,
"CREATE VIEW view_multi_where AS SELECT id, name FROM employees WHERE salary > 50000 AND department_id = 3;"
"CREATE VIEW view_multi_where AS SELECT id, name FROM employees WHERE salary > 50000 AND department_id = 3"
);
// Test 8: View with NULL handling
to_sql_string_test!(
test_create_view_null,
"CREATE VIEW view_null AS SELECT name, COALESCE(salary, 0) AS salary FROM employees;"
"CREATE VIEW view_null AS SELECT name, COALESCE (salary, 0) AS salary FROM employees"
);
// Test 9: View with subquery in WHERE clause
to_sql_string_test!(
test_create_view_subquery_where,
"CREATE VIEW view_subquery_where AS SELECT name FROM employees WHERE department_id IN (SELECT id FROM departments WHERE name = 'Sales');"
"CREATE VIEW view_subquery_where AS SELECT name FROM employees WHERE department_id IN (SELECT id FROM departments WHERE name = 'Sales')"
);
// Test 10: View with arithmetic expression
to_sql_string_test!(
test_create_view_arithmetic,
"CREATE VIEW view_arithmetic AS SELECT name, salary * 1.1 AS adjusted_salary FROM employees;"
"CREATE VIEW view_arithmetic AS SELECT name, salary * 1.1 AS adjusted_salary FROM employees"
);
to_sql_string_test!(test_detach, "DETACH 'x.db';");
to_sql_string_test!(test_detach, "DETACH 'x.db'");
to_sql_string_test!(test_drop_index, "DROP INDEX schema_name.test_index;");
to_sql_string_test!(test_drop_index, "DROP INDEX schema_name.test_index");
to_sql_string_test!(test_drop_table, "DROP TABLE schema_name.test_table;");
to_sql_string_test!(test_drop_table, "DROP TABLE schema_name.test_table");
to_sql_string_test!(test_drop_trigger, "DROP TRIGGER schema_name.test_trigger;");
to_sql_string_test!(test_drop_trigger, "DROP TRIGGER schema_name.test_trigger");
to_sql_string_test!(test_drop_view, "DROP VIEW schema_name.test_view;");
to_sql_string_test!(test_drop_view, "DROP VIEW schema_name.test_view");
to_sql_string_test!(test_pragma_equals, "PRAGMA schema_name.Pragma_name = 1;");
to_sql_string_test!(test_pragma_equals, "PRAGMA schema_name.Pragma_name = 1");
to_sql_string_test!(test_pragma_call, "PRAGMA schema_name.Pragma_name_2(1);");
to_sql_string_test!(test_pragma_call, "PRAGMA schema_name.Pragma_name_2 (1)");
to_sql_string_test!(test_reindex, "REINDEX schema_name.test_table;");
to_sql_string_test!(test_reindex, "REINDEX schema_name.test_table");
to_sql_string_test!(test_reindex_2, "REINDEX;");
to_sql_string_test!(test_reindex_2, "REINDEX");
to_sql_string_test!(test_release, "RELEASE savepoint_name;");
to_sql_string_test!(test_release, "RELEASE savepoint_name");
to_sql_string_test!(test_rollback, "ROLLBACK;");
to_sql_string_test!(test_rollback, "ROLLBACK");
to_sql_string_test!(test_rollback_2, "ROLLBACK TO savepoint_name;");
to_sql_string_test!(test_rollback_2, "ROLLBACK TO savepoint_name");
to_sql_string_test!(test_savepoint, "SAVEPOINT savepoint_name;");
to_sql_string_test!(test_savepoint, "SAVEPOINT savepoint_name");
to_sql_string_test!(test_vacuum, "VACUUM;");
to_sql_string_test!(test_vacuum, "VACUUM");
to_sql_string_test!(test_vacuum_2, "VACUUM schema_name;");
to_sql_string_test!(test_vacuum_2, "VACUUM schema_name");
to_sql_string_test!(test_vacuum_3, "VACUUM schema_name INTO test.db;");
to_sql_string_test!(test_vacuum_3, "VACUUM schema_name INTO test.db");
}

View File

@@ -532,127 +532,127 @@ impl Display for ast::FrameExclude {
mod tests {
use crate::to_sql_string_test;
to_sql_string_test!(test_select_basic, "SELECT 1;");
to_sql_string_test!(test_select_basic, "SELECT 1");
to_sql_string_test!(test_select_table, "SELECT * FROM t;");
to_sql_string_test!(test_select_table, "SELECT * FROM t");
to_sql_string_test!(test_select_table_2, "SELECT a FROM t;");
to_sql_string_test!(test_select_table_2, "SELECT a FROM t");
to_sql_string_test!(test_select_multiple_columns, "SELECT a, b, c FROM t;");
to_sql_string_test!(test_select_multiple_columns, "SELECT a, b, c FROM t");
to_sql_string_test!(test_select_with_alias, "SELECT a AS col1 FROM t;");
to_sql_string_test!(test_select_with_alias, "SELECT a AS col1 FROM t");
to_sql_string_test!(test_select_with_table_alias, "SELECT t1.a FROM t AS t1;");
to_sql_string_test!(test_select_with_table_alias, "SELECT t1.a FROM t AS t1");
to_sql_string_test!(test_select_with_where, "SELECT a FROM t WHERE b = 1;");
to_sql_string_test!(test_select_with_where, "SELECT a FROM t WHERE b = 1");
to_sql_string_test!(
test_select_with_multiple_conditions,
"SELECT a FROM t WHERE b = 1 AND c > 2;"
"SELECT a FROM t WHERE b = 1 AND c > 2"
);
to_sql_string_test!(
test_select_with_order_by,
"SELECT a FROM t ORDER BY a DESC;"
"SELECT a FROM t ORDER BY a DESC"
);
to_sql_string_test!(test_select_with_limit, "SELECT a FROM t LIMIT 10;");
to_sql_string_test!(test_select_with_limit, "SELECT a FROM t LIMIT 10");
to_sql_string_test!(
test_select_with_offset,
"SELECT a FROM t LIMIT 10 OFFSET 5;"
"SELECT a FROM t LIMIT 10 OFFSET 5"
);
to_sql_string_test!(
test_select_with_join,
"SELECT a FROM t JOIN t2 ON t.b = t2.b;"
"SELECT a FROM t JOIN t2 ON t.b = t2.b"
);
to_sql_string_test!(
test_select_with_group_by,
"SELECT a, COUNT(*) FROM t GROUP BY a;"
"SELECT a, COUNT (*) FROM t GROUP BY a"
);
to_sql_string_test!(
test_select_with_having,
"SELECT a, COUNT(*) FROM t GROUP BY a HAVING COUNT(*) > 1;"
"SELECT a, COUNT (*) FROM t GROUP BY a HAVING COUNT (*) > 1"
);
to_sql_string_test!(test_select_with_distinct, "SELECT DISTINCT a FROM t;");
to_sql_string_test!(test_select_with_distinct, "SELECT DISTINCT a FROM t");
to_sql_string_test!(test_select_with_function, "SELECT COUNT(a) FROM t;");
to_sql_string_test!(test_select_with_function, "SELECT COUNT (a) FROM t");
to_sql_string_test!(
test_select_with_subquery,
"SELECT a FROM (SELECT b FROM t) AS sub;"
"SELECT a FROM (SELECT b FROM t) AS sub"
);
to_sql_string_test!(
test_select_nested_subquery,
"SELECT a FROM (SELECT b FROM (SELECT c FROM t WHERE c > 10) AS sub1 WHERE b < 20) AS sub2;"
"SELECT a FROM (SELECT b FROM (SELECT c FROM t WHERE c > 10) AS sub1 WHERE b < 20) AS sub2"
);
to_sql_string_test!(
test_select_multiple_joins,
"SELECT t1.a, t2.b, t3.c FROM t1 JOIN t2 ON t1.id = t2.id LEFT JOIN t3 ON t2.id = t3.id;"
"SELECT t1.a, t2.b, t3.c FROM t1 JOIN t2 ON t1.id = t2.id LEFT OUTER JOIN t3 ON t2.id = t3.id"
);
to_sql_string_test!(
test_select_with_cte,
"WITH cte AS (SELECT a FROM t WHERE b = 1) SELECT a FROM cte WHERE a > 10;"
"WITH cte AS (SELECT a FROM t WHERE b = 1) SELECT a FROM cte WHERE a > 10"
);
to_sql_string_test!(
test_select_with_window_function,
"SELECT a, ROW_NUMBER() OVER (PARTITION BY b ORDER BY c DESC) AS rn FROM t;"
"SELECT a, ROW_NUMBER () OVER (PARTITION BY b ORDER BY c DESC) AS rn FROM t"
);
to_sql_string_test!(
test_select_with_complex_where,
"SELECT a FROM t WHERE b IN (1, 2, 3) AND c BETWEEN 10 AND 20 OR d IS NULL;"
"SELECT a FROM t WHERE b IN (1, 2, 3) AND c BETWEEN 10 AND 20 OR d IS NULL"
);
to_sql_string_test!(
test_select_with_case,
"SELECT CASE WHEN a > 0 THEN 'positive' ELSE 'non-positive' END AS result FROM t;"
"SELECT CASE WHEN a > 0 THEN 'positive' ELSE 'non-positive' END AS result FROM t"
);
to_sql_string_test!(test_select_with_aggregate_and_join, "SELECT t1.a, COUNT(t2.b) FROM t1 LEFT JOIN t2 ON t1.id = t2.id GROUP BY t1.a HAVING COUNT(t2.b) > 5;");
to_sql_string_test!(test_select_with_aggregate_and_join, "SELECT t1.a, COUNT (t2.b) FROM t1 LEFT OUTER JOIN t2 ON t1.id = t2.id GROUP BY t1.a HAVING COUNT (t2.b) > 5");
to_sql_string_test!(test_select_with_multiple_ctes, "WITH cte1 AS (SELECT a FROM t WHERE b = 1), cte2 AS (SELECT c FROM t2 WHERE d = 2) SELECT cte1.a, cte2.c FROM cte1 JOIN cte2 ON cte1.a = cte2.c;");
to_sql_string_test!(test_select_with_multiple_ctes, "WITH cte1 AS (SELECT a FROM t WHERE b = 1), cte2 AS (SELECT c FROM t2 WHERE d = 2) SELECT cte1.a, cte2.c FROM cte1 JOIN cte2 ON cte1.a = cte2.c");
to_sql_string_test!(
test_select_with_union,
"SELECT a FROM t1 UNION SELECT b FROM t2;"
"SELECT a FROM t1 UNION SELECT b FROM t2"
);
to_sql_string_test!(
test_select_with_union_all,
"SELECT a FROM t1 UNION ALL SELECT b FROM t2;"
"SELECT a FROM t1 UNION ALL SELECT b FROM t2"
);
to_sql_string_test!(
test_select_with_exists,
"SELECT a FROM t WHERE EXISTS (SELECT 1 FROM t2 WHERE t2.b = t.a);"
"SELECT a FROM t WHERE EXISTS (SELECT 1 FROM t2 WHERE t2.b = t.a)"
);
to_sql_string_test!(
test_select_with_correlated_subquery,
"SELECT a, (SELECT COUNT(*) FROM t2 WHERE t2.b = t.a) AS count_b FROM t;"
"SELECT a, (SELECT COUNT (*) FROM t2 WHERE t2.b = t.a) AS count_b FROM t"
);
to_sql_string_test!(
test_select_with_complex_order_by,
"SELECT a, b FROM t ORDER BY CASE WHEN a IS NULL THEN 1 ELSE 0 END, b ASC, c DESC;"
"SELECT a, b FROM t ORDER BY CASE WHEN a IS NULL THEN 1 ELSE 0 END, b ASC, c DESC"
);
to_sql_string_test!(
test_select_with_full_outer_join,
"SELECT t1.a, t2.b FROM t1 FULL OUTER JOIN t2 ON t1.id = t2.id;",
"SELECT t1.a, t2.b FROM t1 FULL OUTER JOIN t2 ON t1.id = t2.id",
ignore = "OUTER JOIN is incorrectly parsed in parser"
);
to_sql_string_test!(test_select_with_aggregate_window, "SELECT a, SUM(b) OVER (PARTITION BY c ORDER BY d ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS running_sum FROM t;");
to_sql_string_test!(test_select_with_aggregate_window, "SELECT a, SUM (b) OVER (PARTITION BY c ORDER BY d ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS running_sum FROM t");
to_sql_string_test!(
test_select_with_exclude,
@@ -660,7 +660,7 @@ mod tests {
c.name,
o.order_id,
o.order_amount,
SUM(o.order_amount) OVER (PARTITION BY c.id
SUM (o.order_amount) OVER (PARTITION BY c.id
ORDER BY o.order_date
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
EXCLUDE CURRENT ROW) AS running_total_excluding_current
@@ -669,6 +669,6 @@ JOIN orders o ON c.id = o.customer_id
WHERE EXISTS (SELECT 1
FROM orders o2
WHERE o2.customer_id = c.id
AND o2.order_amount > 1000);"
AND o2.order_amount > 1000)"
);
}

View File

@@ -50,90 +50,90 @@ mod tests {
// Basic UPDATE with a single column
to_sql_string_test!(
test_update_single_column,
"UPDATE employees SET salary = 55000;"
"UPDATE employees SET salary = 55000"
);
// UPDATE with multiple columns
to_sql_string_test!(
test_update_multiple_columns,
"UPDATE employees SET salary = 60000, name = 'John Smith';"
"UPDATE employees SET salary = 60000, name = 'John Smith'"
);
// UPDATE with a WHERE clause
to_sql_string_test!(
test_update_with_where,
"UPDATE employees SET salary = 60000 WHERE id = 1;"
"UPDATE employees SET salary = 60000 WHERE id = 1"
);
// UPDATE with multiple WHERE conditions
to_sql_string_test!(
test_update_with_multi_where,
"UPDATE employees SET salary = 65000 WHERE department_id = 3 AND salary < 50000;"
"UPDATE employees SET salary = 65000 WHERE department_id = 3 AND salary < 50000"
);
// UPDATE with a subquery in SET
to_sql_string_test!(
test_update_with_subquery_set,
"UPDATE employees SET department_id = (SELECT id FROM departments WHERE name = 'Sales') WHERE id = 1;"
"UPDATE employees SET department_id = (SELECT id FROM departments WHERE name = 'Sales') WHERE id = 1"
);
// UPDATE with a subquery in WHERE
to_sql_string_test!(
test_update_with_subquery_where,
"UPDATE employees SET salary = 70000 WHERE department_id IN (SELECT id FROM departments WHERE name = 'Marketing');"
"UPDATE employees SET salary = 70000 WHERE department_id IN (SELECT id FROM departments WHERE name = 'Marketing')"
);
// UPDATE with EXISTS clause
to_sql_string_test!(
test_update_with_exists,
"UPDATE employees SET salary = 75000 WHERE EXISTS (SELECT 1 FROM orders WHERE orders.employee_id = employees.id AND orders.status = 'pending');"
"UPDATE employees SET salary = 75000 WHERE EXISTS (SELECT 1 FROM orders WHERE orders.employee_id = employees.id AND orders.status = 'pending')"
);
// UPDATE with FROM clause (join-like behavior)
to_sql_string_test!(
test_update_with_from,
"UPDATE employees SET salary = 80000 FROM departments WHERE employees.department_id = departments.id AND departments.name = 'Engineering';"
"UPDATE employees SET salary = 80000 FROM departments WHERE employees.department_id = departments.id AND departments.name = 'Engineering'"
);
// UPDATE with RETURNING clause
to_sql_string_test!(
test_update_with_returning,
"UPDATE employees SET salary = 60000 WHERE id = 1 RETURNING id, name, salary;"
"UPDATE employees SET salary = 60000 WHERE id = 1 RETURNING id, name, salary"
);
// UPDATE with expression in SET
to_sql_string_test!(
test_update_with_expression,
"UPDATE employees SET salary = salary * 1.1 WHERE department_id = 2;"
"UPDATE employees SET salary = salary * 1.1 WHERE department_id = 2"
);
// UPDATE with NULL value
to_sql_string_test!(
test_update_with_null,
"UPDATE employees SET department_id = NULL WHERE id = 1;"
"UPDATE employees SET department_id = NULL WHERE id = 1"
);
// UPDATE with schema-qualified table
to_sql_string_test!(
test_update_schema_qualified,
"UPDATE main.employees SET salary = 65000 WHERE id = 1;"
"UPDATE main.employees SET salary = 65000 WHERE id = 1"
);
// UPDATE with CASE expression
to_sql_string_test!(
test_update_with_case,
"UPDATE employees SET salary = CASE WHEN salary < 50000 THEN 55000 ELSE salary * 1.05 END WHERE department_id = 3;"
"UPDATE employees SET salary = CASE WHEN salary < 50000 THEN 55000 ELSE salary * 1.05 END WHERE department_id = 3"
);
// UPDATE with LIKE clause in WHERE
to_sql_string_test!(
test_update_with_like,
"UPDATE employees SET name = 'Updated' WHERE name LIKE 'J%';"
"UPDATE employees SET name = 'Updated' WHERE name LIKE 'J%'"
);
// UPDATE with ON CONFLICT (upsert-like behavior)
to_sql_string_test!(
test_update_with_on_conflict,
"INSERT INTO employees (id, name, salary) VALUES (1, 'John Doe', 50000) ON CONFLICT(id) DO UPDATE SET name = excluded.name, salary = excluded.salary;"
"INSERT INTO employees (id, name, salary) VALUES (1, 'John Doe', 50000) ON CONFLICT (id) DO UPDATE SET name = excluded.name, salary = excluded.salary"
);
}