Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sivchari committed May 24, 2022
1 parent 4c5021f commit 515463f
Showing 1 changed file with 60 additions and 5 deletions.
65 changes: 60 additions & 5 deletions tests/sqlparser_common.rs
Expand Up @@ -2779,13 +2779,23 @@ fn parse_table_function() {

#[test]
fn parse_unnest() {
fn chk(alias: bool, with_offset: bool, dialects: &TestedDialects, want: Vec<TableWithJoins>) {
let sql = &format!(
"SELECT * FROM UNNEST(expr){}{}",
if alias { " AS numbers" } else { "" },
if with_offset { " WITH OFFSET" } else { "" },
);
let select = dialects.verified_only_select(sql);
assert_eq!(select.from, want);
}
let dialects = TestedDialects {
dialects: vec![Box::new(BigQueryDialect {}), Box::new(GenericDialect {})],
};
let sql = "SELECT * FROM UNNEST(expr) AS numbers WITH OFFSET";
let select = dialects.verified_only_select(sql);
assert_eq!(
select.from,
// 1. both Alias and WITH OFFSET clauses.
chk(
true,
true,
&dialects,
vec![TableWithJoins {
relation: TableFactor::UNNEST {
alias: Some(TableAlias {
Expand All @@ -2796,7 +2806,52 @@ fn parse_unnest() {
with_offset: true,
},
joins: vec![],
}]
}],
);
// 2. neither Alias nor WITH OFFSET clause.
chk(
false,
false,
&dialects,
vec![TableWithJoins {
relation: TableFactor::UNNEST {
alias: None,
array_expr: Box::new(Expr::Identifier(Ident::new("expr"))),
with_offset: false,
},
joins: vec![],
}],
);
// 3. Alias but no WITH OFFSET clause.
chk(
false,
true,
&dialects,
vec![TableWithJoins {
relation: TableFactor::UNNEST {
alias: None,
array_expr: Box::new(Expr::Identifier(Ident::new("expr"))),
with_offset: true,
},
joins: vec![],
}],
);
// 4. WITH OFFSET but no Alias.
chk(
true,
false,
&dialects,
vec![TableWithJoins {
relation: TableFactor::UNNEST {
alias: Some(TableAlias {
name: Ident::new("numbers"),
columns: vec![],
}),
array_expr: Box::new(Expr::Identifier(Ident::new("expr"))),
with_offset: false,
},
joins: vec![],
}],
);
}

Expand Down

0 comments on commit 515463f

Please sign in to comment.