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 ¶
type Option func(opt *Options)
func SetLockFileDir ¶
SetLockFileDir sets directory for lock files
func SetLockTimeout ¶
SetLockTimeout sets timeout for locking operation, default to 10 seconds
func SetNumberOfRetry ¶
SetNumberOfRetry sets max retries for acquire the table, default to 5 times
func SetRetryInterval ¶
SetRetryInterval sets sleep duration between each retry, default to 10 seconds
type Options ¶
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