2022-04-05 16:38:25 +02:00
|
|
|
/**
|
|
|
|
* This file is part of VILLASweb-backend-go
|
2019-11-20 11:08:33 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************************/
|
2022-04-05 16:38:25 +02:00
|
|
|
|
2019-09-10 16:28:57 +02:00
|
|
|
package database
|
2019-04-14 21:04:23 +02:00
|
|
|
|
|
|
|
import (
|
2019-09-03 14:16:01 +02:00
|
|
|
"os"
|
2019-04-14 21:04:23 +02:00
|
|
|
"testing"
|
2019-05-23 14:36:42 +02:00
|
|
|
|
2021-01-25 10:57:25 +01:00
|
|
|
"github.com/zpatrick/go-config"
|
|
|
|
|
2019-11-14 10:38:51 +01:00
|
|
|
"git.rwth-aachen.de/acs/public/villas/web-backend-go/configuration"
|
2019-05-23 14:36:42 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-04-14 21:04:23 +02:00
|
|
|
)
|
|
|
|
|
2019-09-03 14:16:01 +02:00
|
|
|
func TestMain(m *testing.M) {
|
2019-04-21 11:56:04 +02:00
|
|
|
|
2020-03-19 10:50:18 +01:00
|
|
|
defer DBpool.Close()
|
2019-09-03 17:19:44 +02:00
|
|
|
os.Exit(m.Run())
|
2019-09-03 14:16:01 +02:00
|
|
|
}
|
2019-04-21 11:56:04 +02:00
|
|
|
|
2020-03-27 11:53:26 +01:00
|
|
|
func TestInitDB(t *testing.T) {
|
|
|
|
|
|
|
|
err := configuration.InitConfig()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
static := map[string]string{}
|
|
|
|
mappings := map[string]string{}
|
|
|
|
|
|
|
|
defaults := config.NewStatic(static)
|
|
|
|
env := config.NewEnvironment(mappings)
|
|
|
|
|
2021-10-19 14:45:45 +02:00
|
|
|
ownConfig := config.NewConfig([]config.Provider{defaults, env})
|
2020-03-27 11:53:26 +01:00
|
|
|
|
2021-10-19 14:45:45 +02:00
|
|
|
err = InitDB(ownConfig, true)
|
2020-03-27 11:53:26 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
2022-04-06 11:09:54 +02:00
|
|
|
dbOptions := []string{"db.name", "db.host", "db.user", "db.pass", "db.ssl"}
|
|
|
|
//dbOptions := []string{"db.name", "db.host", "db.ssl"} // use this line for local testing only
|
2021-10-19 14:45:45 +02:00
|
|
|
for _, opt := range dbOptions {
|
|
|
|
val, err := configuration.GlobalConfig.String(opt)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
static[opt] = val
|
2022-04-06 11:09:54 +02:00
|
|
|
ownConfig = config.NewConfig([]config.Provider{defaults, env})
|
2021-10-19 14:45:45 +02:00
|
|
|
err = InitDB(ownConfig, true)
|
|
|
|
|
|
|
|
if opt == "db.ssl" {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
}
|
2020-03-27 11:53:26 +01:00
|
|
|
|
|
|
|
// Verify that you can connect to the database
|
|
|
|
db := GetDB()
|
|
|
|
err = db.DB().Ping()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2019-09-03 14:16:01 +02:00
|
|
|
func TestUserAssociations(t *testing.T) {
|
2019-09-03 15:12:18 +02:00
|
|
|
|
2020-03-19 10:50:18 +01:00
|
|
|
DropTables()
|
|
|
|
MigrateModels()
|
2019-09-03 15:12:18 +02:00
|
|
|
|
|
|
|
// create copies of global test data
|
2020-03-27 11:53:26 +01:00
|
|
|
scenarioA := Scenario{}
|
|
|
|
scenarioB := Scenario{}
|
|
|
|
userA := User{Username: "abcdef"}
|
|
|
|
userB := User{Username: "ghijkl"}
|
|
|
|
|
|
|
|
// add users to DB
|
2020-03-19 10:50:18 +01:00
|
|
|
assert.NoError(t, DBpool.Create(&userA).Error) // Normal User
|
|
|
|
assert.NoError(t, DBpool.Create(&userB).Error) // Normal User
|
2019-09-03 15:12:18 +02:00
|
|
|
|
|
|
|
// add two scenarios to DB
|
2020-03-19 10:50:18 +01:00
|
|
|
assert.NoError(t, DBpool.Create(&scenarioA).Error)
|
|
|
|
assert.NoError(t, DBpool.Create(&scenarioB).Error)
|
2019-09-03 15:12:18 +02:00
|
|
|
|
|
|
|
// add many-to-many associations between users and scenarios
|
|
|
|
// User HM Scenarios, Scenario HM Users (Many-to-Many)
|
2020-03-19 10:50:18 +01:00
|
|
|
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)
|
2019-09-03 15:12:18 +02:00
|
|
|
|
2019-09-03 14:16:01 +02:00
|
|
|
var usr1 User
|
2021-10-19 13:28:41 +02:00
|
|
|
assert.NoError(t, DBpool.Find(&usr1, "ID = ?", 1).Error, "Find User with ID=1")
|
2019-09-03 14:16:01 +02:00
|
|
|
|
|
|
|
// Get scenarios of usr1
|
|
|
|
var scenarios []Scenario
|
2020-03-19 10:50:18 +01:00
|
|
|
assert.NoError(t, DBpool.Model(&usr1).Related(&scenarios, "Scenarios").Error)
|
2019-09-03 14:16:01 +02:00
|
|
|
if len(scenarios) != 2 {
|
|
|
|
assert.Fail(t, "User Associations",
|
|
|
|
"Expected to have %v Scenarios. Has %v.", 2, len(scenarios))
|
2019-05-15 13:04:12 +02:00
|
|
|
}
|
2019-09-03 14:16:01 +02:00
|
|
|
}
|
2019-05-15 13:04:12 +02:00
|
|
|
|
2019-09-03 14:16:01 +02:00
|
|
|
func TestScenarioAssociations(t *testing.T) {
|
2019-09-03 15:12:18 +02:00
|
|
|
|
2020-03-19 10:50:18 +01:00
|
|
|
DropTables()
|
|
|
|
MigrateModels()
|
2019-09-03 15:12:18 +02:00
|
|
|
|
|
|
|
// create copies of global test data
|
2020-03-27 11:53:26 +01:00
|
|
|
scenarioA := Scenario{}
|
|
|
|
scenarioB := Scenario{}
|
|
|
|
userA := User{Username: "abcdef"}
|
|
|
|
userB := User{Username: "ghijkl"}
|
|
|
|
configA := ComponentConfiguration{}
|
|
|
|
configB := ComponentConfiguration{}
|
|
|
|
dashboardA := Dashboard{}
|
|
|
|
dashboardB := Dashboard{}
|
2020-05-26 11:27:00 +02:00
|
|
|
fileA := File{}
|
|
|
|
fileB := File{}
|
2020-11-19 17:23:24 +01:00
|
|
|
resultA := Result{}
|
|
|
|
resultB := Result{}
|
2019-09-03 15:12:18 +02:00
|
|
|
|
|
|
|
// add scenarios to DB
|
2020-03-19 10:50:18 +01:00
|
|
|
assert.NoError(t, DBpool.Create(&scenarioA).Error)
|
|
|
|
assert.NoError(t, DBpool.Create(&scenarioB).Error)
|
2019-09-03 15:12:18 +02:00
|
|
|
|
|
|
|
// add users to DB
|
2020-03-19 10:50:18 +01:00
|
|
|
assert.NoError(t, DBpool.Create(&userA).Error) // Normal User
|
|
|
|
assert.NoError(t, DBpool.Create(&userB).Error) // Normal User
|
2019-09-03 15:12:18 +02:00
|
|
|
|
2020-03-06 15:14:29 +01:00
|
|
|
// add component configurations to DB
|
2020-03-19 10:50:18 +01:00
|
|
|
assert.NoError(t, DBpool.Create(&configA).Error)
|
|
|
|
assert.NoError(t, DBpool.Create(&configB).Error)
|
2019-09-03 15:12:18 +02:00
|
|
|
|
|
|
|
// add dashboards to DB
|
2020-03-19 10:50:18 +01:00
|
|
|
assert.NoError(t, DBpool.Create(&dashboardA).Error)
|
|
|
|
assert.NoError(t, DBpool.Create(&dashboardB).Error)
|
2019-09-03 15:12:18 +02:00
|
|
|
|
2020-05-26 11:27:00 +02:00
|
|
|
// add files to DB
|
|
|
|
assert.NoError(t, DBpool.Create(&fileA).Error)
|
|
|
|
assert.NoError(t, DBpool.Create(&fileB).Error)
|
|
|
|
|
2020-11-19 17:23:24 +01:00
|
|
|
// add results to DB
|
|
|
|
assert.NoError(t, DBpool.Create(&resultA).Error)
|
|
|
|
assert.NoError(t, DBpool.Create(&resultB).Error)
|
|
|
|
|
2019-09-03 15:12:18 +02:00
|
|
|
// add many-to-many associations between users and scenarios
|
|
|
|
// User HM Scenarios, Scenario HM Users (Many-to-Many)
|
2020-03-19 10:50:18 +01:00
|
|
|
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)
|
2019-09-03 15:12:18 +02:00
|
|
|
|
2020-05-26 11:27:00 +02:00
|
|
|
// add Component Configuration has many files associations
|
|
|
|
assert.NoError(t, DBpool.Model(&scenarioA).Association("Files").Append(&fileA).Error)
|
|
|
|
assert.NoError(t, DBpool.Model(&scenarioA).Association("Files").Append(&fileB).Error)
|
|
|
|
|
2020-03-06 15:14:29 +01:00
|
|
|
// add scenario has many component configurations associations
|
2020-03-19 10:50:18 +01:00
|
|
|
assert.NoError(t, DBpool.Model(&scenarioA).Association("ComponentConfigurations").Append(&configA).Error)
|
|
|
|
assert.NoError(t, DBpool.Model(&scenarioA).Association("ComponentConfigurations").Append(&configB).Error)
|
2019-09-03 15:12:18 +02:00
|
|
|
|
|
|
|
// Scenario HM Dashboards
|
2020-03-19 10:50:18 +01:00
|
|
|
assert.NoError(t, DBpool.Model(&scenarioA).Association("Dashboards").Append(&dashboardA).Error)
|
|
|
|
assert.NoError(t, DBpool.Model(&scenarioA).Association("Dashboards").Append(&dashboardB).Error)
|
2019-09-03 15:12:18 +02:00
|
|
|
|
2020-11-19 17:23:24 +01:00
|
|
|
// Scenario HM Results
|
|
|
|
assert.NoError(t, DBpool.Model(&scenarioA).Association("Results").Append(&resultA).Error)
|
|
|
|
assert.NoError(t, DBpool.Model(&scenarioA).Association("Results").Append(&resultB).Error)
|
|
|
|
|
2019-09-03 14:16:01 +02:00
|
|
|
var scenario1 Scenario
|
2021-10-19 13:28:41 +02:00
|
|
|
assert.NoError(t, DBpool.Find(&scenario1, 1).Error, "Find Scenario with ID=1")
|
2019-09-03 14:16:01 +02:00
|
|
|
|
|
|
|
// Get users of scenario1
|
|
|
|
var users []User
|
2020-03-19 10:50:18 +01:00
|
|
|
assert.NoError(t, DBpool.Model(&scenario1).Association("Users").Find(&users).Error)
|
2019-09-03 14:16:01 +02:00
|
|
|
if len(users) != 2 {
|
|
|
|
assert.Fail(t, "Scenario Associations",
|
|
|
|
"Expected to have %v Users. Has %v.", 2, len(users))
|
2019-05-21 12:49:39 +02:00
|
|
|
}
|
2019-04-21 11:56:04 +02:00
|
|
|
|
2020-03-06 15:14:29 +01:00
|
|
|
// Get component configurations of scenario1
|
|
|
|
var configs []ComponentConfiguration
|
2020-03-19 10:50:18 +01:00
|
|
|
assert.NoError(t, DBpool.Model(&scenario1).Related(&configs, "ComponentConfigurations").Error)
|
2020-03-06 15:14:29 +01:00
|
|
|
if len(configs) != 2 {
|
2019-09-03 14:16:01 +02:00
|
|
|
assert.Fail(t, "Scenario Associations",
|
2020-03-06 15:14:29 +01:00
|
|
|
"Expected to have %v component configs. Has %v.", 2, len(configs))
|
2019-04-21 11:56:04 +02:00
|
|
|
}
|
|
|
|
|
2019-09-03 14:16:01 +02:00
|
|
|
// Get dashboards of scenario1
|
|
|
|
var dashboards []Dashboard
|
2020-03-19 10:50:18 +01:00
|
|
|
assert.NoError(t, DBpool.Model(&scenario1).Related(&dashboards, "Dashboards").Error)
|
2019-09-03 14:16:01 +02:00
|
|
|
if len(dashboards) != 2 {
|
|
|
|
assert.Fail(t, "Scenario Associations",
|
|
|
|
"Expected to have %v Dashboards. Has %v.", 2, len(dashboards))
|
2019-04-21 11:56:04 +02:00
|
|
|
}
|
2020-05-26 11:27:00 +02:00
|
|
|
|
|
|
|
// Get files of scenario1
|
|
|
|
var files []File
|
|
|
|
assert.NoError(t, DBpool.Model(&scenario1).Related(&files, "Files").Error)
|
|
|
|
if len(files) != 2 {
|
|
|
|
assert.Fail(t, "Scenario Associations",
|
|
|
|
"Expected to have %v Files. Has %v.", 2, len(files))
|
|
|
|
}
|
2020-11-19 17:23:24 +01:00
|
|
|
|
|
|
|
// Get results of scenario1
|
|
|
|
var results []File
|
|
|
|
assert.NoError(t, DBpool.Model(&scenario1).Related(&results, "Results").Error)
|
|
|
|
if len(files) != 2 {
|
|
|
|
assert.Fail(t, "Scenario Associations",
|
|
|
|
"Expected to have %v Results. Has %v.", 2, len(results))
|
|
|
|
}
|
2019-09-03 14:16:01 +02:00
|
|
|
}
|
2019-04-21 11:56:04 +02:00
|
|
|
|
2020-03-04 16:34:24 +01:00
|
|
|
func TestICAssociations(t *testing.T) {
|
2019-09-03 15:12:18 +02:00
|
|
|
|
2020-03-19 10:50:18 +01:00
|
|
|
DropTables()
|
|
|
|
MigrateModels()
|
2019-09-03 15:12:18 +02:00
|
|
|
|
|
|
|
// create copies of global test data
|
2020-03-27 11:53:26 +01:00
|
|
|
icA := InfrastructureComponent{}
|
|
|
|
icB := InfrastructureComponent{}
|
|
|
|
configA := ComponentConfiguration{}
|
|
|
|
configB := ComponentConfiguration{}
|
2019-09-03 15:12:18 +02:00
|
|
|
|
2020-03-04 16:34:24 +01:00
|
|
|
// add ICs to DB
|
2020-03-19 10:50:18 +01:00
|
|
|
assert.NoError(t, DBpool.Create(&icA).Error)
|
|
|
|
assert.NoError(t, DBpool.Create(&icB).Error)
|
2019-09-03 15:12:18 +02:00
|
|
|
|
2020-03-06 15:14:29 +01:00
|
|
|
// add component configurations to DB
|
2020-03-19 10:50:18 +01:00
|
|
|
assert.NoError(t, DBpool.Create(&configA).Error)
|
|
|
|
assert.NoError(t, DBpool.Create(&configB).Error)
|
2019-09-03 15:12:18 +02:00
|
|
|
|
2020-03-06 15:14:29 +01:00
|
|
|
// add IC has many component configurations association to DB
|
2020-03-19 10:50:18 +01:00
|
|
|
assert.NoError(t, DBpool.Model(&icA).Association("ComponentConfigurations").Append(&configA).Error)
|
|
|
|
assert.NoError(t, DBpool.Model(&icA).Association("ComponentConfigurations").Append(&configB).Error)
|
2019-09-03 15:12:18 +02:00
|
|
|
|
2020-03-04 16:34:24 +01:00
|
|
|
var ic1 InfrastructureComponent
|
2021-10-19 13:28:41 +02:00
|
|
|
assert.NoError(t, DBpool.Find(&ic1, 1).Error, "Find InfrastructureComponent with ID=1")
|
2019-09-03 14:16:01 +02:00
|
|
|
|
2020-03-06 15:14:29 +01:00
|
|
|
// Get Component Configurations of ic1
|
|
|
|
var configs []ComponentConfiguration
|
2020-03-19 10:50:18 +01:00
|
|
|
assert.NoError(t, DBpool.Model(&ic1).Association("ComponentConfigurations").Find(&configs).Error)
|
2020-03-06 15:14:29 +01:00
|
|
|
if len(configs) != 2 {
|
2020-03-04 16:34:24 +01:00
|
|
|
assert.Fail(t, "InfrastructureComponent Associations",
|
2020-03-06 15:14:29 +01:00
|
|
|
"Expected to have %v Component Configurations. Has %v.", 2, len(configs))
|
2019-06-18 14:15:43 +02:00
|
|
|
}
|
2019-09-03 14:16:01 +02:00
|
|
|
}
|
2019-06-18 14:15:43 +02:00
|
|
|
|
2020-03-06 15:14:29 +01:00
|
|
|
func TestComponentConfigurationAssociations(t *testing.T) {
|
2019-09-03 15:12:18 +02:00
|
|
|
|
2020-03-19 10:50:18 +01:00
|
|
|
DropTables()
|
|
|
|
MigrateModels()
|
2019-09-03 15:12:18 +02:00
|
|
|
|
|
|
|
// create copies of global test data
|
2020-03-27 11:53:26 +01:00
|
|
|
configA := ComponentConfiguration{}
|
|
|
|
configB := ComponentConfiguration{}
|
|
|
|
outSignalA := Signal{Direction: "out"}
|
|
|
|
outSignalB := Signal{Direction: "out"}
|
|
|
|
inSignalA := Signal{Direction: "in"}
|
|
|
|
inSignalB := Signal{Direction: "in"}
|
|
|
|
icA := InfrastructureComponent{}
|
|
|
|
icB := InfrastructureComponent{}
|
2019-09-03 15:12:18 +02:00
|
|
|
|
2020-03-06 15:14:29 +01:00
|
|
|
// add Component Configurations to DB
|
2020-03-19 10:50:18 +01:00
|
|
|
assert.NoError(t, DBpool.Create(&configA).Error)
|
|
|
|
assert.NoError(t, DBpool.Create(&configB).Error)
|
2019-09-03 15:12:18 +02:00
|
|
|
|
|
|
|
// add signals to DB
|
2020-03-19 10:50:18 +01:00
|
|
|
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)
|
2019-09-03 15:12:18 +02:00
|
|
|
|
2020-03-04 16:34:24 +01:00
|
|
|
// add ICs to DB
|
2020-03-19 10:50:18 +01:00
|
|
|
assert.NoError(t, DBpool.Create(&icA).Error)
|
|
|
|
assert.NoError(t, DBpool.Create(&icB).Error)
|
2019-09-03 15:12:18 +02:00
|
|
|
|
2020-03-06 15:14:29 +01:00
|
|
|
// add Component Configuration has many signals associations
|
2020-03-19 10:50:18 +01:00
|
|
|
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)
|
2019-09-03 15:12:18 +02:00
|
|
|
|
2020-03-06 15:14:29 +01:00
|
|
|
// associate Component Configurations with IC
|
2020-03-19 10:50:18 +01:00
|
|
|
assert.NoError(t, DBpool.Model(&icA).Association("ComponentConfigurations").Append(&configA).Error)
|
|
|
|
assert.NoError(t, DBpool.Model(&icA).Association("ComponentConfigurations").Append(&configB).Error)
|
2019-09-03 15:12:18 +02:00
|
|
|
|
2020-03-06 15:14:29 +01:00
|
|
|
var config1 ComponentConfiguration
|
2021-10-19 13:28:41 +02:00
|
|
|
assert.NoError(t, DBpool.Find(&config1, 1).Error, "Find ComponentConfiguration with ID=1")
|
2019-04-21 11:56:04 +02:00
|
|
|
|
2020-03-04 16:34:24 +01:00
|
|
|
// Check IC ID
|
2020-03-06 15:14:29 +01:00
|
|
|
if config1.ICID != 1 {
|
|
|
|
assert.Fail(t, "Component Configurations expected to have Infrastructure Component ID 1, but is %v", config1.ICID)
|
2019-06-06 16:36:12 +02:00
|
|
|
}
|
2019-04-21 11:56:04 +02:00
|
|
|
|
2020-03-06 15:14:29 +01:00
|
|
|
// Get OutputMapping signals of config1
|
2019-09-03 14:16:01 +02:00
|
|
|
var signals []Signal
|
2020-03-19 10:50:18 +01:00
|
|
|
assert.NoError(t, DBpool.Model(&config1).Where("Direction = ?", "out").Related(&signals, "OutputMapping").Error)
|
2019-09-03 14:16:01 +02:00
|
|
|
if len(signals) != 2 {
|
2020-03-06 15:14:29 +01:00
|
|
|
assert.Fail(t, "ComponentConfiguration Associations",
|
2019-09-03 14:16:01 +02:00
|
|
|
"Expected to have %v Output Signals. Has %v.", 2, len(signals))
|
2019-04-21 11:56:04 +02:00
|
|
|
}
|
|
|
|
|
2019-09-03 14:16:01 +02:00
|
|
|
}
|
2019-04-21 11:56:04 +02:00
|
|
|
|
2019-09-03 14:16:01 +02:00
|
|
|
func TestDashboardAssociations(t *testing.T) {
|
2019-09-03 15:12:18 +02:00
|
|
|
|
2020-03-19 10:50:18 +01:00
|
|
|
DropTables()
|
|
|
|
MigrateModels()
|
2019-09-03 15:12:18 +02:00
|
|
|
|
|
|
|
// create copies of global test data
|
2020-03-27 11:53:26 +01:00
|
|
|
dashboardA := Dashboard{}
|
|
|
|
dashboardB := Dashboard{}
|
|
|
|
widgetA := Widget{}
|
|
|
|
widgetB := Widget{}
|
2019-09-03 15:12:18 +02:00
|
|
|
|
|
|
|
// add dashboards to DB
|
2020-03-19 10:50:18 +01:00
|
|
|
assert.NoError(t, DBpool.Create(&dashboardA).Error)
|
|
|
|
assert.NoError(t, DBpool.Create(&dashboardB).Error)
|
2019-09-03 15:12:18 +02:00
|
|
|
|
|
|
|
// add widgets to DB
|
2020-03-19 10:50:18 +01:00
|
|
|
assert.NoError(t, DBpool.Create(&widgetA).Error)
|
|
|
|
assert.NoError(t, DBpool.Create(&widgetB).Error)
|
2019-09-03 15:12:18 +02:00
|
|
|
|
|
|
|
// add dashboard has many widgets associations to DB
|
2020-03-19 10:50:18 +01:00
|
|
|
assert.NoError(t, DBpool.Model(&dashboardA).Association("Widgets").Append(&widgetA).Error)
|
|
|
|
assert.NoError(t, DBpool.Model(&dashboardA).Association("Widgets").Append(&widgetB).Error)
|
2019-09-03 15:12:18 +02:00
|
|
|
|
2019-09-03 14:16:01 +02:00
|
|
|
var dashboard1 Dashboard
|
2021-10-19 13:28:41 +02:00
|
|
|
assert.NoError(t, DBpool.Find(&dashboard1, 1).Error, "Find Dashboard with ID=1")
|
2019-09-03 14:16:01 +02:00
|
|
|
|
|
|
|
//Get widgets of dashboard1
|
|
|
|
var widgets []Widget
|
2020-03-19 10:50:18 +01:00
|
|
|
assert.NoError(t, DBpool.Model(&dashboard1).Related(&widgets, "Widgets").Error)
|
2019-09-03 14:16:01 +02:00
|
|
|
if len(widgets) != 2 {
|
|
|
|
assert.Fail(t, "Dashboard Associations",
|
|
|
|
"Expected to have %v Widget. Has %v.", 2, len(widgets))
|
2019-04-21 11:56:04 +02:00
|
|
|
}
|
2019-09-03 14:16:01 +02:00
|
|
|
}
|
2019-04-21 11:56:04 +02:00
|
|
|
|
2019-09-03 14:16:01 +02:00
|
|
|
func TestWidgetAssociations(t *testing.T) {
|
2019-09-03 15:12:18 +02:00
|
|
|
|
2020-03-19 10:50:18 +01:00
|
|
|
DropTables()
|
|
|
|
MigrateModels()
|
2019-09-03 15:12:18 +02:00
|
|
|
|
|
|
|
// create copies of global test data
|
2020-03-27 11:53:26 +01:00
|
|
|
widgetA := Widget{}
|
|
|
|
widgetB := Widget{}
|
2019-09-03 15:12:18 +02:00
|
|
|
|
|
|
|
// add widgets to DB
|
2020-03-19 10:50:18 +01:00
|
|
|
assert.NoError(t, DBpool.Create(&widgetA).Error)
|
|
|
|
assert.NoError(t, DBpool.Create(&widgetB).Error)
|
2019-09-03 15:12:18 +02:00
|
|
|
|
2019-09-03 14:16:01 +02:00
|
|
|
var widget1 Widget
|
2021-10-19 13:28:41 +02:00
|
|
|
assert.NoError(t, DBpool.Find(&widget1, 1).Error, "Find Widget with ID=1")
|
2019-09-03 14:16:01 +02:00
|
|
|
}
|
2019-05-17 15:32:58 +02:00
|
|
|
|
2019-09-03 14:16:01 +02:00
|
|
|
func TestFileAssociations(t *testing.T) {
|
2019-09-03 15:12:18 +02:00
|
|
|
|
2020-03-19 10:50:18 +01:00
|
|
|
DropTables()
|
|
|
|
MigrateModels()
|
2019-09-03 15:12:18 +02:00
|
|
|
|
|
|
|
// create copies of global test data
|
2020-03-27 11:53:26 +01:00
|
|
|
fileA := File{}
|
|
|
|
fileB := File{}
|
|
|
|
fileC := File{}
|
|
|
|
fileD := File{}
|
2019-09-03 15:12:18 +02:00
|
|
|
|
|
|
|
// add files to DB
|
2020-03-19 10:50:18 +01:00
|
|
|
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)
|
2019-09-03 15:12:18 +02:00
|
|
|
|
2019-09-03 14:16:01 +02:00
|
|
|
var file1 File
|
2021-10-19 13:28:41 +02:00
|
|
|
assert.NoError(t, DBpool.Find(&file1, 1).Error, "Find File with ID=1")
|
2019-09-10 16:28:57 +02:00
|
|
|
}
|