Skip to content

Commit

Permalink
Make MustNotInsert stricter
Browse files Browse the repository at this point in the history
Add a stricter version of `MustNotInsert` which expects the insert
operation to fail with a specific error code.
  • Loading branch information
andrew-farries committed Jan 18, 2024
1 parent 994a1aa commit 1b5522f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
14 changes: 12 additions & 2 deletions pkg/migrations/op_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,12 +492,22 @@ func MustInsert(t *testing.T, db *sql.DB, schema, version, table string, record
}
}

func MustNotInsert(t *testing.T, db *sql.DB, schema, version, table string, record map[string]string) {
func MustNotInsert(t *testing.T, db *sql.DB, schema, version, table string, record map[string]string, errorCode string) {
t.Helper()

if err := insert(t, db, schema, version, table, record); err == nil {
err := insert(t, db, schema, version, table, record)
if err == nil {
t.Fatal("Expected INSERT to fail")
}

var pqErr *pq.Error
if ok := errors.As(err, &pqErr); ok {
if pqErr.Code.Name() != errorCode {
t.Fatalf("Expected INSERT to fail with %q, got %q", errorCode, pqErr.Code.Name())
}
} else {
t.Fatalf("INSERT failed with unknown error: %v", err)
}
}

func insert(t *testing.T, db *sql.DB, schema, version, table string, record map[string]string) error {
Expand Down
8 changes: 8 additions & 0 deletions pkg/testutils/error_codes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package testutils

const (
CheckViolationErrorCode string = "check_violation"
FKViolationErrorCode string = "foreign_key_violation"
NotNullViolationErrorCode string = "not_null_violation"
UniqueViolationErrorCode string = "unique_violation"
)

0 comments on commit 1b5522f

Please sign in to comment.