-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* [!] add support for `pgx.Batch`, closes #199 * [+] add awaited pgx `Fn()` functionality * [+] bump jackc/pgx/v5 from 5.5.5 to 5.6.0
- Loading branch information
1 parent
e0fce08
commit 3f620d4
Showing
7 changed files
with
255 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package pgxmock | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
pgx "github.com/jackc/pgx/v5" | ||
pgconn "github.com/jackc/pgx/v5/pgconn" | ||
) | ||
|
||
type batchResults struct { | ||
mock *pgxmock | ||
batch *pgx.Batch | ||
expectedBatch *ExpectedBatch | ||
qqIdx int | ||
err error | ||
} | ||
|
||
func (br *batchResults) nextQueryAndArgs() (query string, args []any, err error) { | ||
if br.err != nil { | ||
return "", nil, br.err | ||
} | ||
if br.batch == nil { | ||
return "", nil, errors.New("no batch expectations set") | ||
} | ||
if br.qqIdx >= len(br.batch.QueuedQueries) { | ||
return "", nil, errors.New("no more queries in batch") | ||
} | ||
bi := br.batch.QueuedQueries[br.qqIdx] | ||
query = bi.SQL | ||
args = bi.Arguments | ||
br.qqIdx++ | ||
return | ||
} | ||
|
||
func (br *batchResults) Exec() (pgconn.CommandTag, error) { | ||
query, arguments, err := br.nextQueryAndArgs() | ||
if err != nil { | ||
return pgconn.NewCommandTag(""), err | ||
} | ||
return br.mock.Exec(context.Background(), query, arguments...) | ||
} | ||
|
||
func (br *batchResults) Query() (pgx.Rows, error) { | ||
query, arguments, err := br.nextQueryAndArgs() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return br.mock.Query(context.Background(), query, arguments...) | ||
} | ||
|
||
func (br *batchResults) QueryRow() pgx.Row { | ||
query, arguments, err := br.nextQueryAndArgs() | ||
if err != nil { | ||
return errRow{err: err} | ||
} | ||
return br.mock.QueryRow(context.Background(), query, arguments...) | ||
} | ||
|
||
func (br *batchResults) Close() error { | ||
if br.err != nil { | ||
return br.err | ||
} | ||
// Read and run fn for all remaining items | ||
for br.err == nil && br.expectedBatch != nil && !br.expectedBatch.closed && br.qqIdx < len(br.batch.QueuedQueries) { | ||
if qq := br.batch.QueuedQueries[br.qqIdx]; qq != nil { | ||
br.err = errors.Join(br.err, br.callQuedQueryFn(qq)) | ||
} | ||
} | ||
br.expectedBatch.closed = true | ||
return br.err | ||
} | ||
|
||
func (br *batchResults) callQuedQueryFn(qq *pgx.QueuedQuery) error { | ||
if qq.Fn != nil { | ||
return qq.Fn(br) | ||
} | ||
_, err := br.Exec() | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package pgxmock | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
pgx "github.com/jackc/pgx/v5" | ||
pgconn "github.com/jackc/pgx/v5/pgconn" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBatch(t *testing.T) { | ||
t.Parallel() | ||
mock, _ := NewConn() | ||
a := assert.New(t) | ||
|
||
// define our expectations | ||
eb := mock.ExpectBatch() | ||
eb.ExpectQuery("select").WillReturnRows(NewRows([]string{"sum"}).AddRow(2)) | ||
eb.ExpectExec("update").WithArgs(true, 1).WillReturnResult(NewResult("UPDATE", 1)) | ||
|
||
// run the test | ||
batch := &pgx.Batch{} | ||
batch.Queue("select 1 + 1").QueryRow(func(row pgx.Row) error { | ||
var n int32 | ||
return row.Scan(&n) | ||
}) | ||
batch.Queue("update users set active = $1 where id = $2", true, 1).Exec(func(ct pgconn.CommandTag) (err error) { | ||
if ct.RowsAffected() != 1 { | ||
err = errors.New("expected 1 row to be affected") | ||
} | ||
return | ||
}) | ||
|
||
err := mock.SendBatch(ctx, batch).Close() | ||
a.NoError(err) | ||
a.NoError(mock.ExpectationsWereMet()) | ||
} | ||
|
||
func TestExplicitBatch(t *testing.T) { | ||
t.Parallel() | ||
mock, _ := NewConn() | ||
a := assert.New(t) | ||
|
||
// define our expectations | ||
eb := mock.ExpectBatch() | ||
eb.ExpectQuery("select").WillReturnRows(NewRows([]string{"sum"}).AddRow(2)) | ||
eb.ExpectQuery("select").WillReturnRows(NewRows([]string{"answer"}).AddRow(42)) | ||
eb.ExpectExec("update").WithArgs(true, 1).WillReturnResult(NewResult("UPDATE", 1)) | ||
|
||
// run the test | ||
batch := &pgx.Batch{} | ||
batch.Queue("select 1 + 1") | ||
batch.Queue("select 42") | ||
batch.Queue("update users set active = $1 where id = $2", true, 1) | ||
|
||
var sum int | ||
br := mock.SendBatch(ctx, batch) | ||
err := br.QueryRow().Scan(&sum) | ||
a.NoError(err) | ||
a.Equal(2, sum) | ||
|
||
var answer int | ||
rows, err := br.Query() | ||
a.NoError(err) | ||
rows.Next() | ||
err = rows.Scan(&answer) | ||
a.NoError(err) | ||
a.Equal(42, answer) | ||
|
||
ct, err := br.Exec() | ||
a.NoError(err) | ||
a.True(ct.Update()) | ||
a.EqualValues(1, ct.RowsAffected()) | ||
|
||
// no more queries | ||
_, err = br.Exec() | ||
a.Error(err) | ||
_, err = br.Query() | ||
a.Error(err) | ||
err = br.QueryRow().Scan(&sum) | ||
a.Error(err) | ||
|
||
a.NoError(mock.ExpectationsWereMet()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters