Skip to content

Commit

Permalink
[-] fix Deallocate() to return expected.deallocateErr (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
pashagolub committed Jan 19, 2024
1 parent c0a12c2 commit a7298db
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
11 changes: 6 additions & 5 deletions pgxmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,11 @@ func (c *pgxmock) Deallocate(ctx context.Context, name string) error {
if expected == nil {
return fmt.Errorf("Deallocate: prepared statement name '%s' doesn't exist", name)
}
if ctx.Err() != nil {
return ctx.Err()
}
expected.deallocated = true
return expected.waitForDelay(ctx)
return expected.deallocateErr
}

func (c *pgxmock) Commit(ctx context.Context) error {
Expand Down Expand Up @@ -480,11 +483,9 @@ func (c *pgxmock) Ping(ctx context.Context) (err error) {
}

func (c *pgxmock) Reset() {
ex, err := findExpectation[*ExpectedReset](c, "Reset()")
if err != nil {
return
if ex, err := findExpectation[*ExpectedReset](c, "Reset()"); err == nil {
_ = ex.waitForDelay(context.Background())
}
_ = ex.waitForDelay(context.Background())
}

type expectationType[t any] interface {
Expand Down
6 changes: 4 additions & 2 deletions pgxmock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ func TestPrepareExpectations(t *testing.T) {
t.Parallel()
mock, _ := NewConn()
a := assert.New(t)

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

stmt, err := mock.Prepare(context.Background(), "baz", "SELECT (.+) FROM articles WHERE id = ?")
Expand All @@ -252,6 +252,8 @@ func TestPrepareExpectations(t *testing.T) {
stmt, err = mock.Prepare(context.Background(), "foo", "SELECT (.+) FROM articles WHERE id = $1")
a.NoError(err)
a.NotNil(stmt)
err = mock.Deallocate(context.Background(), "foo")
a.EqualError(err, expErr.Error())

// expect something else, w/o ExpectPrepare()
var id int
Expand Down

0 comments on commit a7298db

Please sign in to comment.