Skip to content

Commit

Permalink
[-] fix Myabe() modifier check
Browse files Browse the repository at this point in the history
  • Loading branch information
pashagolub committed Sep 6, 2023
1 parent f08c6e8 commit cf04b04
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 54 deletions.
97 changes: 46 additions & 51 deletions expectations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,71 +9,74 @@ import (
"time"

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

var ctx = context.Background()

func TestMaybe(t *testing.T) {
mock, _ := NewConn()
a := assert.New(t)
mock.ExpectPing().Maybe()
mock.ExpectBegin().Maybe()
mock.ExpectQuery("SET TIME ZONE 'Europe/Rome'").Maybe() //only if we're in Italy
cmdtag := pgconn.NewCommandTag("SELECT 1")
mock.ExpectExec("select").WillReturnResult(cmdtag)
mock.ExpectCommit().Maybe()

res, err := mock.Exec(ctx, "select version()")
a.Equal(cmdtag, res)
a.NoError(err)
a.NoError(mock.ExpectationsWereMet())
}

func TestPanic(t *testing.T) {
mock, _ := NewConn()
a := assert.New(t)
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("expectation were not met: %s", err)
}
a.NotNil(recover(), "The code did not panic")
a.NoError(mock.ExpectationsWereMet())
}()

ex := mock.ExpectPing()
ex.WillPanic("i'm tired")
fmt.Println(ex)
if err := mock.Ping(context.Background()); err != nil {
t.Errorf("unexpected error: %s", err)
}
a.NoError(mock.Ping(ctx))
}

func TestCallModifier(t *testing.T) {
mock, _ := NewConn()
f := func() {
err := mock.ExpectationsWereMet()
if err != nil {
t.Errorf("expectation were not met: %s", err)
}
}
a := assert.New(t)

mock.ExpectPing().WillDelayFor(time.Second).Maybe().Times(4)
f() //should produce no error since Ping() call is optional
a.NoError(mock.ExpectationsWereMet()) //should produce no error since Ping() call is optional

if err := mock.Ping(context.Background()); err != nil {
t.Errorf("unexpected error: %s", err)
}
f() //should produce no error since Ping() was called actually
a.NoError(mock.Ping(ctx))
a.NoError(mock.ExpectationsWereMet()) //should produce no error since Ping() was called actually
}

func TestCopyFromBug(t *testing.T) {
mock, _ := NewConn()
defer func() {
err := mock.ExpectationsWereMet()
if err != nil {
t.Errorf("expectation were not met: %s", err)
}
}()
a := assert.New(t)

mock.ExpectCopyFrom(pgx.Identifier{"foo"}, []string{"bar"}).WillReturnResult(1)

var rows [][]any
rows = append(rows, []any{"baz"})

_, err := mock.CopyFrom(context.Background(), pgx.Identifier{"foo"}, []string{"bar"}, pgx.CopyFromRows(rows))
if err != nil {
t.Errorf("unexpected error: %s", err)
}
r, err := mock.CopyFrom(ctx, pgx.Identifier{"foo"}, []string{"bar"}, pgx.CopyFromRows(rows))
a.EqualValues(len(rows), r)
a.NoError(err)
a.NoError(mock.ExpectationsWereMet())
}

func ExampleExpectedExec() {
mock, _ := NewConn()
ex := mock.ExpectExec("^INSERT (.+)").WillReturnResult(NewResult("INSERT", 15))
ex.WillDelayFor(time.Second)
fmt.Print(ex)
res, _ := mock.Exec(context.Background(), "INSERT something")
res, _ := mock.Exec(ctx, "INSERT something")
fmt.Println(res)
// Output: ExpectedExec => expecting call to Exec():
// - matches sql: '^INSERT (.+)'
Expand All @@ -83,48 +86,40 @@ func ExampleExpectedExec() {
// INSERT 15
}

func TestUnmonitoredPing(t *testing.T) {
mock, _ := NewConn()
p := mock.ExpectPing()
if p == nil {
t.Error("ExpectPing should return *ExpectedPing")
}
}

func TestUnexpectedPing(t *testing.T) {
mock, _ := NewConn()
err := mock.Ping(context.Background())
err := mock.Ping(ctx)
if err == nil {
t.Error("Ping should return error for unexpected call")
}
mock.ExpectExec("foo")
err = mock.Ping(context.Background())
err = mock.Ping(ctx)
if err == nil {
t.Error("Ping should return error for unexpected call")
}
}

func TestUnexpectedPrepare(t *testing.T) {
mock, _ := NewConn()
_, err := mock.Prepare(context.Background(), "foo", "bar")
_, err := mock.Prepare(ctx, "foo", "bar")
if err == nil {
t.Error("Prepare should return error for unexpected call")
}
mock.ExpectExec("foo")
_, err = mock.Prepare(context.Background(), "foo", "bar")
_, err = mock.Prepare(ctx, "foo", "bar")
if err == nil {
t.Error("Prepare should return error for unexpected call")
}
}

func TestUnexpectedCopyFrom(t *testing.T) {
mock, _ := NewConn()
_, err := mock.CopyFrom(context.Background(), pgx.Identifier{"schema", "table"}, []string{"foo", "bar"}, nil)
_, err := mock.CopyFrom(ctx, pgx.Identifier{"schema", "table"}, []string{"foo", "bar"}, nil)
if err == nil {
t.Error("CopyFrom should return error for unexpected call")
}
mock.ExpectExec("foo")
_, err = mock.CopyFrom(context.Background(), pgx.Identifier{"schema", "table"}, []string{"foo", "bar"}, nil)
_, err = mock.CopyFrom(ctx, pgx.Identifier{"schema", "table"}, []string{"foo", "bar"}, nil)
if err == nil {
t.Error("CopyFrom should return error for unexpected call")
}
Expand All @@ -151,10 +146,10 @@ func TestBuildQuery(t *testing.T) {
mock.ExpectExec(query)
mock.ExpectPrepare("foo", query)

_ = mock.Ping(context.Background())
mock.QueryRow(context.Background(), query)
_, _ = mock.Exec(context.Background(), query)
_, _ = mock.Prepare(context.Background(), "foo", query)
_ = mock.Ping(ctx)
mock.QueryRow(ctx, query)
_, _ = mock.Exec(ctx, query)
_, _ = mock.Prepare(ctx, "foo", query)

if err := mock.ExpectationsWereMet(); err != nil {
t.Error(err)
Expand All @@ -180,7 +175,7 @@ func TestQueryRowScan(t *testing.T) {
expectedIntValue := 2
expectedArrayValue := []string{"Three", "Four"}
mock.ExpectQuery(query).WillReturnRows(mock.NewRows([]string{"One", "Two", "Three"}).AddRow(expectedStringValue, expectedIntValue, []string{"Three", "Four"}))
row := mock.QueryRow(context.Background(), query)
row := mock.QueryRow(ctx, query)
var stringValue string
var intValue int
var arrayValue []string
Expand All @@ -206,7 +201,7 @@ func TestMissingWithArgs(t *testing.T) {
// No arguments expected
mock.ExpectExec("INSERT something")
// Receiving argument
_, err := mock.Exec(context.Background(), "INSERT something", "something")
_, err := mock.Exec(ctx, "INSERT something", "something")
if err == nil {
t.Error("arguments do not match error was expected")
}
Expand Down
10 changes: 9 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ module github.com/pashagolub/pgxmock/v3

go 1.20

require github.com/jackc/pgx/v5 v5.4.3
require (
github.com/jackc/pgx/v5 v5.4.3
github.com/stretchr/testify v1.8.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/text v0.9.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
Expand All @@ -8,18 +10,30 @@ github.com/jackc/pgx/v5 v5.4.3 h1:cxFyXhxlvAifxnkKKdlxv8XqUf59tDlYjnV5YYfsJJY=
github.com/jackc/pgx/v5 v5.4.3/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
17 changes: 15 additions & 2 deletions pgxmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,10 @@ func (c *pgxmock) copyFrom(tableName pgx.Identifier, columnNames []string) (*Exp
if expected, ok = next.(*ExpectedCopyFrom); ok {
break
}

next.Unlock()
if !ok && !next.required() {
continue
}
return nil, fmt.Errorf("call to CopyFrom statement with table name '%s', was not expected, next expectation is: %s", tableName, next)
}

Expand Down Expand Up @@ -471,8 +473,10 @@ func (c *pgxmock) prepare(name string, query string) (*ExpectedPrepare, error) {
if expected, ok = next.(*ExpectedPrepare); ok {
break
}

next.Unlock()
if !ok && !next.required() {
continue
}
return nil, fmt.Errorf("call to Prepare statement with query '%s', was not expected, next expectation is: %s", query, next)
}

Expand Down Expand Up @@ -564,6 +568,9 @@ func (c *pgxmock) query(query string, args []interface{}) (*ExpectedQuery, error
break
}
next.Unlock()
if !ok && !next.required() {
continue
}
return nil, fmt.Errorf("call to Query '%s' with args %+v, was not expected, next expectation is: %s", query, args, next)
}
if qr, ok := next.(*ExpectedQuery); ok {
Expand Down Expand Up @@ -652,6 +659,9 @@ func (c *pgxmock) exec(query string, args []interface{}) (*ExpectedExec, error)
break
}
next.Unlock()
if !ok && !next.required() {
continue
}
return nil, fmt.Errorf("call to Exec '%s' with args %+v, was not expected, next expectation is: %s", query, args, next)
}
if exec, ok := next.(*ExpectedExec); ok {
Expand Down Expand Up @@ -725,6 +735,9 @@ func findExpectation[ET ExpectationType[t], t any](c *pgxmock, method string) (E

next.Unlock()
if c.ordered {
if !ok && !next.required() {
continue
}
return nil, fmt.Errorf("call to method %s, was not expected, next expectation is: %s", method, next)
}
}
Expand Down

0 comments on commit cf04b04

Please # to comment.