Skip to content

Commit

Permalink
Rename ConstraintMust -> CheckConstraintMust
Browse files Browse the repository at this point in the history
Change the assertion to look for `CHECK` constraints and rename the
assertion functions accordingly.
  • Loading branch information
andrew-farries committed Jan 17, 2024
1 parent 92963bd commit 5c4cad9
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
4 changes: 2 additions & 2 deletions pkg/migrations/op_add_column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,12 +547,12 @@ func TestAddNotNullColumnWithNoDefault(t *testing.T) {
afterRollback: func(t *testing.T, db *sql.DB) {
// the check constraint has been dropped.
constraintName := migrations.NotNullConstraintName("description")
ConstraintMustNotExist(t, db, "public", "products", constraintName)
CheckConstraintMustNotExist(t, db, "public", "products", constraintName)
},
afterComplete: func(t *testing.T, db *sql.DB) {
// the check constraint has been dropped.
constraintName := migrations.NotNullConstraintName("description")
ConstraintMustNotExist(t, db, "public", "products", constraintName)
CheckConstraintMustNotExist(t, db, "public", "products", constraintName)

// can't insert a null description into the new view; the column now has a NOT NULL constraint.
MustNotInsert(t, db, "public", "02_add_column", "products", map[string]string{
Expand Down
11 changes: 6 additions & 5 deletions pkg/migrations/op_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,16 @@ func TriggerMustExist(t *testing.T, db *sql.DB, schema, table, trigger string) {
}
}

func ConstraintMustNotExist(t *testing.T, db *sql.DB, schema, table, constraint string) {
func CheckConstraintMustNotExist(t *testing.T, db *sql.DB, schema, table, constraint string) {
t.Helper()
if constraintExists(t, db, schema, table, constraint) {
if checkConstraintExists(t, db, schema, table, constraint) {
t.Fatalf("Expected constraint %q to not exist", constraint)
}
}

func ConstraintMustExist(t *testing.T, db *sql.DB, schema, table, constraint string) {
func CheckConstraintMustExist(t *testing.T, db *sql.DB, schema, table, constraint string) {
t.Helper()
if !constraintExists(t, db, schema, table, constraint) {
if !checkConstraintExists(t, db, schema, table, constraint) {
t.Fatalf("Expected constraint %q to exist", constraint)
}
}
Expand Down Expand Up @@ -278,7 +278,7 @@ func indexExists(t *testing.T, db *sql.DB, schema, table, index string) bool {
return exists
}

func constraintExists(t *testing.T, db *sql.DB, schema, table, constraint string) bool {
func checkConstraintExists(t *testing.T, db *sql.DB, schema, table, constraint string) bool {
t.Helper()

var exists bool
Expand All @@ -288,6 +288,7 @@ func constraintExists(t *testing.T, db *sql.DB, schema, table, constraint string
FROM pg_catalog.pg_constraint
WHERE conrelid = $1::regclass
AND conname = $2
AND contype = 'c'
)`,
fmt.Sprintf("%s.%s", schema, table), constraint).Scan(&exists)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/migrations/op_create_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func TestCreateTable(t *testing.T) {
},
afterStart: func(t *testing.T, db *sql.DB) {
// The check constraint exists on the new table.
ConstraintMustExist(t, db, "public", migrations.TemporaryName("users"), "check_name_length")
CheckConstraintMustExist(t, db, "public", migrations.TemporaryName("users"), "check_name_length")

// Inserting a row into the table succeeds when the check constraint is satisfied.
MustInsert(t, db, "public", "01_create_table", "users", map[string]string{
Expand All @@ -217,7 +217,7 @@ func TestCreateTable(t *testing.T) {
},
afterComplete: func(t *testing.T, db *sql.DB) {
// The check constraint exists on the new table.
ConstraintMustExist(t, db, "public", "users", "check_name_length")
CheckConstraintMustExist(t, db, "public", "users", "check_name_length")

// Inserting a row into the table succeeds when the check constraint is satisfied.
MustInsert(t, db, "public", "01_create_table", "users", map[string]string{
Expand Down
6 changes: 3 additions & 3 deletions pkg/migrations/op_set_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestSetCheckConstraint(t *testing.T) {
ColumnMustExist(t, db, "public", "posts", migrations.TemporaryName("title"))

// A check constraint has been added to the temporary column
ConstraintMustExist(t, db, "public", "posts", "check_title_length")
CheckConstraintMustExist(t, db, "public", "posts", "check_title_length")

// Inserting a row that meets the check constraint into the old view works.
MustInsert(t, db, "public", "01_add_table", "posts", map[string]string{
Expand Down Expand Up @@ -100,7 +100,7 @@ func TestSetCheckConstraint(t *testing.T) {
ColumnMustNotExist(t, db, "public", "posts", migrations.TemporaryName("title"))

// The check constraint no longer exists.
ConstraintMustNotExist(t, db, "public", "posts", "check_title_length")
CheckConstraintMustNotExist(t, db, "public", "posts", "check_title_length")

// The up function no longer exists.
FunctionMustNotExist(t, db, "public", migrations.TriggerFunctionName("posts", "title"))
Expand All @@ -114,7 +114,7 @@ func TestSetCheckConstraint(t *testing.T) {
},
afterComplete: func(t *testing.T, db *sql.DB) {
// The check constraint exists on the new table.
ConstraintMustExist(t, db, "public", "posts", "check_title_length")
CheckConstraintMustExist(t, db, "public", "posts", "check_title_length")

// Inserting a row that meets the check constraint into the new view works.
MustInsert(t, db, "public", "02_add_check_constraint", "posts", map[string]string{
Expand Down

0 comments on commit 5c4cad9

Please sign in to comment.