Skip to content

Commit

Permalink
[*] improve test failure handling (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
Massinja authored Mar 4, 2024
1 parent 4866405 commit 60f15ba
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 47 deletions.
16 changes: 8 additions & 8 deletions argument_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestAnyTimeArgument(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}

mock.ExpectExec("INSERT INTO users").
Expand All @@ -43,7 +43,7 @@ func TestAnyTimeNamedArgument(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}

mock.ExpectExec("INSERT INTO users").
Expand All @@ -67,7 +67,7 @@ func TestByteSliceArgument(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}

username := []byte("user")
Expand Down Expand Up @@ -96,7 +96,7 @@ func TestExpectQueryRewriterFail(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}

mock.ExpectQuery(`INSERT INTO users\(username\) VALUES \(\@user\)`).
Expand All @@ -111,7 +111,7 @@ func TestQueryRewriterFail(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
mock.ExpectExec(`INSERT INTO .+`).WithArgs("foo")
_, err = mock.Exec(context.Background(), "INSERT INTO users(username) VALUES (@user)", failQryRW{})
Expand All @@ -123,7 +123,7 @@ func TestByteSliceNamedArgument(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}

username := []byte("user")
Expand All @@ -149,7 +149,7 @@ func TestAnyArgument(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}

mock.ExpectExec("INSERT INTO users").
Expand All @@ -170,7 +170,7 @@ func TestAnyNamedArgument(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}

mock.ExpectExec("INSERT INTO users").
Expand Down
12 changes: 6 additions & 6 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
func TestTwoOpenConnectionsOnTheSameDSN(t *testing.T) {
mock, err := NewConn()
if err != nil {
t.Errorf("expected no error, but got: %s", err)
t.Fatalf("expected no error, but got: %s", err)
}
mock2, err := NewConn()
if err != nil {
t.Errorf("expected no error, but got: %s", err)
t.Fatalf("expected no error, but got: %s", err)
}
if mock == mock2 {
t.Errorf("expected not the same mock instance, but it is the same")
Expand All @@ -24,11 +24,11 @@ func TestTwoOpenConnectionsOnTheSameDSN(t *testing.T) {
func TestPools(t *testing.T) {
mock, err := NewPool()
if err != nil {
t.Errorf("expected no error, but got: %s", err)
t.Fatalf("expected no error, but got: %s", err)
}
mock2, err := NewPool()
if err != nil {
t.Errorf("expected no error, but got: %s", err)
t.Fatalf("expected no error, but got: %s", err)
}
if mock == mock2 {
t.Errorf("expected not the same mock instance, but it is the same")
Expand All @@ -44,7 +44,7 @@ func TestPools(t *testing.T) {
func TestAcquire(t *testing.T) {
mock, err := NewPool()
if err != nil {
t.Errorf("expected no error, but got: %s", err)
t.Fatalf("expected no error, but got: %s", err)
}
_, err = mock.Acquire(context.Background())
if err == nil {
Expand All @@ -55,7 +55,7 @@ func TestAcquire(t *testing.T) {
func TestPoolStat(t *testing.T) {
mock, err := NewPool()
if err != nil {
t.Errorf("expected no error, but got: %s", err)
t.Fatalf("expected no error, but got: %s", err)
}
s := mock.Stat()
if s == nil {
Expand Down
56 changes: 28 additions & 28 deletions pgxmock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestIssue14EscapeSQL(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer mock.Close(context.Background())
mock.ExpectExec("INSERT INTO mytable\\(a, b\\)").
Expand All @@ -51,7 +51,7 @@ func TestIssue4(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer mock.Close(context.Background())

Expand All @@ -67,7 +67,7 @@ func TestMockQuery(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer mock.Close(context.Background())

Expand All @@ -79,13 +79,13 @@ func TestMockQuery(t *testing.T) {

rows, err := mock.Query(context.Background(), "SELECT (.+) FROM articles WHERE id = ?", 5)
if err != nil {
t.Errorf("error '%s' was not expected while retrieving mock rows", err)
t.Fatalf("error '%s' was not expected while retrieving mock rows", err)
}

defer rows.Close()

if !rows.Next() {
t.Error("it must have had one row as result, but got empty result set instead")
t.Fatal("it must have had one row as result, but got empty result set instead")
}

var id int
Expand Down Expand Up @@ -143,7 +143,7 @@ func TestMockQueryTypes(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer mock.Close(context.Background())

Expand All @@ -159,7 +159,7 @@ func TestMockQueryTypes(t *testing.T) {

rows, err := mock.Query(context.Background(), "SELECT (.+) FROM sales WHERE id = ?", 5)
if err != nil {
t.Errorf("error '%s' was not expected while retrieving mock rows", err)
t.Fatalf("error '%s' was not expected while retrieving mock rows", err)
}
defer rows.Close()
if !rows.Next() {
Expand Down Expand Up @@ -280,7 +280,7 @@ func TestPreparedQueryExecutions(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer mock.Close(context.Background())

Expand All @@ -298,7 +298,7 @@ func TestPreparedQueryExecutions(t *testing.T) {

_, err = mock.Prepare(context.Background(), "foo", "SELECT id, title FROM articles WHERE id = ?")
if err != nil {
t.Errorf("error '%s' was not expected while creating a prepared statement", err)
t.Fatalf("error '%s' was not expected while creating a prepared statement", err)
}

var id int
Expand Down Expand Up @@ -338,7 +338,7 @@ func TestUnorderedPreparedQueryExecutions(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer mock.Close(context.Background())

Expand All @@ -358,7 +358,7 @@ func TestUnorderedPreparedQueryExecutions(t *testing.T) {

_, err = mock.Prepare(context.Background(), "authors_stmt", "SELECT id, name FROM authors WHERE id = ?")
if err != nil {
t.Errorf("error '%s' was not expected while creating a prepared statement", err)
t.Fatalf("error '%s' was not expected while creating a prepared statement", err)
}

err = mock.QueryRow(context.Background(), "authors_stmt", 1).Scan(&id, &name)
Expand All @@ -375,14 +375,14 @@ func TestUnexpectedOperations(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer mock.Close(context.Background())

mock.ExpectPrepare("foo", "SELECT (.+) FROM articles WHERE id = ?")
_, err = mock.Prepare(context.Background(), "foo", "SELECT id, title FROM articles WHERE id = ?")
if err != nil {
t.Errorf("error '%s' was not expected while creating a prepared statement", err)
t.Fatalf("error '%s' was not expected while creating a prepared statement", err)
}

var id int
Expand All @@ -404,7 +404,7 @@ func TestWrongExpectations(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer mock.Close(context.Background())

Expand Down Expand Up @@ -451,7 +451,7 @@ func TestExecExpectations(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer mock.Close(context.Background())

Expand All @@ -462,7 +462,7 @@ func TestExecExpectations(t *testing.T) {

res, err := mock.Exec(context.Background(), "INSERT INTO articles (title) VALUES (?)", "hello")
if err != nil {
t.Errorf("error '%s' was not expected, while inserting a row", err)
t.Fatalf("error '%s' was not expected, while inserting a row", err)
}

if res.RowsAffected() != 1 {
Expand All @@ -478,7 +478,7 @@ func TestRowBuilderAndNilTypes(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer mock.Close(context.Background())

Expand All @@ -490,7 +490,7 @@ func TestRowBuilderAndNilTypes(t *testing.T) {

rows, err := mock.Query(context.Background(), "SELECT * FROM sales")
if err != nil {
t.Errorf("error '%s' was not expected while retrieving mock rows", err)
t.Fatalf("error '%s' was not expected while retrieving mock rows", err)
}
defer rows.Close()

Expand All @@ -503,7 +503,7 @@ func TestRowBuilderAndNilTypes(t *testing.T) {
)

if !rows.Next() {
t.Error("it must have had row in rows, but got empty result set instead")
t.Fatal("it must have had row in rows, but got empty result set instead")
}

err = rows.Scan(&id, &active, &created, &status)
Expand Down Expand Up @@ -533,7 +533,7 @@ func TestRowBuilderAndNilTypes(t *testing.T) {

// test second row
if !rows.Next() {
t.Error("it must have had row in rows, but got empty result set instead")
t.Fatal("it must have had row in rows, but got empty result set instead")
}

err = rows.Scan(&id, &active, &created, &status)
Expand Down Expand Up @@ -566,7 +566,7 @@ func TestArgumentReflectValueTypeError(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer mock.Close(context.Background())

Expand All @@ -584,7 +584,7 @@ func TestGoroutineExecutionWithUnorderedExpectationMatching(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer mock.Close(context.Background())

Expand Down Expand Up @@ -726,7 +726,7 @@ func TestEmptyRowSet(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer mock.Close(context.Background())

Expand All @@ -738,7 +738,7 @@ func TestEmptyRowSet(t *testing.T) {

rows, err := mock.Query(context.Background(), "SELECT (.+) FROM articles WHERE id = ?", 5)
if err != nil {
t.Errorf("error '%s' was not expected while retrieving mock rows", err)
t.Fatalf("error '%s' was not expected while retrieving mock rows", err)
}
defer rows.Close()

Expand All @@ -757,7 +757,7 @@ func TestPrepareExpectationNotFulfilled(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer mock.Close(context.Background())

Expand Down Expand Up @@ -1046,7 +1046,7 @@ func TestExecExpectationErrorDelay(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer mock.Close(context.Background())

Expand Down Expand Up @@ -1107,7 +1107,7 @@ func TestNewRows(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer mock.Close(context.Background())
columns := []string{"col1", "col2"}
Expand All @@ -1124,7 +1124,7 @@ func TestNewRows(t *testing.T) {
func TestQueryWithTimeout(t *testing.T) {
mock, err := NewConn()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer mock.Close(context.Background())

Expand Down
Loading

0 comments on commit 60f15ba

Please sign in to comment.