Skip to content

Commit

Permalink
[+] improve TestPrepareExpectations()
Browse files Browse the repository at this point in the history
  • Loading branch information
pashagolub committed Sep 8, 2023
1 parent 4b56137 commit d45eb73
Showing 1 changed file with 16 additions and 28 deletions.
44 changes: 16 additions & 28 deletions pgxmock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,17 @@ func TestMockCopyFrom(t *testing.T) {

res, err := mock.CopyFrom(context.Background(), pgx.Identifier{"error", "error"}, []string{"error"}, nil)
a.Error(err, "incorrect table should raise an error")
a.Less(res, 0)
a.EqualValues(res, -1)
a.Error(mock.ExpectationsWereMet(), "there must be unfulfilled expectations")

res, err = mock.CopyFrom(context.Background(), pgx.Identifier{"fooschema", "baztable"}, []string{"error"}, nil)
a.Error(err, "incorrect columns should raise an error")
a.Less(res, 0)
a.EqualValues(res, -1)
a.Error(mock.ExpectationsWereMet(), "there must be unfulfilled expectations")

res, err = mock.CopyFrom(context.Background(), pgx.Identifier{"fooschema", "baztable"}, []string{"col1"}, nil)
a.NoError(err)
a.Equal(res, 2)
a.EqualValues(res, 2)

mock.ExpectCopyFrom(pgx.Identifier{"fooschema", "baztable"}, []string{"col1"}).
WillReturnError(errors.New("error is here"))
Expand Down Expand Up @@ -238,23 +238,20 @@ func TestTransactionExpectations(t *testing.T) {

func TestPrepareExpectations(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)
}
defer mock.Close(context.Background())
mock, _ := NewConn()
a := assert.New(t)

mock.ExpectPrepare("foo", "SELECT (.+) FROM articles WHERE id = ?").
WillReturnCloseError(errors.New("invaders must die")).
WillDelayFor(1 * time.Second)

stmt, err := mock.Prepare(context.Background(), "foo", "SELECT (.+) FROM articles WHERE id = $1")
if err != nil {
t.Errorf("error '%s' was not expected while creating a prepared statement", err)
}
if stmt == nil {
t.Errorf("stmt was expected while creating a prepared statement")
}
stmt, err := mock.Prepare(context.Background(), "baz", "SELECT (.+) FROM articles WHERE id = ?")
a.Error(err, "wrong prepare stmt name should raise an error")
a.Nil(stmt)

stmt, err = mock.Prepare(context.Background(), "foo", "SELECT (.+) FROM articles WHERE id = $1")
a.NoError(err)
a.NotNil(stmt)

// expect something else, w/o ExpectPrepare()
var id int
Expand All @@ -266,24 +263,15 @@ func TestPrepareExpectations(t *testing.T) {
WillReturnRows(rs)

err = mock.QueryRow(context.Background(), "foo", 5).Scan(&id, &title)
if err != nil {
t.Errorf("error '%s' was not expected while retrieving mock rows", err)
}
a.NoError(err)

mock.ExpectPrepare("foo", "SELECT (.+) FROM articles WHERE id = ?").
WillReturnError(fmt.Errorf("Some DB error occurred"))

stmt, err = mock.Prepare(context.Background(), "foo", "SELECT id FROM articles WHERE id = $1")
if err == nil {
t.Error("error was expected while creating a prepared statement")
}
if stmt != nil {
t.Errorf("stmt was not expected while creating a prepared statement returning error")
}

if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
a.Error(err)
a.Nil(stmt)
a.NoError(mock.ExpectationsWereMet())
}

func TestPreparedQueryExecutions(t *testing.T) {
Expand Down

0 comments on commit d45eb73

Please sign in to comment.