dbunit

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 28, 2019 License: MIT Imports: 7 Imported by: 0

README

Golang Database Testing tool

GoDoc Go Report Card

Install

go get github.com/cjwcjswo/dbunit

Usage

1. Init database config function
func init() {
	dbunit.InitSetupFunc(loadConfig)
}

func loadConfig() dbunit.ConfigMap {
	return dbunit.ConfigMap{
		DbName: {
			DriverName: "mysql",
			Host:       "127.0.0.1",
			Port:       3306,
			UserName:   "root",
			Password:   "",
			Name:       DbName,
			SqlPaths: []string{
				"fixtures/init_table.sql", // table scheme files
			},
		},
	}
}
2. Define 'Before Table Data' & "After Table Data"
  • 'Before Table Data': Table data needed to run the handler

  • 'After Table Data': Expected table Data after handler execution

func BeforeFunc() dbunit.FixtureData {
	return dbunit.FixtureData{
		"TB_USER": {
			{
				"name":    "cjwoov",
				"age":     28,
				"country": "korea",
			},
			{
				"name":    "battlecook",
				"age":     34,
				"country": "korea",
			},
		},
	}
}
func AfterFunc() dbunit.FixtureData {
	return dbunit.FixtureData{
		"TB_USER": {
			{
				"name": "cjwoov",
				"age":  29,
			},
			{
				"name":    "battlecook",
				"age":     34,
				"country": "korea",
			},
		},
	}
}

3. Initialize the data needed to execute the logic
	c := dbunit.FixtureCollection{
		DbName: &dbunit.FixtureElement{
			Before: BeforeFunc,
			After:  AfterFunc,
		},
	}
	dbunit.Init(&c) // Table data initialized after this function is called.
4. Compare Real Table data to Expected Table data
dbunit.AssertTableData(t)
Example test result, If result is not correct.
=== RUN   TestFail
TB_USER
------------------------------------------------------------------------------------------------------------------------
|            seq              |            name             |            age              |          country           |
------------------------------------------------------------------------------------------------------------------------
|             1               |           cjwoov            |         29(!= 27)           |           korea            |
------------------------------------------------------------------------------------------------------------------------
|             2               |         battlecook          |             34              |           korea            |
------------------------------------------------------------------------------------------------------------------------


--- FAIL: TestFail (0.16s)
FAIL

Support drivers

  • mysql

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertTableData

func AssertTableData(t *testing.T)

Compare Fixture 'After' Table Data to Real Database Table Data

If debug mode on, print the log regardless of the result value

If return value is true, expected and real data same.

func DebugMode

func DebugMode(mode bool)

If debug mode is true, print the result log.

func Init

func Init(collection *FixtureCollection)

1. Load config

Config function is initialized by 'InitSetupFunc'. If you want to set load config function, call 'InitSetupFunc'.

2. Connect to Database

It is set based on the configuration file. If the 'SqlBytes' field of 'Config' struct is set, Database executes 'SqlBytes'. 'SqlBytes' means byte stream of '.sql' file.

3. Insert Database row data, that defined in the 'Before function' field of 'FixtureElement'.

func InitSetupFunc

func InitSetupFunc(loadConfigFunc func() ConfigMap)

Initialized 'setup' function. loadConfigFunc is called by 'Init' function

Types

type ColumnName

type ColumnName = string

type ConfigMap

type ConfigMap map[DbName]DbConfig

type DataValue

type DataValue = interface{}

type DbConfig

type DbConfig struct {
	DriverName string
	Host       string
	Port       int
	UserName   string
	Password   string
	Name       DbName

	SqlPaths []string
}

type DbName

type DbName = string

type FixtureCollection

type FixtureCollection map[DbName]*FixtureElement

type FixtureData

type FixtureData map[TableName]TableData

type FixtureElement

type FixtureElement struct {
	Before func() FixtureData
	After  func() FixtureData
}

type RowData

type RowData = map[ColumnName]DataValue

type TableData

type TableData []RowData

type TableName

type TableName = string

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL