pgtesthelpers

package
v1.4.1-beta Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2025 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package pgtesthelpers provides test utilities for PostgreSQL EventStore testing with multi-adapter support.

This package enables testing across different PostgreSQL drivers (pgx, sql.DB, sqlx.DB) through a unified Wrapper interface. Test adapter selection is controlled via the ADAPTER_TYPE environment variable, enabling comprehensive testing of all database implementations.

Adapter Types:

PGXPoolWrapper: wraps pgx.Pool for high-performance connection pooling
SQLDBWrapper: wraps database/sql for standard library compatibility
SQLXWrapper: wraps sqlx.DB for extended SQL functionality

Utility Functions:

CreateWrapperWithTestConfig: creates appropriate wrapper based on ADAPTER_TYPE env var
CreateWrapperWithBenchmarkConfig: creates wrapper optimized for benchmarking
CleanUp: removes all events from database for test isolation
GetGreatestOccurredAtTimeFromDB: retrieves latest event timestamp for testing
GetLatestBookIDFromDB: retrieves most recent book ID for benchmark continuity
CleanUpBookEvents: removes events for specific book ID
OptimizeDBWhileBenchmarking: runs PostgreSQL optimization during benchmarks

Environment Variables:

ADAPTER_TYPE: selects adapter (pgx.pool, sql.db, sqlx.db)
TEST_PRIMARY_DSN: PostgreSQL primary instance DSN
TEST_REPLICA_DSN: PostgreSQL replica instance DSN

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanUp

func CleanUp(t testing.TB, wrapper Wrapper)

CleanUp truncates both the events and snapshots tables to prepare for the next test. This is needed for tests that use the snapshot functionality.

func CleanUpBookEvents

func CleanUpBookEvents(ctx context.Context, wrapper Wrapper, bookID uuid.UUID) (rowsAffected int64, err error)

CleanUpBookEvents deletes all events for a specific BookID and returns the number of rows affected.

func GetGreatestOccurredAtTimeFromDB

func GetGreatestOccurredAtTimeFromDB(t testing.TB, wrapper Wrapper) time.Time

GetGreatestOccurredAtTimeFromDB retrieves the maximum occurred_at timestamp from the events table.

func GetLatestBookIDFromDB

func GetLatestBookIDFromDB(t testing.TB, wrapper Wrapper) uuid.UUID

GetLatestBookIDFromDB retrieves the most recent BookID from the events table payload.

func GuardThatThereAreEnoughFixtureEventsInStore

func GuardThatThereAreEnoughFixtureEventsInStore(wrapper Wrapper, expectedNumEvents int)

GuardThatThereAreEnoughFixtureEventsInStore verifies that the events table contains at least the expected number of events.

func OptimizeDBWhileBenchmarking

func OptimizeDBWhileBenchmarking(ctx context.Context, wrapper Wrapper) error

OptimizeDBWhileBenchmarking runs VACUUM ANALYZE on the events table to optimize performance during benchmarking.

func TryCreateEventStoreWithTableName

func TryCreateEventStoreWithTableName(t testing.TB, options ...postgresengine.Option) error

TryCreateEventStoreWithTableName attempts to create an event store with custom options to test table name validation.

Types

type PGXPoolWrapper

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

PGXPoolWrapper wraps pgxpool-based testing.

func (*PGXPoolWrapper) Close

func (e *PGXPoolWrapper) Close()

Close closes the underlying PGX pool connection.

func (*PGXPoolWrapper) GetEventStore

func (e *PGXPoolWrapper) GetEventStore() *postgresengine.EventStore

GetEventStore returns the event store instance for the PGX pool wrapper.

type SQLDBWrapper

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

SQLDBWrapper wraps sql.DB-based testing.

func (*SQLDBWrapper) Close

func (e *SQLDBWrapper) Close()

Close closes the underlying SQL DB connection.

func (*SQLDBWrapper) GetEventStore

func (e *SQLDBWrapper) GetEventStore() *postgresengine.EventStore

GetEventStore returns the event store instance for the SQL DB wrapper.

type SQLXWrapper

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

SQLXWrapper wraps sqlx.DB-based testing.

func (*SQLXWrapper) Close

func (e *SQLXWrapper) Close()

Close closes the underlying SQLX DB connection.

func (*SQLXWrapper) GetEventStore

func (e *SQLXWrapper) GetEventStore() *postgresengine.EventStore

GetEventStore returns the event store instance for the SQLX wrapper.

type Wrapper

type Wrapper interface {
	GetEventStore() *postgresengine.EventStore
	Close()
}

Wrapper interface to abstract over different engine types.

func CreateWrapperWithBenchmarkConfig

func CreateWrapperWithBenchmarkConfig(t testing.TB, options ...postgresengine.Option) Wrapper

CreateWrapperWithBenchmarkConfig creates a database wrapper configured for benchmarking.

func CreateWrapperWithTestConfig

func CreateWrapperWithTestConfig(t testing.TB, options ...postgresengine.Option) Wrapper

CreateWrapperWithTestConfig creates a database wrapper configured for testing with the specified options.

Jump to

Keyboard shortcuts

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