check_fk_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package executor
  2. import (
  3. "errors"
  4. "testing"
  5. "github.com/danfragoso/pizzasql-next/pkg/pgerror"
  6. )
  7. func TestCheckConstraintsRejectedAtDDL(t *testing.T) {
  8. _, schema, table := newTestDB(t)
  9. e := newExec(schema, table)
  10. for _, sql := range []string{
  11. "CREATE TABLE t1 (id INTEGER PRIMARY KEY, age INTEGER CHECK (age > 0))",
  12. "CREATE TABLE t2 (id INTEGER PRIMARY KEY, age INTEGER, CHECK (age > 0))",
  13. "CREATE TABLE t3 (id INTEGER PRIMARY KEY, age INTEGER, CONSTRAINT positive_age CHECK (age > 0))",
  14. } {
  15. if _, err := execSQL(e, sql); !errors.Is(err, pgerror.ErrFeatureNotSupported) {
  16. t.Fatalf("%s: error = %v, want ErrFeatureNotSupported", sql, err)
  17. }
  18. }
  19. execMust(t, e, "CREATE TABLE t (id INTEGER PRIMARY KEY)")
  20. if _, err := execSQL(e, "ALTER TABLE t ADD COLUMN age INTEGER CHECK (age > 0)"); !errors.Is(err, pgerror.ErrFeatureNotSupported) {
  21. t.Fatalf("ALTER CHECK error = %v, want ErrFeatureNotSupported", err)
  22. }
  23. }
  24. func TestForeignKeyConstraintsRejectedAtDDL(t *testing.T) {
  25. _, schema, table := newTestDB(t)
  26. e := newExec(schema, table)
  27. execMust(t, e, "CREATE TABLE parent (id INTEGER PRIMARY KEY)")
  28. for _, sql := range []string{
  29. "CREATE TABLE child1 (id INTEGER PRIMARY KEY, parent_id INTEGER REFERENCES parent(id))",
  30. "CREATE TABLE child2 (id INTEGER PRIMARY KEY, parent_id INTEGER, FOREIGN KEY (parent_id) REFERENCES parent(id))",
  31. "CREATE TABLE child3 (id INTEGER PRIMARY KEY, parent_id INTEGER, CONSTRAINT child_parent FOREIGN KEY (parent_id) REFERENCES parent(id) ON DELETE CASCADE)",
  32. } {
  33. if _, err := execSQL(e, sql); !errors.Is(err, pgerror.ErrFeatureNotSupported) {
  34. t.Fatalf("%s: error = %v, want ErrFeatureNotSupported", sql, err)
  35. }
  36. }
  37. execMust(t, e, "CREATE TABLE t (id INTEGER PRIMARY KEY)")
  38. if _, err := execSQL(e, "ALTER TABLE t ADD COLUMN parent_id INTEGER REFERENCES parent(id)"); !errors.Is(err, pgerror.ErrFeatureNotSupported) {
  39. t.Fatalf("ALTER FK error = %v, want ErrFeatureNotSupported", err)
  40. }
  41. }