Skip to content

Commit

Permalink
return db info
Browse files Browse the repository at this point in the history
  • Loading branch information
cglotr committed Jun 7, 2022
1 parent cc6ae74 commit e107c7a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 16 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/go-sql-driver/mysql v1.6.0
github.com/hooligram/kifu v1.2.1
github.com/hooligram/kifu v1.2.4
github.com/testcontainers/testcontainers-go v0.11.1
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ github.com/hashicorp/go-multierror v0.0.0-20161216184304-ed905158d874/go.mod h1:
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hooligram/kifu v1.2.1 h1:2eupNh9dykFr1bW1RGaPksdLRSUC5fdpF9FWgsxvFcM=
github.com/hooligram/kifu v1.2.1/go.mod h1:J+D2Lx9TuRGq4R+EFZ0T3lsjO2QL69hIEeslj87zO+Y=
github.com/hooligram/kifu v1.2.4 h1:FOknP3mL4hGh5jR1x0bdChD5QKUEPmpS4Rmn5G3POlo=
github.com/hooligram/kifu v1.2.4/go.mod h1:J+D2Lx9TuRGq4R+EFZ0T3lsjO2QL69hIEeslj87zO+Y=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
Expand Down
12 changes: 12 additions & 0 deletions mysqltestcontainer/result.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package mysqltestcontainer

import "database/sql"

type Result struct {
Db *sql.DB
Username string
Password string
Ip string
Port string
Database string
}
16 changes: 12 additions & 4 deletions mysqltestcontainer/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package mysqltestcontainer

import (
"context"
"database/sql"
"fmt"

_ "github.com/go-sql-driver/mysql"
Expand All @@ -15,7 +14,7 @@ const (
rootPassword = "password"
)

func Start(databaseName string, migrationDir string) (*sql.DB, error) {
func Start(databaseName string, migrationDir string) (*Result, error) {
kifu.Info("Starting MySQL test container...")
req := testcontainers.ContainerRequest{
Image: "mysql:5.6",
Expand All @@ -42,7 +41,8 @@ func Start(databaseName string, migrationDir string) (*sql.DB, error) {
if err != nil {
return nil, err
}
db, err := open(host, fmt.Sprint(port.Int()), rootPassword, databaseName)
p := fmt.Sprint(port.Int())
db, err := open(host, p, rootPassword, databaseName)
if err != nil {
return nil, err
}
Expand All @@ -57,5 +57,13 @@ func Start(databaseName string, migrationDir string) (*sql.DB, error) {
}
}
kifu.Info("MySQL test container started successfully!")
return db, nil
result := &Result{
Db: db,
Username: "root",
Password: rootPassword,
Ip: host,
Port: p,
Database: databaseName,
}
return result, nil
}
4 changes: 2 additions & 2 deletions mysqltestcontainer/start_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import (
)

func ExampleStart() {
db, _ := mysqltestcontainer.Start("test", "./../migration")
db.Ping()
result, _ := mysqltestcontainer.Start("test", "./../migration/example")
result.Db.Ping()
}
17 changes: 10 additions & 7 deletions mysqltestcontainer/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@ import (
)

func TestStart(t *testing.T) {
db, err := mysqltestcontainer.Start("test", "")
result, err := mysqltestcontainer.Start("test", "")
if err != nil {
t.Errorf("Failed to start: %v", err.Error())
}
err = db.Ping()
err = result.Db.Ping()
if err != nil {
t.Errorf("Ping failed: %v", err.Error())
}
}

func TestStartExample(t *testing.T) {
db, err := mysqltestcontainer.Start("test", "./../migration/example")
result, err := mysqltestcontainer.Start("test", "./../migration/example")
if err != nil {
t.Errorf("Got=%v\n", err.Error())
}
db := result.Db
names := []string{"Clare", "Teresa", "Priscilla"}
for _, name := range names {
db.Exec(`INSERT INTO user (username, name) VALUES (?, ?);`, name, name)
Expand Down Expand Up @@ -49,10 +50,12 @@ func TestStartMigration(t *testing.T) {
}

func TestStartMissing(t *testing.T) {
_, err := mysqltestcontainer.Start("test", "./../migration/missing")
if err != nil && err.Error() != "open ./../migration/missing: no such file or directory" {
panic(err)
}
defer func() {
if r := recover(); r == nil {
t.Error("Expected panic to happen when migration files are missing")
}
}()
mysqltestcontainer.Start("test", "./../migration/missing")
}

func TestStartInvalid(t *testing.T) {
Expand Down

0 comments on commit e107c7a

Please sign in to comment.