Documentation
¶
Index ¶
- func GetTestDB(ctx context.Context, t *testing.T, dbConfig *database.Config) *database.DB
- func GetTestLogging(t *testing.T) *logging.Logging
- func LoadTestConfig[T config.Validator](t *testing.T, configFile T)
- func MakeRandomString(t *testing.T) string
- func SkipTestIfDBConfigIsMissing(t *testing.T)
- type DBCleaner
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetTestDB ¶
GetTestDB returns a database.DB instance for testing purposes.
It connects to the database using the provided configuration and ensures that the connection is successful. If the connection or ping fails, it will fail the test.
func GetTestLogging ¶ added in v1.0.0
GetTestLogging returns a logging.Logging instance for testing purposes.
It sets the logging level to Debug and uses a zaptest logger to capture logs during tests.
func LoadTestConfig ¶ added in v1.0.0
LoadTestConfig loads the configuration from environment variables into the provided config struct for testing purposes, populating it with values from the environment variables prefixed with "ICINGA_NOTIFICATIONS_".
Callers must first ensure the test database is configured, e.g. via SkipTestIfDBConfigIsMissing.
func MakeRandomString ¶
MakeRandomString returns a 20 byte random hex string.
func SkipTestIfDBConfigIsMissing ¶ added in v1.0.0
SkipTestIfDBConfigIsMissing skips the calling test if the required test database environment variable is not set.
Call this before daemon.InjectTestConfig, not from inside its callback: InjectTestConfig only ever invokes its callback once per test binary (it guards a package-wide singleton), so if the first test to reach it is the one that skips, every later test calling InjectTestConfig silently gets an empty, never-loaded config instead of being skipped itself. Checking the environment independently in each test, before touching the singleton, ensures every test in the package skips consistently when the database isn't configured.
Types ¶
type DBCleaner ¶ added in v1.0.0
type DBCleaner struct {
// contains filtered or unexported fields
}
DBCleaner deletes only the rows a test created from a shared test database, instead of truncating whole tables, so that tests running concurrently against the same database don't clobber each other's data.
Register conditions for the registered tables with Add or AddLazy, and then call Clean (typically via t.Cleanup) to delete all rows matching those conditions. Clean will only ever delete rows from tables that have registered conditions, and won't touch any other tables, so it's safe to use in parallel tests that share a database.
func NewDBCleaner ¶ added in v1.0.0
NewDBCleaner returns a DBCleaner that will only ever touch the given tables, deleted in the given order.
The order must respect foreign key constraints, i.e. children before their parents, since Clean will perform plain filtered DELETEs rather than relying on cascading deletes.
func (*DBCleaner) Add ¶ added in v1.0.0
Add registers a SQL WHERE condition for rows this test owns in the given table.
The condition is used verbatim without any adjustments. You have to build it with fmt.Sprintf and embed values directly into your condition, since the cleaner doesn't support parameterized queries. Multiple conditions added for the same table are OR-ed together.
func (*DBCleaner) AddLazy ¶ added in v1.0.0
AddLazy registers a SQL WHERE condition for the given table, but the condition is only evaluated when Clean is called.
This is useful if the condition depends on values that are only known at the time of cleaning, or you just want to defer whatever logic is needed to build the condition until the last possible moment. f is called with the context passed to Clean, and should return the actual SQL WHERE condition string. If it returns an empty string, Clean will skip it if no other conditions were registered for it.
func (*DBCleaner) Clean ¶ added in v1.0.0
Clean deletes all registered tables, one by one, in the order given to NewDBCleaner.
It is safe to call concurrently from parallel subtests, but it will only delete rows from tables that have registered conditions. If no conditions are registered for a table, it will be skipped.