helpers

package
v0.12.1 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2025 License: MIT Imports: 14 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

Package helpers provides common testing utilities for the Scanorama project. It includes helpers for database setup, mock servers, network testing, and test data generation.

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 GetAvailablePort added in v0.8.0

func GetAvailablePort() (int, error)

GetAvailablePort returns an available port 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 IsPortOpen added in v0.8.0

func IsPortOpen(host string, port int) bool

IsPortOpen checks if a port is open

func IsPostgreSQLRunning

func IsPostgreSQLRunning(port int) bool

IsPostgreSQLRunning checks if PostgreSQL is running on the given port.

func JSONHandler added in v0.8.0

func JSONHandler(status int, jsonBody string) http.HandlerFunc

JSONHandler returns an HTTP handler that serves JSON

func LogTestName added in v0.8.0

func LogTestName(t *testing.T)

LogTestName logs the test name for debugging

func RequireDocker added in v0.8.0

func RequireDocker(t *testing.T)

RequireDocker skips test if Docker is not available

func RequireNetwork added in v0.8.0

func RequireNetwork(t *testing.T)

RequireNetwork skips test if network is not available

func SetupNetworkTest added in v0.8.0

func SetupNetworkTest(t *testing.T) (*NetworkTestHelper, *TestCleanup)

SetupNetworkTest is a convenience function for network test setup

func SetupTestDatabase added in v0.8.0

func SetupTestDatabase(t *testing.T) (*TestDatabase, *TestCleanup)

SetupTestDatabase is a convenience function for common database test setup

func SimpleHTTPHandler added in v0.8.0

func SimpleHTTPHandler(status int, body string) http.HandlerFunc

SimpleHTTPHandler returns a simple HTTP handler for testing

func SkipIfShort added in v0.8.0

func SkipIfShort(t *testing.T, reason string)

SkipIfShort skips a test if running with -short flag

func TestContext added in v0.8.0

func TestContext(timeout time.Duration) (context.Context, context.CancelFunc)

TestContext provides a context with reasonable timeout for tests

func WaitForDatabase

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

WaitForDatabase waits for database to become available with timeout.

func WaitForPort added in v0.8.0

func WaitForPort(host string, port int, timeout time.Duration) error

WaitForPort waits for a port to become available or 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 MockHTTPServer added in v0.8.0

type MockHTTPServer struct {
	Server *httptest.Server
	URL    string
	Port   int
}

MockHTTPServer provides utilities for HTTP testing

func NewMockHTTPServer added in v0.8.0

func NewMockHTTPServer(handler http.Handler) *MockHTTPServer

NewMockHTTPServer creates a new mock HTTP server for testing

func (*MockHTTPServer) Close added in v0.8.0

func (ms *MockHTTPServer) Close()

Close closes the mock server

type NetworkTestHelper added in v0.8.0

type NetworkTestHelper struct {
	OpenPorts   []int
	ClosedPorts []int
	MockServers []*MockHTTPServer
}

NetworkTestHelper provides utilities for network testing

func NewNetworkTestHelper added in v0.8.0

func NewNetworkTestHelper() *NetworkTestHelper

NewNetworkTestHelper creates a new network test helper

func (*NetworkTestHelper) Cleanup added in v0.8.0

func (nth *NetworkTestHelper) Cleanup()

Cleanup closes all mock servers

func (*NetworkTestHelper) StartMockService added in v0.8.0

func (nth *NetworkTestHelper) StartMockService(handler http.Handler) *MockHTTPServer

StartMockService starts a mock service on a random available port

type TestCleanup added in v0.8.0

type TestCleanup struct {
	// contains filtered or unexported fields
}

TestCleanup provides a standard cleanup function for tests

func NewTestCleanup added in v0.8.0

func NewTestCleanup() *TestCleanup

NewTestCleanup creates a new test cleanup helper

func (*TestCleanup) Add added in v0.8.0

func (tc *TestCleanup) Add(f func())

Add adds a cleanup function

func (*TestCleanup) Execute added in v0.8.0

func (tc *TestCleanup) Execute()

Execute runs all cleanup functions in reverse order

type TestDatabase added in v0.8.0

type TestDatabase struct {
	DB       *sqlx.DB
	URL      string
	Host     string
	Port     string
	Name     string
	User     string
	Password string
}

TestDatabase provides utilities for database testing

func NewTestDatabase added in v0.8.0

func NewTestDatabase(t *testing.T) *TestDatabase

NewTestDatabase creates a new test database connection It checks for environment variables or uses defaults suitable for testing

func (*TestDatabase) Cleanup added in v0.8.0

func (td *TestDatabase) Cleanup(t *testing.T)

Cleanup removes test data from the database

func (*TestDatabase) Close added in v0.8.0

func (td *TestDatabase) Close()

Close closes the database connection

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