From 515463f8e66859de44ab084dcb27da07d9563e9a Mon Sep 17 00:00:00 2001 From: sivchari Date: Wed, 25 May 2022 02:31:29 +0900 Subject: [PATCH] Add some tests --- tests/sqlparser_common.rs | 65 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 5 deletions(-) diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index cf91bdc81..e3d12bf18 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -2779,13 +2779,23 @@ fn parse_table_function() { #[test] fn parse_unnest() { + fn chk(alias: bool, with_offset: bool, dialects: &TestedDialects, want: Vec) { + 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 { @@ -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![], + }], ); }