Documentation
¶
Overview ¶
dbtestify package provides core logic of dbtestify.
It is public but not intended to be used directly by users. github.com/shibukawa/dbtestify/assertdb provides API for database seeding and assertion for testing.
Index ¶
- Variables
- func DumpDiffCLICallback(showTableName, quiet bool) func(result AssertTableResult)
- func Seed(ctx context.Context, dbc DBConnector, data *DataSet, opt SeedOpt) error
- type AssertOpt
- type AssertResult
- type AssertStatus
- type AssertTableResult
- type DBConnector
- type DataSet
- type Diff
- type ErrMissingPrimaryKey
- type MatchStrategy
- type NormalizedTable
- type Operation
- type RowDiff
- type SeedOpt
- type Table
- type Value
Constants ¶
This section is empty.
Variables ¶
var DefaultBatchSize = 50
DefaultBatchSize is the default number of rows to process in a single batch during seeding.
var ErrInvalidDBDriver = errors.New("invalid db driver")
Functions ¶
func DumpDiffCLICallback ¶
func DumpDiffCLICallback(showTableName, quiet bool) func(result AssertTableResult)
Types ¶
type AssertOpt ¶
type AssertOpt struct {
IncludeTags []string // Tags to filter rows of dataset.
ExcludeTags []string // Tags to filter rows of dataset.
TargetTables []string // Only specified tables will be processed. If empty, all tables will be processed.
Callback func(targetTable string, mode MatchStrategy, start bool, err error) // Callback function to report progress and errors during the assertion process.
DiffCallback func(result AssertTableResult) // Callback function to report differences in rows during the assertion process.
}
MatchStrategy defines the strategy for matching rows in a table.
type AssertResult ¶
type AssertResult struct {
Tables []AssertTableResult
}
AssertResult represents the result of an assertion operation on a dataset.
type AssertStatus ¶
type AssertStatus string
AssertStatus defines the status of an assertion result.
const ( Match AssertStatus = "match" NotMatch AssertStatus = "not-match" OnlyOnExpect AssertStatus = "only-e" OnlyOnActual AssertStatus = "only-a" WrongDataSet AssertStatus = "wrongDataSet" // primary keys are missing )
type AssertTableResult ¶
type AssertTableResult struct {
Name string
PrimaryKeys []string
Rows []RowDiff
Status AssertStatus
}
AssertTableResult represents the result of an assertion on a single table in AssertResult.
func Assert ¶
func Assert(ctx context.Context, dbc DBConnector, expected *DataSet, opt AssertOpt) (bool, []AssertTableResult, error)
Assert performs an assertion on the provided dataset against the database.
type DBConnector ¶
type DBConnector interface {
TableNames(ctx context.Context, schema ...string) ([]string, error)
PrimaryKeys(ctx context.Context, table string) ([]string, error)
Insert(ctx context.Context, tx *sql.Tx, tableName string, columns []string, values []any) error
Delete(ctx context.Context, tx *sql.Tx, tableName string, columns []string, values []any) error
Upsert(ctx context.Context, tx *sql.Tx, tableName string, columns, pKeys []string, values []any) error
Truncate(ctx context.Context, tx *sql.Tx, tableName string) error
DB() *sql.DB
}
DBConnector is an interface for database operations that is used from dbtestify package. It absorbs the database driver specific operations like getting table names, primary keys, and performing CRUD operations.
func NewDBConnector ¶
func NewDBConnector(ctx context.Context, source string) (DBConnector, error)
NewDBConnector creates a new DBConnector based on the provided source string. The source string should be in the format of "mysql://", "sqlite://", or "postgres://".
The context is used to manage the lifecycle of the database connection. So you should pass a context that can be cancelled.
This package uses https://github.com/jackc/pgx for PostgreSQL, https://github.com/go-sql-driver/mysql for MySQL, https://github.com/mattn/go-sqlite3 for SQLite3. To study the detail of the connection string, check the documentation of each driver.
- postgres://user:pass@localhost:5432/dbname?sslmode=disable
- mysql://root:pass@tcp(localhost:3306)/foo?tls=skip-verify
- sqlite://file:dbfilename.db
type DataSet ¶
type DataSet struct {
Operation map[string]Operation
Match map[string]MatchStrategy
Tables []*Table
}
DataSet represents a collection of tables and their associated operations and match strategies.
type Diff ¶
type Diff struct {
Key string `json:"key"`
Expect any `json:"expect"`
Actual any `json:"actual"`
Status AssertStatus `json:"status"`
}
Value represents a single value in a row, including its key and value.
type ErrMissingPrimaryKey ¶
ErrMissingPrimaryKey is an error type that indicates that some primary keys are missing from a row.
func (ErrMissingPrimaryKey) Error ¶
func (e ErrMissingPrimaryKey) Error() string
type MatchStrategy ¶
type MatchStrategy string
MatchStrategy defines how to match rows in the database.
const ( ExactMatchStrategy MatchStrategy = "exact" SubMatchStrategy MatchStrategy = "sub" InvalidMatchStrategy MatchStrategy = "invalid" )
func (MatchStrategy) String ¶
func (s MatchStrategy) String() string
type NormalizedTable ¶
NormalizedTable represents a normalized version of a table with its name and sorted rows.
type Operation ¶
type Operation string
Operation represents the type of operation to be performed on the database.
type RowDiff ¶
type RowDiff struct {
Fields []Diff `json:"fields"`
Status AssertStatus `json:"status"`
}
RowDiff represents the difference in a row between the expected and actual data.
type SeedOpt ¶
type SeedOpt struct {
BatchSize int // default: 50
Operations map[string]Operation // Operations to apply to each table. If empty, defaults to ClearInsertOperation.
IncludeTags []string // Tags to filter rows of dataset.
ExcludeTags []string // Tags to filter rows of dataset.
TargetTables []string // Only specified tables will be processed.
Callback func(targetTable, task string, start bool, err error) // Callback function to report progress and errors during the seeding process.
}
SeedOpt defines options for the seeding process.
type Table ¶
Table represents a single table in the dataset, including its name, rows, and tags.
func (Table) SortAndFilter ¶
func (t Table) SortAndFilter(primaryKeys, includeTags, excludeTags []string) (*NormalizedTable, error)
Table.SortAndFilter sorts the rows of the table based on the provided primary keys and filters them based on include and exclude tags.