Documentation
¶
Overview ¶
Package helpers provides testing utilities for database connections, environment setup, and test data management for Scanorama integration tests.
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 ¶
- func CreateTestDatabase(config *DatabaseConfig) error
- func EnsureTestSchema(ctx context.Context, database *db.DB) error
- func GetAvailablePort() (int, error)
- func GetDatabaseStatus() string
- func IsDatabaseAvailable(config *DatabaseConfig) bool
- func IsPortOpen(host string, port int) bool
- func IsPostgreSQLRunning(port int) bool
- func JSONHandler(status int, jsonBody string) http.HandlerFunc
- func LogTestName(t *testing.T)
- func RequireDocker(t *testing.T)
- func RequireNetwork(t *testing.T)
- func SetupNetworkTest(t *testing.T) (*NetworkTestHelper, *TestCleanup)
- func SetupTestDatabase(t *testing.T) (*TestDatabase, *TestCleanup)
- func SimpleHTTPHandler(status int, body string) http.HandlerFunc
- func SkipIfShort(t *testing.T, reason string)
- func TestContext(timeout time.Duration) (context.Context, context.CancelFunc)
- func WaitForDatabase(config *DatabaseConfig, timeout time.Duration) error
- func WaitForPort(host string, port int, timeout time.Duration) error
- type DatabaseConfig
- type MockHTTPServer
- type NetworkTestHelper
- type TestCleanup
- type TestDatabase
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 ¶
EnsureTestSchema ensures the database schema is set up for testing.
func GetAvailablePort ¶ added in v0.8.0
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
IsPortOpen checks if a port is open
func IsPostgreSQLRunning ¶
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
LogTestName logs the test name for debugging
func RequireDocker ¶ added in v0.8.0
RequireDocker skips test if Docker is not available
func RequireNetwork ¶ added in v0.8.0
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
SkipIfShort skips a test if running with -short flag
func TestContext ¶ added in v0.8.0
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.
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 ¶
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
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