Skip to content

Commit

Permalink
[+] implement ExpectReset() function and tests, closes #146 (#148)
Browse files Browse the repository at this point in the history
Co-authored-by: sweep-ai[bot] <128439645+sweep-ai[bot]@users.noreply.github.com>
Co-authored-by: Pavlo Golub <[email protected]>
  • Loading branch information
sweep-ai[bot] and pashagolub authored Aug 17, 2023
1 parent 36433f8 commit 19a4aaf
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
9 changes: 9 additions & 0 deletions expectations.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,3 +455,12 @@ func (e *ExpectedCopyFrom) WillReturnResult(result int64) *ExpectedCopyFrom {
e.rowsAffected = result
return e
}

// ExpectedReset is used to manage pgx.Reset expectation
type ExpectedReset struct {
commonExpectation
}

func (e *ExpectedReset) String() string {
return "ExpectedReset => expecting database Reset"
}
42 changes: 36 additions & 6 deletions pgxmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ type pgxMockIface interface {
// the *ExpectedCommit allows to mock database response
ExpectCommit() *ExpectedCommit

// ExpectReset expects pgxpool.Reset() to be called.
// The *ExpectedReset allows to mock database response
ExpectReset() *ExpectedReset

// ExpectRollback expects pgx.Tx.Rollback to be called.
// the *ExpectedRollback allows to mock database response
ExpectRollback() *ExpectedRollback
Expand Down Expand Up @@ -119,7 +123,6 @@ type pgxIface interface {
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
QueryRow(context.Context, string, ...interface{}) pgx.Row
Reset()
Ping(context.Context) error
Prepare(context.Context, string, string) (*pgconn.StatementDescription, error)
Deallocate(ctx context.Context, name string) error
Expand All @@ -131,15 +134,17 @@ type PgxConnIface interface {
pgx.Tx
Close(ctx context.Context) error
}

type PgxPoolIface interface {
pgxIface
pgx.Tx
Acquire(ctx context.Context) (*pgxpool.Conn, error)
AcquireAllIdle(ctx context.Context) []*pgxpool.Conn
AcquireFunc(ctx context.Context, f func(*pgxpool.Conn) error) error
AsConn() PgxConnIface
Close()
Stat() *pgxpool.Stat
AsConn() PgxConnIface
Reset()
}

type pgxmock struct {
Expand All @@ -162,10 +167,6 @@ func (c *pgxmock) AcquireFunc(_ context.Context, _ func(*pgxpool.Conn) error) er
return nil
}

func (c *pgxmock) Reset() {

}

// region Expectations
func (c *pgxmock) ExpectClose() *ExpectedClose {
e := &ExpectedClose{}
Expand Down Expand Up @@ -251,6 +252,13 @@ func (c *pgxmock) ExpectCopyFrom(expectedTableName pgx.Identifier, expectedColum
return e
}

// ExpectReset expects Reset to be called.
func (c *pgxmock) ExpectReset() *ExpectedReset {
e := &ExpectedReset{}
c.expected = append(c.expected, e)
return e
}

func (c *pgxmock) ExpectPing() *ExpectedPing {
if !c.monitorPings {
log.Println("ExpectPing will have no effect as monitoring pings is disabled. Use MonitorPingsOption to enable.")
Expand Down Expand Up @@ -884,3 +892,25 @@ func (c *pgxmock) ping() (*ExpectedPing, error) {
expected.Unlock()
return expected, expected.err
}

func (c *pgxmock) Reset() {
var expected *ExpectedReset
var ok bool
for _, next := range c.expected {
next.Lock()
if next.fulfilled() {
next.Unlock()
continue
}

if expected, ok = next.(*ExpectedReset); ok {
break
}
next.Unlock()
}
if expected == nil {
return
}
expected.triggered = true
expected.Unlock()
}
23 changes: 23 additions & 0 deletions pgxmock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1254,3 +1254,26 @@ func TestNewRowsWithColumnDefinition(t *testing.T) {
t.Error("NewRows failed")
}
}

func TestExpectReset(t *testing.T) {
mock, err := NewPool()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
}
defer mock.Close()

// Successful scenario
_ = mock.ExpectReset()
mock.Reset()
err = mock.ExpectationsWereMet()
if err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}

// Unsuccessful scenario
mock.ExpectReset()
err = mock.ExpectationsWereMet()
if err == nil {
t.Error("was expecting an error, but there was none")
}
}

0 comments on commit 19a4aaf

Please sign in to comment.