helpers

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2025 License: MIT Imports: 10 Imported by: 0

README

Test Helpers for Scanorama

This package provides common test helper functions for Scanorama tests.

Docker Environment Helpers

The docker.go module provides utilities for interacting with the Docker test environment:

  • TestEnvironment: Configuration for the test environment and available services
  • SetupTestEnvironment: Ensures Docker services are running and available
  • CheckService: Tests connectivity to a specific service
  • GetAvailableServices: Returns a list of all available service ports

Usage Example

import (
    "testing"
    
    "github.com/anstrom/scanorama/test/helpers"
)

func TestWithDocker(t *testing.T) {
    // Get default test environment
    env := helpers.DefaultTestEnvironment()
    
    // Skip the test if Docker environment is not ready
    if !env.SetupTestEnvironment(t) {
        return // Test was skipped
    }
    
    // Get list of available services
    availablePorts := env.GetAvailableServices(t)
    
    // Use services in your test...
}

Adding New Helpers

When adding new helper functions:

  1. Group related functionality in appropriate files
  2. Add comprehensive documentation with examples
  3. Ensure helpers are generic enough to be reused across tests
  4. Update this README with information about new helpers

Documentation

Overview

Package helpers provides testing utilities for database connections, environment setup, and test data management for Scanorama integration tests.

Package helpers provides test helper functions for Scanorama

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateTestDatabase

func CreateTestDatabase(config *DatabaseConfig) error

CreateTestDatabase creates a test database if it doesn't exist.

func EnsureTestSchema

func EnsureTestSchema(ctx context.Context, database *db.DB) error

EnsureTestSchema ensures the database schema is set up for testing.

func GetDatabaseStatus

func GetDatabaseStatus() string

GetDatabaseStatus returns a human-readable status of database availability.

func IsDatabaseAvailable

func IsDatabaseAvailable(config *DatabaseConfig) bool

IsDatabaseAvailable checks if a database is available and accessible.

func IsPostgreSQLRunning

func IsPostgreSQLRunning(port int) bool

IsPostgreSQLRunning checks if PostgreSQL is running on the given port.

func WaitForDatabase

func WaitForDatabase(config *DatabaseConfig, timeout time.Duration) error

WaitForDatabase waits for database to become available with timeout.

Types

type DatabaseConfig

type DatabaseConfig struct {
	Host     string
	Port     int
	Database string
	Username string
	Password string
	SSLMode  string
}

DatabaseConfig represents a database configuration for testing.

func ConnectToTestDatabase

func ConnectToTestDatabase(ctx context.Context) (*db.DB, *DatabaseConfig, error)

ConnectToTestDatabase attempts to connect to an available test database.

func GetAvailableDatabase

func GetAvailableDatabase() (*DatabaseConfig, error)

GetAvailableDatabase returns the first available database from the test configurations.

func GetTestDatabaseConfigs

func GetTestDatabaseConfigs() []DatabaseConfig

GetTestDatabaseConfigs returns a list of database configurations to try for testing. It tries test database first, then development database as fallback.

type TestEnvironment

type TestEnvironment struct {
	Services      map[string]TestService
	ContainerName string
	Timeout       int
	RequireAll    bool
}

TestEnvironment defines the test environment configuration.

func DefaultTestEnvironment

func DefaultTestEnvironment() *TestEnvironment

DefaultTestEnvironment returns the default test environment configuration.

func (*TestEnvironment) CheckService

func (env *TestEnvironment) CheckService(t *testing.T, serviceName string, maxRetries int) bool

CheckService attempts to connect to a service with retries.

func (*TestEnvironment) GetAvailableServices

func (env *TestEnvironment) GetAvailableServices(t *testing.T) []string

GetAvailableServices returns a list of available service ports.

func (*TestEnvironment) SetupTestEnvironment

func (env *TestEnvironment) SetupTestEnvironment(t *testing.T) bool

SetupTestEnvironment returns true if all required services are available, false otherwise.

type TestService

type TestService struct {
	Name string
	Port string
}

TestService defines a service available in the Docker test environment.

Jump to

Keyboard shortcuts

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