use kumosql_sql::ast::Statement;
use kumosql_sql::formatting::{
complexity, format_sql, format_with_diagnostics, CommaPosition, FormatPreferences, IndentUnit,
KeywordCase,
};
use kumosql_sql::parser::parse_statements;
use kumosql_sql::printer::to_sql_compact;
mod common;
use common::{PIPES, QUERIES, STATEMENTS};
fn same_meaning(a: &str, b: &str) {
let left = parse_statements(a).unwrap();
let right =
parse_statements(b).unwrap_or_else(|e| panic!("formatted SQL does not parse: {e}\n{b}"));
assert_eq!(left.len(), right.len());
for (x, y) in left.iter().zip(&right) {
assert_eq!(
to_sql_compact(x).to_uppercase(),
to_sql_compact(y).to_uppercase(),
"\n{a}\n---\n{b}"
);
}
}
fn check_all(prefs: &FormatPreferences) {
for sql in QUERIES.iter().chain(PIPES).chain(STATEMENTS) {
let outcome = format_with_diagnostics(sql, prefs);
let raw_only = parse_statements(sql)
.unwrap()
.iter()
.all(|s| matches!(s, Statement::Raw(_)));
if raw_only {
assert_eq!(outcome.sql, *sql);
assert_eq!(outcome.diagnostics[0].code, "parse_error");
continue;
}
assert!(
outcome
.diagnostics
.iter()
.all(|d| d.code == "statements_not_formatted"),
"{sql:?}: {:?}",
outcome.diagnostics
);
let formatted = outcome.sql;
same_meaning(sql, &formatted);
let again = format_sql(&formatted, prefs).unwrap();
assert_eq!(again, formatted, "not idempotent for {sql:?}");
}
}
#[test]
fn formats_the_corpus_with_defaults() {
check_all(&FormatPreferences::default());
}
#[test]
fn formats_the_corpus_with_other_preferences() {
check_all(&FormatPreferences {
keyword_case: KeywordCase::Lower,
comma_position: CommaPosition::Leading,
indent_unit: IndentUnit::Tab,
max_line_length: 40,
..FormatPreferences::default()
});
check_all(&FormatPreferences {
rules: vec!["capitalisation".into()],
keyword_case: KeywordCase::Capitalise,
..FormatPreferences::default()
});
}
#[test]
fn keeps_every_comment() {
let sql = "/* lead */\n-- one\nSELECT a, -- after a\n b /* inline */ FROM t # hash\nWHERE x = 1; -- after statement\n\n-- before second\nSELECT 2;\n";
let formatted = format_sql(sql, &FormatPreferences::default()).unwrap();
for comment in [
"/* lead */",
"-- one",
"-- after a",
"/* inline */",
"# hash",
"-- after statement",
"-- before second",
] {
assert!(formatted.contains(comment), "lost {comment}:\n{formatted}");
}
assert!(formatted.ends_with("SELECT 2;\n"), "{formatted:?}");
assert_eq!(
format_sql(&formatted, &FormatPreferences::default()).unwrap(),
formatted
);
}
#[test]
fn keeps_quoted_names_and_strings_exactly() {
let sql = "select `My-Proj.DS.Tbl`.`Col`, 'MiXeD', r'\\d+', myUdf(x), `p.d.Fn`(y), safe.divide(a, b) from `My-Proj.DS.Tbl`";
let formatted = format_sql(sql, &FormatPreferences::default()).unwrap();
for exact in [
"`My-Proj.DS.Tbl`.`Col`",
"'MiXeD'",
"r'\\d+'",
"myUdf(x)",
"`p.d.Fn`(y)",
"safe.divide(a, b)",
] {
assert!(formatted.contains(exact), "changed {exact}:\n{formatted}");
}
}
#[test]
fn created_functions_keep_their_case() {
let sql = "create temp function lower(x string) as (x); select lower('A')";
let formatted = format_sql(sql, &FormatPreferences::default()).unwrap();
assert!(formatted.contains("SELECT lower('A')"), "{formatted}");
}
#[test]
fn complexity_of_the_corpus() {
for sql in QUERIES.iter().chain(PIPES) {
let c = complexity(sql).unwrap_or_else(|e| panic!("{sql:?}: {e}"));
assert!(c.score >= 0.0);
}
let deep = complexity("SELECT * FROM (SELECT * FROM (SELECT * FROM (SELECT 1)))").unwrap();
assert_eq!(deep.metrics.max_nesting, 3);
assert_eq!(deep.metrics.subqueries, 3);
assert_eq!(deep.score, 15.0);
}