|
@@ -1,12 +1,15 @@
|
|
|
package infrastructure
|
|
|
|
|
|
import (
|
|
|
- "code.osinet.fr/fgm/kurz/domain"
|
|
|
"database/sql"
|
|
|
- "github.com/pressly/goose"
|
|
|
+ "fmt"
|
|
|
"os"
|
|
|
"testing"
|
|
|
|
|
|
+ "code.osinet.fr/fgm/kurz/domain"
|
|
|
+ "github.com/pressly/goose"
|
|
|
+ "github.com/spf13/viper"
|
|
|
+
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
)
|
|
|
|
|
@@ -65,7 +68,7 @@ func TestMySQLSad(test *testing.T) {
|
|
|
|
|
|
/**
|
|
|
mySQLTestSetup opens the database and initialized it from the production database schema.
|
|
|
- */
|
|
|
+*/
|
|
|
func mySQLTestSetup(t *testing.T) *sql.DB {
|
|
|
db, err := DbDial(ParseTestDbCred())
|
|
|
if err != nil {
|
|
@@ -74,7 +77,23 @@ func mySQLTestSetup(t *testing.T) *sql.DB {
|
|
|
|
|
|
// Ensure current schema for test DB.
|
|
|
cwd, _ := os.Getwd()
|
|
|
- goose.Up(db, cwd)
|
|
|
+ current, err := goose.GetDBVersion(db)
|
|
|
+ if err != nil {
|
|
|
+ t.Errorf("setup failed to obtain the test DB version: %s", err)
|
|
|
+ t.FailNow()
|
|
|
+ }
|
|
|
+ versions, err := goose.CollectMigrations(cwd, current, int64((1<<63)-1))
|
|
|
+ if err != nil {
|
|
|
+ t.Errorf("setup failed to obtain the test DB migrations: %s", err)
|
|
|
+ t.FailNow()
|
|
|
+ }
|
|
|
+ if versions != nil {
|
|
|
+ err = goose.Up(db, cwd)
|
|
|
+ if err != nil {
|
|
|
+ t.Errorf("setup failed to upgrade the test DB schema: %s", err)
|
|
|
+ t.FailNow()
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
// Ensure empty test DB.
|
|
|
_, err = db.Exec("DELETE FROM map")
|
|
@@ -96,6 +115,15 @@ func mySQLTestTeardown(t *testing.T, db *sql.DB) {
|
|
|
}
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
+ viper.SetConfigName("config")
|
|
|
+ viper.AddConfigPath(".")
|
|
|
+ viper.AddConfigPath("$HOME/.kurz")
|
|
|
+ err := viper.ReadInConfig()
|
|
|
+ if err != nil {
|
|
|
+ fmt.Fprintln(os.Stderr, err)
|
|
|
+ os.Exit(2)
|
|
|
+ }
|
|
|
+
|
|
|
exitCode := m.Run()
|
|
|
os.Exit(exitCode)
|
|
|
}
|