Skip to content

Commit

Permalink
[*] improve Times()
Browse files Browse the repository at this point in the history
  • Loading branch information
pashagolub committed Sep 7, 2023
1 parent c7e2cec commit e6dc2ae
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 63 deletions.
13 changes: 7 additions & 6 deletions expectations.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Expectation interface {

type CallModifyer interface {
Maybe() CallModifyer
Times(n int) CallModifyer
Times(n uint) CallModifyer
WillDelayFor(duration time.Duration) CallModifyer
WillReturnError(err error)
WillPanic(v any)
Expand All @@ -35,12 +35,12 @@ type CallModifyer interface {
// satisfies the expectation interface
type commonExpectation struct {
sync.Mutex
triggered int // how many times method was called
triggered uint // how many times method was called
err error // should method return error
optional bool // can method be skipped
panicArgument any // panic value to return for recovery
plannedDelay time.Duration // should method delay before return
plannedCalls int // how many sequentional calls should be made
plannedCalls uint // how many sequentional calls should be made
}

func (e *commonExpectation) error() error {
Expand All @@ -52,7 +52,7 @@ func (e *commonExpectation) fulfill() {
}

func (e *commonExpectation) fulfilled() bool {
return e.triggered > e.plannedCalls
return e.triggered >= max(e.plannedCalls, 1)

Check failure on line 55 in expectations.go

View workflow job for this annotation

GitHub Actions / Test and Build on Ubuntu

undefined: max

Check failure on line 55 in expectations.go

View workflow job for this annotation

GitHub Actions / Test and Build on Ubuntu

undefined: max
}

func (e *commonExpectation) required() bool {
Expand All @@ -79,8 +79,9 @@ func (e *commonExpectation) Maybe() CallModifyer {
return e
}

// Times indicates that that the expected method should only fire the indicated number of times
func (e *commonExpectation) Times(n int) CallModifyer {
// Times indicates that that the expected method should only fire the indicated number of times.
// Zero value is ignored and means the same as one.
func (e *commonExpectation) Times(n uint) CallModifyer {
e.plannedCalls = n
return e
}
Expand Down
12 changes: 12 additions & 0 deletions expectations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ import (

var ctx = context.Background()

func TestTimes(t *testing.T) {
mock, _ := NewConn()
a := assert.New(t)
mock.ExpectPing().Times(2)
err := mock.Ping(ctx)
a.NoError(err)
a.Error(mock.ExpectationsWereMet()) // must be two Ping() calls
err = mock.Ping(ctx)
a.NoError(err)
a.NoError(mock.ExpectationsWereMet())
}

func TestMaybe(t *testing.T) {
mock, _ := NewConn()
a := assert.New(t)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/pashagolub/pgxmock/v3

go 1.20
go 1.21

require (
github.com/jackc/pgx/v5 v5.4.3
Expand Down
75 changes: 19 additions & 56 deletions pgxmock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"time"

pgx "github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/stretchr/testify/assert"
)

func cancelOrder(db pgxIface, orderID int) error {
Expand Down Expand Up @@ -1210,71 +1212,32 @@ func queryWithTimeout(t time.Duration, db pgxIface, query string, args ...interf
}
}

func TestCon(t *testing.T) {
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())
defer func() {
if r := recover(); r == nil {
t.Errorf("The Conn() did not panic")
}
}()
_ = mock.Conn()
}

func TestConnInfo(t *testing.T) {
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.Config()
}

func TestPgConn(t *testing.T) {
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.PgConn()
func TestUnmockedMethods(t *testing.T) {
mock, _ := NewPool()
a := assert.New(t)
a.NotNil(mock.Config())
a.NotNil(mock.PgConn())
a.NotNil(mock.AcquireAllIdle(ctx))
a.Nil(mock.AcquireFunc(ctx, func(*pgxpool.Conn) error { return nil }))
a.Nil(mock.SendBatch(ctx, nil))
a.Zero(mock.LargeObjects())
a.Panics(func() { _ = mock.Conn() })
}

func TestNewRowsWithColumnDefinition(t *testing.T) {
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())
r := mock.NewRowsWithColumnDefinition(*mock.NewColumn("foo"))
if len(r.defs) != 1 {
t.Error("NewRows failed")
}
mock, _ := NewConn()
assert.Equal(t, 1, mock.NewRowsWithColumnDefinition(*mock.NewColumn("foo")))
}

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()

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

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

0 comments on commit e6dc2ae

Please sign in to comment.