Skip to content

Commit 6a54eb0

Browse files
committed
add test example
1 parent b90e8e9 commit 6a54eb0

24 files changed

+1705
-36
lines changed

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0f
173173
github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
174174
github.com/jackc/puddle v1.1.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
175175
github.com/jackc/puddle v1.1.1/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
176+
github.com/jackc/puddle v1.1.3 h1:JnPg/5Q9xVJGfjsO5CPUOjnJps1JaRUm8I9FXVCFK94=
176177
github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
177178
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
178179
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
File renamed without changes.

order_dao_example_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package pgxpoolmock_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/driftprogramming/pgxpoolmock"
7+
"github.com/driftprogramming/pgxpoolmock/testdata"
8+
"github.com/golang/mock/gomock"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestName(t *testing.T) {
13+
t.Parallel()
14+
ctrl := gomock.NewController(t)
15+
defer ctrl.Finish()
16+
17+
// given
18+
mockPool := pgxpoolmock.NewMockPgxPool(ctrl)
19+
columns := []string{"id", "price"}
20+
pgxRows := pgxpoolmock.NewRows(columns).AddRow(100, 100000.9).ToPgxRows()
21+
mockPool.EXPECT().Query(gomock.Any(), gomock.Any(), gomock.Any()).Return(pgxRows, nil)
22+
orderDao := testdata.OrderDAO{
23+
Pool: mockPool,
24+
}
25+
26+
// when
27+
actualOrder := orderDao.GetOrderByID(1)
28+
29+
// then
30+
assert.NotNil(t, actualOrder)
31+
assert.Equal(t, 100, actualOrder.ID)
32+
assert.Equal(t, 100000.9, actualOrder.Price)
33+
}

pgx_pool_mock_test.go

Lines changed: 0 additions & 36 deletions
This file was deleted.

testdata/main.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package testdata
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/jackc/pgx/v4/pgxpool"
8+
)
9+
10+
func main() {
11+
connectionString := getConnectionString()
12+
realPool, _ := pgxpool.Connect(context.Background(), connectionString)
13+
orderDao := OrderDAO{
14+
Pool: realPool,
15+
}
16+
order := orderDao.GetOrderByID(100)
17+
fmt.Print(order)
18+
}
19+
20+
func getConnectionString() string {
21+
databaseHost := "10.0.20.101"
22+
databaseUser := "admin"
23+
databasePassword := "admin_password"
24+
databaseName := "orderdatabase"
25+
databaseConnectionPoolMaxSize := 100
26+
databaseConnectionPoolMinSize := 10
27+
defaultPoolHealthCheckPeriod := "10s"
28+
connectionString := fmt.Sprintf(
29+
"host=%s user=%s password=%s dbname=%s pool_max_conns=%s pool_min_conns=%s pool_health_check_period=%s",
30+
databaseHost,
31+
databaseUser,
32+
databasePassword,
33+
databaseName,
34+
databaseConnectionPoolMaxSize,
35+
databaseConnectionPoolMinSize,
36+
defaultPoolHealthCheckPeriod,
37+
)
38+
39+
return connectionString
40+
}

testdata/order.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package testdata
2+
3+
import (
4+
"context"
5+
6+
"github.com/driftprogramming/pgxpoolmock"
7+
)
8+
9+
type Order struct {
10+
ID int
11+
Price float64
12+
}
13+
14+
type OrderDAO struct {
15+
Pool pgxpoolmock.PgxPool
16+
}
17+
18+
func (dao *OrderDAO) GetOrderByID(id int) *Order {
19+
rows, _ := dao.Pool.Query(context.Background(), "SELECT ID,Price FROM order WHERE ID =$1", id)
20+
for rows.Next() {
21+
order := &Order{}
22+
rows.Scan(&order.ID, &order.Price)
23+
return order
24+
}
25+
26+
return nil
27+
}

vendor/github.com/jackc/pgx/v4/pgxpool/batch_results.go

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/jackc/pgx/v4/pgxpool/conn.go

Lines changed: 107 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/jackc/pgx/v4/pgxpool/doc.go

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)