Skip to content

Commit

Permalink
Set meaningful names for databases used in tests (#172)
Browse files Browse the repository at this point in the history
* set meaningful database name for tests

* protect against out of bounds error

* add backticks to allow hypens

* fix last breaking test
  • Loading branch information
vxio authored Jun 25, 2021
1 parent 9ee3439 commit 7287929
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 142 deletions.
2 changes: 1 addition & 1 deletion database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func Test_NewAndMigration_MySql(t *testing.T) {
mySQLConfig, err := findOrLaunchMySQLContainer()
require.NoError(t, err)

databaseName, err := createTemporaryDatabase(mySQLConfig)
databaseName, err := createTemporaryDatabase(t, mySQLConfig)
require.NoError(t, err)

config := DatabaseConfig{
Expand Down
24 changes: 17 additions & 7 deletions database/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ import (
"testing"
"time"

"github.com/ory/dockertest/v3"
"github.com/stretchr/testify/require"

"github.com/moov-io/base"
"github.com/moov-io/base/docker"

kitprom "github.com/go-kit/kit/metrics/prometheus"
gomysql "github.com/go-sql-driver/mysql"
"github.com/ory/dockertest/v3"
dc "github.com/ory/dockertest/v3/docker"
stdprom "github.com/prometheus/client_golang/prometheus"

Expand Down Expand Up @@ -159,7 +158,7 @@ func CreateTestMySQLDB(t *testing.T) *TestMySQLDB {
require.NoError(t, err)
})

dbName, err := createTemporaryDatabase(sharedMySQLConfig)
dbName, err := createTemporaryDatabase(t, sharedMySQLConfig)
require.NoError(t, err)

dbConfig := &DatabaseConfig{
Expand Down Expand Up @@ -187,22 +186,33 @@ func CreateTestMySQLDB(t *testing.T) *TestMySQLDB {

// We connect as root to MySQL server and create database with random name to
// run our migrations on it later.
func createTemporaryDatabase(config *MySQLConfig) (string, error) {
func createTemporaryDatabase(t *testing.T, config *MySQLConfig) (string, error) {
dsn := fmt.Sprintf("%s:%s@%s/", "root", config.Password, config.Address)
db, err := sql.Open("mysql", dsn)
if err != nil {
return "", err
}
defer db.Close()

dbName := "test" + base.ID()
maxIdx := len(t.Name()) - 1
if maxIdx > 20 {
maxIdx = 20
}

// Set dbName to something like `TestCreateTemporaryD-Jun-25-08:30:07`
dbName := fmt.Sprintf(
"%s %s",
t.Name()[:maxIdx],
time.Now().Local().Format(time.Stamp),
)
dbName = strings.ReplaceAll(dbName, " ", "-")

_, err = db.ExecContext(context.Background(), fmt.Sprintf("create database %s", dbName))
_, err = db.ExecContext(context.Background(), fmt.Sprintf("create database `%s`", dbName))
if err != nil {
return "", err
}

_, err = db.ExecContext(context.Background(), fmt.Sprintf("grant all on %s.* to '%s'@'%%'", dbName, config.User))
_, err = db.ExecContext(context.Background(), fmt.Sprintf("grant all on `%s`.* to '%s'@'%%'", dbName, config.User))
if err != nil {
return "", err
}
Expand Down
4 changes: 2 additions & 2 deletions database/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ func TestCreateTemporaryDatabase(t *testing.T) {
config, err := findOrLaunchMySQLContainer()
require.NoError(t, err)

name, err := createTemporaryDatabase(config)
name, err := createTemporaryDatabase(t, config)
require.NoError(t, err)
require.Contains(t, name, "test")
require.Contains(t, name, "Test")
}

func TestMySQLModes(t *testing.T) {
Expand Down
6 changes: 0 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ module github.com/moov-io/base
go 1.13

require (
github.com/coreos/bbolt v1.3.2 // indirect
github.com/coreos/etcd v3.3.13+incompatible // indirect
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
github.com/go-kit/kit v0.10.0
github.com/go-sql-driver/mysql v1.6.0
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-migrate/migrate/v4 v4.14.1
github.com/google/uuid v1.2.0
github.com/gorilla/mux v1.8.0
Expand All @@ -18,11 +14,9 @@ require (
github.com/mattn/go-sqlite3 v1.14.7
github.com/ory/dockertest/v3 v3.7.0
github.com/prometheus/client_golang v1.11.0
github.com/prometheus/tsdb v0.7.1 // indirect
github.com/rickar/cal v1.0.5
github.com/spf13/viper v1.8.1
github.com/stretchr/testify v1.7.0
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5 // indirect
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22 // indirect
golang.org/x/text v0.3.6 // indirect
)
Loading

0 comments on commit 7287929

Please sign in to comment.