Documentation ¶
Overview ¶
Package testdb is a test convenience that creates test databases, migrates schema and tears down cleanly.
Ideal for projects that use https://github.com/upper/db.
Migrations are backed by https://github.com/golang-migrate/migrate
The simplest way to use testdb in testing:
package main import ( "github.com/tnsimon/testdb" ) func setup() { migrationPath := "/path/to/migrations.sql" cfg := testdb.Config{ Host: "localhost", Port: 5432, User: "username", Password: "password", Database: "database", Options: map[string]string{ "sslmode": "require", }, } testDB, err := testdb.New(cfg, testdb.WithMigrate(migrationPath) ) } func cleanup() { testDB.Teardown() }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { Host string Port int User string Password string Database string Options map[string]string }
Config holds configuration for connecting to a SQL database
type Options ¶
Options applies database options on the TestDB instance
func WithMigrate ¶
WithMigrate migrates to the highest level
type TestDB ¶
type TestDB struct { db.Session // contains filtered or unexported fields }
TestDB connects to a test database, creates
func (*TestDB) MigrateDown ¶
MigrateDown migrates database to the lowest version
func (*TestDB) NewSession ¶
NewSession establishes a new session for the test database
Click to show internal directories.
Click to hide internal directories.