use spg_engine::Engine;
fn err(e: &mut Engine, sql: &str) -> String {
format!("{}", e.execute(sql).unwrap_err())
}
fn ok(e: &mut Engine, sql: &str) {
e.execute(sql)
.unwrap_or_else(|err| panic!("{sql}: {err:?}"));
}
#[test]
fn second_primary_key_is_rejected() {
let mut e = Engine::new();
ok(&mut e, "CREATE TABLE t3(a int PRIMARY KEY)");
assert!(
err(&mut e, "ALTER TABLE t3 ADD PRIMARY KEY (a)")
.contains("multiple primary keys for table \"t3\" are not allowed")
);
ok(&mut e, "CREATE TABLE t4(a int)");
ok(&mut e, "ALTER TABLE t4 ADD PRIMARY KEY (a)");
assert!(
err(&mut e, "ALTER TABLE t4 ADD PRIMARY KEY (a)")
.contains("multiple primary keys for table \"t4\" are not allowed")
);
}
#[test]
fn ddl_object_error_wording_matches_pg() {
let mut e = Engine::new();
ok(&mut e, "CREATE TABLE t1(a int, b text)");
assert!(
err(&mut e, "ALTER TABLE t1 ADD COLUMN a int")
.contains("column \"a\" of relation \"t1\" already exists")
);
assert!(
err(&mut e, "ALTER TABLE t1 DROP COLUMN nope")
.contains("column \"nope\" of relation \"t1\" does not exist")
);
assert!(
err(&mut e, "DROP TABLE nonexist_tbl").contains("table \"nonexist_tbl\" does not exist")
);
}
#[test]
fn identity_generated_always_error_has_detail_hint() {
let mut e = Engine::new();
ok(
&mut e,
"CREATE TABLE idt(a int GENERATED ALWAYS AS IDENTITY, b text)",
);
ok(&mut e, "INSERT INTO idt(b) VALUES ('x')");
let msg = err(&mut e, "INSERT INTO idt(a,b) VALUES (100,'z')");
assert!(msg.contains("cannot insert a non-DEFAULT value into column \"a\""));
assert!(
msg.contains("DETAIL: Column \"a\" is an identity column defined as GENERATED ALWAYS.")
);
assert!(msg.contains("HINT: Use OVERRIDING SYSTEM VALUE to override."));
}
#[test]
fn drop_if_exists_is_silent() {
let mut e = Engine::new();
ok(&mut e, "DROP TABLE IF EXISTS nonexist_tbl");
ok(&mut e, "CREATE TABLE t5(a int, b int)");
ok(&mut e, "ALTER TABLE t5 DROP COLUMN IF EXISTS nope");
}