Skip to content

Commit

Permalink
recursion test + ability to configure recursion limit for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomershaniii committed Oct 14, 2024
1 parent 94d5466 commit e474fd2
Show file tree
Hide file tree
Showing 14 changed files with 346 additions and 480 deletions.
42 changes: 14 additions & 28 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12279,10 +12279,8 @@ mod tests {
#[test]
fn test_ansii_character_string_types() {
// Character string types: <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#character-string-type>
let dialect = TestedDialects {
dialects: vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})],
options: None,
};
let dialect =
TestedDialects::new(vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})]);

test_parse_data_type!(dialect, "CHARACTER", DataType::Character(None));

Expand Down Expand Up @@ -12409,10 +12407,8 @@ mod tests {
#[test]
fn test_ansii_character_large_object_types() {
// Character large object types: <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#character-large-object-length>
let dialect = TestedDialects {
dialects: vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})],
options: None,
};
let dialect =
TestedDialects::new(vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})]);

test_parse_data_type!(
dialect,
Expand Down Expand Up @@ -12442,10 +12438,9 @@ mod tests {

#[test]
fn test_parse_custom_types() {
let dialect = TestedDialects {
dialects: vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})],
options: None,
};
let dialect =
TestedDialects::new(vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})]);

test_parse_data_type!(
dialect,
"GEOMETRY",
Expand Down Expand Up @@ -12474,10 +12469,8 @@ mod tests {
#[test]
fn test_ansii_exact_numeric_types() {
// Exact numeric types: <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#exact-numeric-type>
let dialect = TestedDialects {
dialects: vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})],
options: None,
};
let dialect =
TestedDialects::new(vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})]);

test_parse_data_type!(dialect, "NUMERIC", DataType::Numeric(ExactNumberInfo::None));

Expand Down Expand Up @@ -12525,10 +12518,8 @@ mod tests {
#[test]
fn test_ansii_date_type() {
// Datetime types: <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#datetime-type>
let dialect = TestedDialects {
dialects: vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})],
options: None,
};
let dialect =
TestedDialects::new(vec![Box::new(GenericDialect {}), Box::new(AnsiDialect {})]);

test_parse_data_type!(dialect, "DATE", DataType::Date);

Expand Down Expand Up @@ -12637,10 +12628,8 @@ mod tests {
}};
}

let dialect = TestedDialects {
dialects: vec![Box::new(GenericDialect {}), Box::new(MySqlDialect {})],
options: None,
};
let dialect =
TestedDialects::new(vec![Box::new(GenericDialect {}), Box::new(MySqlDialect {})]);

test_parse_table_constraint!(
dialect,
Expand Down Expand Up @@ -12759,10 +12748,7 @@ mod tests {

#[test]
fn test_parse_multipart_identifier_positive() {
let dialect = TestedDialects {
dialects: vec![Box::new(GenericDialect {})],
options: None,
};
let dialect = TestedDialects::new(vec![Box::new(GenericDialect {})]);

// parse multipart with quotes
let expected = vec![
Expand Down
27 changes: 25 additions & 2 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use pretty_assertions::assert_eq;
pub struct TestedDialects {
pub dialects: Vec<Box<dyn Dialect>>,
pub options: Option<ParserOptions>,
pub recursion_limit: Option<usize>,
}

impl TestedDialects {
Expand All @@ -52,16 +53,38 @@ impl TestedDialects {
Self {
dialects,
options: None,
recursion_limit: None,
}
}

pub fn new_with_options(dialects: Vec<Box<dyn Dialect>>, options: ParserOptions) -> Self {
Self {
dialects,
options: Some(options),
recursion_limit: None,
}
}

pub fn with_recursion_limit(mut self, recursion_limit: usize) -> Self {
self.recursion_limit = Some(recursion_limit);
self
}

fn new_parser<'a>(&self, dialect: &'a dyn Dialect) -> Parser<'a> {
let parser = Parser::new(dialect);
if let Some(options) = &self.options {
let parser = if let Some(options) = &self.options {
parser.with_options(options.clone())
} else {
parser
}
};

let parser = if let Some(recursion_limit) = &self.recursion_limit {
parser.with_recursion_limit(recursion_limit.clone())
} else {
parser
};

parser
}

/// Run the given function for all of `self.dialects`, assert that they
Expand Down
21 changes: 9 additions & 12 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ fn parse_literal_string() {
r#""""triple-double\"escaped""", "#,
r#""""triple-double"unescaped""""#,
);
let dialect = TestedDialects {
dialects: vec![Box::new(BigQueryDialect {})],
options: Some(ParserOptions::new().with_unescape(false)),
};
let dialect = TestedDialects::new_with_options(
vec![Box::new(BigQueryDialect {})],
ParserOptions::new().with_unescape(false),
);
let select = dialect.verified_only_select(sql);
assert_eq!(10, select.projection.len());
assert_eq!(
Expand Down Expand Up @@ -1936,17 +1936,14 @@ fn parse_big_query_declare() {
}

fn bigquery() -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(BigQueryDialect {})],
options: None,
}
TestedDialects::new(vec![Box::new(BigQueryDialect {})])
}

fn bigquery_and_generic() -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(BigQueryDialect {}), Box::new(GenericDialect {})],
options: None,
}
TestedDialects::new(vec![
Box::new(BigQueryDialect {}),
Box::new(GenericDialect {}),
])
}

#[test]
Expand Down
13 changes: 5 additions & 8 deletions tests/sqlparser_clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1613,15 +1613,12 @@ fn parse_explain_table() {
}

fn clickhouse() -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(ClickHouseDialect {})],
options: None,
}
TestedDialects::new(vec![Box::new(ClickHouseDialect {})])
}

fn clickhouse_and_generic() -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(ClickHouseDialect {}), Box::new(GenericDialect {})],
options: None,
}
TestedDialects::new(vec![
Box::new(ClickHouseDialect {}),
Box::new(GenericDialect {}),
])
}
Loading

0 comments on commit e474fd2

Please sign in to comment.