testutil

package
v0.0.0-...-6f40dae Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package testutil provides a scriptable database/sql driver for testing sqlkit without a live database.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Call

type Call struct {
	SQL  string
	Args []any
}

Call records one statement sent to the fake database.

type Fake

type Fake struct {

	// QueryFunc serves QueryContext calls. Nil means every query returns an
	// empty result set.
	QueryFunc func(sql string, args []any) (Result, error)
	// ExecFunc serves ExecContext calls. Nil means success.
	ExecFunc func(sql string, args []any) error
	// LastInsertID is reported by the Result of every ExecContext call, so the
	// RETURNING-less write-back path (MySQL-style LastInsertId) is testable.
	LastInsertID int64
	// RowsAffected, when non-nil, is reported by the Result of every ExecContext
	// call (otherwise 1), so the optimistic-locking conflict path (zero rows
	// affected) is testable.
	RowsAffected *int64
	// contains filtered or unexported fields
}

Fake is a scriptable in-memory database driver. Set QueryFunc/ExecFunc to control responses; all statements are recorded.

func (*Fake) BeginOptions

func (f *Fake) BeginOptions() []driver.TxOptions

BeginOptions returns the transaction options of each begun transaction, in order, so tests can observe the isolation level and read-only flag requested.

func (*Fake) Begins

func (f *Fake) Begins() int

Begins returns the number of transactions begun.

func (*Fake) Commits

func (f *Fake) Commits() int

Commits returns the number of committed transactions.

func (*Fake) DB

func (f *Fake) DB() *stdsql.DB

DB opens a database/sql handle backed by this fake.

func (*Fake) Execs

func (f *Fake) Execs() []Call

Execs returns the recorded exec statements.

func (*Fake) Queries

func (f *Fake) Queries() []Call

Queries returns the recorded query statements.

func (*Fake) Rollbacks

func (f *Fake) Rollbacks() int

Rollbacks returns the number of rolled-back transactions, so tests can assert that a failed or panicking unit of work actually rolled its transaction back rather than merely failing to commit.

func (*Fake) RowCloses

func (f *Fake) RowCloses() int

RowCloses returns the number of result-set cursors that were closed, so tests can assert a query's rows are always released — on the happy path and when scanning or iteration fails.

func (*Fake) RowReads

func (f *Fake) RowReads() int

RowReads returns the number of rows handed to the caller across all queries, which observes how far result sets were actually read.

func (*Fake) Statements

func (f *Fake) Statements() []string

Statements returns all recorded statements in order of execution.

type Result

type Result struct {
	Columns []string
	Rows    [][]driver.Value
}

Result is a canned query result.

Jump to

Keyboard shortcuts

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