Skip to content

Commit

Permalink
add test cases for failing QueryRewriters
Browse files Browse the repository at this point in the history
  • Loading branch information
pashagolub committed Oct 17, 2023
1 parent 8c15af1 commit c5f7ae8
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions argument_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package pgxmock

import (
"context"
"errors"
"testing"
"time"

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

type AnyTime struct{}
Expand Down Expand Up @@ -81,6 +83,41 @@ func TestByteSliceArgument(t *testing.T) {
}
}

type failQryRW struct {
pgx.QueryRewriter
}

func (fqrw failQryRW) RewriteQuery(_ context.Context, _ *pgx.Conn, sql string, args []any) (newSQL string, newArgs []any, err error) {

Check warning on line 90 in argument_test.go

View workflow job for this annotation

GitHub Actions / Test and Build on Ubuntu

unused-parameter: parameter 'args' seems to be unused, consider removing or renaming it as _ (revive)
return "", nil, errors.New("cannot rewrite query " + sql)
}

func TestExpectQueryRewriterFail(t *testing.T) {

t.Parallel()
mock, err := NewConn()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
}

mock.ExpectExec(`INSERT INTO users\(username\) VALUES \(\@user\)`).
WithArgs(failQryRW{})
_, err = mock.Exec(context.Background(), "INSERT INTO users(username) VALUES (@user)", "baz")
assert.Error(t, err)
}

func TestQueryRewriterFail(t *testing.T) {

t.Parallel()
mock, err := NewConn()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
}
mock.ExpectExec(`INSERT INTO .+`).WithArgs("foo")
_, err = mock.Exec(context.Background(), "INSERT INTO users(username) VALUES (@user)", failQryRW{})
assert.Error(t, err)

}

func TestByteSliceNamedArgument(t *testing.T) {
t.Parallel()
mock, err := NewConn()
Expand Down

0 comments on commit c5f7ae8

Please sign in to comment.