Skip to content

Commit

Permalink
Use new MustNotInsert test assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-farries committed Jan 18, 2024
1 parent 61fe965 commit d630699
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 25 deletions.
21 changes: 11 additions & 10 deletions pkg/migrations/op_add_column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/assert"

"github.com/xataio/pgroll/pkg/migrations"
"github.com/xataio/pgroll/pkg/testutils"
)

func TestAddColumn(t *testing.T) {
Expand Down Expand Up @@ -184,10 +185,10 @@ func TestAddForeignKeyColumn(t *testing.T) {
})

// Inserting a row into the referencing table fails as the referenced row does not exist.
MustNotInsert(t, db, "public", "02_create_table_with_fk", "orders", map[string]string{
MustNotInsert(t, db, "public", "02_add_column", "orders", map[string]string{
"user_id": "2",
"quantity": "200",
})
}, testutils.FKViolationErrorCode)
},
afterRollback: func(t *testing.T, db *sql.DB) {
// The new column has been dropped, so the foreign key constraint is gone.
Expand All @@ -211,7 +212,7 @@ func TestAddForeignKeyColumn(t *testing.T) {
MustNotInsert(t, db, "public", "02_add_column", "orders", map[string]string{
"user_id": "3",
"quantity": "300",
})
}, testutils.FKViolationErrorCode)
},
},
{
Expand Down Expand Up @@ -287,10 +288,10 @@ func TestAddForeignKeyColumn(t *testing.T) {
})

// Inserting a row into the referencing table fails as the referenced row does not exist.
MustNotInsert(t, db, "public", "02_create_table_with_fk", "orders", map[string]string{
MustNotInsert(t, db, "public", "02_add_column", "orders", map[string]string{
"user_id": "2",
"quantity": "200",
})
}, testutils.FKViolationErrorCode)
},
afterRollback: func(t *testing.T, db *sql.DB) {
// The new column has been dropped, so the foreign key constraint is gone.
Expand All @@ -314,7 +315,7 @@ func TestAddForeignKeyColumn(t *testing.T) {
MustNotInsert(t, db, "public", "02_add_column", "orders", map[string]string{
"user_id": "3",
"quantity": "300",
})
}, testutils.FKViolationErrorCode)
},
},
})
Expand Down Expand Up @@ -542,7 +543,7 @@ func TestAddNotNullColumnWithNoDefault(t *testing.T) {
// Inserting a null description through the new view fails.
MustNotInsert(t, db, "public", "02_add_column", "products", map[string]string{
"name": "banana",
})
}, testutils.CheckViolationErrorCode)
},
afterRollback: func(t *testing.T, db *sql.DB) {
// the check constraint has been dropped.
Expand All @@ -557,7 +558,7 @@ func TestAddNotNullColumnWithNoDefault(t *testing.T) {
// 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{
"name": "orange",
})
}, testutils.NotNullViolationErrorCode)
},
}})
}
Expand Down Expand Up @@ -733,7 +734,7 @@ func TestAddColumnWithCheckConstraint(t *testing.T) {
MustNotInsert(t, db, "public", "02_add_column", "users", map[string]string{
"name": "bob",
"age": "3",
})
}, testutils.CheckViolationErrorCode)
},
afterRollback: func(t *testing.T, db *sql.DB) {
},
Expand All @@ -748,7 +749,7 @@ func TestAddColumnWithCheckConstraint(t *testing.T) {
MustNotInsert(t, db, "public", "02_add_column", "users", map[string]string{
"name": "dana",
"age": "3",
})
}, testutils.CheckViolationErrorCode)
},
}})
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/migrations/op_create_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/xataio/pgroll/pkg/migrations"
"github.com/xataio/pgroll/pkg/testutils"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -145,7 +146,7 @@ func TestCreateTable(t *testing.T) {
MustNotInsert(t, db, "public", "02_create_table_with_fk", "orders", map[string]string{
"user_id": "2",
"quantity": "200",
})
}, testutils.FKViolationErrorCode)
},
afterRollback: func(t *testing.T, db *sql.DB) {
// The table has been dropped, so the foreign key constraint is gone.
Expand All @@ -168,7 +169,7 @@ func TestCreateTable(t *testing.T) {
MustNotInsert(t, db, "public", "02_create_table_with_fk", "orders", map[string]string{
"user_id": "3",
"quantity": "300",
})
}, testutils.FKViolationErrorCode)
},
},
{
Expand Down Expand Up @@ -210,7 +211,7 @@ func TestCreateTable(t *testing.T) {
// Inserting a row into the table fails when the check constraint is not satisfied.
MustNotInsert(t, db, "public", "01_create_table", "users", map[string]string{
"name": "b",
})
}, testutils.CheckViolationErrorCode)
},
afterRollback: func(t *testing.T, db *sql.DB) {
// The table has been dropped, so the check constraint is gone.
Expand All @@ -227,7 +228,7 @@ func TestCreateTable(t *testing.T) {
// Inserting a row into the table fails when the check constraint is not satisfied.
MustNotInsert(t, db, "public", "01_create_table", "users", map[string]string{
"name": "c",
})
}, testutils.CheckViolationErrorCode)
},
},
{
Expand Down
7 changes: 4 additions & 3 deletions pkg/migrations/op_drop_constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/xataio/pgroll/pkg/migrations"
"github.com/xataio/pgroll/pkg/testutils"
)

func TestDropConstraint(t *testing.T) {
Expand Down Expand Up @@ -75,7 +76,7 @@ func TestDropConstraint(t *testing.T) {
// Inserting a row that does not meet the check constraint into the old view fails.
MustNotInsert(t, db, "public", "02_add_check_constraint", "posts", map[string]string{
"title": "b",
})
}, testutils.CheckViolationErrorCode)

// The inserted row has been backfilled into the new view.
rows := MustSelect(t, db, "public", "03_drop_check_constraint", "posts")
Expand Down Expand Up @@ -320,7 +321,7 @@ func TestDropConstraint(t *testing.T) {
MustNotInsert(t, db, "public", "02_add_fk_constraint", "posts", map[string]string{
"title": "post by unknown user",
"user_id": "3",
})
}, testutils.FKViolationErrorCode)

// The post that was inserted successfully has been backfilled into the new view.
rows = MustSelect(t, db, "public", "03_drop_fk_constraint", "posts")
Expand Down Expand Up @@ -422,7 +423,7 @@ func TestDropConstraint(t *testing.T) {
// Inserting a row that does not meet the unique constraint into the old view fails.
MustNotInsert(t, db, "public", "01_add_tables", "users", map[string]string{
"name": "alice",
})
}, testutils.UniqueViolationErrorCode)

// Inserting a row that does not meet the unique constraint into the new view works.
MustInsert(t, db, "public", "02_drop_unique_constraint", "users", map[string]string{
Expand Down
5 changes: 3 additions & 2 deletions pkg/migrations/op_set_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/xataio/pgroll/pkg/migrations"
"github.com/xataio/pgroll/pkg/testutils"
)

func TestSetCheckConstraint(t *testing.T) {
Expand Down Expand Up @@ -85,7 +86,7 @@ func TestSetCheckConstraint(t *testing.T) {
// Inserting a row that does not meet the check constraint into the new view fails.
MustNotInsert(t, db, "public", "02_add_check_constraint", "posts", map[string]string{
"title": "d",
})
}, testutils.CheckViolationErrorCode)

// The row that was inserted into the new view has been backfilled into the old view.
rows = MustSelect(t, db, "public", "01_add_table", "posts")
Expand Down Expand Up @@ -124,7 +125,7 @@ func TestSetCheckConstraint(t *testing.T) {
// Inserting a row that does not meet the check constraint into the new view fails.
MustNotInsert(t, db, "public", "02_add_check_constraint", "posts", map[string]string{
"title": "e",
})
}, testutils.CheckViolationErrorCode)

// The data in the new `posts` view is as expected.
rows := MustSelect(t, db, "public", "02_add_check_constraint", "posts")
Expand Down
5 changes: 3 additions & 2 deletions pkg/migrations/op_set_fk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/xataio/pgroll/pkg/migrations"
"github.com/xataio/pgroll/pkg/testutils"
)

func TestSetForeignKey(t *testing.T) {
Expand Down Expand Up @@ -96,7 +97,7 @@ func TestSetForeignKey(t *testing.T) {
MustNotInsert(t, db, "public", "02_add_fk_constraint", "posts", map[string]string{
"title": "post by unknown user",
"user_id": "3",
})
}, testutils.FKViolationErrorCode)

// The post that was inserted successfully has been backfilled into the old view.
rows := MustSelect(t, db, "public", "01_add_tables", "posts")
Expand Down Expand Up @@ -156,7 +157,7 @@ func TestSetForeignKey(t *testing.T) {
MustNotInsert(t, db, "public", "02_add_fk_constraint", "posts", map[string]string{
"title": "post by unknown user",
"user_id": "3",
})
}, testutils.FKViolationErrorCode)

// The data in the new `posts` view is as expected.
rows := MustSelect(t, db, "public", "02_add_fk_constraint", "posts")
Expand Down
5 changes: 3 additions & 2 deletions pkg/migrations/op_set_notnull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/xataio/pgroll/pkg/migrations"
"github.com/xataio/pgroll/pkg/testutils"
)

func TestSetNotNull(t *testing.T) {
Expand Down Expand Up @@ -67,7 +68,7 @@ func TestSetNotNull(t *testing.T) {
MustNotInsert(t, db, "public", "02_set_nullable", "reviews", map[string]string{
"username": "alice",
"product": "apple",
})
}, testutils.CheckViolationErrorCode)

// Inserting a non-NULL value into the new `review` column should succeed
MustInsert(t, db, "public", "02_set_nullable", "reviews", map[string]string{
Expand Down Expand Up @@ -143,7 +144,7 @@ func TestSetNotNull(t *testing.T) {
MustNotInsert(t, db, "public", "02_set_nullable", "reviews", map[string]string{
"username": "daisy",
"product": "durian",
})
}, testutils.NotNullViolationErrorCode)

// The up function no longer exists.
FunctionMustNotExist(t, db, "public", migrations.TriggerFunctionName("reviews", "review"))
Expand Down
5 changes: 3 additions & 2 deletions pkg/migrations/op_set_unique_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/xataio/pgroll/pkg/migrations"
"github.com/xataio/pgroll/pkg/testutils"
)

func TestSetColumnUnique(t *testing.T) {
Expand Down Expand Up @@ -76,7 +77,7 @@ func TestSetColumnUnique(t *testing.T) {
})
MustNotInsert(t, db, "public", "02_set_unique", "reviews", map[string]string{
"username": "dana", "product": "durian", "review": "bad",
})
}, testutils.UniqueViolationErrorCode)
},
afterRollback: func(t *testing.T, db *sql.DB) {
// The new (temporary) `review` column should not exist on the underlying table.
Expand Down Expand Up @@ -112,7 +113,7 @@ func TestSetColumnUnique(t *testing.T) {
})
MustNotInsert(t, db, "public", "02_set_unique", "reviews", map[string]string{
"username": "flora", "product": "fig", "review": "ok",
})
}, testutils.UniqueViolationErrorCode)
},
},
{
Expand Down

0 comments on commit d630699

Please sign in to comment.