Documentation ¶
Index ¶
- type Config
- type Postgres
- type TestContainer
- type TestDB
- func (c TestDB) Count(ctx context.Context, tableName string) (int, error)
- func (c TestDB) CreateSchema(ctx context.Context, schemaName string) error
- func (c TestDB) DropSchema(ctx context.Context, schemaName string) error
- func (c TestDB) DropTable(ctx context.Context, tableName string) error
- func (c TestDB) SchemaExists(ctx context.Context, schemaName string) (bool, error)
- func (c TestDB) TableExists(ctx context.Context, tableName string) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { DBHost string // DBHost represents the database host DBPort int // DBPort is the database port DBName string // DBName is the database name DBUser string // DBUser is the database user used to connect DBPassword string // DBPassword is the database password DBSchema string // DBSchema represents the database schema MaxConnections int // MaxConnections represents the number of max connections in the pool MinConnections int // MinConnections represents the number of minimum connections in the pool MaxConnectionLifetime time.Duration // MaxConnectionLifetime represents the duration since creation after which a connection will be automatically closed. MaxConnIdleTime time.Duration // MaxConnIdleTime is the duration after which an idle connection will be automatically closed by the health check. HealthCheckPeriod time.Duration // HeathCheckPeriod is the duration between checks of the health of idle connections. }
Config defines the postgres configuration This configuration does not take into consideration the SSL mode TODO: enhance with SSL mode
type Postgres ¶
type Postgres interface { // Connect connects to the underlying database Connect(ctx context.Context) error // Disconnect closes the underlying opened underlying connection database Disconnect(ctx context.Context) error // Select fetches a single row from the database and automatically scanned it into the dst. // It returns an error in case of failure. When there is no record no errors is return. Select(ctx context.Context, dst any, query string, args ...any) error // SelectAll fetches a set of rows as defined by the query and scanned those record in the dst. // It returns nil when there is no records to fetch. SelectAll(ctx context.Context, dst any, query string, args ...any) error // Exec executes an SQL statement against the database and returns the appropriate result or an error. Exec(ctx context.Context, query string, args ...any) (pgconn.CommandTag, error) // BeginTx helps start an SQL transaction. The return transaction object is expected to be used in // the subsequent queries following the BeginTx. BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error) }
Postgres will be implemented by concrete RDBMS store
type TestContainer ¶
type TestContainer struct {
// contains filtered or unexported fields
}
TestContainer helps creates a Postgres docker container to run unit tests
func NewTestContainer ¶
func NewTestContainer(dbName, dbUser, dbPassword string) *TestContainer
NewTestContainer create a Postgres test container useful for unit and integration tests This function will exit when there is an error.Call this function inside your SetupTest to create the container before each test.
func (TestContainer) Cleanup ¶
func (c TestContainer) Cleanup()
Cleanup frees the resource by removing a container and linked volumes from docker. Call this function inside your TearDownSuite to clean-up resources after each test
func (TestContainer) GetTestDB ¶
func (c TestContainer) GetTestDB() *TestDB
GetTestDB returns a Postgres TestDB that can be used in the tests to perform some database queries
func (TestContainer) Host ¶
func (c TestContainer) Host() string
Host return the host of the test container
func (TestContainer) Port ¶
func (c TestContainer) Port() int
Port return the port of the test container
func (TestContainer) Schema ¶
func (c TestContainer) Schema() string
Schema return the test schema of the test container
type TestDB ¶
type TestDB struct {
Postgres
}
TestDB is used in test to perform some database queries
func (TestDB) Count ¶
Count utility function to help count the number of rows in a Postgres table. tableName is in the format: <schemaName.tableName>. e.g: public.users It returns -1 when there is an error
func (TestDB) CreateSchema ¶
CreateSchema helps create a test schema in a Postgres database
func (TestDB) DropSchema ¶
DropSchema utility function to drop a database schema
func (TestDB) SchemaExists ¶
SchemaExists helps check the existence of a Postgres schema. Very useful when implementing tests