Skip to content

Commit

Permalink
move integration test into /test directory
Browse files Browse the repository at this point in the history
removed also the // +build !integration flags of the unit tests.
  • Loading branch information
fraenky8 committed Feb 22, 2020
1 parent aefec5b commit 09bc032
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 45 deletions.
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: all build
.PHONY: all install test

all: help

Expand All @@ -9,6 +9,12 @@ help: ## Show this help
install: ## Installs tables-to-go. Same behavior like `go install -mod=vendor .`
go install -mod=vendor .

test:
go test -race ./...

integration-test:
go test -race -tags=integration ./...

sqlite3: ## Installs tables-to-go with sqlite3 driver and the \
## User Authentication feature enabled. \
## For more information see the documentation of the driver at \
Expand Down
2 changes: 0 additions & 2 deletions internal/cli/tables-to-go-cli_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build !integration

package cli

import (
Expand Down
2 changes: 0 additions & 2 deletions pkg/database/postgresql_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build !integration

package database

import (
Expand Down
2 changes: 0 additions & 2 deletions pkg/database/sqlite_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build !integration

package database

import (
Expand Down
2 changes: 0 additions & 2 deletions pkg/output/decorator_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build !integration

package output

import (
Expand Down
2 changes: 0 additions & 2 deletions pkg/output/writer_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build !integration

package output

import (
Expand Down
2 changes: 0 additions & 2 deletions pkg/settings/settings_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build !integration

package settings

import (
Expand Down
2 changes: 0 additions & 2 deletions pkg/tagger/mastermind_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build !integration

package tagger

import (
Expand Down
2 changes: 0 additions & 2 deletions pkg/tagger/tagger_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build !integration

package tagger

import (
Expand Down
78 changes: 50 additions & 28 deletions internal/cli/integration_test.go → test/integration_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build integration

package cli
package test

import (
"bytes"
Expand All @@ -18,16 +18,12 @@ import (
"github.com/ory/dockertest"
"github.com/stretchr/testify/assert"

"github.com/fraenky8/tables-to-go/internal/cli"
"github.com/fraenky8/tables-to-go/pkg/database"
"github.com/fraenky8/tables-to-go/pkg/output"
"github.com/fraenky8/tables-to-go/pkg/settings"
)

const (
expectedFilePath = "testdata/expected"
outputFilePath = "testdata/output"
)

type cliWriter struct{}

func (c cliWriter) Write(tableName string, content string) error {
Expand All @@ -43,40 +39,63 @@ type dbSettings struct {
env []string
}

func (s *dbSettings) imageName() string {
return s.dockerImage + s.version
}

func (s *dbSettings) setSettings(ss *settings.Settings) {
s.Settings = ss
s.Settings.OutputFilePath = filepath.Join(s.imageName(), "output")
}

func (s *dbSettings) getTestdataFilepath() string {
return filepath.Join(s.imageName(), "testdata")
}

func (s *dbSettings) getExceptedFilepath() string {
return filepath.Join(s.imageName(), "expected")
}

func TestIntegration(t *testing.T) {
log.Println("running Tables-to-Go integration tests")

tests := []struct {
desc string
settings func() dbSettings
settings *dbSettings
}{
{
desc: "mysql 8",
settings: func() dbSettings {
settings: func() *dbSettings {
s := settings.New()
s.DbType = settings.DbTypeMySQL
s.User = "root"
s.Pswd = "mysecretpassword"
s.DbName = "public"
s.Host = "localhost"
s.Port = "3306"
s.OutputFilePath = outputFilePath
//s.Verbose = true
//s.VVerbose = true

return dbSettings{
dbs := &dbSettings{
dockerImage: "mysql",
version: "8",
env: []string{"MYSQL_DATABASE=public", "MYSQL_ROOT_PASSWORD=mysecretpassword"},
Settings: s,
env: []string{
"MYSQL_DATABASE=public",
"MYSQL_ROOT_PASSWORD=mysecretpassword",
},
Settings: s,
}
},

dbs.setSettings(s)

return dbs
}(),
},
}

for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
s := test.settings()
s := test.settings

db, purgeFn, err := setupDatabase(s)
if err != nil {
Expand All @@ -93,31 +112,32 @@ func TestIntegration(t *testing.T) {
}
}

_ = os.RemoveAll(outputFilePath)
_ = os.MkdirAll(outputFilePath, 0755)
// TODO need flag for not removing generated output but
// save it into the expected directory
_ = os.RemoveAll(s.Settings.OutputFilePath)
_ = os.MkdirAll(s.Settings.OutputFilePath, 0755)
}()

writer := output.NewFileWriter(outputFilePath)
writer := output.NewFileWriter(s.Settings.OutputFilePath)
//writer := cliWriter{}

s.Settings.OutputFilePath = outputFilePath
prefix := strings.Title(s.dockerImage + s.version)
prefix := strings.Title(s.imageName())
s.Settings.Prefix = prefix + "_"

err = Run(s.Settings, db, writer)
err = cli.Run(s.Settings, db, writer)
assert.NoError(t, err)

checkFiles(t, prefix)
checkFiles(t, s)
})
}
}

func checkFiles(t *testing.T, prefix string) {
expectedPattern := filepath.Join(expectedFilePath, prefix+"*")
func checkFiles(t *testing.T, s *dbSettings) {
expectedPattern := filepath.Join(s.getExceptedFilepath(), s.Settings.Prefix+"*")
expected, err := filepath.Glob(expectedPattern)
assert.NoError(t, err)

actualPattern := filepath.Join(outputFilePath, prefix+"*")
actualPattern := filepath.Join(s.Settings.OutputFilePath, s.Settings.Prefix+"*")
actual, err := filepath.Glob(actualPattern)
assert.NoError(t, err)

Expand All @@ -139,7 +159,7 @@ func checkFiles(t *testing.T, prefix string) {
}
}

func setupDatabase(settings dbSettings) (database.Database, func() error, error) {
func setupDatabase(settings *dbSettings) (database.Database, func() error, error) {
log.Printf("spinning up Database %s:%s ...\n", settings.dockerImage, settings.version)
pool, err := dockertest.NewPool("")
if err != nil {
Expand Down Expand Up @@ -178,16 +198,18 @@ func setupDatabase(settings dbSettings) (database.Database, func() error, error)
return nil, purgeFn, fmt.Errorf("could not connect to Docker: %v", err)
}

err = createTestData(db.SQLDriver(), settings.dockerImage+settings.version)
err = populateData(db.SQLDriver(), settings)
if err != nil {
return nil, purgeFn, err
}

return db, purgeFn, nil
}

func createTestData(db *sqlx.DB, dockerImage string) error {
data, err := ioutil.ReadFile("testdata/" + dockerImage + ".sql")
func populateData(db *sqlx.DB, s *dbSettings) error {
// TODO account for multiple SQL files
f := filepath.Join(s.getTestdataFilepath(), s.imageName()+".sql")
data, err := ioutil.ReadFile(f)
if err != nil {
return fmt.Errorf("could not read sql testdata: %v", err)
}
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 09bc032

Please sign in to comment.