Documentation ¶
Overview ¶
Package testsql provides helpers for working with PostgreSQL databases in unit tests, from creating per-test DBs to also running the migrations.
This package should be used for all unit tests to safely create isolated database environments for your tests.
When this package is inserted, it will introduce a `-gorm-debug` flag to the global flags package. This flag will turn on verbose output that logs all SQL statements.
Index ¶
- Variables
- func TestAssertCount(t testing.T, db *gorm.DB, want int)
- func TestDBCommit(t testing.T, db *gorm.DB)
- func TestDBConnect(t testing.T, family, dbName string) *gorm.DB
- func TestDBConnectSuper(t testing.T, family, dbName string) *gorm.DB
- func TestDBSave(t testing.T, db *gorm.DB, m interface{})
- func TestPostgresDB(t testing.T, dbName string) *gorm.DB
- func TestPostgresDBString(t testing.T, dbName string) string
- func TestPostgresDBWithOpts(t testing.T, dbName string, opts *TestDBOptions) *gorm.DB
- type TestDBOptions
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultDBName is the default name of the database to create for tests. DefaultDBName = "hzn_test" // UserName and UserPassword are the username and password, respectively, // of the test user to create with access to the test database. UserName = "testuser" UserPassword = "4555dcc0519478e4ab83fc8b78285022bdbf82da" // MigrationsDir is the directory path that is looked for for migrations. // If this is relative, then TestDB will walk parent directories until // this path is found and load migrations from there. MigrationsDir = filepath.Join("pkg", "control", "migrations") )
These variables control the database created for tests. They should not be modified while any active tests are running. These generally don't need to be modified at all.
Functions ¶
func TestAssertCount ¶
TestAssertCount is a helper for asserting the expected number of rows exist in the DB. It requires that the db argument is passed a *gorm.DB that must already have had a Table selection and optionally a where clause added to specify what to count. This helper will run `Count()` on the db passed and assert it succeeds and finds the desired number of records. Examples:
// Assert foo is empty models.TestAssertCount(t, db.Table("foo"), 0) // Assert 3 providers exist for a given module models.TestAssertCount(t, db.Model(&models.ModuleProvider{}).Where("provider = ?", provider), 3)
func TestDBCommit ¶
TestDBCommit commits a query and verifies it succeeds. This function is useful for one-liners in tests to load data or make changes. For example:
var count int TestDBCommit(t, db.Begin().Table("foos").Count(&count))
func TestDBConnect ¶
TestDBConnect connects to the local test database but does not recreate it.
func TestDBConnectSuper ¶
TestDBConnectSuper connects to the local database as a super user but does not recreate it.
func TestDBSave ¶
TestDBSave saves a changed model.
func TestPostgresDB ¶
TestPostgresDB sets up the test DB to use, including running any migrations.
This expects a local Postgres to be running with default "postgres/postgres" superuser credentials.
This also expects that at this or some parent directory, there is the path represented by MigrationsDir where the migration files can be found. If no migrations are found, then an error is raised.
func TestPostgresDBString ¶
TestPostgresDBString returns the connection string for the database. This is safe to call alongside TestPostgresDB. This won't be valid until TestPostgresDB is called.
func TestPostgresDBWithOpts ¶
func TestPostgresDBWithOpts(t testing.T, dbName string, opts *TestDBOptions) *gorm.DB
TestPostgresDB sets up the test DB to use, including running any migrations. In case the ReuseDB option is set to true, this function might not create a new database.
This expects a local Postgres to be running with default "postgres/postgres" superuser credentials.
This also expects that at this or some parent directory, there is the path represented by MigrationsDir where the migration files can be found. If no migrations are found, then an error is raised.
Types ¶
type TestDBOptions ¶
type TestDBOptions struct { // SkipMigration allows skipping over the migration of the database. SkipMigration bool // DBReuse indicates whether the potentially existing test database can be // reused. If set, the database is created and migrated at least once, but // won't be destroyed and recreated every time one of the `Test<Type>DB` // functions is called. ReuseDB bool }
TestDBOptions collects options that customize the test databases.