Golang package for database testing (like PHP DBunit)
- Download and install it:
$ go get -u github.com/deadkrolik/godbt
- Import it in your code:
package test
import (
"github.com/deadkrolik/godbt"
"github.com/deadkrolik/godbt/contract"
)
- Create test object, shared for all test functions and configure mysql conection:
var (
tester *godbt.Tester
err error
)
func init() {
tester, err = godbt.GetTester(contract.InstallerConfig{
Type: "mysql",
ConnString: "user:password@/database?charset=utf8&parseTime=True&loc=Local",
ClearMethod: contract.ClearMethodTruncate,
})
if err != nil {
panic(err.Error())
}
}
Image - set of rows you can load from different sources.
- XML-file as a source:
image, err := tester.GetImageManager().LoadImage("path/to/file.xml")
if err != nil {
//...
}
- XML-formatted string as a source:
image, err := tester.GetImageManager().LoadImage(`
<?xml version="1.0" ?>
<dataset>
<data id="1" k1="1" k2="2"/>
<data id="2" k1="2" k2="3"/>
</dataset>`)
if err != nil {
//...
}
Root tag should be "dataset", "data" in example is a table name, "k1", "k2" - columns names.
- Second optional parameter for LoadImage is a list of replacement functions:
contract.ModifiersList{
"value1": func(table string, key string, value string) string {
return "modified_" + value
},
})
If we have XML-file like that:
<?xml version="1.0" ?>
<dataset>
<data k1="value1" k2="value2" k3="---value1---"/>
</dataset>
The result Image will have k1="modified_value1", k2="value2", k3="---modified_value1---". So replacement function will be called if attribute value contains your string. It can be used to deal with templates (replace "{CURRENT_DATE}" to real date, for example).
- Here is a code, that inserts dataset to real database:
err = tester.GetInstaller().InstallImage(image)
if err != nil {
//...
}
- It can be done in transaction (it's faster, but you can't use ClearMethodTruncate):
installer := tester.GetInstaller()
err = installer.WithTransaction()
if err != nil {
//...
}
err = installer.SetClearMethod(contract.ClearMethodDeleteAll).InstallImage(yourImage)
if err != nil {
//...
}
//here is you app code
err = installer.Rollback()
if err != nil {
//...
}
-
After loading and installing image you should run your real app code, that works with database.
-
Then you can check if you database state correct and the same as you predefined state, that can be described as another Image.
-
Checking count of rows in real table:
count, err := tester.GetInstaller().GetTableRowsCount("data")
if err != nil {
//...
}
//count assert ...
- Checking state:
realImage, err := tester.GetInstaller().GetTableImage(
"data",//table name
[]string{"column1", "column2", "column3"},//only columns
map[string]int{//rows order is important
"column1": contract.SortAsc,
"column2": contract.SortDesc,
},
)
if err != nil {
//...
}
testImage, err := tester.GetImageManager().LoadImage(`
<?xml version="1.0" ?>
<dataset>
<data column1="1" column2="2" column3="1"/>
<data column1="2" column2="2" column3="2"/>
</dataset>`
)
if err != nil {
//...
}
diffs := tester.GetImageManager().GetImagesDiff(testImage, realImage)
t.Log(diffs)
If images a same diffs will have zero length. If not - will contain error messages.