Skip to content

Commit 2cfaf40

Browse files
committed
test: keep the call-depth tests branch-free and in their own files
The lint and format call-depth tests assert against their expected outcomes without branching, and move with the Databricks named-window test out of the oversized lint test file. The default and maximum depth are SQLClean constants rather than a re-export. The format e2e test also covers the default limit, which leaves 300 nested calls as written and names the key that raises it.
1 parent a735c9a commit 2cfaf40

7 files changed

Lines changed: 220 additions & 172 deletions

File tree

‎crates/sqlclean/src/sql_lint/constants.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
pub const LINT_API_VERSION: u32 = 1;
2-
/// Nested function calls a request accepts by default, and the most it may allow.
3-
pub use sqlquack_parse::parsing::constants::{MAX_CALL_DEPTH_CEILING, MAX_FUNCTION_CALL_DEPTH};
2+
/// Nested function calls a request accepts by default.
3+
pub const MAX_FUNCTION_CALL_DEPTH: usize =
4+
sqlquack_parse::parsing::constants::MAX_FUNCTION_CALL_DEPTH;
5+
/// The most nested function calls a request may allow.
6+
pub const MAX_CALL_DEPTH_CEILING: usize =
7+
sqlquack_parse::parsing::constants::MAX_CALL_DEPTH_CEILING;
48
/// The dialect name SQLQuack lays out as Snowflake.
59
pub(crate) const SNOWFLAKE_DIALECT: &str = "snowflake";
610
/// The dialect name SQLQuack lays out as DuckDB.

‎crates/sqlclean/src/sql_lint/tests.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ mod empty_fixture_lint;
1010
mod format_corpus;
1111
#[path = "tests/test_function_call_spacing.rs"]
1212
mod function_call_spacing;
13+
#[path = "tests/test_function_depth.rs"]
14+
mod function_depth;
1315
#[path = "tests/helpers.rs"]
1416
mod helpers;
1517
#[path = "tests/test_layout_rules.rs"]
@@ -18,6 +20,8 @@ mod layout_rules;
1820
mod line_wrap;
1921
#[path = "tests/test_lint_exemptions.rs"]
2022
mod lint_exemptions;
23+
#[path = "tests/test_named_windows.rs"]
24+
mod named_windows;
2125
#[path = "tests/test_set_operation_formatter.rs"]
2226
mod set_operation_formatter;
2327
#[path = "tests/test_sql_lint.rs"]
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
use serde_json::{Value, json};
2+
3+
use crate::sql_lint::main::engine::lint_json;
4+
use crate::sql_lint::main::formatter::format_json;
5+
use crate::sql_lint::tests::{helpers, test_types};
6+
7+
#[test]
8+
fn given_nested_function_calls_when_linting_then_the_requested_or_default_depth_bounds_them() {
9+
let test_cases = [
10+
test_types::FunctionDepthTestCase {
11+
description: "the default depth of 256 parses",
12+
depth: 256,
13+
max_function_call_depth: None,
14+
expected_error: None,
15+
},
16+
test_types::FunctionDepthTestCase {
17+
description: "one call past the default fails and names the setting",
18+
depth: 257,
19+
max_function_call_depth: None,
20+
expected_error: Some("configured limit 256; raise max_function_call_depth, up to 512"),
21+
},
22+
test_types::FunctionDepthTestCase {
23+
description: "a requested depth bounds the parse",
24+
depth: 65,
25+
max_function_call_depth: Some(64),
26+
expected_error: Some("configured limit 64; raise max_function_call_depth"),
27+
},
28+
test_types::FunctionDepthTestCase {
29+
description: "a requested depth above the default lets deeper calls parse",
30+
depth: 300,
31+
max_function_call_depth: Some(300),
32+
expected_error: None,
33+
},
34+
];
35+
for test_case in test_cases {
36+
let request = json!({
37+
"version": 1,
38+
"sql": helpers::nested_function_sql(test_case.depth),
39+
"dialect": "snowflake",
40+
"max_function_call_depth": test_case.max_function_call_depth,
41+
});
42+
let outcome: Result<String, String> = lint_json(&request.to_string());
43+
let error: String = outcome.clone().err().unwrap_or_default();
44+
let diagnostics: Value = outcome
45+
.ok()
46+
.and_then(|response| serde_json::from_str::<Value>(&response).ok())
47+
.map_or_else(|| json!([]), |response| response["diagnostics"].clone());
48+
assert_eq!(
49+
!error.is_empty(),
50+
test_case.expected_error.is_some(),
51+
"{}: {error}",
52+
test_case.description
53+
);
54+
assert!(
55+
error.contains(test_case.expected_error.unwrap_or_default()),
56+
"{}: {error}",
57+
test_case.description
58+
);
59+
assert_eq!(diagnostics, json!([]), "{}", test_case.description);
60+
}
61+
}
62+
63+
#[test]
64+
fn given_nested_function_calls_when_formatting_then_the_requested_or_default_depth_bounds_them() {
65+
let test_cases = [
66+
test_types::FunctionDepthTestCase {
67+
description: "the default depth of 256 formats",
68+
depth: 256,
69+
max_function_call_depth: None,
70+
expected_error: None,
71+
},
72+
test_types::FunctionDepthTestCase {
73+
description: "one call past the default is declined, naming the setting that raises it",
74+
depth: 257,
75+
max_function_call_depth: None,
76+
expected_error: Some("configured limit 256; raise max_function_call_depth, up to 512"),
77+
},
78+
test_types::FunctionDepthTestCase {
79+
description: "a requested depth bounds the formatter",
80+
depth: 65,
81+
max_function_call_depth: Some(64),
82+
expected_error: Some("configured limit 64"),
83+
},
84+
test_types::FunctionDepthTestCase {
85+
description: "a requested depth above the most allowed is declined",
86+
depth: 2,
87+
max_function_call_depth: Some(513),
88+
expected_error: Some("max_function_call_depth"),
89+
},
90+
];
91+
for test_case in test_cases {
92+
let request = json!({
93+
"version": 1,
94+
"sql": helpers::nested_function_sql(test_case.depth),
95+
"dialect": "snowflake",
96+
"max_function_call_depth": test_case.max_function_call_depth,
97+
});
98+
let response: Value =
99+
serde_json::from_str(&format_json(&request.to_string()).expect("a format response"))
100+
.expect("JSON");
101+
let reason: &str = response["reason"].as_str().unwrap_or_default();
102+
assert_eq!(
103+
response["formatted"] == json!(false),
104+
test_case.expected_error.is_some(),
105+
"{}: {reason}",
106+
test_case.description
107+
);
108+
assert!(
109+
reason.contains(test_case.expected_error.unwrap_or_default()),
110+
"{}: {reason}",
111+
test_case.description
112+
);
113+
}
114+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use crate::sql_lint::tests::{helpers, test_types};
2+
3+
/// Databricks keeps a WINDOW clause written after a parenthesized query on the subquery, so a ranking call naming that window is still checked.
4+
#[test]
5+
fn given_databricks_window_after_parentheses_when_linting_then_ranking_rules_read_it()
6+
-> Result<(), String> {
7+
let test_cases = [
8+
test_types::NamedWindowRankingTestCase {
9+
description: "a ranking sort above the cap in a WINDOW after the parentheses",
10+
sql: "(SELECT o.order_id, ROW_NUMBER() OVER w AS rn FROM orders AS o) WINDOW w AS (ORDER BY o.a, o.b, o.c, o.d, o.e, o.f)",
11+
rule: "SQLCLEAN043",
12+
expected_anchors: &["ROW_NUMBER"],
13+
},
14+
test_types::NamedWindowRankingTestCase {
15+
description: "the same with a WITH before the parentheses",
16+
sql: "WITH c AS (SELECT 1 AS x) (SELECT o.order_id, ROW_NUMBER() OVER w AS rn FROM orders AS o) WINDOW w AS (ORDER BY o.a, o.b, o.c, o.d, o.e, o.f)",
17+
rule: "SQLCLEAN043",
18+
expected_anchors: &["ROW_NUMBER"],
19+
},
20+
test_types::NamedWindowRankingTestCase {
21+
description: "an ordering without a declared key in a WINDOW after the parentheses is unproven",
22+
sql: "(SELECT o.order_id, ROW_NUMBER() OVER w AS rn FROM orders AS o) WINDOW w AS (ORDER BY o.ordered_at)",
23+
rule: "SQLCLEAN018",
24+
expected_anchors: &["ROW_NUMBER"],
25+
},
26+
test_types::NamedWindowRankingTestCase {
27+
description: "a ranking sort within the cap in a WINDOW after the parentheses is clean",
28+
sql: "(SELECT o.order_id, ROW_NUMBER() OVER w AS rn FROM orders AS o) WINDOW w AS (ORDER BY o.a)",
29+
rule: "SQLCLEAN043",
30+
expected_anchors: &[],
31+
},
32+
];
33+
34+
for test_case in &test_cases {
35+
let diagnostics =
36+
helpers::diagnostics_for_dialect(test_case.sql, "databricks", &[test_case.rule])?;
37+
let anchors: Vec<String> = diagnostics
38+
.iter()
39+
.map(|diagnostic| {
40+
let start = diagnostic["start"].as_u64().unwrap_or_default() as usize;
41+
let end = diagnostic["end"].as_u64().unwrap_or_default() as usize;
42+
test_case
43+
.sql
44+
.chars()
45+
.skip(start)
46+
.take(end - start)
47+
.collect()
48+
})
49+
.collect();
50+
assert_eq!(
51+
anchors, test_case.expected_anchors,
52+
"{}",
53+
test_case.description
54+
);
55+
}
56+
Ok(())
57+
}

‎crates/sqlclean/src/sql_lint/tests/test_sql_lint.rs‎

Lines changed: 0 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -5,62 +5,6 @@ use crate::sql_lint::tests::helpers::{fixture_harness, format_batch_json};
55
use crate::sql_lint::tests::{helpers, test_types};
66
use crate::sql_tokens::main::query_fingerprint::query_fingerprint;
77

8-
#[test]
9-
fn given_nested_function_calls_when_linting_then_the_requested_or_default_depth_bounds_them() {
10-
let test_cases = [
11-
test_types::FunctionDepthTestCase {
12-
description: "the default depth of 256 parses",
13-
depth: 256,
14-
max_function_call_depth: None,
15-
expected_error: None,
16-
},
17-
test_types::FunctionDepthTestCase {
18-
description: "one call past the default fails and names the setting",
19-
depth: 257,
20-
max_function_call_depth: None,
21-
expected_error: Some("configured limit 256"),
22-
},
23-
test_types::FunctionDepthTestCase {
24-
description: "a requested depth bounds the parse",
25-
depth: 65,
26-
max_function_call_depth: Some(64),
27-
expected_error: Some("configured limit 64"),
28-
},
29-
test_types::FunctionDepthTestCase {
30-
description: "a requested depth above the default lets deeper calls parse",
31-
depth: 300,
32-
max_function_call_depth: Some(300),
33-
expected_error: None,
34-
},
35-
];
36-
for test_case in test_cases {
37-
let request = json!({
38-
"version": 1,
39-
"sql": helpers::nested_function_sql(test_case.depth),
40-
"dialect": "snowflake",
41-
"max_function_call_depth": test_case.max_function_call_depth,
42-
});
43-
let outcome: Result<Value, String> = lint_json(&request.to_string())
44-
.and_then(|response| serde_json::from_str(&response).map_err(|e| e.to_string()));
45-
match (outcome, test_case.expected_error) {
46-
(Ok(response), None) => assert_eq!(
47-
response["diagnostics"],
48-
json!([]),
49-
"{}",
50-
test_case.description
51-
),
52-
(Err(error), Some(limit)) => assert!(
53-
error.contains("E_GUARD_FUNCTION_NESTING_DEPTH_EXCEEDED")
54-
&& error.contains(limit)
55-
&& error.contains("max_function_call_depth"),
56-
"{}: {error}",
57-
test_case.description
58-
),
59-
(outcome, _) => panic!("{}: {outcome:?}", test_case.description),
60-
}
61-
}
62-
}
63-
648
#[test]
659
fn given_sql_cases_when_linting_then_diagnostics_match() -> Result<(), String> {
6610
let test_cases = [
@@ -1826,62 +1770,6 @@ fn given_tsql_bare_union_when_linting_then_diagnostic_has_no_invalid_fix() -> Re
18261770
Ok(())
18271771
}
18281772

1829-
/// Databricks keeps a WINDOW clause written after a parenthesized query on the subquery, so a ranking call naming that window is still checked.
1830-
#[test]
1831-
fn given_databricks_window_after_parentheses_when_linting_then_ranking_rules_read_it()
1832-
-> Result<(), String> {
1833-
let test_cases = [
1834-
test_types::NamedWindowRankingTestCase {
1835-
description: "a ranking sort above the cap in a WINDOW after the parentheses",
1836-
sql: "(SELECT o.order_id, ROW_NUMBER() OVER w AS rn FROM orders AS o) WINDOW w AS (ORDER BY o.a, o.b, o.c, o.d, o.e, o.f)",
1837-
rule: "SQLCLEAN043",
1838-
expected_anchors: &["ROW_NUMBER"],
1839-
},
1840-
test_types::NamedWindowRankingTestCase {
1841-
description: "the same with a WITH before the parentheses",
1842-
sql: "WITH c AS (SELECT 1 AS x) (SELECT o.order_id, ROW_NUMBER() OVER w AS rn FROM orders AS o) WINDOW w AS (ORDER BY o.a, o.b, o.c, o.d, o.e, o.f)",
1843-
rule: "SQLCLEAN043",
1844-
expected_anchors: &["ROW_NUMBER"],
1845-
},
1846-
test_types::NamedWindowRankingTestCase {
1847-
description: "an ordering without a declared key in a WINDOW after the parentheses is unproven",
1848-
sql: "(SELECT o.order_id, ROW_NUMBER() OVER w AS rn FROM orders AS o) WINDOW w AS (ORDER BY o.ordered_at)",
1849-
rule: "SQLCLEAN018",
1850-
expected_anchors: &["ROW_NUMBER"],
1851-
},
1852-
test_types::NamedWindowRankingTestCase {
1853-
description: "a ranking sort within the cap in a WINDOW after the parentheses is clean",
1854-
sql: "(SELECT o.order_id, ROW_NUMBER() OVER w AS rn FROM orders AS o) WINDOW w AS (ORDER BY o.a)",
1855-
rule: "SQLCLEAN043",
1856-
expected_anchors: &[],
1857-
},
1858-
];
1859-
1860-
for test_case in &test_cases {
1861-
let diagnostics =
1862-
helpers::diagnostics_for_dialect(test_case.sql, "databricks", &[test_case.rule])?;
1863-
let anchors: Vec<String> = diagnostics
1864-
.iter()
1865-
.map(|diagnostic| {
1866-
let start = diagnostic["start"].as_u64().unwrap_or_default() as usize;
1867-
let end = diagnostic["end"].as_u64().unwrap_or_default() as usize;
1868-
test_case
1869-
.sql
1870-
.chars()
1871-
.skip(start)
1872-
.take(end - start)
1873-
.collect()
1874-
})
1875-
.collect();
1876-
assert_eq!(
1877-
anchors, test_case.expected_anchors,
1878-
"{}",
1879-
test_case.description
1880-
);
1881-
}
1882-
Ok(())
1883-
}
1884-
18851773
#[test]
18861774
fn given_ordered_sql_batch_when_formatting_then_outputs_are_parallel_and_ordered()
18871775
-> Result<(), String> {
@@ -2050,57 +1938,3 @@ fn given_each_documented_dialect_when_reading_sql_then_sqlquack_was_built_for_it
20501938
}
20511939
Ok(())
20521940
}
2053-
2054-
#[test]
2055-
fn given_nested_function_calls_when_formatting_then_the_requested_or_default_depth_bounds_them() {
2056-
let test_cases = [
2057-
test_types::FunctionDepthTestCase {
2058-
description: "the default depth of 256 formats",
2059-
depth: 256,
2060-
max_function_call_depth: None,
2061-
expected_error: None,
2062-
},
2063-
test_types::FunctionDepthTestCase {
2064-
description: "one call past the default is declined, naming the setting that raises it",
2065-
depth: 257,
2066-
max_function_call_depth: None,
2067-
expected_error: Some("configured limit 256; raise max_function_call_depth, up to 512"),
2068-
},
2069-
test_types::FunctionDepthTestCase {
2070-
description: "a requested depth bounds the formatter",
2071-
depth: 65,
2072-
max_function_call_depth: Some(64),
2073-
expected_error: Some("configured limit 64"),
2074-
},
2075-
test_types::FunctionDepthTestCase {
2076-
description: "a requested depth above the most allowed is declined",
2077-
depth: 2,
2078-
max_function_call_depth: Some(513),
2079-
expected_error: Some("max_function_call_depth"),
2080-
},
2081-
];
2082-
for test_case in test_cases {
2083-
let request = json!({
2084-
"version": 1,
2085-
"sql": helpers::nested_function_sql(test_case.depth),
2086-
"dialect": "snowflake",
2087-
"max_function_call_depth": test_case.max_function_call_depth,
2088-
});
2089-
let response: Value = serde_json::from_str(
2090-
&crate::sql_lint::main::formatter::format_json(&request.to_string())
2091-
.expect("a format response"),
2092-
)
2093-
.expect("JSON");
2094-
let declined: Option<&str> = (response["formatted"] == json!(false))
2095-
.then(|| response["reason"].as_str().unwrap_or_default());
2096-
match (declined, test_case.expected_error) {
2097-
(None, None) => {}
2098-
(Some(reason), Some(limit)) => assert!(
2099-
reason.contains(limit),
2100-
"{}: {reason}",
2101-
test_case.description
2102-
),
2103-
(declined, _) => panic!("{}: {declined:?}", test_case.description),
2104-
}
2105-
}
2106-
}

0 commit comments

Comments
 (0)