项目作者: dogukanayd

项目描述 :
go-test-database
高级语言: Go
项目地址: git://github.com/dogukanayd/go-test-database.git
创建时间: 2020-05-11T18:50:57Z
项目社区:https://github.com/dogukanayd/go-test-database

开源协议:MIT License

下载


go-test-database

What this package does?

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.

alt text

Requirements

  • :3305 port must not be used
  • docker

INSTALL

  1. go get github.com/dogukanayd/go-test-database

USAGE

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.

  1. connection, def := mysql_unit.NewUnit()

and below you can find full dummy example

  1. package greetings
  2. import (
  3. mysql_unit "github.com/dogukanayd/go-test-database/mysql-unit"
  4. "log"
  5. "testing"
  6. )
  7. func TestHello(t *testing.T) {
  8. connection, def := mysql_unit.NewUnit()
  9. q := `CREATE TABLE test_table(id int(11),name varchar(500)) ENGINE = InnoDB DEFAULT CHARSET = utf8;`
  10. _, err := connection.Query(q)
  11. if err != nil {
  12. log.Fatal(err)
  13. }
  14. defer def()
  15. t.Log("ping success")
  16. }

When you call the NewUnit function;

  1. 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;

  1. func NewUnit() (*sql.DB, func()) {}