testutil

package
v0.22.1 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2026 License: MIT Imports: 15 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanupMySQLDB

func CleanupMySQLDB(t *testing.T, db *sql.DB)

CleanupMySQLDB truncates all tables in the MySQL database.

func CleanupPostgresDB

func CleanupPostgresDB(t *testing.T, db *sql.DB)

CleanupPostgresDB truncates all tables in the PostgreSQL database.

func CreateTestClient added in v0.9.0

func CreateTestClient(t *testing.T, db *sql.DB, driver, name string) uuid.UUID

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

func CreateTestDek(t *testing.T, db *sql.DB, driver, name string, kekID uuid.UUID) uuid.UUID

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

func CreateTestKek(t *testing.T, db *sql.DB, driver, name string) uuid.UUID

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

func SetupMySQLDB(t *testing.T) *sql.DB

SetupMySQLDB creates a new MySQL database connection and runs migrations.

func SetupPostgresDB

func SetupPostgresDB(t *testing.T) *sql.DB

SetupPostgresDB creates a new PostgreSQL database connection and runs migrations.

func SkipIfNoMySQL added in v0.22.0

func SkipIfNoMySQL(t *testing.T)

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

func SkipIfNoPostgres(t *testing.T)

SkipIfNoPostgres skips the test if PostgreSQL test database is not available. Useful for running tests in environments without database access.

func TeardownDB

func TeardownDB(t *testing.T, db *sql.DB)

TeardownDB closes the database connection and cleans up.

func ValidateTestClient added in v0.22.0

func ValidateTestClient(t *testing.T, db *sql.DB, driver string, clientID uuid.UUID) bool

ValidateTestClient verifies that a test client was created with expected values. Returns true if the client exists and is active, false otherwise.

func ValidateTestKek added in v0.22.0

func ValidateTestKek(t *testing.T, db *sql.DB, driver string, kekID uuid.UUID) bool

ValidateTestKek verifies that a test KEK was created with expected values. Returns true if the KEK exists, false otherwise.

Types

This section is empty.

Jump to

Keyboard shortcuts

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