Commit 65b67b7
Changed files (47)
tests
fixtures
basic_select_two_columns
between_expression
cast_expression
distinct
empty_input
function_calls
idempotent_formatting
in_subquery
invalid_sql_passthrough
is_null
multiple_statements
nested_expressions
quoted_identifiers
semicolon_addition
simple_select_single_line
whitespace_only
with_case_expression
with_cte
with_group_by_having
with_joins
with_order_by_limit
with_subquery
with_union
tests/fixtures/basic_select_two_columns/input.sql
@@ -0,0 +1,1 @@
+SELECT id, name FROM users WHERE active = true
\ No newline at end of file
tests/fixtures/basic_select_two_columns/output.sql
@@ -0,0 +1,6 @@
+SELECT
+ id,
+ name
+FROM
+ users
+WHERE active = TRUE;
\ No newline at end of file
tests/fixtures/between_expression/input.sql
@@ -0,0 +1,1 @@
+SELECT * FROM products WHERE price BETWEEN 10 AND 100
\ No newline at end of file
tests/fixtures/between_expression/output.sql
@@ -0,0 +1,4 @@
+SELECT *
+FROM
+ products
+WHERE price BETWEEN 10 AND 100;
\ No newline at end of file
tests/fixtures/cast_expression/input.sql
@@ -0,0 +1,1 @@
+SELECT CAST(id AS VARCHAR) FROM users
\ No newline at end of file
tests/fixtures/cast_expression/output.sql
@@ -0,0 +1,4 @@
+SELECT
+ CAST(id AS VARCHAR)
+FROM
+ users;
\ No newline at end of file
tests/fixtures/distinct/input.sql
@@ -0,0 +1,1 @@
+SELECT DISTINCT department FROM employees
\ No newline at end of file
tests/fixtures/distinct/output.sql
@@ -0,0 +1,3 @@
+SELECT DISTINCT department
+FROM
+ employees;
\ No newline at end of file
tests/fixtures/empty_input/input.sql
tests/fixtures/empty_input/output.sql
tests/fixtures/function_calls/input.sql
@@ -0,0 +1,1 @@
+SELECT COUNT(*), MAX(age), UPPER(name) FROM users
\ No newline at end of file
tests/fixtures/function_calls/output.sql
@@ -0,0 +1,6 @@
+SELECT
+ COUNT(*),
+ MAX(age),
+ UPPER(name)
+FROM
+ users;
\ No newline at end of file
tests/fixtures/idempotent_formatting/input.sql
@@ -0,0 +1,6 @@
+SELECT
+ id,
+ name
+FROM
+ users
+WHERE active = TRUE;
\ No newline at end of file
tests/fixtures/idempotent_formatting/output.sql
@@ -0,0 +1,6 @@
+SELECT
+ id,
+ name
+FROM
+ users
+WHERE active = TRUE;
\ No newline at end of file
tests/fixtures/in_subquery/input.sql
@@ -0,0 +1,1 @@
+SELECT * FROM users WHERE id IN (SELECT user_id FROM orders)
\ No newline at end of file
tests/fixtures/in_subquery/output.sql
@@ -0,0 +1,8 @@
+SELECT *
+FROM
+ users
+WHERE id IN (
+ SELECT user_id
+ FROM
+ orders
+);
\ No newline at end of file
tests/fixtures/invalid_sql_passthrough/input.sql
@@ -0,0 +1,1 @@
+INVALID SQL SYNTAX
\ No newline at end of file
tests/fixtures/invalid_sql_passthrough/output.sql
@@ -0,0 +1,1 @@
+INVALID SQL SYNTAX
\ No newline at end of file
tests/fixtures/is_null/input.sql
@@ -0,0 +1,1 @@
+SELECT * FROM users WHERE email IS NOT NULL
\ No newline at end of file
tests/fixtures/is_null/output.sql
@@ -0,0 +1,4 @@
+SELECT *
+FROM
+ users
+WHERE email IS NOT NULL;
\ No newline at end of file
tests/fixtures/multiple_statements/input.sql
@@ -0,0 +1,1 @@
+SELECT * FROM users; SELECT * FROM posts
\ No newline at end of file
tests/fixtures/multiple_statements/output.sql
@@ -0,0 +1,6 @@
+SELECT *
+FROM
+ users
+SELECT *
+FROM
+ posts;
\ No newline at end of file
tests/fixtures/nested_expressions/input.sql
@@ -0,0 +1,1 @@
+SELECT * FROM users WHERE (active = true AND age > 18) OR role = 'admin'
\ No newline at end of file
tests/fixtures/nested_expressions/output.sql
@@ -0,0 +1,4 @@
+SELECT *
+FROM
+ users
+WHERE (active = TRUE AND age > 18) OR role = 'admin';
\ No newline at end of file
tests/fixtures/quoted_identifiers/input.sql
@@ -0,0 +1,1 @@
+SELECT "id", "user_name" FROM "admin_users" WHERE "active" = true
\ No newline at end of file
tests/fixtures/quoted_identifiers/output.sql
@@ -0,0 +1,6 @@
+SELECT
+ id,
+ user_name
+FROM
+ "admin_users"
+WHERE active = TRUE;
\ No newline at end of file
tests/fixtures/semicolon_addition/input.sql
@@ -0,0 +1,1 @@
+SELECT * FROM users
\ No newline at end of file
tests/fixtures/semicolon_addition/output.sql
@@ -0,0 +1,3 @@
+SELECT *
+FROM
+ users;
\ No newline at end of file
tests/fixtures/simple_select_single_line/input.sql
@@ -0,0 +1,1 @@
+SELECT 1 AS one FROM users
\ No newline at end of file
tests/fixtures/simple_select_single_line/output.sql
@@ -0,0 +1,3 @@
+SELECT 1 AS one
+FROM
+ users;
\ No newline at end of file
tests/fixtures/whitespace_only/input.sql
@@ -0,0 +1,2 @@
+
+
tests/fixtures/whitespace_only/output.sql
tests/fixtures/with_case_expression/input.sql
@@ -0,0 +1,1 @@
+SELECT CASE WHEN age < 18 THEN 'minor' ELSE 'adult' END AS age_group FROM users
\ No newline at end of file
tests/fixtures/with_case_expression/output.sql
@@ -0,0 +1,7 @@
+SELECT
+ CASE
+ WHEN age < 18 THEN 'minor'
+ ELSE 'adult'
+END AS age_group
+FROM
+ users;
\ No newline at end of file
tests/fixtures/with_cte/input.sql
@@ -0,0 +1,1 @@
+WITH active_users AS (SELECT * FROM users WHERE active = true) SELECT * FROM active_users
\ No newline at end of file
tests/fixtures/with_cte/output.sql
@@ -0,0 +1,10 @@
+WITH
+ active_users AS (
+ SELECT *
+ FROM
+ users
+ WHERE active = TRUE
+ )
+SELECT *
+FROM
+ active_users;
\ No newline at end of file
tests/fixtures/with_group_by_having/input.sql
@@ -0,0 +1,1 @@
+SELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*) > 5
\ No newline at end of file
tests/fixtures/with_group_by_having/output.sql
@@ -0,0 +1,7 @@
+SELECT
+ department,
+ COUNT(*)
+FROM
+ employees
+GROUP BY department
+HAVING COUNT(*) > 5;
\ No newline at end of file
tests/fixtures/with_joins/input.sql
@@ -0,0 +1,1 @@
+SELECT u.name, p.title FROM users u INNER JOIN posts p ON u.id = p.user_id
\ No newline at end of file
tests/fixtures/with_joins/output.sql
@@ -0,0 +1,6 @@
+SELECT
+ u.name,
+ p.title
+FROM
+ users AS u
+ INNER JOIN posts AS p ON u.id = p.user_id;
\ No newline at end of file
tests/fixtures/with_order_by_limit/input.sql
@@ -0,0 +1,1 @@
+SELECT * FROM users ORDER BY name ASC LIMIT 10
\ No newline at end of file
tests/fixtures/with_order_by_limit/output.sql
@@ -0,0 +1,5 @@
+SELECT *
+FROM
+ users
+ORDER BY name ASC
+LIMIT 10;
\ No newline at end of file
tests/fixtures/with_subquery/input.sql
@@ -0,0 +1,1 @@
+SELECT * FROM (SELECT id, name FROM users WHERE active = true) AS active_users
\ No newline at end of file
tests/fixtures/with_subquery/output.sql
@@ -0,0 +1,10 @@
+SELECT *
+FROM
+ (
+ SELECT
+ id,
+ name
+ FROM
+ users
+ WHERE active = TRUE
+) AS active_users;
\ No newline at end of file
tests/fixtures/with_union/input.sql
@@ -0,0 +1,1 @@
+SELECT id FROM users UNION SELECT id FROM customers
\ No newline at end of file
tests/fixtures/with_union/output.sql
@@ -0,0 +1,7 @@
+SELECT id
+FROM
+ users
+UNION
+SELECT id
+FROM
+ customers;
\ No newline at end of file
tests/integration.rs
@@ -2,232 +2,6 @@ use assert_cmd::Command;
use std::fs;
use std::path::{Path, PathBuf};
-#[test]
-fn test_cli_basic_select() {
- let mut cmd = Command::cargo_bin("xlg-sqlfmt").unwrap();
- cmd.write_stdin("SELECT id, name FROM users WHERE active = true")
- .assert()
- .success()
- .stdout("SELECT\n id,\n name\nFROM\n users\nWHERE active = TRUE;\n");
-}
-
-#[test]
-fn test_cli_simple_select_single_line() {
- let mut cmd = Command::cargo_bin("xlg-sqlfmt").unwrap();
- cmd.write_stdin("SELECT 1 AS one FROM users")
- .assert()
- .success()
- .stdout("SELECT 1 AS one\nFROM\n users;\n");
-}
-
-#[test]
-fn test_cli_multiple_and_conditions() {
- let mut cmd = Command::cargo_bin("xlg-sqlfmt").unwrap();
- cmd.write_stdin("SELECT * FROM users WHERE active = true AND age > 18 AND name LIKE 'A%'")
- .assert()
- .success()
- .stdout(
- "SELECT *\nFROM\n users\nWHERE active = TRUE\n AND age > 18\n AND name LIKE 'A%';\n",
- );
-}
-
-#[test]
-fn test_cli_with_joins() {
- let mut cmd = Command::cargo_bin("xlg-sqlfmt").unwrap();
- cmd.write_stdin("SELECT u.name, p.title FROM users u INNER JOIN posts p ON u.id = p.user_id")
- .assert()
- .success()
- .stdout("SELECT\n u.name,\n p.title\nFROM\n users AS u\n INNER JOIN posts AS p ON u.id = p.user_id;\n");
-}
-
-#[test]
-fn test_cli_with_subquery() {
- let mut cmd = Command::cargo_bin("xlg-sqlfmt").unwrap();
- cmd.write_stdin("SELECT * FROM (SELECT id, name FROM users WHERE active = true) AS active_users")
- .assert()
- .success()
- .stdout("SELECT *\nFROM\n (\n SELECT\n id,\n name\n FROM\n users\n WHERE active = TRUE\n) AS active_users;\n");
-}
-
-#[test]
-fn test_cli_with_cte() {
- let mut cmd = Command::cargo_bin("xlg-sqlfmt").unwrap();
- cmd.write_stdin("WITH active_users AS (SELECT * FROM users WHERE active = true) SELECT * FROM active_users")
- .assert()
- .success()
- .stdout("WITH\n active_users AS (\n SELECT *\n FROM\n users\n WHERE active = TRUE\n )\nSELECT *\nFROM\n active_users;\n");
-}
-
-#[test]
-fn test_cli_with_case_expression() {
- let mut cmd = Command::cargo_bin("xlg-sqlfmt").unwrap();
- cmd.write_stdin("SELECT CASE WHEN age < 18 THEN 'minor' ELSE 'adult' END AS age_group FROM users")
- .assert()
- .success()
- .stdout("SELECT\n CASE\n WHEN age < 18 THEN 'minor'\n ELSE 'adult'\nEND AS age_group\nFROM\n users;\n");
-}
-
-#[test]
-fn test_cli_with_union() {
- let mut cmd = Command::cargo_bin("xlg-sqlfmt").unwrap();
- cmd.write_stdin("SELECT id FROM users UNION SELECT id FROM customers")
- .assert()
- .success()
- .stdout("SELECT id\nFROM\n users\nUNION\nSELECT id\nFROM\n customers;\n");
-}
-
-#[test]
-fn test_cli_with_group_by_having() {
- let mut cmd = Command::cargo_bin("xlg-sqlfmt").unwrap();
- cmd.write_stdin("SELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*) > 5")
- .assert()
- .success()
- .stdout("SELECT\n department,\n COUNT(*)\nFROM\n employees\nGROUP BY department\nHAVING COUNT(*) > 5;\n");
-}
-
-#[test]
-fn test_cli_with_order_by_limit() {
- let mut cmd = Command::cargo_bin("xlg-sqlfmt").unwrap();
- cmd.write_stdin("SELECT * FROM users ORDER BY name ASC LIMIT 10")
- .assert()
- .success()
- .stdout("SELECT *\nFROM\n users\nORDER BY name ASC\nLIMIT 10;\n");
-}
-
-#[test]
-fn test_cli_invalid_sql_passthrough() {
- let mut cmd = Command::cargo_bin("xlg-sqlfmt").unwrap();
- cmd.write_stdin("INVALID SQL SYNTAX")
- .assert()
- .failure()
- .stdout("INVALID SQL SYNTAX\n");
-}
-
-#[test]
-fn test_cli_empty_input() {
- let mut cmd = Command::cargo_bin("xlg-sqlfmt").unwrap();
- cmd.write_stdin("").assert().success().stdout("");
-}
-
-#[test]
-fn test_cli_whitespace_only() {
- let mut cmd = Command::cargo_bin("xlg-sqlfmt").unwrap();
- cmd.write_stdin(" \n \n ")
- .assert()
- .success()
- .stdout("");
-}
-
-#[test]
-fn test_cli_semicolon_addition() {
- let mut cmd = Command::cargo_bin("xlg-sqlfmt").unwrap();
- cmd.write_stdin("SELECT * FROM users")
- .assert()
- .success()
- .stdout("SELECT *\nFROM\n users;\n");
-}
-
-#[test]
-fn test_cli_idempotent_formatting() {
- let formatted_sql = "SELECT\n id,\n name\nFROM\n users\nWHERE active = TRUE;\n";
-
- let mut cmd = Command::cargo_bin("xlg-sqlfmt").unwrap();
- cmd.write_stdin(formatted_sql)
- .assert()
- .success()
- .stdout(formatted_sql);
-}
-
-#[test]
-fn test_cli_function_calls() {
- let mut cmd = Command::cargo_bin("xlg-sqlfmt").unwrap();
- cmd.write_stdin("SELECT COUNT(*), MAX(age), UPPER(name) FROM users")
- .assert()
- .success()
- .stdout("SELECT\n COUNT(*),\n MAX(age),\n UPPER(name)\nFROM\n users;\n");
-}
-
-#[test]
-fn test_cli_distinct() {
- let mut cmd = Command::cargo_bin("xlg-sqlfmt").unwrap();
- cmd.write_stdin("SELECT DISTINCT department FROM employees")
- .assert()
- .success()
- .stdout("SELECT DISTINCT department\nFROM\n employees;\n");
-}
-
-#[test]
-fn test_cli_between_expression() {
- let mut cmd = Command::cargo_bin("xlg-sqlfmt").unwrap();
- cmd.write_stdin("SELECT * FROM products WHERE price BETWEEN 10 AND 100")
- .assert()
- .success()
- .stdout("SELECT *\nFROM\n products\nWHERE price BETWEEN 10 AND 100;\n");
-}
-
-#[test]
-fn test_cli_in_subquery() {
- let mut cmd = Command::cargo_bin("xlg-sqlfmt").unwrap();
- cmd.write_stdin("SELECT * FROM users WHERE id IN (SELECT user_id FROM orders)")
- .assert()
- .success()
- .stdout("SELECT *\nFROM\n users\nWHERE id IN (\n SELECT user_id\n FROM\n orders\n);\n");
-}
-
-#[test]
-fn test_cli_is_null() {
- let mut cmd = Command::cargo_bin("xlg-sqlfmt").unwrap();
- cmd.write_stdin("SELECT * FROM users WHERE email IS NOT NULL")
- .assert()
- .success()
- .stdout("SELECT *\nFROM\n users\nWHERE email IS NOT NULL;\n");
-}
-
-#[test]
-fn test_cli_cast_expression() {
- let mut cmd = Command::cargo_bin("xlg-sqlfmt").unwrap();
- cmd.write_stdin("SELECT CAST(id AS VARCHAR) FROM users")
- .assert()
- .success()
- .stdout("SELECT\n CAST(id AS VARCHAR)\nFROM\n users;\n");
-}
-
-#[test]
-fn test_cli_nested_expressions() {
- let mut cmd = Command::cargo_bin("xlg-sqlfmt").unwrap();
- cmd.write_stdin("SELECT * FROM users WHERE (active = true AND age > 18) OR role = 'admin'")
- .assert()
- .success()
- .stdout("SELECT *\nFROM\n users\nWHERE (active = TRUE AND age > 18) OR role = 'admin';\n");
-}
-
-#[test]
-fn test_cli_multiple_statements() {
- let mut cmd = Command::cargo_bin("xlg-sqlfmt").unwrap();
- cmd.write_stdin("SELECT * FROM users; SELECT * FROM posts")
- .assert()
- .success()
- .stdout("SELECT *\nFROM\n users\nSELECT *\nFROM\n posts;\n");
-}
-
-#[test]
-fn test_cli_quoted_identifiers() {
- let mut cmd = Command::cargo_bin("xlg-sqlfmt").unwrap();
- cmd.write_stdin("SELECT \"id\", \"user_name\" FROM \"admin_users\" WHERE \"active\" = true")
- .assert()
- .success()
- .stdout("SELECT\n id,\n user_name\nFROM\n \"admin_users\"\nWHERE active = TRUE;\n");
-}
-
-#[test]
-fn test_cli_your_original_example() {
- let mut cmd = Command::cargo_bin("xlg-sqlfmt").unwrap();
- cmd.write_stdin("SELECT 1 AS one FROM \"admin_roles\" WHERE \"admin_roles\".\"name\" = 'root' AND \"admin_roles\".\"organization_id\" = 1 LIMIT 1")
- .assert()
- .success()
- .stdout("SELECT 1 AS one\nFROM\n \"admin_roles\"\nWHERE admin_roles.name = 'root'\n AND admin_roles.organization_id = 1\nLIMIT 1;\n");
-}
-
fn discover_fixtures() -> Vec<PathBuf> {
let fixtures_dir = Path::new("tests/fixtures");
let mut fixtures = Vec::new();
@@ -264,11 +38,16 @@ fn run_fixture_test(fixture_path: &Path) {
let mut cmd = Command::cargo_bin("xlg-sqlfmt").unwrap();
- if scenario_name == "invalid_sql" {
+ if scenario_name == "invalid_sql" || scenario_name == "invalid_sql_passthrough" {
cmd.write_stdin(input_sql.as_str())
.assert()
.failure()
.stdout(format!("{}\n", expected_output));
+ } else if scenario_name == "empty_input" || scenario_name == "whitespace_only" {
+ cmd.write_stdin(input_sql.as_str())
+ .assert()
+ .success()
+ .stdout(expected_output);
} else {
cmd.write_stdin(input_sql.as_str())
.assert()
@@ -293,4 +72,4 @@ fn test_all_fixtures() {
run_fixture_test(&fixture_path);
}
-}
+}
\ No newline at end of file