Skip to content

Commit c1227c9

Browse files
committed
test: add EXCLUDE constraint parsing tests
1 parent c021efb commit c1227c9

1 file changed

Lines changed: 134 additions & 0 deletions

File tree

tests/sqlparser_postgres.rs

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7753,3 +7753,137 @@ CONSTRAINT check_date CHECK (order_date >= '2023-01-01')\
77537753
_ => panic!("Expected CreateTable"),
77547754
}
77557755
}
7756+
7757+
#[test]
7758+
fn parse_exclude_constraint_basic() {
7759+
let sql =
7760+
"CREATE TABLE t (room INT, CONSTRAINT no_overlap EXCLUDE USING gist (room WITH =))";
7761+
match pg().verified_stmt(sql) {
7762+
Statement::CreateTable(create_table) => {
7763+
assert_eq!(1, create_table.constraints.len());
7764+
match &create_table.constraints[0] {
7765+
TableConstraint::Exclusion(c) => {
7766+
assert_eq!(c.name, Some(Ident::new("no_overlap")));
7767+
assert_eq!(c.index_method, Some(Ident::new("gist")));
7768+
assert_eq!(c.elements.len(), 1);
7769+
assert_eq!(c.elements[0].operator, "=");
7770+
assert_eq!(c.include.len(), 0);
7771+
assert!(c.where_clause.is_none());
7772+
}
7773+
other => panic!("Expected Exclusion, got {other:?}"),
7774+
}
7775+
}
7776+
_ => panic!("Expected CreateTable"),
7777+
}
7778+
}
7779+
7780+
#[test]
7781+
fn parse_exclude_constraint_multi_element() {
7782+
let sql =
7783+
"CREATE TABLE t (room INT, during INT, EXCLUDE USING gist (room WITH =, during WITH &&))";
7784+
match pg().verified_stmt(sql) {
7785+
Statement::CreateTable(create_table) => {
7786+
assert_eq!(1, create_table.constraints.len());
7787+
match &create_table.constraints[0] {
7788+
TableConstraint::Exclusion(c) => {
7789+
assert!(c.name.is_none());
7790+
assert_eq!(c.index_method, Some(Ident::new("gist")));
7791+
assert_eq!(c.elements.len(), 2);
7792+
assert_eq!(c.elements[0].operator, "=");
7793+
assert_eq!(c.elements[1].operator, "&&");
7794+
}
7795+
other => panic!("Expected Exclusion, got {other:?}"),
7796+
}
7797+
}
7798+
_ => panic!("Expected CreateTable"),
7799+
}
7800+
}
7801+
7802+
#[test]
7803+
fn parse_exclude_constraint_with_where() {
7804+
let sql =
7805+
"CREATE TABLE t (col INT, EXCLUDE USING gist (col WITH =) WHERE (col > 0))";
7806+
match pg().verified_stmt(sql) {
7807+
Statement::CreateTable(create_table) => {
7808+
assert_eq!(1, create_table.constraints.len());
7809+
match &create_table.constraints[0] {
7810+
TableConstraint::Exclusion(c) => {
7811+
assert!(c.where_clause.is_some());
7812+
}
7813+
other => panic!("Expected Exclusion, got {other:?}"),
7814+
}
7815+
}
7816+
_ => panic!("Expected CreateTable"),
7817+
}
7818+
}
7819+
7820+
#[test]
7821+
fn parse_exclude_constraint_with_include() {
7822+
let sql =
7823+
"CREATE TABLE t (col INT, EXCLUDE USING gist (col WITH =) INCLUDE (col))";
7824+
match pg().verified_stmt(sql) {
7825+
Statement::CreateTable(create_table) => {
7826+
assert_eq!(1, create_table.constraints.len());
7827+
match &create_table.constraints[0] {
7828+
TableConstraint::Exclusion(c) => {
7829+
assert_eq!(c.include, vec![Ident::new("col")]);
7830+
}
7831+
other => panic!("Expected Exclusion, got {other:?}"),
7832+
}
7833+
}
7834+
_ => panic!("Expected CreateTable"),
7835+
}
7836+
}
7837+
7838+
#[test]
7839+
fn parse_exclude_constraint_no_using() {
7840+
let sql = "CREATE TABLE t (col INT, EXCLUDE (col WITH =))";
7841+
match pg().verified_stmt(sql) {
7842+
Statement::CreateTable(create_table) => {
7843+
assert_eq!(1, create_table.constraints.len());
7844+
match &create_table.constraints[0] {
7845+
TableConstraint::Exclusion(c) => {
7846+
assert!(c.index_method.is_none());
7847+
}
7848+
other => panic!("Expected Exclusion, got {other:?}"),
7849+
}
7850+
}
7851+
_ => panic!("Expected CreateTable"),
7852+
}
7853+
}
7854+
7855+
#[test]
7856+
fn parse_exclude_constraint_deferrable() {
7857+
let sql =
7858+
"CREATE TABLE t (col INT, EXCLUDE USING gist (col WITH =) DEFERRABLE INITIALLY DEFERRED)";
7859+
match pg().verified_stmt(sql) {
7860+
Statement::CreateTable(create_table) => {
7861+
assert_eq!(1, create_table.constraints.len());
7862+
match &create_table.constraints[0] {
7863+
TableConstraint::Exclusion(c) => {
7864+
let characteristics = c.characteristics.as_ref().unwrap();
7865+
assert_eq!(characteristics.deferrable, Some(true));
7866+
assert_eq!(
7867+
characteristics.initially,
7868+
Some(DeferrableInitial::Deferred)
7869+
);
7870+
}
7871+
other => panic!("Expected Exclusion, got {other:?}"),
7872+
}
7873+
}
7874+
_ => panic!("Expected CreateTable"),
7875+
}
7876+
}
7877+
7878+
#[test]
7879+
fn parse_exclude_constraint_in_alter_table() {
7880+
let sql =
7881+
"ALTER TABLE t ADD CONSTRAINT no_overlap EXCLUDE USING gist (room WITH =)";
7882+
pg().verified_stmt(sql);
7883+
}
7884+
7885+
#[test]
7886+
fn roundtrip_exclude_constraint() {
7887+
let sql = "CREATE TABLE t (CONSTRAINT no_overlap EXCLUDE USING gist (room WITH =, during WITH &&) INCLUDE (id) WHERE (active = true))";
7888+
pg().verified_stmt(sql);
7889+
}

0 commit comments

Comments
 (0)