Documentation
¶
Overview ¶
Package testutil provides testing utilities for database integration tests.
Environment Variables:
Database connection strings can be customized via environment variables:
- TEST_POSTGRES_DSN: PostgreSQL connection string (default: postgres://testuser:testpassword@localhost:5433/testdb?sslmode=disable)
- TEST_MYSQL_DSN: MySQL connection string (default: testuser:testpassword@tcp(localhost:3307)/testdb?parseTime=true&multiStatements=true)
Database Setup:
db := testutil.SetupPostgresDB(t) defer testutil.TeardownDB(t, db) defer testutil.CleanupPostgresDB(t, db)
Test Fixtures (for foreign key constraints):
clientID := testutil.CreateTestClient(t, db, "postgres", "my-test-client") kekID := testutil.CreateTestKek(t, db, "postgres", "my-test-kek") // Or both: clientID, kekID := testutil.CreateTestClientAndKek(t, db, "postgres", "my-test")
Migration Path:
Migrations are automatically discovered by walking up from the current working directory until a "migrations/{dbType}" directory is found.
Index ¶
- func CleanupMySQLDB(t *testing.T, db *sql.DB)
- func CleanupPostgresDB(t *testing.T, db *sql.DB)
- func CreateTestClient(t *testing.T, db *sql.DB, driver, name string) uuid.UUID
- func CreateTestClientAndKek(t *testing.T, db *sql.DB, driver, baseName string) (clientID, kekID uuid.UUID)
- func CreateTestDek(t *testing.T, db *sql.DB, driver, name string, kekID uuid.UUID) uuid.UUID
- func CreateTestKek(t *testing.T, db *sql.DB, driver, name string) uuid.UUID
- func GetMySQLTestDSN() string
- func GetPostgresTestDSN() string
- func SetupMySQLDB(t *testing.T) *sql.DB
- func SetupPostgresDB(t *testing.T) *sql.DB
- func SkipIfNoMySQL(t *testing.T)
- func SkipIfNoPostgres(t *testing.T)
- func TeardownDB(t *testing.T, db *sql.DB)
- func ValidateTestClient(t *testing.T, db *sql.DB, driver string, clientID uuid.UUID) bool
- func ValidateTestKek(t *testing.T, db *sql.DB, driver string, kekID uuid.UUID) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CleanupMySQLDB ¶
CleanupMySQLDB truncates all tables in the MySQL database.
func CleanupPostgresDB ¶
CleanupPostgresDB truncates all tables in the PostgreSQL database.
func CreateTestClient ¶ added in v0.9.0
CreateTestClient creates a minimal active test client for repository tests. Returns the client ID for use in foreign key relationships. The client is created with a wildcard policy allowing all capabilities on all paths.
func CreateTestClientAndKek ¶ added in v0.9.0
func CreateTestClientAndKek(t *testing.T, db *sql.DB, driver, baseName string) (clientID, kekID uuid.UUID)
CreateTestClientAndKek creates both a test client and KEK, returning both IDs. Convenience wrapper for tests that need both fixtures.
func CreateTestDek ¶ added in v0.22.0
CreateTestDek creates a minimal test DEK (Data Encryption Key) for repository tests. Returns the DEK ID. The DEK is associated with the provided KEK ID.
func CreateTestKek ¶ added in v0.9.0
CreateTestKek creates a minimal test KEK for repository tests that need to reference a KEK (e.g., signed audit logs). Returns the KEK ID. The KEK is created with algorithm 'aes-gcm' and random encrypted key data.
func GetMySQLTestDSN ¶ added in v0.22.0
func GetMySQLTestDSN() string
GetMySQLTestDSN returns the MySQL test DSN, checking environment variable first.
func GetPostgresTestDSN ¶ added in v0.22.0
func GetPostgresTestDSN() string
GetPostgresTestDSN returns the PostgreSQL test DSN, checking environment variable first.
func SetupMySQLDB ¶
SetupMySQLDB creates a new MySQL database connection and runs migrations.
func SetupPostgresDB ¶
SetupPostgresDB creates a new PostgreSQL database connection and runs migrations.
func SkipIfNoMySQL ¶ added in v0.22.0
SkipIfNoMySQL skips the test if MySQL test database is not available. Useful for running tests in environments without database access.
func SkipIfNoPostgres ¶ added in v0.22.0
SkipIfNoPostgres skips the test if PostgreSQL test database is not available. Useful for running tests in environments without database access.
func TeardownDB ¶
TeardownDB closes the database connection and cleans up.
func ValidateTestClient ¶ added in v0.22.0
ValidateTestClient verifies that a test client was created with expected values. Returns true if the client exists and is active, false otherwise.
Types ¶
This section is empty.