unittest

package
v0.11.721 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package unittest builds the SQL that runs a Bruin unit test on the asset's configured connection. The tables the asset reads are replaced by inline fixture CTEs, so the test runs as a single read-only SELECT that issues no DDL and leaves no artifacts on the target.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildWarehouseQuery

func BuildWarehouseQuery(p Rewriter, dialect, renderedSQL string, test pipeline.UnitTest, schemas map[string][]pipeline.Column) (string, error)

BuildWarehouseQuery rewrites an asset's rendered SQL into a single read-only SELECT that reads from inline fixture CTEs instead of real tables, so a unit test can run on the configured warehouse with no DDL and no artifacts.

Every table the query reads must be accounted for, or it would hit real data: a mocked input becomes a fixture CTE built from its rows; an unmocked read whose asset declares columns becomes an empty (zero-row) typed CTE; an unmocked read with no declared columns is an error.

To assert an intermediate CTE instead of the final output, derive its query from this result with Rewriter.SelectFromCTE (WITH … SELECT * FROM <cte>) — the fixtures are already injected, so it needs no separate build.

func ResolveFixtures

func ResolveFixtures(available []pipeline.Fixture, test pipeline.UnitTest) ([]pipeline.UnitTestInput, error)

ResolveFixtures expands the named fixtures a test references into mock inputs and merges them with the test's own inputs. A pipeline-level fixture supplies rows for one asset; a test pulls it in by name (UnitTest.Fixtures) so many tests can share a baseline set of rows (typically lookup or dimension tables) without repeating them.

The test's own inputs win: an explicit inputs entry for an asset fully replaces a referenced fixture for that same asset. It is an error to reference a fixture the pipeline does not define, or to reference two fixtures that both supply rows for the same asset (ambiguous).

Types

type Rewriter

type Rewriter interface {
	ExtractSelect(sql string, dialect string) (string, error)
	UsedTables(sql string, dialect string) ([]string, error)
	RenameTables(sql string, dialect string, mapping map[string]string) (string, error)
	PrependCTEs(sql string, dialect string, ctes []sqlparser.CTE) (string, error)
	SelectFromCTE(sql string, dialect string, cteName string) (string, error)
	FreezeTime(sql string, dialect string, executionTime string) (string, error)
}

Rewriter is the subset of the SQL parser the warehouse builder needs. *sqlparser.SQLParser satisfies it.

type UnitTestResult

type UnitTestResult struct {
	Passed  bool
	Message string // human-readable explanation when Passed is false
}

UnitTestResult is the outcome of running a single unit test.

func CompareCTEResult

func CompareCTEResult(expected pipeline.UnitTestCTEExpected, columns []string, rows [][]interface{}) *UnitTestResult

CompareCTEResult compares the rows produced by a named intermediate CTE against its expectation, with the same semantics as the final output.

func CompareResult

func CompareResult(expected pipeline.UnitTestExpected, columns []string, rows [][]interface{}) *UnitTestResult

CompareResult compares a query result (columns + rows, as returned by a connection via SelectWithSchema) against a unit test's expectation.

Jump to

Keyboard shortcuts

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