Skip to content

Commit 2596e1b

Browse files
committed
test(syntax): keep the parse error position tests in their own file
test_fixes.rs grew past the 2,000-line limit once main's tests joined it; the tests that check where a parse error points and what it says move to test_parse_errors.rs.
1 parent cd64a37 commit 2596e1b

3 files changed

Lines changed: 110 additions & 106 deletions

File tree

‎crates/sqlquack-syntax-snowflake/tests/parser.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ mod test_fixes;
88
mod test_located;
99
#[path = "parser/test_lossless.rs"]
1010
mod test_lossless;
11+
#[path = "parser/test_parse_errors.rs"]
12+
mod test_parse_errors;
1113
#[path = "parser/test_scripting.rs"]
1214
mod test_scripting;
1315
#[path = "parser/test_snowflake_alter.rs"]

‎crates/sqlquack-syntax-snowflake/tests/parser/test_fixes.rs‎

Lines changed: 2 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
use serde_json::json;
22

3-
use sqlquack_syntax::{CallMode, Dialect, ParseOptions};
4-
use sqlquack_syntax_snowflake::parse_statements_with;
3+
use sqlquack_syntax::{CallMode, Dialect};
54

65
use crate::helpers::{accepted, cast_targets, projection_at, statement_at, statements};
76
use crate::test_types::{
8-
AcceptanceTestCase, CastTargetTestCase, ParseErrorTestCase, ParseMessageTestCase,
9-
ProjectionTestCase, StatementTestCase,
7+
AcceptanceTestCase, CastTargetTestCase, ProjectionTestCase, StatementTestCase,
108
};
119

1210
#[test]
@@ -1952,105 +1950,3 @@ fn given_a_keyword_lambda_parameter_when_parsing_duckdb_then_it_names_the_parame
19521950
);
19531951
}
19541952
}
1955-
1956-
#[test]
1957-
fn given_group_or_order_by_after_an_operator_when_parsing_snowflake_then_the_error_is_at_the_clause(
1958-
) {
1959-
let test_cases = [
1960-
ParseErrorTestCase {
1961-
description: "AND before GROUP BY on the next line fails at GROUP, not at BY",
1962-
sql: "SELECT customer_id, SUM(total) AS revenue\nFROM orders\nWHERE status = 'shipped' AND\nGROUP BY customer_id",
1963-
expected_error_at: Some("GROUP"),
1964-
},
1965-
ParseErrorTestCase {
1966-
description: "AND before ORDER BY on the same line fails at ORDER",
1967-
sql: "SELECT order_id FROM orders WHERE total > 1 AND ORDER BY order_id",
1968-
expected_error_at: Some("ORDER"),
1969-
},
1970-
ParseErrorTestCase {
1971-
description: "NOT before GROUP BY fails at GROUP",
1972-
sql: "SELECT status FROM orders WHERE NOT\ngroup by status",
1973-
expected_error_at: Some("group"),
1974-
},
1975-
ParseErrorTestCase {
1976-
description: "GROUP BY and ORDER BY after WHERE still parse",
1977-
sql: "SELECT status FROM orders WHERE total > 1\nGROUP BY status\nORDER BY status",
1978-
expected_error_at: None,
1979-
},
1980-
ParseErrorTestCase {
1981-
description: "a quoted GROUP column still parses",
1982-
sql: "SELECT \"GROUP\" FROM orders ORDER BY 1",
1983-
expected_error_at: None,
1984-
},
1985-
];
1986-
1987-
for test_case in test_cases {
1988-
let error_at = parse_statements_with(
1989-
test_case.sql,
1990-
Dialect::Snowflake,
1991-
&ParseOptions::default(),
1992-
CallMode::Faithful,
1993-
)
1994-
.err()
1995-
.map(|error| {
1996-
let start = error.start().unwrap_or_default();
1997-
let end = error.end().unwrap_or_default();
1998-
test_case.sql.get(start..end).unwrap_or_default().to_owned()
1999-
});
2000-
assert_eq!(
2001-
error_at.as_deref(),
2002-
test_case.expected_error_at,
2003-
"{}",
2004-
test_case.description
2005-
);
2006-
}
2007-
}
2008-
2009-
#[test]
2010-
fn given_a_statement_ending_where_an_expression_belongs_when_parsing_snowflake_then_the_error_names_what_is_missing(
2011-
) {
2012-
let test_cases = [
2013-
ParseMessageTestCase {
2014-
description: "an empty WHERE asks for a condition",
2015-
sql: "SELECT * FROM orders WHERE",
2016-
expected_message: "Expected a condition after WHERE",
2017-
},
2018-
ParseMessageTestCase {
2019-
description: "an empty WHERE before a semicolon asks for a condition",
2020-
sql: "SELECT * FROM orders WHERE ;",
2021-
expected_message: "Expected a condition after WHERE",
2022-
},
2023-
ParseMessageTestCase {
2024-
description: "an empty HAVING asks for a condition",
2025-
sql: "SELECT status FROM orders GROUP BY status HAVING",
2026-
expected_message: "Expected a condition after HAVING",
2027-
},
2028-
ParseMessageTestCase {
2029-
description: "a join's empty ON asks for a condition",
2030-
sql: "SELECT 1 FROM orders o JOIN customers c ON",
2031-
expected_message: "Expected a condition after ON",
2032-
},
2033-
ParseMessageTestCase {
2034-
description: "a comparison with no right side asks for an expression",
2035-
sql: "SELECT 1 FROM orders WHERE total >",
2036-
expected_message: "Expected an expression after >",
2037-
},
2038-
];
2039-
2040-
for test_case in test_cases {
2041-
let message = parse_statements_with(
2042-
test_case.sql,
2043-
Dialect::Snowflake,
2044-
&ParseOptions::default(),
2045-
CallMode::Faithful,
2046-
)
2047-
.err()
2048-
.map(|error| error.to_string())
2049-
.unwrap_or_default();
2050-
assert!(
2051-
message.contains(test_case.expected_message),
2052-
"{}: {message}",
2053-
test_case.description
2054-
);
2055-
}
2056-
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
use sqlquack_syntax::{CallMode, Dialect, ParseOptions};
2+
use sqlquack_syntax_snowflake::parse_statements_with;
3+
4+
use crate::test_types::{ParseErrorTestCase, ParseMessageTestCase};
5+
6+
#[test]
7+
fn given_group_or_order_by_after_an_operator_when_parsing_snowflake_then_the_error_is_at_the_clause(
8+
) {
9+
let test_cases = [
10+
ParseErrorTestCase {
11+
description: "AND before GROUP BY on the next line fails at GROUP, not at BY",
12+
sql: "SELECT customer_id, SUM(total) AS revenue\nFROM orders\nWHERE status = 'shipped' AND\nGROUP BY customer_id",
13+
expected_error_at: Some("GROUP"),
14+
},
15+
ParseErrorTestCase {
16+
description: "AND before ORDER BY on the same line fails at ORDER",
17+
sql: "SELECT order_id FROM orders WHERE total > 1 AND ORDER BY order_id",
18+
expected_error_at: Some("ORDER"),
19+
},
20+
ParseErrorTestCase {
21+
description: "NOT before GROUP BY fails at GROUP",
22+
sql: "SELECT status FROM orders WHERE NOT\ngroup by status",
23+
expected_error_at: Some("group"),
24+
},
25+
ParseErrorTestCase {
26+
description: "GROUP BY and ORDER BY after WHERE still parse",
27+
sql: "SELECT status FROM orders WHERE total > 1\nGROUP BY status\nORDER BY status",
28+
expected_error_at: None,
29+
},
30+
ParseErrorTestCase {
31+
description: "a quoted GROUP column still parses",
32+
sql: "SELECT \"GROUP\" FROM orders ORDER BY 1",
33+
expected_error_at: None,
34+
},
35+
];
36+
37+
for test_case in test_cases {
38+
let error_at = parse_statements_with(
39+
test_case.sql,
40+
Dialect::Snowflake,
41+
&ParseOptions::default(),
42+
CallMode::Faithful,
43+
)
44+
.err()
45+
.map(|error| {
46+
let start = error.start().unwrap_or_default();
47+
let end = error.end().unwrap_or_default();
48+
test_case.sql.get(start..end).unwrap_or_default().to_owned()
49+
});
50+
assert_eq!(
51+
error_at.as_deref(),
52+
test_case.expected_error_at,
53+
"{}",
54+
test_case.description
55+
);
56+
}
57+
}
58+
59+
#[test]
60+
fn given_a_statement_ending_where_an_expression_belongs_when_parsing_snowflake_then_the_error_names_what_is_missing(
61+
) {
62+
let test_cases = [
63+
ParseMessageTestCase {
64+
description: "an empty WHERE asks for a condition",
65+
sql: "SELECT * FROM orders WHERE",
66+
expected_message: "Expected a condition after WHERE",
67+
},
68+
ParseMessageTestCase {
69+
description: "an empty WHERE before a semicolon asks for a condition",
70+
sql: "SELECT * FROM orders WHERE ;",
71+
expected_message: "Expected a condition after WHERE",
72+
},
73+
ParseMessageTestCase {
74+
description: "an empty HAVING asks for a condition",
75+
sql: "SELECT status FROM orders GROUP BY status HAVING",
76+
expected_message: "Expected a condition after HAVING",
77+
},
78+
ParseMessageTestCase {
79+
description: "a join's empty ON asks for a condition",
80+
sql: "SELECT 1 FROM orders o JOIN customers c ON",
81+
expected_message: "Expected a condition after ON",
82+
},
83+
ParseMessageTestCase {
84+
description: "a comparison with no right side asks for an expression",
85+
sql: "SELECT 1 FROM orders WHERE total >",
86+
expected_message: "Expected an expression after >",
87+
},
88+
];
89+
90+
for test_case in test_cases {
91+
let message = parse_statements_with(
92+
test_case.sql,
93+
Dialect::Snowflake,
94+
&ParseOptions::default(),
95+
CallMode::Faithful,
96+
)
97+
.err()
98+
.map(|error| error.to_string())
99+
.unwrap_or_default();
100+
assert!(
101+
message.contains(test_case.expected_message),
102+
"{}: {message}",
103+
test_case.description
104+
);
105+
}
106+
}

0 commit comments

Comments
 (0)