storagefake

package
v1.53.4 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package storagefake provides a configurable double for storage.Database, storage.Transaction, and storage.DBTX, for tests that exercise real service/server composition or drive sqlc-generated queries without a live DB connection.

This lives in its own subpackage rather than directly under internal/testutil: pkg/storage (and pkg/base/pkg/config, which it depends on) already import internal/testutil from their own internal test files (for ChdirRepoRoot), so a FakeDatabase living there and importing pkg/storage would create an import cycle for those packages' tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Row

func Row(fn func(dest ...any) error) pgx.Row

Row returns a pgx.Row whose Scan delegates to fn.

func Rows

func Rows(scans ...func(dest ...any) error) pgx.Rows

Rows returns a pgx.Rows yielding one row per scan func, in order.

Types

type FakeDatabase

type FakeDatabase struct {
	QueryFunc     func(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
	QueryRowFunc  func(ctx context.Context, sql string, args ...any) pgx.Row
	ExecFunc      func(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
	BeginTxFunc   func(ctx context.Context) (storage.Transaction, error)
	PoolStatsFunc func() (dbstats.PoolStats, bool)
	PingErr       error
	CloseErr      error
}

FakeDatabase is a configurable double for storage.Database (used at the service/server composition level) and storage.Transaction/storage.DBTX (used to drive sqlc-generated queries directly). Set only the hooks the code under test is expected to call; an unset Query/QueryRow/Exec/BeginTx hook returns a descriptive error so an unexpected call fails loudly instead of touching a nil driver. Ping, Close, Commit, and Rollback default to succeeding, since most callers only care about business-logic reachability, not connection/transaction lifecycle.

func (*FakeDatabase) BeginTx

func (f *FakeDatabase) BeginTx(ctx context.Context) (storage.Transaction, error)

func (*FakeDatabase) Close

func (f *FakeDatabase) Close() error

func (*FakeDatabase) Commit

func (f *FakeDatabase) Commit(context.Context) error

func (*FakeDatabase) Exec

func (f *FakeDatabase) Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)

func (*FakeDatabase) Ping

func (*FakeDatabase) PoolStats added in v1.44.0

func (f *FakeDatabase) PoolStats() (dbstats.PoolStats, bool)

PoolStats satisfies pkg/telemetry/db.PoolStatter, so tests can opt in a FakeDatabase to prove pool-metric wiring without a real pgxpool. Unset PoolStatsFunc reports ok=false, matching a not-connected pool.

func (*FakeDatabase) Query

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

func (*FakeDatabase) QueryRow

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

func (*FakeDatabase) Rollback

func (f *FakeDatabase) Rollback(context.Context) error

Jump to

Keyboard shortcuts

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