This is modeled after the internal/docker package to make it easy to use as a
replacement. Instead of creating a new docker container for each test,
it will create a new database in an existing database container, using a
template database as a source. This assumes that the template database
exists and already has the schema migrations run against it.
The default template is `boundary_template` which should already have
all migrations applied. However, some tests require an empty database to
test the migration code. In these cases the `template1` template
database should be used, ie:
dbtest.StartWithTemplate("postgres", dbtest.WithTemplate("template1"))
31 lines
627 B
Go
31 lines
627 B
Go
package dbtest
|
|
|
|
// GetOpts - iterate the inbound Options and return a struct.
|
|
func GetOpts(opt ...Option) Options {
|
|
opts := getDefaultOptions()
|
|
for _, o := range opt {
|
|
o(&opts)
|
|
}
|
|
return opts
|
|
}
|
|
|
|
// Option - how Options are passed as arguments.
|
|
type Option func(*Options)
|
|
|
|
// Options - how Options are represented.
|
|
type Options struct {
|
|
withTemplate string
|
|
}
|
|
|
|
func getDefaultOptions() Options {
|
|
return Options{}
|
|
}
|
|
|
|
// WithTemplate tells the command with database template to use
|
|
// when creating a new test database.
|
|
func WithTemplate(template string) Option {
|
|
return func(o *Options) {
|
|
o.withTemplate = template
|
|
}
|
|
}
|