Documentation ¶
Overview ¶
Package dbcleaner helps cleaning up database's tables upon unit test. With the help of https://github.com/stretchr/testify/tree/master/suite, we can easily acquire the tables using in the test in SetupTest or SetupSuite, and cleanup all data using TearDownTest or TearDownSuite
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrTableNeverLockBefore is paniced if calling Release on table that havent' been acquired before ErrTableNeverLockBefore = errors.New("table has never been locked before") )
Functions ¶
This section is empty.
Types ¶
type DbCleaner ¶
type DbCleaner interface { // SetEngine sets dbEngine, can be mysql, postgres... SetEngine(dbEngine engine.Engine) // Acquire will lock tables passed in params so data in the table would not be deleted by other test cases Acquire(tables ...string) // Clean calls Truncate the tables Clean(tables ...string) // Close calls corresponding method on dbEngine to release connection to db Close() error }
DbCleaner interface
type Option ¶ added in v2.2.0
type Option func(opt *Options)
func SetLockFileDir ¶ added in v2.2.1
SetLockFileDir sets directory for lock files
func SetLockTimeout ¶ added in v2.2.0
SetLockTimeout sets timeout for locking operation, default to 10 seconds
func SetNumberOfRetry ¶ added in v2.2.0
SetNumberOfRetry sets max retries for acquire the table, default to 5 times
func SetRetryInterval ¶ added in v2.2.0
SetRetryInterval sets sleep duration between each retry, default to 10 seconds
type Options ¶ added in v2.2.0
type Options struct { Logger logging.Logger LockTimeout time.Duration NumberOfRetry int RetryInterval time.Duration LockFileDir string }
Options defines properties that dbEngine would use during trying to acquire and clean tables, including Logger: default to Noop LockTimeout: max duration while trying to acquire lock for a table NumberOfRetry: max number of retry when failed to acquire the table RetryInterval: sleep between each retry until reach NumberOfRetry