querycount

package
v0.9.0 Latest Latest
Warning

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

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

Documentation

Overview

Package querycount counts the statements a test's database sends to the driver. It wraps the database testdb opens for the test on any of the three test dialects, so a count is taken at the same point on each. It is test-only and internal: production code and hosts never see it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Counter

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

Counter records each statement that reaches the driver, in order. Statements sent through a transaction count the same as any other.

func Attach

func Attach(t testing.TB) *Counter

Attach routes the database testdb opens for t through a recording driver and returns the Counter that records it. Statements are recorded from the moment testdb opens the database, so call Reset after any setup the test does not want counted.

func (*Counter) Events

func (c *Counter) Events() []Event

Events returns every statement and transaction boundary recorded so far, in the order the driver saw them. Statements is the statement subset of it.

func (*Counter) FailMatching

func (c *Counter) FailMatching(fragment string, err error)

FailMatching makes each later statement that contains fragment fail with err before it reaches the driver, and does not record it. It exists so a test can drive an error path that the database would not produce on its own.

func (*Counter) FailNextRollback

func (c *Counter) FailNextRollback(err error)

FailNextRollback makes the next rollback the database is asked for roll the transaction back in the driver and then return err, so a test can drive the path where a deferred rollback fails. It is armed once.

func (*Counter) FailNthMatching

func (c *Counter) FailNthMatching(fragment string, n int, err error)

FailNthMatching makes the nth later statement (1-based) that contains fragment fail with err before it reaches the driver, and lets every other statement through. It exists so a test can fail a later chunk of a read whose earlier chunks must succeed.

func (*Counter) Matching

func (c *Counter) Matching(fragment string) int

Matching returns how many recorded statements contain fragment as a literal substring. It does not interpret fragment as a pattern.

func (*Counter) Reset

func (c *Counter) Reset()

Reset discards the statements recorded so far.

func (*Counter) RunBeforeMatching

func (c *Counter) RunBeforeMatching(fragment string, n int, fn func())

RunBeforeMatching calls fn once, just before the nth later statement (1-based) that contains fragment reaches the driver. fn runs on the statement's goroutine and may use another connection of the same database to change the data, so a test can commit a write between two statements of one read at a chosen point, without sleeping.

func (*Counter) Statements

func (c *Counter) Statements() []string

Statements returns the SQL text of each recorded statement, in order.

type Event

type Event struct {
	Kind      Kind
	Statement string
	Isolation sql.IsolationLevel
	ReadOnly  bool
}

Event is one entry in the order the driver saw it. A statement event carries its SQL text. A begin event carries the isolation level and the read-only flag the transaction was opened with; the other kinds carry neither.

type Kind

type Kind string

Kind says what an Event records.

const (
	// KindStatement is a statement that reached the driver.
	KindStatement Kind = "statement"
	// KindBegin is a transaction the driver began.
	KindBegin Kind = "begin"
	// KindCommit is a transaction the driver was asked to commit.
	KindCommit Kind = "commit"
	// KindRollback is a transaction the driver was asked to roll back.
	KindRollback Kind = "rollback"
)

Jump to

Keyboard shortcuts

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