From e6dc2ae54b08b1b958780dd842d50284f2c4e4b3 Mon Sep 17 00:00:00 2001 From: Pavlo Golub Date: Thu, 7 Sep 2023 14:27:49 +0200 Subject: [PATCH] [*] improve `Times()` --- expectations.go | 13 ++++---- expectations_test.go | 12 +++++++ go.mod | 2 +- pgxmock_test.go | 75 +++++++++++--------------------------------- 4 files changed, 39 insertions(+), 63 deletions(-) diff --git a/expectations.go b/expectations.go index f622868..128d4a8 100644 --- a/expectations.go +++ b/expectations.go @@ -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) @@ -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 { @@ -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) } func (e *commonExpectation) required() bool { @@ -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 } diff --git a/expectations_test.go b/expectations_test.go index c17650b..cb900c5 100644 --- a/expectations_test.go +++ b/expectations_test.go @@ -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) diff --git a/go.mod b/go.mod index d81dec8..071a6e9 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/pgxmock_test.go b/pgxmock_test.go index 0b9eca4..595f3b8 100644 --- a/pgxmock_test.go +++ b/pgxmock_test.go @@ -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 { @@ -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()) }