pgxpoolmock

package module
v0.0.0-...-53d77c5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 22, 2023 License: MIT Imports: 10 Imported by: 0

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:

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 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)
}

Documentation

Overview

Code generated by MockGen. DO NOT EDIT. Source: pgx_pool.go

Generated by this command:

mockgen.exe -source=pgx_pool.go -destination=pgx_pool_mock.go

Package mock_pgxpoolmock is a generated GoMock package.

Index

Constants

This section is empty.

Variables

View Source
var CSVColumnParser = func(s string) interface{} {
	switch {
	case strings.ToLower(s) == "null":
		return nil
	}
	return s
}

CSVColumnParser is a function which converts trimmed csv column string to a []byte representation. Currently transforms NULL to nil

Functions

This section is empty.

Types

type MockPgxPool

type MockPgxPool struct {
	// contains filtered or unexported fields
}

MockPgxPool is a mock of PgxPool interface.

func NewMockPgxPool

func NewMockPgxPool(ctrl *gomock.Controller) *MockPgxPool

NewMockPgxPool creates a new mock instance.

func (*MockPgxPool) Acquire

func (m *MockPgxPool) Acquire(ctx context.Context) (*pgxpool.Conn, error)

Acquire mocks base method.

func (*MockPgxPool) AcquireAllIdle

func (m *MockPgxPool) AcquireAllIdle(ctx context.Context) []*pgxpool.Conn

AcquireAllIdle mocks base method.

func (*MockPgxPool) AcquireFunc

func (m *MockPgxPool) AcquireFunc(ctx context.Context, f func(*pgxpool.Conn) error) error

AcquireFunc mocks base method.

func (*MockPgxPool) Begin

func (m *MockPgxPool) Begin(ctx context.Context) (pgx.Tx, error)

Begin mocks base method.

func (*MockPgxPool) BeginTx

func (m *MockPgxPool) BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error)

BeginTx mocks base method.

func (*MockPgxPool) Close

func (m *MockPgxPool) Close()

Close mocks base method.

func (*MockPgxPool) Config

func (m *MockPgxPool) Config() *pgxpool.Config

Config mocks base method.

func (*MockPgxPool) CopyFrom

func (m *MockPgxPool) CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error)

CopyFrom mocks base method.

func (*MockPgxPool) EXPECT

func (m *MockPgxPool) EXPECT() *MockPgxPoolMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockPgxPool) Exec

func (m *MockPgxPool) Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error)

Exec mocks base method.

func (*MockPgxPool) Ping

func (m *MockPgxPool) Ping(ctx context.Context) error

Ping mocks base method.

func (*MockPgxPool) Query

func (m *MockPgxPool) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)

Query mocks base method.

func (*MockPgxPool) QueryRow

func (m *MockPgxPool) QueryRow(ctx context.Context, sql string, args ...any) pgx.Row

QueryRow mocks base method.

func (*MockPgxPool) Reset

func (m *MockPgxPool) Reset()

Reset mocks base method.

func (*MockPgxPool) SendBatch

func (m *MockPgxPool) SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults

SendBatch mocks base method.

func (*MockPgxPool) Stat

func (m *MockPgxPool) Stat() *pgxpool.Stat

Stat mocks base method.

type MockPgxPoolMockRecorder

type MockPgxPoolMockRecorder struct {
	// contains filtered or unexported fields
}

MockPgxPoolMockRecorder is the mock recorder for MockPgxPool.

func (*MockPgxPoolMockRecorder) Acquire

func (mr *MockPgxPoolMockRecorder) Acquire(ctx any) *gomock.Call

Acquire indicates an expected call of Acquire.

func (*MockPgxPoolMockRecorder) AcquireAllIdle

func (mr *MockPgxPoolMockRecorder) AcquireAllIdle(ctx any) *gomock.Call

AcquireAllIdle indicates an expected call of AcquireAllIdle.

func (*MockPgxPoolMockRecorder) AcquireFunc

func (mr *MockPgxPoolMockRecorder) AcquireFunc(ctx, f any) *gomock.Call

AcquireFunc indicates an expected call of AcquireFunc.

func (*MockPgxPoolMockRecorder) Begin

func (mr *MockPgxPoolMockRecorder) Begin(ctx any) *gomock.Call

Begin indicates an expected call of Begin.

func (*MockPgxPoolMockRecorder) BeginTx

func (mr *MockPgxPoolMockRecorder) BeginTx(ctx, txOptions any) *gomock.Call

BeginTx indicates an expected call of BeginTx.

func (*MockPgxPoolMockRecorder) Close

func (mr *MockPgxPoolMockRecorder) Close() *gomock.Call

Close indicates an expected call of Close.

func (*MockPgxPoolMockRecorder) Config

func (mr *MockPgxPoolMockRecorder) Config() *gomock.Call

Config indicates an expected call of Config.

func (*MockPgxPoolMockRecorder) CopyFrom

func (mr *MockPgxPoolMockRecorder) CopyFrom(ctx, tableName, columnNames, rowSrc any) *gomock.Call

CopyFrom indicates an expected call of CopyFrom.

func (*MockPgxPoolMockRecorder) Exec

func (mr *MockPgxPoolMockRecorder) Exec(ctx, sql any, arguments ...any) *gomock.Call

Exec indicates an expected call of Exec.

func (*MockPgxPoolMockRecorder) Ping

func (mr *MockPgxPoolMockRecorder) Ping(ctx any) *gomock.Call

Ping indicates an expected call of Ping.

func (*MockPgxPoolMockRecorder) Query

func (mr *MockPgxPoolMockRecorder) Query(ctx, sql any, args ...any) *gomock.Call

Query indicates an expected call of Query.

func (*MockPgxPoolMockRecorder) QueryRow

func (mr *MockPgxPoolMockRecorder) QueryRow(ctx, sql any, args ...any) *gomock.Call

QueryRow indicates an expected call of QueryRow.

func (*MockPgxPoolMockRecorder) Reset

func (mr *MockPgxPoolMockRecorder) Reset() *gomock.Call

Reset indicates an expected call of Reset.

func (*MockPgxPoolMockRecorder) SendBatch

func (mr *MockPgxPoolMockRecorder) SendBatch(ctx, b any) *gomock.Call

SendBatch indicates an expected call of SendBatch.

func (*MockPgxPoolMockRecorder) Stat

func (mr *MockPgxPoolMockRecorder) Stat() *gomock.Call

Stat indicates an expected call of Stat.

type PgxPool

type PgxPool interface {
	Acquire(ctx context.Context) (*pgxpool.Conn, error)
	AcquireAllIdle(ctx context.Context) []*pgxpool.Conn
	AcquireFunc(ctx context.Context, f func(*pgxpool.Conn) error) error
	Begin(ctx context.Context) (pgx.Tx, error)
	BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error)
	Close()
	Config() *pgxpool.Config
	CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error)
	Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error)
	Ping(ctx context.Context) error
	Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row
	Reset()
	SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults
	Stat() *pgxpool.Stat
}

type Row

type Row struct {
	// contains filtered or unexported fields
}

Row is a mocked row that can be returned from QueryRow

func NewRow

func NewRow(columns []string, values ...interface{}) *Row

func (*Row) RowError

func (r *Row) RowError(row int, err error) *Row

RowError allows to set an error which will be returned when the row is read

func (*Row) Scan

func (r *Row) Scan(dest ...interface{}) error

type Rows

type Rows struct {
	// contains filtered or unexported fields
}

Rows is a mocked collection of rows to return for Query result

func NewRows

func NewRows(columns []string) *Rows

NewMockRows allows Rows to be created from a sql interface{} slice or from the CSV string and to be used as sql driver.Rows. Use Sqlmock.NewRows instead if using a custom converter

func NewRowsWithColumnDefinition

func NewRowsWithColumnDefinition(columns ...pgconn.FieldDescription) *Rows

NewRowsWithColumnDefinition return rows with columns metadata

func (*Rows) AddRow

func (r *Rows) AddRow(values ...interface{}) *Rows

AddRow composed from database interface{} slice return the same instance to perform subsequent actions. Note that the number of values must match the number of columns

func (*Rows) CloseError

func (r *Rows) CloseError(err error) *Rows

CloseError allows to set an error which will be returned by rows.Close function.

The close error will be triggered only in cases when rows.Next() EOF was not yet reached, that is a default sql library behavior

func (*Rows) FromCSVString

func (r *Rows) FromCSVString(s string) *Rows

FromCSVString build rows from csv string. return the same instance to perform subsequent actions. Note that the number of values must match the number of columns

func (*Rows) RowError

func (r *Rows) RowError(row int, err error) *Rows

RowError allows to set an error which will be returned when a given row number is read

func (*Rows) ToPgxRows

func (r *Rows) ToPgxRows() pgx.Rows

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL