Documentation ¶
Index ¶
- func BulkInsertDataTables(ctx context.Context, db *pgxpool.Pool, tables interface{}) error
- func GetAllRows(ctx context.Context, t *testing.T, db *pgxpool.Pool, table string, ...) interface{}
- func GetExplain(t *testing.T, db *pgxpool.Pool, statement string, args ...interface{}) string
- func GetRowChanges[T any](ctx context.Context, t *testing.T, db *pgxpool.Pool, table string, ...) (missingRows, newRows []T)
- func NewCockroachDBForTests(ctx context.Context, t testing.TB) *pgxpool.Pool
- func NewCockroachDBForTestsWithProductionSchema(ctx context.Context, t testing.TB) *pgxpool.Pool
- type RowsOrder
- type SQLExporter
- type SQLScanner
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BulkInsertDataTables ¶
BulkInsertDataTables adds all the data from tables to the provided database. tables is expected to be a struct that contains fields which are slices of SQLExporter. The tables will be inserted in the same order that the fields are in the struct - if there are foreign key relationships, be sure to order them correctly. This method panics if the passed in tables parameter is of the wrong type.
func GetAllRows ¶
func GetAllRows(ctx context.Context, t *testing.T, db *pgxpool.Pool, table string, row interface{}, whereClauses ...string) interface{}
GetAllRows returns all rows for a given table. The passed in row param must be a pointer type that implements the sqltest.Scanner interface. The passed in row may optionally implement the sqltest.RowsOrder interface to specify an ordering to return the rows in (this can make for easier to debug tests). The returned value is a slice of the provided row type (without a pointer) and can be converted to a normal slice via a type assertion. If anything goes wrong, the function will panic.
func GetExplain ¶
GetExplain returns the query plan for a given statement and arguments.
func GetRowChanges ¶
func GetRowChanges[T any](ctx context.Context, t *testing.T, db *pgxpool.Pool, table string, t0 time.Time) (missingRows, newRows []T)
GetRowChanges compares the rows found in the given table at instant t0 against those found at present time. It returns a slice of rows found in the table at t0 but not at present (aka missing rows), and a slice of rows found in the table at present but not at t0 (aka new rows).
The row type may optionally implement the RowsOrder interface to specify an ordering to return the rows in, which can make for easier to debug tests.
Usage ideas:
- A test does not make any changes to a table, then asserts that the returned slices are empty.
- A test adds a row, then asserts that the slice with missing rows is empty, and that the slice with new rows contains the added row.
- A test deletes a row, then asserts that the slice with missing rows contains the deleted row, and that the slice with new rows is empty.
- A test updates a row, then asserts that the slice with missing rows contains the affected row prior to the update, and that the slice with new rows contains the affected row after the update.
func NewCockroachDBForTests ¶
NewCockroachDBForTests creates a randomly named database on a test CockroachDB instance (aka the CockroachDB emulator). The returned pool will automatically be closed after the test finishes.
Types ¶
type RowsOrder ¶
type RowsOrder interface { // RowsOrderBy returns a SQL fragment like "ORDER BY my_field DESC". RowsOrderBy() string }
RowsOrder is an option that rows in a table can implement to specify the ordering of the returned data (to make for easier to debug tests).
type SQLExporter ¶
type SQLExporter interface { // ToSQLRow returns the column names and the column data that should be written for this row. ToSQLRow() (colNames []string, colData []interface{}) }
SQLExporter is an abstraction around a type that can be written as a single row in a SQL table.
type SQLScanner ¶
type SQLScanner interface { // ScanFrom takes in a function that takes in any number of pointers and will fill in the data. // The arguments passed into scan should be the row fields in the order they appear in the // table schema, as they will be filled in via a SELECT * FROM Table. ScanFrom(scan func(...interface{}) error) error }
SQLScanner is an abstraction around reading a single row from an SQL table.