Documentation ¶
Index ¶
- type Config
- type DBConn
- type Postgres
- func (p *Postgres) BeginTx(ctx context.Context, txOptions *sql.TxOptions) (*sql.Tx, error)
- func (p *Postgres) Connect(ctx context.Context) error
- func (p *Postgres) Disconnect(ctx context.Context) error
- func (p *Postgres) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (p *Postgres) Select(ctx context.Context, dst interface{}, query string, args ...interface{}) error
- func (p *Postgres) SelectAll(ctx context.Context, dst interface{}, query string, args ...interface{}) error
- type SchemaUtils
- func (s SchemaUtils) CreateSchema(ctx context.Context, schemaName string) error
- func (s SchemaUtils) DropSchema(ctx context.Context, schemaName string) error
- func (s SchemaUtils) DropTable(ctx context.Context, tableName string) error
- func (s SchemaUtils) SchemaExists(ctx context.Context, schemaName string) (bool, error)
- func (s SchemaUtils) TableExists(ctx context.Context, schema, tableName string) (bool, error)
- type TestContainer
- type TestDB
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 MaxOpenConnections int // MaxOpenConnections represents the number of open connections in the pool MaxIdleConnections int // MaxIdleConnections represents the number of idle connections in the pool ConnectionMaxLifetime time.Duration // ConnectionMaxLifetime represents the connection max life time }
Config is the postgres database configuration
type DBConn ¶
type DBConn 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 interface{}, query string, args ...interface{}) 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 interface{}, query string, args ...interface{}) error // Exec executes an SQL statement against the database and returns the appropriate result or an error. Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, 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 *sql.TxOptions) (*sql.Tx, error) }
DBConn will be implemented by concrete RDBMS storage
type Postgres ¶
type Postgres struct {
// contains filtered or unexported fields
}
Postgres helps interact with the Postgres database
func (*Postgres) Disconnect ¶
Disconnect the database connection.
type SchemaUtils ¶
type SchemaUtils struct { DBConn // contains filtered or unexported fields }
SchemaUtils helps interact with the database and perform some postgres DDL task
func NewSchemaUtils ¶
func NewSchemaUtils(db DBConn) *SchemaUtils
NewSchemaUtils returns an instance of SchemaUtils
func (SchemaUtils) CreateSchema ¶
func (s SchemaUtils) CreateSchema(ctx context.Context, schemaName string) error
CreateSchema helps create a test schema in a Postgres database
func (SchemaUtils) DropSchema ¶
func (s SchemaUtils) DropSchema(ctx context.Context, schemaName string) error
DropSchema utility function to drop a database schema
func (SchemaUtils) DropTable ¶
func (s SchemaUtils) DropTable(ctx context.Context, tableName string) error
DropTable utility function to drop a database table
func (SchemaUtils) SchemaExists ¶
SchemaExists helps check the existence of a Postgres schema.
func (SchemaUtils) TableExists ¶
TableExists utility function to help check the existence of table in Postgres tableName is in the format: <schemaName.tableName>. e.g: public.users
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