Skip to content

Commit

Permalink
Update tests for set unique operation
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-farries committed Sep 21, 2023
1 parent e33a559 commit 36271b8
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions pkg/migrations/op_set_unique_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/xataio/pg-roll/pkg/migrations"
)

func TestSetColumnsUnique(t *testing.T) {
func TestSetColumnUnique(t *testing.T) {
t.Parallel()

ExecuteTests(t, TestCases{{
Expand Down Expand Up @@ -46,48 +46,50 @@ func TestSetColumnsUnique(t *testing.T) {
{
Name: "02_set_unique",
Operations: migrations.Operations{
&migrations.OpSetUnique{
Name: "reviews_username_product_unique",
Table: "reviews",
Columns: []string{"username", "product"},
&migrations.OpAlterColumn{
Table: "reviews",
Column: "review",
Unique: &migrations.UniqueConstraint{
Name: "reviews_review_unique",
},
},
},
},
},
afterStart: func(t *testing.T, db *sql.DB) {
// The unique index has been created on the underlying table.
IndexMustExist(t, db, "public", "reviews", "reviews_username_product_unique")
IndexMustExist(t, db, "public", "reviews", "reviews_review_unique")

// Inserting values into the old schema that violate uniqueness should fail.
MustInsert(t, db, "public", "01_add_table", "reviews", map[string]string{
"username": "alice", "product": "apple", "review": "good",
})
MustNotInsert(t, db, "public", "01_add_table", "reviews", map[string]string{
"username": "alice", "product": "apple", "review": "bad",
"username": "bob", "product": "banana", "review": "good",
})

// Inserting values into the new schema that violate uniqueness should fail.
MustInsert(t, db, "public", "02_set_unique", "reviews", map[string]string{
"username": "bob", "product": "orange", "review": "good",
"username": "carl", "product": "carrot", "review": "bad",
})
MustNotInsert(t, db, "public", "02_set_unique", "reviews", map[string]string{
"username": "bob", "product": "orange", "review": "bad",
"username": "dana", "product": "durian", "review": "bad",
})
},
afterRollback: func(t *testing.T, db *sql.DB) {
// The unique index has been dropped from the the underlying table.
IndexMustNotExist(t, db, "public", "reviews", "reviews_username_product_unique")
IndexMustNotExist(t, db, "public", "reviews", "reviews_review_unique")
},
afterComplete: func(t *testing.T, db *sql.DB) {
// The unique constraint has been created on the underlying table.
ConstraintMustExist(t, db, "public", "reviews", "reviews_username_product_unique")
ConstraintMustExist(t, db, "public", "reviews", "reviews_review_unique")

// Inserting values into the new schema that violate uniqueness should fail.
MustInsert(t, db, "public", "02_set_unique", "reviews", map[string]string{
"username": "carl", "product": "banana", "review": "good",
"username": "earl", "product": "elderberry", "review": "ok",
})
MustNotInsert(t, db, "public", "02_set_unique", "reviews", map[string]string{
"username": "carl", "product": "banana", "review": "bad",
"username": "flora", "product": "fig", "review": "ok",
})
},
}})
Expand Down

0 comments on commit 36271b8

Please sign in to comment.