Commit 70b2ded8 authored by Sonja Happ's avatar Sonja Happ
Browse files

modularize main function, use DBpool instead of local DB in database package...

modularize main function, use DBpool instead of local DB in database package and all testing functions
parent 29e0e0cc
......@@ -34,29 +34,29 @@ import (
var DBpool *gorm.DB // database used by backend
// Initialize connection to the database
func InitDB(cfg *config.Config) (*gorm.DB, error) {
func InitDB(cfg *config.Config) error {
name, err := cfg.String("db.name")
if err != nil {
return nil, err
return err
}
host, err := cfg.String("db.host")
if err != nil {
return nil, err
return err
}
user, err := cfg.String("db.user")
if err != nil && !strings.Contains(err.Error(), "Required setting 'db.user' not set") {
return nil, err
return err
}
pass := ""
if user != "" {
pass, err = cfg.String("db.pass")
if err != nil {
return nil, err
return err
}
}
sslmode, err := cfg.String("db.ssl")
if err != nil {
return nil, err
return err
}
dbinfo := fmt.Sprintf("host=%s sslmode=%s dbname=%s", host, sslmode, name)
......@@ -66,14 +66,14 @@ func InitDB(cfg *config.Config) (*gorm.DB, error) {
db, err := gorm.Open("postgres", dbinfo)
if err != nil {
return nil, err
return err
}
DBpool = db
MigrateModels(db)
MigrateModels()
log.Println("Database connection established")
return db, nil
return nil
}
// Connection pool to already opened DB
......@@ -84,27 +84,27 @@ func GetDB() *gorm.DB {
// Drop all the tables of the database
// TODO: Remove that function from the codebase and substitute the body
// to the Dummy*() where it is called
func DropTables(db *gorm.DB) {
db.DropTableIfExists(&InfrastructureComponent{})
db.DropTableIfExists(&Signal{})
db.DropTableIfExists(&ComponentConfiguration{})
db.DropTableIfExists(&File{})
db.DropTableIfExists(&Scenario{})
db.DropTableIfExists(&User{})
db.DropTableIfExists(&Dashboard{})
db.DropTableIfExists(&Widget{})
func DropTables() {
DBpool.DropTableIfExists(&InfrastructureComponent{})
DBpool.DropTableIfExists(&Signal{})
DBpool.DropTableIfExists(&ComponentConfiguration{})
DBpool.DropTableIfExists(&File{})
DBpool.DropTableIfExists(&Scenario{})
DBpool.DropTableIfExists(&User{})
DBpool.DropTableIfExists(&Dashboard{})
DBpool.DropTableIfExists(&Widget{})
// The following statement deletes the many to many relationship between users and scenarios
db.DropTableIfExists("user_scenarios")
DBpool.DropTableIfExists("user_scenarios")
}
// AutoMigrate the models
func MigrateModels(db *gorm.DB) {
db.AutoMigrate(&InfrastructureComponent{})
db.AutoMigrate(&Signal{})
db.AutoMigrate(&ComponentConfiguration{})
db.AutoMigrate(&File{})
db.AutoMigrate(&Scenario{})
db.AutoMigrate(&User{})
db.AutoMigrate(&Dashboard{})
db.AutoMigrate(&Widget{})
func MigrateModels() {
DBpool.AutoMigrate(&InfrastructureComponent{})
DBpool.AutoMigrate(&Signal{})
DBpool.AutoMigrate(&ComponentConfiguration{})
DBpool.AutoMigrate(&File{})
DBpool.AutoMigrate(&Scenario{})
DBpool.AutoMigrate(&User{})
DBpool.AutoMigrate(&Dashboard{})
DBpool.AutoMigrate(&Widget{})
}
......@@ -23,7 +23,6 @@ package database
import (
"fmt"
"github.com/jinzhu/gorm"
"log"
"os"
"testing"
......@@ -32,33 +31,31 @@ import (
"github.com/stretchr/testify/assert"
)
var db *gorm.DB
func TestMain(m *testing.M) {
err := configuration.InitConfig()
if err != nil {
panic(m)
}
db, err = InitDB(configuration.GolbalConfig)
err = InitDB(configuration.GolbalConfig)
if err != nil {
panic(m)
}
// Verify that you can connect to the database
err = db.DB().Ping()
err = DBpool.DB().Ping()
if err != nil {
log.Panic("Error: DB must ping to run tests")
}
defer db.Close()
defer DBpool.Close()
os.Exit(m.Run())
}
func TestUserAssociations(t *testing.T) {
DropTables(db)
MigrateModels(db)
DropTables()
MigrateModels()
// create copies of global test data
scenarioA := ScenarioA
......@@ -68,28 +65,28 @@ func TestUserAssociations(t *testing.T) {
userB := UserB
// add three users to DB
assert.NoError(t, db.Create(&user0).Error) // Admin
assert.NoError(t, db.Create(&userA).Error) // Normal User
assert.NoError(t, db.Create(&userB).Error) // Normal User
assert.NoError(t, DBpool.Create(&user0).Error) // Admin
assert.NoError(t, DBpool.Create(&userA).Error) // Normal User
assert.NoError(t, DBpool.Create(&userB).Error) // Normal User
// add two scenarios to DB
assert.NoError(t, db.Create(&scenarioA).Error)
assert.NoError(t, db.Create(&scenarioB).Error)
assert.NoError(t, DBpool.Create(&scenarioA).Error)
assert.NoError(t, DBpool.Create(&scenarioB).Error)
// add many-to-many associations between users and scenarios
// User HM Scenarios, Scenario HM Users (Many-to-Many)
assert.NoError(t, db.Model(&userA).Association("Scenarios").Append(&scenarioA).Error)
assert.NoError(t, db.Model(&userA).Association("Scenarios").Append(&scenarioB).Error)
assert.NoError(t, db.Model(&userB).Association("Scenarios").Append(&scenarioA).Error)
assert.NoError(t, db.Model(&userB).Association("Scenarios").Append(&scenarioB).Error)
assert.NoError(t, DBpool.Model(&userA).Association("Scenarios").Append(&scenarioA).Error)
assert.NoError(t, DBpool.Model(&userA).Association("Scenarios").Append(&scenarioB).Error)
assert.NoError(t, DBpool.Model(&userB).Association("Scenarios").Append(&scenarioA).Error)
assert.NoError(t, DBpool.Model(&userB).Association("Scenarios").Append(&scenarioB).Error)
var usr1 User
assert.NoError(t, db.Find(&usr1, "ID = ?", 2).Error, fmt.Sprintf("Find User with ID=2"))
assert.NoError(t, DBpool.Find(&usr1, "ID = ?", 2).Error, fmt.Sprintf("Find User with ID=2"))
assert.EqualValues(t, "User_A", usr1.Username)
// Get scenarios of usr1
var scenarios []Scenario
assert.NoError(t, db.Model(&usr1).Related(&scenarios, "Scenarios").Error)
assert.NoError(t, DBpool.Model(&usr1).Related(&scenarios, "Scenarios").Error)
if len(scenarios) != 2 {
assert.Fail(t, "User Associations",
"Expected to have %v Scenarios. Has %v.", 2, len(scenarios))
......@@ -98,8 +95,8 @@ func TestUserAssociations(t *testing.T) {
func TestScenarioAssociations(t *testing.T) {
DropTables(db)
MigrateModels(db)
DropTables()
MigrateModels()
// create copies of global test data
scenarioA := ScenarioA
......@@ -113,44 +110,44 @@ func TestScenarioAssociations(t *testing.T) {
dashboardB := DashboardB
// add scenarios to DB
assert.NoError(t, db.Create(&scenarioA).Error)
assert.NoError(t, db.Create(&scenarioB).Error)
assert.NoError(t, DBpool.Create(&scenarioA).Error)
assert.NoError(t, DBpool.Create(&scenarioB).Error)
// add users to DB
assert.NoError(t, db.Create(&user0).Error) // Admin
assert.NoError(t, db.Create(&userA).Error) // Normal User
assert.NoError(t, db.Create(&userB).Error) // Normal User
assert.NoError(t, DBpool.Create(&user0).Error) // Admin
assert.NoError(t, DBpool.Create(&userA).Error) // Normal User
assert.NoError(t, DBpool.Create(&userB).Error) // Normal User
// add component configurations to DB
assert.NoError(t, db.Create(&configA).Error)
assert.NoError(t, db.Create(&configB).Error)
assert.NoError(t, DBpool.Create(&configA).Error)
assert.NoError(t, DBpool.Create(&configB).Error)
// add dashboards to DB
assert.NoError(t, db.Create(&dashboardA).Error)
assert.NoError(t, db.Create(&dashboardB).Error)
assert.NoError(t, DBpool.Create(&dashboardA).Error)
assert.NoError(t, DBpool.Create(&dashboardB).Error)
// add many-to-many associations between users and scenarios
// User HM Scenarios, Scenario HM Users (Many-to-Many)
assert.NoError(t, db.Model(&scenarioA).Association("Users").Append(&userA).Error)
assert.NoError(t, db.Model(&scenarioA).Association("Users").Append(&userB).Error)
assert.NoError(t, db.Model(&scenarioB).Association("Users").Append(&userA).Error)
assert.NoError(t, db.Model(&scenarioB).Association("Users").Append(&userB).Error)
assert.NoError(t, DBpool.Model(&scenarioA).Association("Users").Append(&userA).Error)
assert.NoError(t, DBpool.Model(&scenarioA).Association("Users").Append(&userB).Error)
assert.NoError(t, DBpool.Model(&scenarioB).Association("Users").Append(&userA).Error)
assert.NoError(t, DBpool.Model(&scenarioB).Association("Users").Append(&userB).Error)
// add scenario has many component configurations associations
assert.NoError(t, db.Model(&scenarioA).Association("ComponentConfigurations").Append(&configA).Error)
assert.NoError(t, db.Model(&scenarioA).Association("ComponentConfigurations").Append(&configB).Error)
assert.NoError(t, DBpool.Model(&scenarioA).Association("ComponentConfigurations").Append(&configA).Error)
assert.NoError(t, DBpool.Model(&scenarioA).Association("ComponentConfigurations").Append(&configB).Error)
// Scenario HM Dashboards
assert.NoError(t, db.Model(&scenarioA).Association("Dashboards").Append(&dashboardA).Error)
assert.NoError(t, db.Model(&scenarioA).Association("Dashboards").Append(&dashboardB).Error)
assert.NoError(t, DBpool.Model(&scenarioA).Association("Dashboards").Append(&dashboardA).Error)
assert.NoError(t, DBpool.Model(&scenarioA).Association("Dashboards").Append(&dashboardB).Error)
var scenario1 Scenario
assert.NoError(t, db.Find(&scenario1, 1).Error, fmt.Sprintf("Find Scenario with ID=1"))
assert.NoError(t, DBpool.Find(&scenario1, 1).Error, fmt.Sprintf("Find Scenario with ID=1"))
assert.EqualValues(t, "Scenario_A", scenario1.Name)
// Get users of scenario1
var users []User
assert.NoError(t, db.Model(&scenario1).Association("Users").Find(&users).Error)
assert.NoError(t, DBpool.Model(&scenario1).Association("Users").Find(&users).Error)
if len(users) != 2 {
assert.Fail(t, "Scenario Associations",
"Expected to have %v Users. Has %v.", 2, len(users))
......@@ -158,7 +155,7 @@ func TestScenarioAssociations(t *testing.T) {
// Get component configurations of scenario1
var configs []ComponentConfiguration
assert.NoError(t, db.Model(&scenario1).Related(&configs, "ComponentConfigurations").Error)
assert.NoError(t, DBpool.Model(&scenario1).Related(&configs, "ComponentConfigurations").Error)
if len(configs) != 2 {
assert.Fail(t, "Scenario Associations",
"Expected to have %v component configs. Has %v.", 2, len(configs))
......@@ -166,7 +163,7 @@ func TestScenarioAssociations(t *testing.T) {
// Get dashboards of scenario1
var dashboards []Dashboard
assert.NoError(t, db.Model(&scenario1).Related(&dashboards, "Dashboards").Error)
assert.NoError(t, DBpool.Model(&scenario1).Related(&dashboards, "Dashboards").Error)
if len(dashboards) != 2 {
assert.Fail(t, "Scenario Associations",
"Expected to have %v Dashboards. Has %v.", 2, len(dashboards))
......@@ -175,8 +172,8 @@ func TestScenarioAssociations(t *testing.T) {
func TestICAssociations(t *testing.T) {
DropTables(db)
MigrateModels(db)
DropTables()
MigrateModels()
// create copies of global test data
icA := ICA
......@@ -185,24 +182,24 @@ func TestICAssociations(t *testing.T) {
configB := ConfigB
// add ICs to DB
assert.NoError(t, db.Create(&icA).Error)
assert.NoError(t, db.Create(&icB).Error)
assert.NoError(t, DBpool.Create(&icA).Error)
assert.NoError(t, DBpool.Create(&icB).Error)
// add component configurations to DB
assert.NoError(t, db.Create(&configA).Error)
assert.NoError(t, db.Create(&configB).Error)
assert.NoError(t, DBpool.Create(&configA).Error)
assert.NoError(t, DBpool.Create(&configB).Error)
// add IC has many component configurations association to DB
assert.NoError(t, db.Model(&icA).Association("ComponentConfigurations").Append(&configA).Error)
assert.NoError(t, db.Model(&icA).Association("ComponentConfigurations").Append(&configB).Error)
assert.NoError(t, DBpool.Model(&icA).Association("ComponentConfigurations").Append(&configA).Error)
assert.NoError(t, DBpool.Model(&icA).Association("ComponentConfigurations").Append(&configB).Error)
var ic1 InfrastructureComponent
assert.NoError(t, db.Find(&ic1, 1).Error, fmt.Sprintf("Find InfrastructureComponent with ID=1"))
assert.NoError(t, DBpool.Find(&ic1, 1).Error, fmt.Sprintf("Find InfrastructureComponent with ID=1"))
assert.EqualValues(t, "Host_A", ic1.Host)
// Get Component Configurations of ic1
var configs []ComponentConfiguration
assert.NoError(t, db.Model(&ic1).Association("ComponentConfigurations").Find(&configs).Error)
assert.NoError(t, DBpool.Model(&ic1).Association("ComponentConfigurations").Find(&configs).Error)
if len(configs) != 2 {
assert.Fail(t, "InfrastructureComponent Associations",
"Expected to have %v Component Configurations. Has %v.", 2, len(configs))
......@@ -211,8 +208,8 @@ func TestICAssociations(t *testing.T) {
func TestComponentConfigurationAssociations(t *testing.T) {
DropTables(db)
MigrateModels(db)
DropTables()
MigrateModels()
// create copies of global test data
configA := ConfigA
......@@ -229,41 +226,41 @@ func TestComponentConfigurationAssociations(t *testing.T) {
icB := ICB
// add Component Configurations to DB
assert.NoError(t, db.Create(&configA).Error)
assert.NoError(t, db.Create(&configB).Error)
assert.NoError(t, DBpool.Create(&configA).Error)
assert.NoError(t, DBpool.Create(&configB).Error)
// add signals to DB
assert.NoError(t, db.Create(&outSignalA).Error)
assert.NoError(t, db.Create(&outSignalB).Error)
assert.NoError(t, db.Create(&inSignalA).Error)
assert.NoError(t, db.Create(&inSignalB).Error)
assert.NoError(t, DBpool.Create(&outSignalA).Error)
assert.NoError(t, DBpool.Create(&outSignalB).Error)
assert.NoError(t, DBpool.Create(&inSignalA).Error)
assert.NoError(t, DBpool.Create(&inSignalB).Error)
// add files to DB
assert.NoError(t, db.Create(&fileA).Error)
assert.NoError(t, db.Create(&fileB).Error)
assert.NoError(t, db.Create(&fileC).Error)
assert.NoError(t, db.Create(&fileD).Error)
assert.NoError(t, DBpool.Create(&fileA).Error)
assert.NoError(t, DBpool.Create(&fileB).Error)
assert.NoError(t, DBpool.Create(&fileC).Error)
assert.NoError(t, DBpool.Create(&fileD).Error)
// add ICs to DB
assert.NoError(t, db.Create(&icA).Error)
assert.NoError(t, db.Create(&icB).Error)
assert.NoError(t, DBpool.Create(&icA).Error)
assert.NoError(t, DBpool.Create(&icB).Error)
// add Component Configuration has many signals associations
assert.NoError(t, db.Model(&configA).Association("InputMapping").Append(&inSignalA).Error)
assert.NoError(t, db.Model(&configA).Association("InputMapping").Append(&inSignalB).Error)
assert.NoError(t, db.Model(&configA).Association("OutputMapping").Append(&outSignalA).Error)
assert.NoError(t, db.Model(&configA).Association("OutputMapping").Append(&outSignalB).Error)
assert.NoError(t, DBpool.Model(&configA).Association("InputMapping").Append(&inSignalA).Error)
assert.NoError(t, DBpool.Model(&configA).Association("InputMapping").Append(&inSignalB).Error)
assert.NoError(t, DBpool.Model(&configA).Association("OutputMapping").Append(&outSignalA).Error)
assert.NoError(t, DBpool.Model(&configA).Association("OutputMapping").Append(&outSignalB).Error)
// add Component Configuration has many files associations
assert.NoError(t, db.Model(&configA).Association("Files").Append(&fileC).Error)
assert.NoError(t, db.Model(&configA).Association("Files").Append(&fileD).Error)
assert.NoError(t, DBpool.Model(&configA).Association("Files").Append(&fileC).Error)
assert.NoError(t, DBpool.Model(&configA).Association("Files").Append(&fileD).Error)
// associate Component Configurations with IC
assert.NoError(t, db.Model(&icA).Association("ComponentConfigurations").Append(&configA).Error)
assert.NoError(t, db.Model(&icA).Association("ComponentConfigurations").Append(&configB).Error)
assert.NoError(t, DBpool.Model(&icA).Association("ComponentConfigurations").Append(&configA).Error)
assert.NoError(t, DBpool.Model(&icA).Association("ComponentConfigurations").Append(&configB).Error)
var config1 ComponentConfiguration
assert.NoError(t, db.Find(&config1, 1).Error, fmt.Sprintf("Find ComponentConfiguration with ID=1"))
assert.NoError(t, DBpool.Find(&config1, 1).Error, fmt.Sprintf("Find ComponentConfiguration with ID=1"))
assert.EqualValues(t, ConfigA.Name, config1.Name)
// Check IC ID
......@@ -273,7 +270,7 @@ func TestComponentConfigurationAssociations(t *testing.T) {
// Get OutputMapping signals of config1
var signals []Signal
assert.NoError(t, db.Model(&config1).Where("Direction = ?", "out").Related(&signals, "OutputMapping").Error)
assert.NoError(t, DBpool.Model(&config1).Where("Direction = ?", "out").Related(&signals, "OutputMapping").Error)
if len(signals) != 2 {
assert.Fail(t, "ComponentConfiguration Associations",
"Expected to have %v Output Signals. Has %v.", 2, len(signals))
......@@ -281,7 +278,7 @@ func TestComponentConfigurationAssociations(t *testing.T) {
// Get files of config1
var files []File
assert.NoError(t, db.Model(&config1).Related(&files, "Files").Error)
assert.NoError(t, DBpool.Model(&config1).Related(&files, "Files").Error)
if len(files) != 2 {
assert.Fail(t, "ComponentConfiguration Associations",
"Expected to have %v Files. Has %v.", 2, len(files))
......@@ -290,8 +287,8 @@ func TestComponentConfigurationAssociations(t *testing.T) {
func TestDashboardAssociations(t *testing.T) {
DropTables(db)
MigrateModels(db)
DropTables()
MigrateModels()
// create copies of global test data
dashboardA := DashboardA
......@@ -300,24 +297,24 @@ func TestDashboardAssociations(t *testing.T) {
widgetB := WidgetB
// add dashboards to DB
assert.NoError(t, db.Create(&dashboardA).Error)
assert.NoError(t, db.Create(&dashboardB).Error)
assert.NoError(t, DBpool.Create(&dashboardA).Error)
assert.NoError(t, DBpool.Create(&dashboardB).Error)
// add widgets to DB
assert.NoError(t, db.Create(&widgetA).Error)
assert.NoError(t, db.Create(&widgetB).Error)
assert.NoError(t, DBpool.Create(&widgetA).Error)
assert.NoError(t, DBpool.Create(&widgetB).Error)
// add dashboard has many widgets associations to DB
assert.NoError(t, db.Model(&dashboardA).Association("Widgets").Append(&widgetA).Error)
assert.NoError(t, db.Model(&dashboardA).Association("Widgets").Append(&widgetB).Error)
assert.NoError(t, DBpool.Model(&dashboardA).Association("Widgets").Append(&widgetA).Error)
assert.NoError(t, DBpool.Model(&dashboardA).Association("Widgets").Append(&widgetB).Error)
var dashboard1 Dashboard
assert.NoError(t, db.Find(&dashboard1, 1).Error, fmt.Sprintf("Find Dashboard with ID=1"))
assert.NoError(t, DBpool.Find(&dashboard1, 1).Error, fmt.Sprintf("Find Dashboard with ID=1"))
assert.EqualValues(t, "Dashboard_A", dashboard1.Name)
//Get widgets of dashboard1
var widgets []Widget
assert.NoError(t, db.Model(&dashboard1).Related(&widgets, "Widgets").Error)
assert.NoError(t, DBpool.Model(&dashboard1).Related(&widgets, "Widgets").Error)
if len(widgets) != 2 {
assert.Fail(t, "Dashboard Associations",
"Expected to have %v Widget. Has %v.", 2, len(widgets))
......@@ -326,8 +323,8 @@ func TestDashboardAssociations(t *testing.T) {
func TestWidgetAssociations(t *testing.T) {
DropTables(db)
MigrateModels(db)
DropTables()
MigrateModels()
// create copies of global test data
widgetA := WidgetA
......@@ -338,26 +335,26 @@ func TestWidgetAssociations(t *testing.T) {
fileD := FileD
// add widgets to DB
assert.NoError(t, db.Create(&widgetA).Error)
assert.NoError(t, db.Create(&widgetB).Error)
assert.NoError(t, DBpool.Create(&widgetA).Error)
assert.NoError(t, DBpool.Create(&widgetB).Error)
// add files to DB
assert.NoError(t, db.Create(&fileA).Error)
assert.NoError(t, db.Create(&fileB).Error)
assert.NoError(t, db.Create(&fileC).Error)
assert.NoError(t, db.Create(&fileD).Error)
assert.NoError(t, DBpool.Create(&fileA).Error)
assert.NoError(t, DBpool.Create(&fileB).Error)
assert.NoError(t, DBpool.Create(&fileC).Error)
assert.NoError(t, DBpool.Create(&fileD).Error)
// add widget has many files associations to DB
assert.NoError(t, db.Model(&widgetA).Association("Files").Append(&fileA).Error)
assert.NoError(t, db.Model(&widgetA).Association("Files").Append(&fileB).Error)
assert.NoError(t, DBpool.Model(&widgetA).Association("Files").Append(&fileA).Error)
assert.NoError(t, DBpool.Model(&widgetA).Association("Files").Append(&fileB).Error)
var widget1 Widget
assert.NoError(t, db.Find(&widget1, 1).Error, fmt.Sprintf("Find Widget with ID=1"))
assert.NoError(t, DBpool.Find(&widget1, 1).Error, fmt.Sprintf("Find Widget with ID=1"))
assert.EqualValues(t, widgetA.Name, widget1.Name)
// Get files of widget
var files []File
assert.NoError(t, db.Model(&widget1).Related(&files, "Files").Error)
assert.NoError(t, DBpool.Model(&widget1).Related(&files, "Files").Error)
if len(files) != 2 {
assert.Fail(t, "Widget Associations",
"Expected to have %v Files. Has %v.", 2, len(files))
......@@ -366,8 +363,8 @@ func TestWidgetAssociations(t *testing.T) {
func TestFileAssociations(t *testing.T) {
DropTables(db)
MigrateModels(db)
DropTables()
MigrateModels()
// create copies of global test data
fileA := FileA
......@@ -376,26 +373,26 @@ func TestFileAssociations(t *testing.T) {
fileD := FileD
// add files to DB
assert.NoError(t, db.Create(&fileA).Error)
assert.NoError(t, db.Create(&fileB).Error)
assert.NoError(t, db.Create(&fileC).Error)
assert.NoError(t, db.Create(&fileD).Error)
assert.NoError(t, DBpool.Create(&fileA).Error)
assert.NoError(t, DBpool.Create(&fileB).Error)
assert.NoError(t, DBpool.Create(&fileC).Error)
assert.NoError(t, DBpool.Create(&fileD).Error)
var file1 File
assert.NoError(t, db.Find(&file1, 1).Error, fmt.Sprintf("Find File with ID=1"))
assert.NoError(t, DBpool.Find(&file1, 1).Error, fmt.Sprintf("Find File with ID=1"))
assert.EqualValues(t, "File_A", file1.Name)
}
func TestAddAdmin(t *testing.T) {
DropTables(db)
MigrateModels(db)
DropTables()
MigrateModels()
assert.NoError(t, DBAddAdminUser(db))
assert.NoError(t, DBAddAdminUser())
}
func TestAddAdminAndUsers(t *testing.T) {
DropTables(db)
MigrateModels(db)
DropTables()
MigrateModels()
assert.NoError(t, DBAddAdminAndUserAndGuest(db))
assert.NoError(t, DBAddAdminAndUserAndGuest())
}
......@@ -27,7 +27,6 @@ import (
"fmt"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/helper"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"github.com/jinzhu/gorm/dialects/postgres"
"golang.org/x/crypto/bcrypt"
"io"
......@@ -306,26 +305,26 @@ var WidgetE = Widget{
SignalIDs: []int64{4},
}
func DBAddAdminUser(db *gorm.DB) error {
db.AutoMigrate(&User{})
func DBAddAdminUser() error {
DBpool.AutoMigrate(&User{})
// Check if admin user exists in DB
var users []User
err := db.Where("Role = ?", "Admin").Find(&users).Error
err := DBpool.Where("Role = ?", "Admin").Find(&users).Error
if len(users) == 0 {
fmt.Println("No admin user found in DB, adding default admin user.")
//create a copy of global test data