Skip to content

Commit

Permalink
Merge pull request #157 from pashagolub/v3
Browse files Browse the repository at this point in the history
[!] introduce the new major version v3
  • Loading branch information
pashagolub authored Sep 19, 2023
2 parents 321c33c + 887bff8 commit 8840f5b
Show file tree
Hide file tree
Showing 19 changed files with 644 additions and 1,309 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Set up Golang
uses: actions/setup-go@v4
with:
go-version: '1.20'
go-version: '1.21'

- name: Get dependencies
run: |
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ It's based on the well-known [sqlmock](https://github.com/DATA-DOG/go-sqlmock) l

**pgxmock** has one and only purpose - to simulate **pgx** behavior in tests, without needing a real database connection. It helps to maintain correct **TDD** workflow.

- written based on **go1.15** version, however, should be compatible with **go1.11** and above;
- written based on **go1.21** version;
- does not require any modifications to your source code;
- has strict by default expectation order matching;
- has no third party dependencies except **pgx** packages.

## Install

go get github.com/pashagolub/pgxmock/v2
go get github.com/pashagolub/pgxmock/v3

## Documentation and Examples

Visit [godoc](http://pkg.go.dev/github.com/pashagolub/pgxmock/v2) for general examples and public api reference.
Visit [godoc](http://pkg.go.dev/github.com/pashagolub/pgxmock/v3) for general examples and public api reference.

See implementation examples:

Expand Down Expand Up @@ -92,7 +92,7 @@ import (
"fmt"
"testing"

"github.com/pashagolub/pgxmock/v2"
"github.com/pashagolub/pgxmock/v3"
)

// a successful case
Expand Down Expand Up @@ -175,7 +175,7 @@ provide a standard sql parsing matchers.
## Matching arguments like time.Time

There may be arguments which are of `struct` type and cannot be compared easily by value like `time.Time`. In this case
**pgxmock** provides an [Argument](https://pkg.go.dev/github.com/pashagolub/pgxmock/v2#Argument) interface which
**pgxmock** provides an [Argument](https://pkg.go.dev/github.com/pashagolub/pgxmock/v3#Argument) interface which
can be used in more sophisticated matching. Here is a simple example of time argument matching:

``` go
Expand Down
1 change: 1 addition & 0 deletions argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ type anyArgument struct{}
func (a anyArgument) Match(_ interface{}) bool {
return true
}

16 changes: 3 additions & 13 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,27 @@ type pgxmockConn struct {
}

// NewConn creates PgxConnIface database connection and a mock to manage expectations.
// Accepts options, like ValueConverterOption, to use a ValueConverter from
// a specific driver.
// Pings db so that all expectations could be
// asserted.
// Accepts options, like QueryMatcherOption, to match SQL query strings in more sophisticated ways.
func NewConn(options ...func(*pgxmock) error) (PgxConnIface, error) {
smock := &pgxmockConn{}
smock.ordered = true
return smock, smock.open(options)
}

func (c *pgxmockConn) Close(ctx context.Context) error {
return c.close(ctx)
}

type pgxmockPool struct {
pgxmock
}

// NewPool creates PgxPoolIface pool of database connections and a mock to manage expectations.
// Accepts options, like ValueConverterOption, to use a ValueConverter from
// a specific driver.
// Pings db so that all expectations could be
// asserted.
// Accepts options, like QueryMatcherOption, to match SQL query strings in more sophisticated ways.
func NewPool(options ...func(*pgxmock) error) (PgxPoolIface, error) {
smock := &pgxmockPool{}
smock.ordered = true
return smock, smock.open(options)
}

func (p *pgxmockPool) Close() {
_ = p.close(context.Background())
p.pgxmock.Close(context.Background())
}

func (p *pgxmockPool) Acquire(context.Context) (*pgxpool.Conn, error) {
Expand Down
4 changes: 4 additions & 0 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func TestPools(t *testing.T) {
if mock == mock2 {
t.Errorf("expected not the same mock instance, but it is the same")
}
conn := mock.AsConn()
if conn == nil {
t.Error("expected connection strruct, but got nil")
}
mock.Close()
mock2.Close()
}
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

"github.com/pashagolub/pgxmock/v2"
"github.com/pashagolub/pgxmock/v3"
)

// a successful case
Expand Down
2 changes: 1 addition & 1 deletion examples/blog/blog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"net/http/httptest"
"testing"

"github.com/pashagolub/pgxmock/v2"
"github.com/pashagolub/pgxmock/v3"
)

func (a *api) assertJSON(actual []byte, data interface{}, t *testing.T) {
Expand Down
Loading

0 comments on commit 8840f5b

Please sign in to comment.