@@ -6649,117 +6649,43 @@ fn parse_alter_schema() {
6649
6649
}
6650
6650
6651
6651
#[ 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
+ ] ;
6712
6658
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" ) ,
6721
6670
}
6722
- _ => panic ! ( "Expected TableConstraint::ForeignKey" ) ,
6723
6671
}
6672
+ _ => unreachable ! ( "{:?} should parse to Statement::CreateTable" , sql) ,
6724
6673
}
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
- }
6750
6674
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 ] {
6752
6682
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) ) ;
6758
6684
}
6759
6685
_ => panic ! ( "Expected TableConstraint::ForeignKey" ) ,
6760
- }
6686
+ } ,
6687
+ _ => unreachable ! ( "{:?} should parse to Statement::CreateTable" , sql) ,
6761
6688
}
6762
- _ => unreachable ! ( "{:?} should parse to Statement::CreateTable" , sql) ,
6763
6689
}
6764
6690
}
6765
6691
0 commit comments