Git Product home page Git Product logo

dbcleaner's Introduction

DbCleaner

Build Status GoDoc Go Report Card

Clean database for testing, inspired by database_cleaner for Ruby. It uses flock syscall under the hood to make sure the test can runs in parallel without racing issues.

Basic usage

  • To get the package, execute:
go get gopkg.in/khaiql/dbcleaner.v2
  • To import this package, add the following line to your code:
import "gopkg.in/khaiql/dbcleaner.v2"
  • To install TestSuite:
go get github.com/stretchr/testify
  • For people who is using old version (v1.0), please change your import to
import "gopkg.in/khaiql/dbcleaner.v1"

Options

During running test suites, there might be deadlock when 2 suites try to acquire the same table. Dbcleaner tries to mitigate the issue by providing options for retry and panic when the deadlock couldn't be resolved after excessive retries.

type Options struct {
	Logger        logging.Logger
	LockTimeout   time.Duration
	NumberOfRetry int
	RetryInterval time.Duration
}

type Option func(opt *Options)

// SetLogger to an instance of logging.Logger, default to Noop
func SetLogger(logger logging.Logger) Option {
	return func(opt *Options) {
		opt.Logger = logger
	}
}

// SetLockTimeout sets timeout for locking operation, default to 10 seconds
func SetLockTimeout(d time.Duration) Option {
	return func(opt *Options) {
		opt.LockTimeout = d
	}
}

// SetNumberOfRetry sets max retries for acquire the table, default to 5 times
func SetNumberOfRetry(t int) Option {
	return func(opt *Options) {
		opt.NumberOfRetry = t
	}
}

// SetRetryInterval sets sleep duration between each retry, default to 10 seconds
func SetRetryInterval(d time.Duration) Option {
	return func(opt *Options) {
		opt.RetryInterval = d
	}
}

// SetLockFileDir sets directory for lock files
func SetLockFileDir(dir string) Option {
	return func(opt *Options) {
		opt.LockFileDir = dir
	}
}

cleaner := dbcleaner.New(SetNumberOfRetry(10), SetLockTimeout(5*time.Second))

Using with testify's suite

import (
	"testing"

  	"gopkg.in/khaiql/dbcleaner.v2"
  	"gopkg.in/khaiql/dbcleaner.v2/engine"
	"github.com/stretchr/testify/suite"
)

var Cleaner = dbcleaner.New()

type ExampleSuite struct {
	suite.Suite
}

func (suite *ExampleSuite) SetupSuite() {
  	// Init and set mysql cleanup engine
  	mysql := engine.NewMySQLEngine("YOUR_DB_DSN")
  	Cleaner.SetEngine(mysql)
}

func (suite *ExampleSuite) SetupTest() {
  	Cleaner.Acquire("users")
}

func (suite *ExampleSuite) TearDownTest() {
  	Cleaner.Clean("users")
}

func (suite *ExampleSuite) TestSomething() {
  	// Have some meaningful test
  	suite.Equal(true, true)
}

func TestRunSuite(t *testing.T) {
  	suite.Run(t, new(ExampleSuite))
}

Support drivers

  • postgres
  • mysql
  • sqlite3

Write cleaner for other drivers

Basically all drivers supported by database/sql package are also supported by dbcleaner. Check list of drivers: https://github.com/golang/go/wiki/SQLDrivers

For custom driver, implement your own engine.Engine interface and call SetEngine on dbcleaner.Cleaner instance.

License

MIT

dbcleaner's People

Contributors

awcodify avatar brycereitano avatar cuongpnh avatar khaiql avatar khoinguyen2992 avatar madneal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

dbcleaner's Issues

undefined: sync.Map

go version issue

$ go version
go version go1.8.1 darwin/amd64

$ go test -v
vendor/gopkg.in/khaiql/dbcleaner.v2/dbcleaner.go:44: undefined: sync.Map

Do we need to specify the schema while trying to Aquire tables

I started seeing below error all of a sudden when I added schema_name in front of the table it was working fine.

- FAIL: TestFunctionalTestSuite/TestSendNOAA (100.04s)
        suite.go:62: test panicked: failed to ACQUIRE tables [notice] after 5 times
            goroutine 10 [running]:
            runtime/debug.Stack(0xc0006e1c78, 0x48ced20, 0xc0006321f0)
                /usr/local/go1.12.7.darwin-amd64/src/runtime/debug/stack.go:24 +0x9d
            github.com/stretchr/testify/suite.failOnPanic(0xc00020c000)
                /Users/bvennapureddy/go/pkg/mod/github.com/stretchr/[email protected]/suite/suite.go:62 +0x57
            panic(0x48ced20, 0xc0006321f0)
                /usr/local/go1.12.7.darwin-amd64/src/runtime/panic.go:522 +0x1b5
            gopkg.in/khaiql/dbcleaner%2ev2.(*cleanerImpl).Acquire(0xc000033700, 0xc00022a0c0, 0x4, 0x4)
                /Users/bvennapureddy/go/pkg/mod/gopkg.in/khaiql/[email protected]/dbcleaner.go:160 +0x586
            git.enova.com/services/customer_notices.(*FunctionalTestSuite).SetupTest(0xc000258f20)
                /Users/bvennapureddy/customer_notices/init_test.go:123 +0x126
            github.com/stretchr/testify/suite.Run.func2(0xc00020c000)
                /Users/bvennapureddy/go/pkg/mod/github.com/stretchr/[email protected]/suite/suite.go:119 +0x3e7
            testing.tRunner(0xc00020c000, 0xc000191980)
                /usr/local/go1.12.7.darwin-amd64/src/testing/testing.go:865 +0xc0
            created by testing.(*T).Run
                /usr/local/go1.12.7.darwin-amd64/src/testing/testing.go:916 +0x35a

Disable/Override Logging

The output printed out by the v2 version of dbcleaner is polluting my output for tests and love a way to disable it.

Either it being a way to passing in a NoOP destination/logger, or just a switch that I can flip to disable it entirely.

Example Output:

[Postgres] Executing command TRUNCATE TABLE countries CASCADE
[Postgres] Executing command TRUNCATE TABLE addresses CASCADE

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.