Skip to content

Commit 1c364cb

Browse files
Merged and simplified match test
1 parent dd61527 commit 1c364cb

File tree

1 file changed

+28
-102
lines changed

1 file changed

+28
-102
lines changed

tests/sqlparser_postgres.rs

Lines changed: 28 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -6649,117 +6649,43 @@ fn parse_alter_schema() {
66496649
}
66506650

66516651
#[test]
6652-
fn parse_foreign_key_match_full() {
6653-
let sql = "CREATE TABLE orders (order_id INT PRIMARY KEY REFERENCES another_table (id) MATCH FULL, customer_id INT, FOREIGN KEY (customer_id) REFERENCES customers(customer_id) MATCH FULL)";
6654-
let statement = pg_and_generic().verified_stmt(sql);
6655-
match statement {
6656-
Statement::CreateTable(CreateTable {
6657-
columns,
6658-
constraints,
6659-
..
6660-
}) => {
6661-
// Check column-level foreign key with MATCH FULL
6662-
assert_eq!(columns[0].name.value, "order_id");
6663-
match &columns[0].options[1].option {
6664-
ColumnOption::ForeignKey(constraint) => {
6665-
assert_eq!(constraint.foreign_table.to_string(), "another_table");
6666-
assert_eq!(
6667-
constraint.match_kind,
6668-
Some(ConstraintReferenceMatchKind::Full)
6669-
);
6670-
}
6671-
_ => panic!("Expected ColumnOption::ForeignKey"),
6672-
}
6673-
6674-
// Check table-level foreign key constraint with MATCH FULL
6675-
match &constraints[0] {
6676-
TableConstraint::ForeignKey(constraint) => {
6677-
assert_eq!(constraint.foreign_table.to_string(), "customers");
6678-
assert_eq!(
6679-
constraint.match_kind,
6680-
Some(ConstraintReferenceMatchKind::Full)
6681-
);
6682-
}
6683-
_ => panic!("Expected TableConstraint::ForeignKey"),
6684-
}
6685-
}
6686-
_ => unreachable!("{:?} should parse to Statement::CreateTable", sql),
6687-
}
6688-
}
6689-
6690-
#[test]
6691-
fn parse_foreign_key_match_simple() {
6692-
let sql = "CREATE TABLE orders (order_id INT PRIMARY KEY REFERENCES another_table (id) MATCH SIMPLE, customer_id INT, FOREIGN KEY (customer_id) REFERENCES customers(customer_id) MATCH SIMPLE)";
6693-
let statement = pg_and_generic().verified_stmt(sql);
6694-
match statement {
6695-
Statement::CreateTable(CreateTable {
6696-
columns,
6697-
constraints,
6698-
..
6699-
}) => {
6700-
// Check column-level foreign key with MATCH SIMPLE
6701-
assert_eq!(columns[0].name.value, "order_id");
6702-
match &columns[0].options[1].option {
6703-
ColumnOption::ForeignKey(constraint) => {
6704-
assert_eq!(constraint.foreign_table.to_string(), "another_table");
6705-
assert_eq!(
6706-
constraint.match_kind,
6707-
Some(ConstraintReferenceMatchKind::Simple)
6708-
);
6709-
}
6710-
_ => panic!("Expected ColumnOption::ForeignKey"),
6711-
}
6652+
fn parse_foreign_key_match() {
6653+
let test_cases = [
6654+
("MATCH FULL", ConstraintReferenceMatchKind::Full),
6655+
("MATCH SIMPLE", ConstraintReferenceMatchKind::Simple),
6656+
("MATCH PARTIAL", ConstraintReferenceMatchKind::Partial),
6657+
];
67126658

6713-
// Check table-level foreign key constraint with MATCH SIMPLE
6714-
match &constraints[0] {
6715-
TableConstraint::ForeignKey(constraint) => {
6716-
assert_eq!(constraint.foreign_table.to_string(), "customers");
6717-
assert_eq!(
6718-
constraint.match_kind,
6719-
Some(ConstraintReferenceMatchKind::Simple)
6720-
);
6659+
for (match_clause, expected_kind) in test_cases {
6660+
// Test column-level foreign key
6661+
let sql = format!("CREATE TABLE t (id INT REFERENCES other_table (id) {match_clause})");
6662+
let statement = pg_and_generic().verified_stmt(&sql);
6663+
match statement {
6664+
Statement::CreateTable(CreateTable { columns, .. }) => {
6665+
match &columns[0].options[0].option {
6666+
ColumnOption::ForeignKey(constraint) => {
6667+
assert_eq!(constraint.match_kind, Some(expected_kind));
6668+
}
6669+
_ => panic!("Expected ColumnOption::ForeignKey"),
67216670
}
6722-
_ => panic!("Expected TableConstraint::ForeignKey"),
67236671
}
6672+
_ => unreachable!("{:?} should parse to Statement::CreateTable", sql),
67246673
}
6725-
_ => unreachable!("{:?} should parse to Statement::CreateTable", sql),
6726-
}
6727-
}
6728-
6729-
#[test]
6730-
fn parse_foreign_key_match_partial() {
6731-
let sql = "CREATE TABLE orders (order_id INT PRIMARY KEY REFERENCES another_table (id) MATCH PARTIAL, customer_id INT, FOREIGN KEY (customer_id) REFERENCES customers(customer_id) MATCH PARTIAL)";
6732-
let statement = pg_and_generic().verified_stmt(sql);
6733-
match statement {
6734-
Statement::CreateTable(CreateTable {
6735-
columns,
6736-
constraints,
6737-
..
6738-
}) => {
6739-
assert_eq!(columns[0].name.value, "order_id");
6740-
match &columns[0].options[1].option {
6741-
ColumnOption::ForeignKey(constraint) => {
6742-
assert_eq!(constraint.foreign_table.to_string(), "another_table");
6743-
assert_eq!(
6744-
constraint.match_kind,
6745-
Some(ConstraintReferenceMatchKind::Partial)
6746-
);
6747-
}
6748-
_ => panic!("Expected ColumnOption::ForeignKey"),
6749-
}
67506674

6751-
match &constraints[0] {
6675+
// Test table-level foreign key constraint
6676+
let sql = format!(
6677+
"CREATE TABLE t (id INT, FOREIGN KEY (id) REFERENCES other_table(id) {match_clause})"
6678+
);
6679+
let statement = pg_and_generic().verified_stmt(&sql);
6680+
match statement {
6681+
Statement::CreateTable(CreateTable { constraints, .. }) => match &constraints[0] {
67526682
TableConstraint::ForeignKey(constraint) => {
6753-
assert_eq!(constraint.foreign_table.to_string(), "customers");
6754-
assert_eq!(
6755-
constraint.match_kind,
6756-
Some(ConstraintReferenceMatchKind::Partial)
6757-
);
6683+
assert_eq!(constraint.match_kind, Some(expected_kind));
67586684
}
67596685
_ => panic!("Expected TableConstraint::ForeignKey"),
6760-
}
6686+
},
6687+
_ => unreachable!("{:?} should parse to Statement::CreateTable", sql),
67616688
}
6762-
_ => unreachable!("{:?} should parse to Statement::CreateTable", sql),
67636689
}
67646690
}
67656691

0 commit comments

Comments
 (0)