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 ¶
- func NewMockedBunDB() (*bun.DB, error)
- type Bunoffe
- func (b Bunoffe) DeleteWherePK(ctx context.Context, model any, pks ...string) (sql.Result, error)
- func (b Bunoffe) ExistsWhere(ctx context.Context, model any, cond string, condArgs ...any) (bool, error)
- func (b Bunoffe) ExistsWherePK(ctx context.Context, model any, pks ...string) (bool, error)
- func (b Bunoffe) Insert(ctx context.Context, model any) (sql.Result, error)
- func (b Bunoffe) ScanWhere(ctx context.Context, model any, cond string, condArgs ...any) error
- func (b Bunoffe) ScanWherePK(ctx context.Context, model any, pks ...string) error
- func (b Bunoffe) SelectWhere(ctx context.Context, model any, cond string, args ...any) (sql.Result, error)
- func (b Bunoffe) SelectWherePK(ctx context.Context, model any, pks ...string) (sql.Result, error)
- func (b Bunoffe) Update(ctx context.Context, model any) (sql.Result, error)
- type ExecQuery
- type Executor
- type ExistsQuery
- type MockExecOperation
- type MockExistsOperation
- type MockQueryExecutor
- type MockQueryResult
- type MockScanOperation
- type MockedQueryOperation
- type QueryRealizer
- type ScanQuery
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewMockedBunDB ¶
Creates a *bun.DB with a mocked database.
Types ¶
type Bunoffe ¶ added in v0.2.0
Bunoffe is similar to a repository in some ORMs: a set of commonly used queries.
func (Bunoffe) DeleteWherePK ¶ added in v0.2.0
func (Bunoffe) ExistsWhere ¶ added in v0.2.0
func (Bunoffe) ExistsWherePK ¶ added in v0.2.0
func (Bunoffe) ScanWherePK ¶ added in v0.2.0
func (Bunoffe) SelectWhere ¶ added in v0.2.0
func (Bunoffe) SelectWherePK ¶ added in v0.2.0
type ExecQuery ¶
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 ¶
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 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.
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 ¶
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)