This package creates up and running mysql container(using docker sdk) for your applications and returns a connection to the created database, so you can easily query the test database.
- :3305 port must not be used
- docker
go get github.com/dogukanayd/go-test-database
Let's say you have a package named greetings also in this package you have a function named Hello
and this function send some query to the database, well from at this point you need a database separated
from your local database. Here is all you need to do.
connection, def := mysql_unit.NewUnit()
and below you can find full dummy example
package greetings
import (
mysql_unit "github.com/dogukanayd/go-test-database/mysql-unit"
"log"
"testing"
)
func TestHello(t *testing.T) {
connection, def := mysql_unit.NewUnit()
q := `CREATE TABLE test_table(id int(11),name varchar(500)) ENGINE = InnoDB DEFAULT CHARSET = utf8;`
_, err := connection.Query(q)
if err != nil {
log.Fatal(err)
}
defer def()
t.Log("ping success")
}
When you call the NewUnit
function;
connection, tearDown := NewUnit()
it's return two parameters;
- connection: *sql.DB // connection that allows you to query the database
- tearDown
here is the defination of the NewUnit
function;
func NewUnit() (*sql.DB, func()) {}