Skip to content

Commit

Permalink
add test example
Browse files Browse the repository at this point in the history
  • Loading branch information
denghejun committed Jun 4, 2021
1 parent b90e8e9 commit 6a54eb0
Show file tree
Hide file tree
Showing 24 changed files with 1,705 additions and 36 deletions.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0f
github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v1.1.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v1.1.1/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v1.1.3 h1:JnPg/5Q9xVJGfjsO5CPUOjnJps1JaRUm8I9FXVCFK94=
github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
Expand Down
File renamed without changes.
33 changes: 33 additions & 0 deletions order_dao_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package pgxpoolmock_test

import (
"testing"

"github.com/driftprogramming/pgxpoolmock"
"github.com/driftprogramming/pgxpoolmock/testdata"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)

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)
}
36 changes: 0 additions & 36 deletions pgx_pool_mock_test.go

This file was deleted.

40 changes: 40 additions & 0 deletions testdata/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package testdata

import (
"context"
"fmt"

"github.com/jackc/pgx/v4/pgxpool"
)

func main() {
connectionString := getConnectionString()
realPool, _ := pgxpool.Connect(context.Background(), connectionString)
orderDao := OrderDAO{
Pool: realPool,
}
order := orderDao.GetOrderByID(100)
fmt.Print(order)
}

func getConnectionString() string {
databaseHost := "10.0.20.101"
databaseUser := "admin"
databasePassword := "admin_password"
databaseName := "orderdatabase"
databaseConnectionPoolMaxSize := 100
databaseConnectionPoolMinSize := 10
defaultPoolHealthCheckPeriod := "10s"
connectionString := fmt.Sprintf(
"host=%s user=%s password=%s dbname=%s pool_max_conns=%s pool_min_conns=%s pool_health_check_period=%s",
databaseHost,
databaseUser,
databasePassword,
databaseName,
databaseConnectionPoolMaxSize,
databaseConnectionPoolMinSize,
defaultPoolHealthCheckPeriod,
)

return connectionString
}
27 changes: 27 additions & 0 deletions testdata/order.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package testdata

import (
"context"

"github.com/driftprogramming/pgxpoolmock"
)

type Order struct {
ID int
Price float64
}

type OrderDAO struct {
Pool pgxpoolmock.PgxPool
}

func (dao *OrderDAO) GetOrderByID(id int) *Order {
rows, _ := dao.Pool.Query(context.Background(), "SELECT ID,Price FROM order WHERE ID =$1", id)
for rows.Next() {
order := &Order{}
rows.Scan(&order.ID, &order.Price)
return order
}

return nil
}
52 changes: 52 additions & 0 deletions vendor/github.com/jackc/pgx/v4/pgxpool/batch_results.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

107 changes: 107 additions & 0 deletions vendor/github.com/jackc/pgx/v4/pgxpool/conn.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions vendor/github.com/jackc/pgx/v4/pgxpool/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6a54eb0

Please sign in to comment.