From d546f7bc915ff49373996e48def1f05a11b192fc Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Wed, 13 Nov 2019 21:22:17 +0100 Subject: [PATCH] fix tests --- config/config.go | 11 ++++++----- database/database.go | 2 -- database/database_test.go | 4 +++- routes/dashboard/dashboard_test.go | 5 +++-- routes/file/file_test.go | 5 +++-- routes/healthz/healthz_test.go | 11 ++++++----- routes/scenario/scenario_test.go | 5 +++-- routes/signal/signal_test.go | 5 +++-- routes/simulationmodel/simulationmodel_test.go | 5 +++-- routes/simulator/simulator_test.go | 5 +++-- routes/user/user_test.go | 5 +++-- routes/widget/widget_test.go | 5 +++-- start.go | 13 ++++++------- 13 files changed, 45 insertions(+), 36 deletions(-) diff --git a/config/config.go b/config/config.go index 7a8af4b..c9dbf31 100644 --- a/config/config.go +++ b/config/config.go @@ -75,11 +75,12 @@ func InitConfig() *config.Config { log.Fatal("failed to parse config") } - settings, _ := c.Settings() - - log.Print("All settings:") - for key, val := range settings { - log.Printf(" %s = %s \n", key, val) + if m, _ := c.String("mode"); m != "test" { + settings, _ := c.Settings() + log.Print("All settings:") + for key, val := range settings { + log.Printf(" %s = %s \n", key, val) + } } // Save pointer to global variable diff --git a/database/database.go b/database/database.go index 8f566aa..d7f5eca 100644 --- a/database/database.go +++ b/database/database.go @@ -37,9 +37,7 @@ func InitDB(cfg *config.Config) *gorm.DB { if mode == "test" || init { DropTables(db) log.Println("Database tables dropped") - } - if init { DBAddTestData(db) log.Println("Database initialized with test data") } diff --git a/database/database_test.go b/database/database_test.go index cf33cc8..9506f80 100644 --- a/database/database_test.go +++ b/database/database_test.go @@ -7,12 +7,14 @@ import ( "testing" "github.com/stretchr/testify/assert" + "git.rwth-aachen.de/acs/public/villas/web-backend-go/config" ) var db *gorm.DB func TestMain(m *testing.M) { - db = InitDB(DB_NAME, true) + c := config.InitConfig() + db = InitDB(c) // Verify that you can connect to the database err := db.DB().Ping() diff --git a/routes/dashboard/dashboard_test.go b/routes/dashboard/dashboard_test.go index ea36a75..6b08c38 100644 --- a/routes/dashboard/dashboard_test.go +++ b/routes/dashboard/dashboard_test.go @@ -2,6 +2,7 @@ package dashboard import ( "fmt" + "git.rwth-aachen.de/acs/public/villas/web-backend-go/config" "git.rwth-aachen.de/acs/public/villas/web-backend-go/database" "git.rwth-aachen.de/acs/public/villas/web-backend-go/helper" "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/scenario" @@ -54,8 +55,8 @@ func addScenario(token string) (scenarioID uint) { } func TestMain(m *testing.M) { - - db = database.InitDB(database.DB_NAME, true) + c := config.InitConfig() + db = database.InitDB(c) defer db.Close() router = gin.Default() diff --git a/routes/file/file_test.go b/routes/file/file_test.go index 3d582ea..7626751 100644 --- a/routes/file/file_test.go +++ b/routes/file/file_test.go @@ -5,6 +5,7 @@ import ( "fmt" "git.rwth-aachen.de/acs/public/villas/web-backend-go/database" "git.rwth-aachen.de/acs/public/villas/web-backend-go/helper" + "git.rwth-aachen.de/acs/public/villas/web-backend-go/config" "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/dashboard" "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/scenario" "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/simulationmodel" @@ -159,8 +160,8 @@ func addScenarioAndSimulatorAndSimulationModelAndDashboardAndWidget() (scenarioI } func TestMain(m *testing.M) { - - db = database.InitDB(database.DB_NAME, true) + c := config.InitConfig() + db = database.InitDB(c) defer db.Close() router = gin.Default() diff --git a/routes/healthz/healthz_test.go b/routes/healthz/healthz_test.go index f8c78d9..9d633a3 100644 --- a/routes/healthz/healthz_test.go +++ b/routes/healthz/healthz_test.go @@ -2,7 +2,7 @@ package healthz import ( "git.rwth-aachen.de/acs/public/villas/web-backend-go/amqp" - //"git.rwth-aachen.de/acs/public/villas/web-backend-go/amqp" + "git.rwth-aachen.de/acs/public/villas/web-backend-go/config" "git.rwth-aachen.de/acs/public/villas/web-backend-go/database" "git.rwth-aachen.de/acs/public/villas/web-backend-go/helper" "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/user" @@ -11,7 +11,6 @@ import ( "github.com/stretchr/testify/assert" "net/http" - //"net/http" "testing" ) @@ -20,7 +19,8 @@ var db *gorm.DB func TestHealthz(t *testing.T) { // connect DB - db = database.InitDB(database.DB_NAME, true) + c := config.InitConfig() + db = database.InitDB(c) defer db.Close() assert.NoError(t, database.DBAddAdminAndUserAndGuest(db)) @@ -47,7 +47,7 @@ func TestHealthz(t *testing.T) { assert.Equalf(t, 500, code, "Response body: \n%v\n", resp) // reconnect DB - db = database.InitDB(database.DB_NAME, false) + db = database.InitDB(c) defer db.Close() // test healthz endpoint for connected DB and unconnected AMQP client @@ -56,7 +56,8 @@ func TestHealthz(t *testing.T) { assert.Equalf(t, 500, code, "Response body: \n%v\n", resp) // connect AMQP client (make sure that AMQP_URL is set via command line parameter -amqp) - err = amqp.ConnectAMQP(database.AMQP_URL) + url, _ := c.String("amqp.url") + err = amqp.ConnectAMQP(url) assert.NoError(t, err) // test healthz endpoint for connected DB and AMQP client diff --git a/routes/scenario/scenario_test.go b/routes/scenario/scenario_test.go index 88ca525..0df8b6e 100644 --- a/routes/scenario/scenario_test.go +++ b/routes/scenario/scenario_test.go @@ -3,6 +3,7 @@ package scenario import ( "fmt" "git.rwth-aachen.de/acs/public/villas/web-backend-go/helper" + "git.rwth-aachen.de/acs/public/villas/web-backend-go/config" "github.com/gin-gonic/gin" "github.com/jinzhu/gorm" "github.com/jinzhu/gorm/dialects/postgres" @@ -32,8 +33,8 @@ type UserRequest struct { } func TestMain(m *testing.M) { - - db = database.InitDB(database.DB_NAME, true) + c := config.InitConfig() + db = database.InitDB(c) defer db.Close() router = gin.Default() diff --git a/routes/signal/signal_test.go b/routes/signal/signal_test.go index 852fd5c..7abeefe 100644 --- a/routes/signal/signal_test.go +++ b/routes/signal/signal_test.go @@ -2,6 +2,7 @@ package signal import ( "fmt" + "git.rwth-aachen.de/acs/public/villas/web-backend-go/config" "git.rwth-aachen.de/acs/public/villas/web-backend-go/database" "git.rwth-aachen.de/acs/public/villas/web-backend-go/helper" "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/scenario" @@ -105,8 +106,8 @@ func addScenarioAndSimulatorAndSimulationModel() (scenarioID uint, simulatorID u } func TestMain(m *testing.M) { - - db = database.InitDB(database.DB_NAME, true) + c := config.InitConfig() + db = database.InitDB(c) defer db.Close() router = gin.Default() diff --git a/routes/simulationmodel/simulationmodel_test.go b/routes/simulationmodel/simulationmodel_test.go index 93b3239..a27a324 100644 --- a/routes/simulationmodel/simulationmodel_test.go +++ b/routes/simulationmodel/simulationmodel_test.go @@ -2,6 +2,7 @@ package simulationmodel import ( "fmt" + "git.rwth-aachen.de/acs/public/villas/web-backend-go/config" "git.rwth-aachen.de/acs/public/villas/web-backend-go/database" "git.rwth-aachen.de/acs/public/villas/web-backend-go/helper" "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/scenario" @@ -94,8 +95,8 @@ func addScenarioAndSimulator() (scenarioID uint, simulatorID uint) { } func TestMain(m *testing.M) { - - db = database.InitDB(database.DB_NAME, true) + c := config.InitConfig() + db = database.InitDB(c) defer db.Close() router = gin.Default() diff --git a/routes/simulator/simulator_test.go b/routes/simulator/simulator_test.go index 2c36ebb..893bac9 100644 --- a/routes/simulator/simulator_test.go +++ b/routes/simulator/simulator_test.go @@ -12,6 +12,7 @@ import ( "github.com/gin-gonic/gin" "git.rwth-aachen.de/acs/public/villas/web-backend-go/database" + "git.rwth-aachen.de/acs/public/villas/web-backend-go/config" "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/user" ) @@ -27,8 +28,8 @@ type SimulatorRequest struct { } func TestMain(m *testing.M) { - - db = database.InitDB(database.DB_NAME, true) + c := config.InitConfig() + db = database.InitDB(c) defer db.Close() router = gin.Default() diff --git a/routes/user/user_test.go b/routes/user/user_test.go index 316cf6e..ae81318 100644 --- a/routes/user/user_test.go +++ b/routes/user/user_test.go @@ -15,6 +15,7 @@ import ( "github.com/stretchr/testify/assert" "git.rwth-aachen.de/acs/public/villas/web-backend-go/database" + "git.rwth-aachen.de/acs/public/villas/web-backend-go/config" ) var router *gin.Engine @@ -30,8 +31,8 @@ type UserRequest struct { } func TestMain(m *testing.M) { - - db = database.InitDB(database.DB_NAME, true) + c := config.InitConfig() + db = database.InitDB(c) defer db.Close() router = gin.Default() diff --git a/routes/widget/widget_test.go b/routes/widget/widget_test.go index 13f20d8..08c2d6a 100644 --- a/routes/widget/widget_test.go +++ b/routes/widget/widget_test.go @@ -2,6 +2,7 @@ package widget import ( "fmt" + "git.rwth-aachen.de/acs/public/villas/web-backend-go/config" "git.rwth-aachen.de/acs/public/villas/web-backend-go/database" "git.rwth-aachen.de/acs/public/villas/web-backend-go/helper" "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/dashboard" @@ -79,8 +80,8 @@ func addScenarioAndDashboard(token string) (scenarioID uint, dashboardID uint) { } func TestMain(m *testing.M) { - - db = database.InitDB(database.DB_NAME, true) + c := config.InitConfig() + db = database.InitDB(c) defer db.Close() router = gin.Default() diff --git a/start.go b/start.go index 2ab7736..db48806 100644 --- a/start.go +++ b/start.go @@ -37,17 +37,16 @@ import ( func main() { log.Println("Starting VILLASweb-backend-go") - c.InitConfig() - - db := database.InitDB(c.Config) + c := c.InitConfig() + db := database.InitDB(c) defer db.Close() - if m, _ := c.Config.String("mode"); m == "release" { + if m, _ := c.String("mode"); m == "release" { gin.SetMode(gin.ReleaseMode) } - baseHost, _ := c.Config.String("base.host") - basePath, _ := c.Config.String("base.path") + baseHost, _ := c.String("base.host") + basePath, _ := c.String("base.path") docs.SwaggerInfo.Host = baseHost docs.SwaggerInfo.BasePath = basePath @@ -73,7 +72,7 @@ func main() { r.GET("swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) - amqpurl, _ := c.Config.String("amqp.url") + amqpurl, _ := c.String("amqp.url") if amqpurl != "" { log.Println("Starting AMQP client")