Documentation
¶
Overview ¶
Package dbunit prepares and validates explicit SQL database fixtures.
Index ¶
- Variables
- func SortMismatches(m []Mismatch)
- type AssertionError
- type Case
- type ColumnSpec
- type CompareRequest
- type Dataset
- type Engine
- func (e *Engine) ApplySchema(ctx context.Context, req SchemaApplyRequest) (*SchemaResult, error)
- func (e *Engine) Compare(ctx context.Context, req CompareRequest, options ...RunOption) (*Report, error)
- func (e *Engine) Compile(ctx context.Context, c Case) (*Plan, error)
- func (e *Engine) Exec(ctx context.Context, req ExecRequest, options ...RunOption) (result *ExecResult, err error)
- func (e *Engine) Expect(ctx context.Context, req ExpectRequest, options ...RunOption) (*Report, error)
- func (e *Engine) Export(ctx context.Context, req ExportRequest, options ...RunOption) (*ExportResult, error)
- func (e *Engine) ExtractSchema(ctx context.Context, req SchemaExtractRequest) (*SchemaArtifact, error)
- func (e *Engine) Hydrate(ctx context.Context, req HydrateRequest, options ...RunOption) (*HydrateResult, error)
- func (e *Engine) Map(ctx context.Context, req MapRequest, options ...RunOption) (*MapResult, error)
- func (e *Engine) Ping(ctx context.Context) error
- func (e *Engine) Prepare(ctx context.Context, req PrepareRequest, options ...RunOption) (result *PrepareResult, err error)
- func (e *Engine) Query(ctx context.Context, req QueryRequest, options ...RunOption) (*QueryResult, error)
- func (e *Engine) Run(ctx context.Context, definition Case, options ...RunOption) (report *Report, err error)
- func (e *Engine) RunPlan(ctx context.Context, p *Plan, options ...RunOption) (*Report, error)
- func (e *Engine) Sequence(ctx context.Context, req SequenceRequest, options ...RunOption) (*SequenceResult, error)
- type ExecRequest
- type ExecResult
- type Executor
- type ExpectRequest
- type Expectation
- type ExportRequest
- type ExportResult
- type FixtureSequence
- type FixtureSequenceState
- type Generation
- type HydrateRequest
- type HydrateResult
- type HydratedTable
- type HydrationCapture
- type HydrationStateCapture
- type HydrationTable
- type Identifier
- type LoadOptions
- type MapRequest
- type MapResult
- type Mapping
- type MappingColumn
- type MappingConflictError
- type MappingPlan
- type MappingScope
- type MappingTable
- type MatchOptions
- type Mismatch
- type MissingRequiredPolicy
- type NativeLoadOptions
- type Operation
- type OperationError
- type Option
- type Plan
- type PrepareRequest
- type PrepareResult
- type QueryRequest
- type QueryResult
- type QuerySpec
- type RecordReference
- type Report
- type Run
- type RunOption
- type SchemaApplyRequest
- type SchemaArtifact
- type SchemaExtractRequest
- type SchemaResult
- type Scope
- type SequenceRequest
- type SequenceResult
- type Statement
- type StepResult
- type Strategy
- type TransactionMode
Constants ¶
This section is empty.
Variables ¶
var ErrMappingConflict = errors.New("conflicting mapped row")
Functions ¶
func SortMismatches ¶
func SortMismatches(m []Mismatch)
SortMismatches provides deterministic presentation for consumers merging reports.
Types ¶
type AssertionError ¶
type AssertionError struct{ Report *Report }
func (*AssertionError) Error ¶
func (a *AssertionError) Error() string
type ColumnSpec ¶
type CompareRequest ¶
type CompareRequest struct {
Name string `json:"name,omitempty"`
Expected QueryRequest `json:"expected"`
Actual QueryRequest `json:"actual"`
Match MatchOptions `json:"match"`
}
CompareRequest compares two explicit query results, including named databases. The caller supplies deterministic ORDER BY clauses when Ordered is enabled.
type Dataset ¶
type Dataset struct {
Name string `json:"name,omitempty"`
Database string `json:"database,omitempty"`
Table Identifier `json:"table"`
Source fixture.Source `json:"source"`
Operation Operation `json:"operation,omitempty"`
Load LoadOptions `json:"load,omitempty"`
Columns []ColumnSpec `json:"columns,omitempty"`
Keys []string `json:"keys,omitempty"`
DependsOn []string `json:"dependsOn,omitempty"`
}
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
func (*Engine) ApplySchema ¶
func (e *Engine) ApplySchema(ctx context.Context, req SchemaApplyRequest) (*SchemaResult, error)
func (*Engine) Exec ¶
func (e *Engine) Exec(ctx context.Context, req ExecRequest, options ...RunOption) (result *ExecResult, err error)
func (*Engine) Export ¶
func (e *Engine) Export(ctx context.Context, req ExportRequest, options ...RunOption) (*ExportResult, error)
func (*Engine) ExtractSchema ¶
func (e *Engine) ExtractSchema(ctx context.Context, req SchemaExtractRequest) (*SchemaArtifact, error)
func (*Engine) Hydrate ¶
func (e *Engine) Hydrate(ctx context.Context, req HydrateRequest, options ...RunOption) (*HydrateResult, error)
func (*Engine) Prepare ¶
func (e *Engine) Prepare(ctx context.Context, req PrepareRequest, options ...RunOption) (result *PrepareResult, err error)
func (*Engine) Query ¶
func (e *Engine) Query(ctx context.Context, req QueryRequest, options ...RunOption) (*QueryResult, error)
func (*Engine) Sequence ¶
func (e *Engine) Sequence(ctx context.Context, req SequenceRequest, options ...RunOption) (*SequenceResult, error)
type ExecRequest ¶
type ExecRequest struct {
Database string `json:"database,omitempty"`
Statements []Statement `json:"statements"`
State data.Map `json:"state,omitempty"`
}
ExecRequest runs explicit SQL statements against a single connection/transaction. Only bound arguments are expanded; SQL text is sent as authored. Statements are direct INSERT/UPDATE/DELETE/MERGE/REPLACE commands; CTE-wrapped commands are unsupported. Statements are DML; use ApplySchema for DDL and transaction options for ownership.
type ExecResult ¶
type ExpectRequest ¶
type ExpectRequest struct {
Expectations []Expectation `json:"expectations"`
State data.Map `json:"state,omitempty"`
}
type Expectation ¶
type Expectation struct {
Name string `json:"name,omitempty"`
Database string `json:"database,omitempty"`
Table Identifier `json:"table,omitempty"`
Query *QuerySpec `json:"query,omitempty"`
Expected fixture.Source `json:"expected"`
Scope Scope `json:"scope,omitempty"`
Match MatchOptions `json:"match,omitempty"`
}
type ExportRequest ¶
type ExportRequest struct {
Query QueryRequest `json:"query"`
Format string `json:"format,omitempty"`
}
ExportRequest captures an explicit bounded query as a reusable fixture. Persist Content to an explicit destination chosen by the caller.
type ExportResult ¶
type FixtureSequence ¶
type FixtureSequence struct {
Name string `json:"name"`
Start int64 `json:"start,omitempty"`
// StartFrom is an explicit private-state path for continuation. It is not a template.
StartFrom string `json:"startFrom,omitempty"`
Step int64 `json:"step,omitempty"`
Seed *QueryRequest `json:"seed,omitempty"`
}
FixtureSequence is a local deterministic counter. Start includes zero; Step defaults to one. Seed replaces Start using one exact integer query result. A database seed is a snapshot, never a concurrent database ID reservation.
type FixtureSequenceState ¶
type Generation ¶
type Generation struct {
Field []string `json:"field,omitempty"`
State string `json:"state,omitempty"`
Sequence string `json:"sequence,omitempty"`
UUID bool `json:"uuid,omitempty"`
Value any `json:"value,omitempty"`
HasValue bool `json:"hasValue,omitempty"`
Overwrite bool `json:"overwrite,omitempty"`
}
Generation selects exactly one of Sequence, UUID, or Value. HasValue permits an explicit nil Value. Field is an exact nested map path, never dotted SQL. An existing Field is retained without consuming a value unless Overwrite. State optionally names a private state alias available to following steps.
type HydrateRequest ¶
type HydrateRequest struct {
State data.Map `json:"state,omitempty"`
Sequences []FixtureSequence `json:"sequences,omitempty"`
Tables []HydrationTable `json:"tables"`
// UUID is an invocation-local test hook; nil uses crypto/rand UUID v4.
UUID func() (string, error) `json:"-"`
MaxRows int `json:"maxRows,omitempty"`
StateCaptures []HydrationStateCapture `json:"stateCaptures,omitempty"`
}
type HydrateResult ¶
type HydrateResult struct {
Tables []HydratedTable `json:"tables"`
Sequences map[string]FixtureSequenceState `json:"sequences"`
Captures data.Map `json:"captures"`
Tags map[string]map[string]map[string]any `json:"tags,omitempty"`
}
type HydratedTable ¶
type HydratedTable struct {
Name string `json:"name"`
Table Identifier `json:"table,omitempty"`
Source fixture.Source `json:"source"`
}
type HydrationCapture ¶
type HydrationCapture struct {
Field []string `json:"field,omitempty"`
Path string `json:"path"`
Append bool `json:"append,omitempty"`
}
HydrationCapture publishes a final row or field under an explicit state path. Append collects ordered values; duplicate non-append destinations are errors.
type HydrationStateCapture ¶
type HydrationTable ¶
type HydrationTable struct {
Name string `json:"name,omitempty"`
Table Identifier `json:"table,omitempty"`
Source fixture.Source `json:"source"`
// TagField explicitly selects and removes a tag field, e.g. ["!tag"].
TagField []string `json:"tagField,omitempty"`
// Before runs once, even for an empty source, and writes state aliases only.
Before []Generation `json:"before,omitempty"`
Generate []Generation `json:"generate,omitempty"`
// PostIncrement runs once per nonempty table entry after its rows expand.
PostIncrement []string `json:"postIncrement,omitempty"`
References []RecordReference `json:"references,omitempty"`
Captures []HydrationCapture `json:"captures,omitempty"`
}
type Identifier ¶
type Identifier []string
Identifier preserves components: Table("a.b") names one identifier containing a dot.
func Table ¶
func Table(parts ...string) Identifier
func (*Identifier) UnmarshalJSON ¶
func (i *Identifier) UnmarshalJSON(data []byte) error
type LoadOptions ¶
type MapRequest ¶
type MapRequest struct {
Mapping Mapping `json:"mapping"`
Source fixture.Source `json:"source"`
State data.Map `json:"state,omitempty"`
}
MapRequest projects an explicitly selected logical fixture into physical tables. Source expressions are expanded once; mapping defaults are literal values.
type MapResult ¶
type MapResult struct {
Datasets []Dataset `json:"datasets"`
}
MapResult contains concrete, detached datasets ready for Prepare. Mapping does not write to the database or reserve generated identifiers.
type Mapping ¶
type Mapping struct {
Name string `json:"name" yaml:"name"`
Tables []MappingTable `json:"tables" yaml:"tables"`
}
Mapping projects one logical fixture into explicitly named physical datasets. It has no mutable registry, expression evaluator or database connection.
type MappingColumn ¶
type MappingColumn struct {
Name string `json:"name" yaml:"name"`
Path []string `json:"path,omitempty" yaml:"path,omitempty"`
Scope MappingScope `json:"scope,omitempty" yaml:"scope,omitempty"`
Default any `json:"default,omitempty" yaml:"default,omitempty"`
HasDefault bool `json:"hasDefault,omitempty" yaml:"hasDefault,omitempty"`
Required bool `json:"required,omitempty" yaml:"required,omitempty"`
}
MappingColumn preserves absent/null/zero distinctions. An empty Path defaults to [Name], except for Element. Defaults are literal values applied only when the source field is absent. HasDefault permits an explicit null default in Go; JSON decoding sets it when a default property is present. Required means present and non-null, including false, zero and the empty string.
func (*MappingColumn) UnmarshalJSON ¶
func (c *MappingColumn) UnmarshalJSON(content []byte) error
func (*MappingColumn) UnmarshalYAML ¶
func (c *MappingColumn) UnmarshalYAML(node *yaml.Node) error
type MappingConflictError ¶
type MappingConflictError struct {
Mapping string
Dataset string
Row int
Path string
Column string
Existing any
Incoming any
}
MappingConflictError identifies a conflicting physical column without hiding either value. No output datasets are returned after a conflict.
func (*MappingConflictError) Error ¶
func (e *MappingConflictError) Error() string
func (*MappingConflictError) Unwrap ¶
func (e *MappingConflictError) Unwrap() error
type MappingPlan ¶
type MappingPlan struct {
// contains filtered or unexported fields
}
MappingPlan is an immutable compiled mapping. Concurrent Map calls share no rows, deduplication state, defaults or input references.
func CompileMapping ¶
func CompileMapping(spec Mapping) (*MappingPlan, error)
CompileMapping validates and snapshots a deterministic dependency graph. Cycles and duplicate physical destinations are rejected before any fixture is read. Separate projections to one table must be expressed as one mapping node.
func (*MappingPlan) Map ¶
Map projects concrete logical rows without evaluating expressions or generating identities. Its fresh inline sources are marked Expanded so Prepare preserves literal values. Numeric key values normalize exactly across Go widths and json.Number; strings, numbers, booleans and times occupy distinct key domains. Composite components are length-delimited. Nested column values are atomic: conflicting JSON objects are rejected instead of implicitly deep-merged.
type MappingScope ¶
type MappingScope string
MappingScope identifies the object used by a column path. Parent is the nearest association's source; ParentRow is its projected physical row. Root is the original logical record. Element projects an entire repeated scalar (or object) and requires an empty path.
const ( MappingSource MappingScope = "source" MappingRoot MappingScope = "root" MappingParent MappingScope = "parent" MappingParentRow MappingScope = "parentRow" MappingElement MappingScope = "element" )
type MappingTable ¶
type MappingTable struct {
Name string `json:"name" yaml:"name"`
Table Identifier `json:"table" yaml:"table"`
Path []string `json:"path,omitempty" yaml:"path,omitempty"`
Columns []MappingColumn `json:"columns" yaml:"columns"`
Keys []string `json:"keys,omitempty" yaml:"keys,omitempty"`
DependsOn []string `json:"dependsOn,omitempty" yaml:"dependsOn,omitempty"`
Associations []MappingTable `json:"associations,omitempty" yaml:"associations,omitempty"`
Database string `json:"database,omitempty" yaml:"database,omitempty"`
Operation Operation `json:"operation,omitempty" yaml:"operation,omitempty"`
Load LoadOptions `json:"load,omitempty" yaml:"load,omitempty"`
OnMissingRequired MissingRequiredPolicy `json:"onMissingRequired,omitempty" yaml:"onMissingRequired,omitempty"`
}
MappingTable selects objects or repeated elements using literal path segments. Paths are relative to the logical root for top-level tables and to the nearest parent's selected object for associations. Arrays are traversed in input order. Missing/null paths and empty collections emit no rows. A skipped required row skips its descendants. Associations depend on the parent table automatically; other physical prerequisites are explicit DependsOn names.
Keys enable row deduplication. Without Keys every selected object emits a row. Duplicate keys merge absent columns; conflicting present values are errors. Every table yields a Dataset, including an empty one (so Replace can clear it).
type MatchOptions ¶
type MatchOptions struct {
Keys []string `json:"keys,omitempty"`
Ordered bool `json:"ordered,omitempty"`
OrderBy []string `json:"orderBy,omitempty"`
Multiset bool `json:"multiset,omitempty"`
Contains bool `json:"contains,omitempty"`
StrictFields bool `json:"strictFields,omitempty"`
AllowEmpty bool `json:"allowEmpty,omitempty"`
MaxRows int `json:"maxRows,omitempty"`
JSONColumns []string `json:"jsonColumns,omitempty"`
}
type MissingRequiredPolicy ¶
type MissingRequiredPolicy string
const ( MissingRequiredError MissingRequiredPolicy = "error" MissingRequiredSkip MissingRequiredPolicy = "skip" )
type NativeLoadOptions ¶
type NativeLoadOptions struct {
Transactional bool `json:"transactional,omitempty"`
Format string `json:"format,omitempty"`
MinRows int `json:"minRows,omitempty"`
}
NativeLoadOptions is an explicit caller assertion that the registered SQLX loader has been certified for this driver and workload. Auto remains Batch unless this option is supplied.
type OperationError ¶
func (*OperationError) Error ¶
func (e *OperationError) Error() string
func (*OperationError) Unwrap ¶
func (e *OperationError) Unwrap() error
type Option ¶
func WithDialect ¶
func WithFixtures ¶
func WithMaxRows ¶
func WithNativeLoad ¶
func WithNativeLoad(capability NativeLoadOptions) Option
type PrepareRequest ¶
type PrepareResult ¶
type PrepareResult struct {
Steps []StepResult `json:"steps"`
Rows int64 `json:"rows"`
Transaction string `json:"transaction"`
}
type QueryRequest ¶
type QueryResult ¶
type QueryResult struct {
Columns []string `json:"columns"`
Rows structology.Maps `json:"rows"`
}
type RecordReference ¶
type RecordReference struct {
Field []string `json:"field"`
Table string `json:"table"`
Tag string `json:"tag"`
From []string `json:"from,omitempty"`
Overwrite bool `json:"overwrite,omitempty"`
}
RecordReference links tagged rows after all generation and template expansion. Forward references are allowed. References may depend on other references; cycles are rejected. From is an exact nested path; empty selects the whole row.
type Report ¶
type Report struct {
Name string `json:"name,omitempty"`
Passed int `json:"passed"`
Failed int `json:"failed"`
Mismatches []Mismatch `json:"mismatches,omitempty"`
Preparation *PrepareResult `json:"preparation,omitempty"`
Duration time.Duration `json:"duration,omitempty"`
// contains filtered or unexported fields
}
type Run ¶
func (*Run) Query ¶
func (r *Run) Query(ctx context.Context, req QueryRequest) (*QueryResult, error)
type RunOption ¶
type RunOption func(*runConfig) error
func WithRunState ¶
func WithTransaction ¶
func WithTransactionMode ¶
func WithTransactionMode(mode TransactionMode) RunOption
type SchemaApplyRequest ¶
type SchemaArtifact ¶
type SchemaExtractRequest ¶
type SchemaExtractRequest struct {
Database string `json:"database,omitempty"`
Schema string `json:"schema,omitempty"`
Tables []string `json:"tables"`
AllowPartial bool `json:"allowPartial,omitempty"`
IncludeDependencies bool `json:"includeDependencies,omitempty"`
IncludeViews bool `json:"includeViews,omitempty"`
IncludeTriggers bool `json:"includeTriggers,omitempty"`
IncludeSequenceState bool `json:"includeSequenceState,omitempty"`
}
type SchemaResult ¶
type SequenceRequest ¶
type SequenceRequest struct {
State data.Map `json:"state,omitempty"`
Sequences []FixtureSequence `json:"sequences"`
}
type SequenceResult ¶
type SequenceResult struct {
Sequences map[string]FixtureSequenceState `json:"sequences"`
}
type StepResult ¶
type StepResult struct {
Operation string `json:"operation,omitempty"`
Name string `json:"name,omitempty"`
Table Identifier `json:"table,omitempty"`
Rows int64 `json:"rows"`
Strategy Strategy `json:"strategy,omitempty"`
Reason string `json:"reason,omitempty"`
Duration time.Duration `json:"duration,omitempty"`
Error string `json:"error,omitempty"`
}
type TransactionMode ¶
type TransactionMode string
const ( PrepareAtomic TransactionMode = "prepareAtomic" CallerTransaction TransactionMode = "callerTransaction" CaseRollback TransactionMode = "caseRollback" ExplicitNonAtomic TransactionMode = "nonAtomic" )
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package dbtest integrates dbunit reports with Go's testing package.
|
Package dbtest integrates dbunit reports with Go's testing package. |
|
Package fixture loads explicitly selected test data.
|
Package fixture loads explicitly selected test data. |
|
Package manifest decodes explicit, versioned dbunit cases.
|
Package manifest decodes explicit, versioned dbunit cases. |