Skip to content

Commit 15ede2f

Browse files
committed
Fix postgres test to support CURRENT_USER, CURRENT_ROLE, SESSION_USER
1 parent b94b692 commit 15ede2f

File tree

1 file changed

+67
-20
lines changed

1 file changed

+67
-20
lines changed

tests/sqlparser_postgres.rs

+67-20
Original file line numberDiff line numberDiff line change
@@ -715,29 +715,76 @@ fn parse_alter_table_add_columns() {
715715

716716
#[test]
717717
fn parse_alter_table_owner_to() {
718-
match pg().verified_stmt("ALTER TABLE tab OWNER TO new_owner") {
719-
Statement::AlterTable {
720-
name,
721-
if_exists: _,
722-
only: _,
723-
operations,
724-
location: _,
725-
} => {
726-
assert_eq!(name.to_string(), "tab");
727-
assert_eq!(
718+
struct TestCase {
719+
sql: &'static str,
720+
expected_owner: Owner,
721+
}
722+
723+
let test_cases = vec![
724+
TestCase {
725+
sql: "ALTER TABLE tab OWNER TO new_owner",
726+
expected_owner: Owner::Ident(Ident::new("new_owner".to_string())),
727+
},
728+
TestCase {
729+
sql: "ALTER TABLE tab OWNER TO \"new_owner\"",
730+
expected_owner: Owner::Ident(Ident::with_quote('\"', "new_owner".to_string())),
731+
},
732+
TestCase {
733+
sql: "ALTER TABLE tab OWNER TO CURRENT_USER",
734+
expected_owner: Owner::Expr(Expr::Function(Function {
735+
name: ObjectName(vec![Ident::new("CURRENT_USER")]),
736+
args: FunctionArguments::None,
737+
null_treatment: None,
738+
filter: None,
739+
over: None,
740+
within_group: vec![],
741+
})),
742+
},
743+
TestCase {
744+
sql: "ALTER TABLE tab OWNER TO CURRENT_ROLE",
745+
expected_owner: Owner::Expr(Expr::Function(Function {
746+
name: ObjectName(vec![Ident::new("CURRENT_ROLE")]),
747+
args: FunctionArguments::None,
748+
null_treatment: None,
749+
filter: None,
750+
over: None,
751+
within_group: vec![],
752+
})),
753+
},
754+
TestCase {
755+
sql: "ALTER TABLE tab OWNER TO SESSION_USER",
756+
expected_owner: Owner::Expr(Expr::Function(Function {
757+
name: ObjectName(vec![Ident::new("SESSION_USER")]),
758+
args: FunctionArguments::None,
759+
null_treatment: None,
760+
filter: None,
761+
over: None,
762+
within_group: vec![],
763+
})),
764+
},
765+
];
766+
767+
for case in test_cases {
768+
match pg().verified_stmt(case.sql) {
769+
Statement::AlterTable {
770+
name,
771+
if_exists: _,
772+
only: _,
728773
operations,
729-
vec![
730-
AlterTableOperation::OwnerTo {
731-
new_role: Ident {
732-
value: "new_owner".to_string(),
733-
quote_style: None,
734-
}
735-
},
736-
]
737-
);
774+
location: _,
775+
} => {
776+
assert_eq!(name.to_string(), "tab");
777+
assert_eq!(operations, vec![AlterTableOperation::OwnerTo { new_owner: case.expected_owner.clone() }]);
778+
}
779+
_ => unreachable!("Expected an AlterTable statement"),
738780
}
739-
_ => unreachable!(),
740781
}
782+
783+
let res = pg().parse_sql_statements("ALTER TABLE tab OWNER TO CREATE");
784+
assert_eq!(
785+
ParserError::ParserError("Expected CURRENT_USER, CURRENT_ROLE, SESSION_USER or identifier after OWNER TO clause, found: CREATE".to_string()),
786+
res.unwrap_err()
787+
);
741788
}
742789

743790
#[test]

0 commit comments

Comments
 (0)