Categorygithub.com/cjwcjswo/dbunit
repositorypackage
0.0.1
Repository: https://github.com/cjwcjswo/dbunit.git
Documentation: pkg.go.dev

# Packages

No description provided by the author

# 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