Clean database for testing, inspired by database_cleaner for Ruby

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

Owner
Scott Le
See my blog: https://lequangkhai.wordpress.com/
Scott Le
Similar Resources

Golang HTTP client testing framework

flute Golang HTTP client testing framework Presentation https://speakerdeck.com/szksh/flute-golang-http-client-testing-framework Overview flute is the

Sep 27, 2022

Mutation testing for Go source code

go-mutesting go-mutesting is a framework for performing mutation testing on Go source code. Its main purpose is to find source code, which is not cove

Dec 28, 2022

Extremely flexible golang deep comparison, extends the go testing package and tests HTTP APIs

Extremely flexible golang deep comparison, extends the go testing package and tests HTTP APIs

go-testdeep Extremely flexible golang deep comparison, extends the go testing package. Latest news Synopsis Description Installation Functions Availab

Dec 22, 2022

Minimal and Beautiful Go testing framework

Minimal and Beautiful Go testing framework

Goblin A Mocha like BDD testing framework written in Go that requires no additional dependencies. Requires no extensive documentation nor complicated

Dec 25, 2022

A Comprehensive Coverage Testing System for The Go Programming Language

A Comprehensive Coverage Testing System for The Go Programming Language

goc 中文页 | goc is a comprehensive coverage testing system for The Go Programming Language, especially for some complex scenarios, like system testing c

Jan 8, 2023

Go testing in the browser. Integrates with `go test`. Write behavioral tests in Go.

Go testing in the browser. Integrates with `go test`. Write behavioral tests in Go.

GoConvey is awesome Go testing Welcome to GoConvey, a yummy Go testing tool for gophers. Works with go test. Use it in the terminal or browser accordi

Dec 30, 2022

Testing API Handler written in Golang.

Gofight API Handler Testing for Golang Web framework. Support Framework Http Handler Golang package http provides HTTP client and server implementatio

Dec 16, 2022

Library created for testing JSON against patterns.

Gomatch Library created for testing JSON against patterns. The goal was to be able to validate JSON focusing only on parts essential in given test cas

Oct 28, 2022

Testing framework for Go. Allows writing self-documenting tests/specifications, and executes them concurrently and safely isolated. [UNMAINTAINED]

GoSpec GoSpec is a BDD-style testing framework for the Go programming language. It allows writing self-documenting tests/specs, and executes them in p

Nov 28, 2022
Comments
  • Do we need to specify the schema while trying to Aquire tables

    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

    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
    
  • Add CASCADE for foreign key relationships.

    Add CASCADE for foreign key relationships.

    DBCleaner seems to refuse to work when you have relationships in your database. Within PostgreSQL, appending CASCADE to the end of the TRUNCATE statement fixes it. At least in my use case.

  • reset autoincrement identity

    reset autoincrement identity

    • It's a good approach to reset identity (ID) if we truncate the table.
    • It's also the default behaviour of MySQL truncation.

    This pull request is cover those two points above in PostgreSQL and SQLite

Go Unit Testing Clean Arch

Golang Unit Testing Tutorial melakukan unit testing di Golang yang sudah menerapkan clean architecture Menjalankan Service PSQL_HOST=<IP Database Serv

Feb 12, 2022
API testing framework inspired by frisby-js
API testing framework inspired by frisby-js

frisby REST API testing framework inspired by frisby-js, written in Go Proposals I'm starting to work on frisby again with the following ideas: Read s

Sep 27, 2022
siusiu (suite-suite harmonics) a suite used to manage the suite, designed to free penetration testing engineers from learning and using various security tools, reducing the time and effort spent by penetration testing engineers on installing tools, remembering how to use tools.
siusiu (suite-suite harmonics) a suite used to manage the suite, designed to free penetration testing engineers from learning and using various security tools, reducing the time and effort spent by penetration testing engineers on installing tools, remembering how to use tools.

siusiu (suite-suite harmonics) a suite used to manage the suite, designed to free penetration testing engineers from learning and using various security tools, reducing the time and effort spent by penetration testing engineers on installing tools, remembering how to use tools.

Dec 12, 2022
A yaml data-driven testing format together with golang testing library

Specimen Yaml-based data-driven testing Specimen is a yaml data format for data-driven testing. This enforces separation between feature being tested

Nov 24, 2022
Database testing made easy in Go.

dbtest Database testing made easy in Go. Features Declarative Define the minimum test specification in a YAML-based DSL, then all tests can be generat

Oct 31, 2022
Configuration management tool inspired by Ansible, but with no yaml - just Go

GOSSH This repo is an experiement with creating a declarative IT automation and configuration management package for Golang. Think Ansible, but no Yam

Dec 21, 2022
Fortio load testing library, command line tool, advanced echo server and web UI in go (golang). Allows to specify a set query-per-second load and record latency histograms and other useful stats.
Fortio load testing library, command line tool, advanced echo server and web UI in go (golang). Allows to specify a set query-per-second load and record latency histograms and other useful stats.

Fortio Fortio (Φορτίο) started as, and is, Istio's load testing tool and now graduated to be its own project. Fortio is also used by, among others, Me

Jan 2, 2023
:exclamation:Basic Assertion Library used along side native go testing, with building blocks for custom assertions

Package assert Package assert is a Basic Assertion library used along side native go testing Installation Use go get. go get github.com/go-playground/

Jan 6, 2023
Expressive end-to-end HTTP API testing made easy in Go

baloo Expressive and versatile end-to-end HTTP API testing made easy in Go (golang), built on top of gentleman HTTP client toolkit. Take a look to the

Dec 13, 2022
Simple Go snapshot testing
Simple Go snapshot testing

Incredibly simple Go snapshot testing: cupaloy takes a snapshot of your test output and compares it to a snapshot committed alongside your tests. If t

Jan 5, 2023