testing

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package testing provides shared test utilities for Aegis integration tests.

This file contains Aegis-specific test helpers that depend on the main aegis package. It's separated to avoid import cycles.

Package testing provides shared test utilities for Aegis integration tests.

This package contains helper functions for setting up test infrastructure:

  • Database setup and teardown
  • Redis setup and teardown
  • Aegis instance creation
  • Test user/session creation

These helpers are designed for integration tests that require real database and Redis connections. For unit tests, use mock implementations instead.

Example usage:

func TestIntegration_UserFlow(t *testing.T) {
	aegis := testing.SetupTestAegis(t)
	user := testing.CreateTestUser(t, aegis, "test@example.com", "Password123!")

	// Run test...
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertEventually

func AssertEventually(t testing.TB, timeout, interval time.Duration, assertion func() bool, msgAndArgs ...any)

AssertEventually retries an assertion until it passes or times out.

This is useful for testing asynchronous operations where the result may not be immediately available.

Parameters:

  • t: Testing instance
  • timeout: Maximum time to wait
  • interval: Time between retries
  • assertion: Function that returns true when assertion passes
  • msgAndArgs: Optional message and arguments for failure

func CleanDatabase

func CleanDatabase(t testing.TB, db *sql.DB)

CleanDatabase removes all test data from the database.

This function truncates all Aegis tables to provide a clean state. It should be called before each test that requires database isolation.

Parameters:

  • t: Testing instance
  • db: Database connection

func GenerateTestEmail

func GenerateTestEmail() string

GenerateTestEmail generates a unique test email address.

This is useful for tests that need unique email addresses to avoid conflicts with uniqueness constraints.

Returns:

  • string: Unique email address in format "test_<timestamp>@example.com"

func GenerateTestPassword

func GenerateTestPassword() string

GenerateTestPassword generates a valid test password.

The password meets all common strength requirements:

  • At least 12 characters
  • Contains uppercase letter
  • Contains lowercase letter
  • Contains number
  • Contains special character

Returns:

  • string: Valid test password

func RequireError

func RequireError(t testing.TB, err error, msgAndArgs ...any)

RequireError fails the test immediately if err is nil.

Parameters:

  • t: Testing instance
  • err: Error to check
  • msgAndArgs: Optional message and arguments for failure

func RequireNoError

func RequireNoError(t testing.TB, err error, msgAndArgs ...any)

RequireNoError fails the test immediately if err is not nil.

Parameters:

  • t: Testing instance
  • err: Error to check
  • msgAndArgs: Optional message and arguments for failure

func RunMigrations

func RunMigrations(t testing.TB, db *sql.DB, _ string)

RunMigrations runs database migrations for testing.

This function executes all Aegis migrations to set up the schema. It should be called once per test database setup.

Parameters:

  • t: Testing instance
  • db: Database connection
  • dialect: Database dialect ("postgres", "mysql", "sqlite")

func SetupTestAegis

func SetupTestAegis(t testing.TB, testCfg *TestConfig) *aegis.Aegis

SetupTestAegis creates a configured Aegis instance for testing.

This function:

  • Sets up database connection (or skips if unavailable)
  • Sets up Redis connection (optional)
  • Creates Aegis with test-appropriate configuration
  • Registers cleanup for all resources

Parameters:

  • t: Testing instance for cleanup registration
  • testCfg: Test configuration (use DefaultTestConfig() if nil)

Returns:

  • *aegis.Aegis: Configured Aegis instance

func SetupTestAegisWithConfig

func SetupTestAegisWithConfig(t testing.TB, testCfg *TestConfig, configModifier func(*config.Config)) (*aegis.Aegis, *sql.DB, *redis.Client)

SetupTestAegisWithConfig creates Aegis with custom TestConfig.

This is useful when you need more control over the test infrastructure.

Parameters:

  • t: Testing instance
  • testCfg: Custom test configuration (use DefaultTestConfig() if nil)
  • configModifier: Optional function to modify the Aegis config

Returns:

  • *aegis.Aegis: Configured Aegis instance
  • *sql.DB: Database connection (for direct queries)
  • *redis.Client: Redis client (may be nil)

func SetupTestDB

func SetupTestDB(t testing.TB, cfg *TestConfig) *sql.DB

SetupTestDB creates a test database connection.

This function:

  • Opens a database connection using the configured URL
  • Verifies the connection is alive
  • Registers cleanup to close the connection after the test

If connection fails, the test is skipped (for CI environments without DB).

Parameters:

  • t: Testing instance for cleanup registration
  • cfg: Optional configuration (uses DefaultTestConfig if nil)

Returns:

  • *sql.DB: Database connection (or nil if skipped)

func SetupTestRedis

func SetupTestRedis(t testing.TB, cfg *TestConfig) *redis.Client

SetupTestRedis creates a test Redis client.

This function:

  • Creates a Redis client using the configured URL
  • Verifies the connection is alive
  • Flushes the test database on cleanup
  • Registers cleanup to close the connection

If connection fails, the test is skipped (for CI environments without Redis).

Parameters:

  • t: Testing instance for cleanup registration
  • cfg: Optional configuration (uses DefaultTestConfig if nil)

Returns:

  • *redis.Client: Redis client (or nil if skipped)

Types

type TestConfig

type TestConfig struct {
	// DatabaseURL is the connection string for the test database.
	// Default: uses DATABASE_URL environment variable or in-memory SQLite.
	DatabaseURL string

	// RedisURL is the connection string for test Redis.
	// Default: uses REDIS_URL environment variable or localhost:6379.
	RedisURL string

	// RedisDB is the Redis database number for tests (to isolate test data).
	// Default: 1 (separate from production DB 0)
	RedisDB int

	// SkipRedis skips Redis setup if true.
	// Useful for tests that don't require Redis.
	SkipRedis bool

	// SkipDatabase skips database setup if true.
	// Useful for pure unit tests.
	SkipDatabase bool
}

TestConfig holds configuration for test setup.

func DefaultTestConfig

func DefaultTestConfig() *TestConfig

DefaultTestConfig returns the default test configuration.

Jump to

Keyboard shortcuts

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