Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/driftprogramming/pgxpoolmock
pgx postgresql pool
https://github.com/driftprogramming/pgxpoolmock
go mock pgx pool postgresql
Last synced: 2 days ago
JSON representation
pgx postgresql pool
- Host: GitHub
- URL: https://github.com/driftprogramming/pgxpoolmock
- Owner: driftprogramming
- License: mit
- Created: 2021-06-04T04:27:55.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-03-08T13:57:24.000Z (8 months ago)
- Last Synced: 2024-06-18T17:17:29.216Z (5 months ago)
- Topics: go, mock, pgx, pool, postgresql
- Language: Go
- Homepage:
- Size: 720 KB
- Stars: 38
- Watchers: 3
- Forks: 16
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
### PGX POOL MOCK
This repo is mock `pgxpool` in https://github.com/jackc/pgx. so that you can test your data access code locally, no real database is needed.
I also strongly recommend you use `pgxpool` rather than `pgx` only. see https://github.com/jackc/pgx/tree/master/pgxpool### How to install
```
go get -u github.com/driftprogramming/pgxpoolmock
```### How to Use
See file `order_dao_example_test.go` to figure out how to use it. Or see the below:
```go
package pgxpoolmock_testimport (
"testing""github.com/driftprogramming/pgxpoolmock"
"github.com/driftprogramming/pgxpoolmock/testdata"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)func TestInsertOrder(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()mockPool := pgxpoolmock.NewMockPgxPool(ctrl)
commandTag := pgconn.CommandTag("INSERT 0 1")
mockPool.EXPECT().Exec(gomock.Any(), "insert into order (id, price) values ($1, $2)", 1, 2.3).Return(commandTag, nil)
orderDao := testdata.OrderDAO{
Pool: mockPool,
}order := &testdata.Order{
ID: 1,
Price: 2.3,
}
err := orderDao.InsertOrder(order)
if err != nil {
t.Fatalf("%v", err)
}
}func TestGetOneOrderByID(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockPool := pgxpoolmock.NewMockPgxPool(ctrl)
columns := []string{"id", "price"}
pgxRow := pgxpoolmock.NewRow(columns, 1, 2.3)
mockPool.EXPECT().QueryRow(gomock.Any(), "select id, price from order where id = $1", 1).Return(pgxRow)
orderDao := testdata.OrderDAO{
Pool: mockPool,
}
actualOrder, err := orderDao.GetOneOrderByID(1)
if err != nil {
t.Fatalf("%v", err)
}
assert.NotNil(t, actualOrder)
assert.Equal(t, 1, actualOrder.ID)
assert.Equal(t, 2.3, actualOrder.Price)
}func TestName(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
defer ctrl.Finish()// given
mockPool := pgxpoolmock.NewMockPgxPool(ctrl)
columns := []string{"id", "price"}
pgxRows := pgxpoolmock.NewRows(columns).AddRow(100, 100000.9).ToPgxRows()
mockPool.EXPECT().Query(gomock.Any(), gomock.Any(), gomock.Any()).Return(pgxRows, nil)
orderDao := testdata.OrderDAO{
Pool: mockPool,
}// when
actualOrder := orderDao.GetOrderByID(1)// then
assert.NotNil(t, actualOrder)
assert.Equal(t, 100, actualOrder.ID)
assert.Equal(t, 100000.9, actualOrder.Price)
}```