bunoffe

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: BSD-3-Clause Imports: 7 Imported by: 0

README

Overview

Bunoffe is a small library to facilitate testing bun's queries. One should feel free to copy and paste it directly into the code he/she is working on.

Usage

Exec

Instead of writing

db := bun.NewDB(sqldb, sqlitedialect.New())

result, err := bundb.NewExec().
    Model(&m).
    Exec(ctx)

Do

db := bun.NewDB(sqldb, sqlitedialect.New())
executor := bunoffe.QueryRealizer{}

err := executor.Exec(
    ctx,
    bundb.NewExec().
        Model(&m),
)

Exists

Instead of writing

db := bun.NewDB(sqldb, sqlitedialect.New())

exists, err := bundb.NewSelect().
    Model(&m).
    Exists(ctx)

Do

db := bun.NewDB(sqldb, sqlitedialect.New())
executor := bunoffe.QueryRealizer{}

exists, err := executor.Exists(
    ctx,
    bundb.NewSelect().
        Model(&m),
)

Scan

Instead of writing

db := bun.NewDB(sqldb, sqlitedialect.New())

err := bundb.NewSelect().
    Model(&m).
    Scan(ctx)

Do

db := bun.NewDB(sqldb, sqlitedialect.New())
executor := bunoffe.QueryRealizer{}

err := executor.Scan(
    ctx,
    bundb.NewSelect().
        Model(&m),
)

Testing

Bunoffe provides a set mocked operations. Check it out.

Documentation

Overview

Bunoffe is a small library to facilitate testing bun's (https://github.com/uptrace/bun) queries. One should feel free to copy and paste it directly into the code he/she is working on.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMockedBunDB

func NewMockedBunDB() (*bun.DB, error)

Creates a *bun.DB with a mocked database.

Types

type Bunoffe added in v0.2.0

type Bunoffe struct {
	X  Executor
	DB bun.IDB
}

Bunoffe is similar to a repository in some ORMs: a set of commonly used queries.

func (Bunoffe) DeleteWherePK added in v0.2.0

func (b Bunoffe) DeleteWherePK(
	ctx context.Context,
	model any,
	pks ...string,
) (sql.Result, error)

func (Bunoffe) ExistsWhere added in v0.2.0

func (b Bunoffe) ExistsWhere(
	ctx context.Context,
	model any,
	cond string,
	condArgs ...any,
) (bool, error)

func (Bunoffe) ExistsWherePK added in v0.2.0

func (b Bunoffe) ExistsWherePK(
	ctx context.Context,
	model any,
	pks ...string,
) (bool, error)

func (Bunoffe) Insert added in v0.2.0

func (b Bunoffe) Insert(ctx context.Context, model any) (sql.Result, error)

func (Bunoffe) ScanWhere added in v0.2.0

func (b Bunoffe) ScanWhere(
	ctx context.Context,
	model any,
	cond string,
	condArgs ...any,
) error

func (Bunoffe) ScanWherePK added in v0.2.0

func (b Bunoffe) ScanWherePK(ctx context.Context, model any, pks ...string) error

func (Bunoffe) SelectWhere added in v0.2.0

func (b Bunoffe) SelectWhere(
	ctx context.Context,
	model any,
	cond string,
	args ...any,
) (sql.Result, error)

func (Bunoffe) SelectWherePK added in v0.2.0

func (b Bunoffe) SelectWherePK(
	ctx context.Context,
	model any,
	pks ...string,
) (sql.Result, error)

func (Bunoffe) Update added in v0.2.0

func (b Bunoffe) Update(ctx context.Context, model any) (sql.Result, error)

type ExecQuery

type ExecQuery interface {
	Exec(context.Context, ...any) (sql.Result, error)
	GetModel() bun.Model
}

ExecQuery is the interface that wraps the method Exec. Every bun query can run Exec.

Besides de Exec method, the GetModel method is required for the MockQueryExecutor.

type Executor

type Executor interface {
	Exec(context.Context, ExecQuery, ...any) (sql.Result, error)
	Scan(context.Context, ScanQuery, ...any) error
	Exists(context.Context, ExistsQuery) (bool, error)
}

Executor is the interface that wraps the methods of a query executor type. Bun's queries can be executed with one of the following methods: Exec, Scan, and Exists. Instead of calling them directly, when using and Executor, you should use them indirectly. For instance:

err := executor.Scan(
    ctx,
    db.NewSelect().Model(&m).WherePK(),
)

type ExistsQuery

type ExistsQuery interface {
	Exists(context.Context) (bool, error)
	GetModel() bun.Model
}

ExistsQuery is the interface that wraps the method Exists.

Besides de Exec method, the GetModel method is required for the MockQueryExecutor.

type MockExecOperation

type MockExecOperation struct {
	// If Model is not nil and Error is nil, when Exec is called, it will
	// contain the value passed to the query method `.Model(&m)`.
	Model any

	// If Args is not nil and Error is nil, when Exec is called, each of
	// its values will be assigned to parameter `...args`.
	Args []any

	// If Result is not nil and Error is nil, when Exec is called, it will
	// return Result.
	Result sql.Result

	// If Error is not nil, Exec will return a nil sql.Result and this
	// Error.
	Error error
}

MockExecOperation is a type to mock a Exec call.

type MockExistsOperation

type MockExistsOperation struct {
	// If Error is not nil, this value will be returned when Exists is
	// called. Otherwise false is returned.
	Exists bool

	// If Error is not nil, Scan will return it.
	Error error
}

type MockQueryExecutor

type MockQueryExecutor struct {
	// Ops is a slice of operations. Each time an Executor method
	// is called, next operation in line (starting with the first)
	// will be executed.
	Ops []MockedQueryOperation
	// contains filtered or unexported fields
}

MockQueryExecutor is an Executor that ignores the queries passed to its methods (Exec, Scan, and Exists). Instead, the returned values and values assigned to the model are the ones provided to operations (Ops field).

func (*MockQueryExecutor) Exec

func (ex *MockQueryExecutor) Exec(
	ctx context.Context,
	q ExecQuery,
	args ...any,
) (sql.Result, error)

Exec mocks a query.Exec call. See the MockExecOperation documentation for details.

func (*MockQueryExecutor) Exists

func (ex *MockQueryExecutor) Exists(ctx context.Context, q ExistsQuery) (bool, error)

Exec mocks a query.Exists call. See the MockExistsOperation documentation for details.

func (*MockQueryExecutor) Scan

func (ex *MockQueryExecutor) Scan(ctx context.Context, q ScanQuery, args ...any) error

Exec mocks a query.Scan call. See the MockScanOperation documentation for details.

type MockQueryResult

type MockQueryResult struct {
	LastInsertIdValue int64
	LastInsertIdError error

	RowsAffectedValue int64
	RowsAffectedError error
}

func (MockQueryResult) LastInsertId

func (r MockQueryResult) LastInsertId() (int64, error)

func (MockQueryResult) RowsAffected

func (r MockQueryResult) RowsAffected() (int64, error)

type MockScanOperation

type MockScanOperation struct {
	// If Model is not nil and Error is nil, when Scan is called, it will
	// be assigned the value passed to the query method `.Model(&m)`.
	Model any

	// If Args is not nil and Error is nil, when Exec is called, each of
	// its values will be assigned to parameter `...args`.
	Args []any

	// If Error is not nil, Scan will return it.
	Error error
}

MockScanOperation is a type to mock a Scan call.

type MockedQueryOperation

type MockedQueryOperation interface {
	// contains filtered or unexported methods
}

MockedQueryOperation is interface that works as common type for all mock operations.

type QueryRealizer

type QueryRealizer struct{}

QueryRealizer is the type of a Executor that executes the queries that are passed to one of its methods. Using the realizer has the same effect of executing a bun query directly.

func (QueryRealizer) Exec

func (QueryRealizer) Exec(
	ctx context.Context,
	q ExecQuery,
	args ...any,
) (sql.Result, error)

Exec executes a bun query that has the Exec method. Calling:

executor.Exec(ctx, query, args...)

is equivalent to running

query.Exec(ctx, args...)

func (QueryRealizer) Exists

func (QueryRealizer) Exists(ctx context.Context, q ExistsQuery) (bool, error)

Exists executes a bun query that has the Exists method. Calling:

executor.Exists(ctx, query)

is equivalent to running

query.Exists(ctx)

func (QueryRealizer) Scan

func (QueryRealizer) Scan(ctx context.Context, q ScanQuery, args ...any) error

Scan executes a bun query that has the Scan method. Calling:

executor.Scan(ctx, query, args...)

is equivalent to running

query.Scan(ctx, args...)

type ScanQuery

type ScanQuery interface {
	Scan(context.Context, ...any) error
	GetModel() bun.Model
}

ScanQuery is the interface that wraps the method Scan.

Besides de Exec method, the GetModel method is required for the MockQueryExecutor.

Jump to

Keyboard shortcuts

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