mirror of
https://git.rwth-aachen.de/acs/public/villas/web-backend-go/
synced 2025-03-30 00:00:12 +01:00
revise dashboard testing
This commit is contained in:
parent
d79fb53cb2
commit
a06d90d085
7 changed files with 74 additions and 154 deletions
|
@ -121,6 +121,10 @@ func DummyPopulateDB(test_db *gorm.DB) {
|
||||||
checkErr(test_db.Create(&SimulationModelA).Error)
|
checkErr(test_db.Create(&SimulationModelA).Error)
|
||||||
checkErr(test_db.Create(&SimulationModelB).Error)
|
checkErr(test_db.Create(&SimulationModelB).Error)
|
||||||
|
|
||||||
|
// Dashboards
|
||||||
|
checkErr(test_db.Create(&DashboardA).Error)
|
||||||
|
checkErr(test_db.Create(&DashboardB).Error)
|
||||||
|
|
||||||
file_A := File{Name: "File_A"}
|
file_A := File{Name: "File_A"}
|
||||||
file_B := File{Name: "File_B"}
|
file_B := File{Name: "File_B"}
|
||||||
file_C := File{Name: "File_C"}
|
file_C := File{Name: "File_C"}
|
||||||
|
@ -130,11 +134,6 @@ func DummyPopulateDB(test_db *gorm.DB) {
|
||||||
checkErr(test_db.Create(&file_C).Error)
|
checkErr(test_db.Create(&file_C).Error)
|
||||||
checkErr(test_db.Create(&file_D).Error)
|
checkErr(test_db.Create(&file_D).Error)
|
||||||
|
|
||||||
dab_A := Dashboard{Name: "Dashboard_A"}
|
|
||||||
dab_B := Dashboard{Name: "Dashboard_B"}
|
|
||||||
checkErr(test_db.Create(&dab_A).Error)
|
|
||||||
checkErr(test_db.Create(&dab_B).Error)
|
|
||||||
|
|
||||||
widg_A := Widget{Name: "Widget_A"}
|
widg_A := Widget{Name: "Widget_A"}
|
||||||
widg_B := Widget{Name: "Widget_B"}
|
widg_B := Widget{Name: "Widget_B"}
|
||||||
checkErr(test_db.Create(&widg_A).Error)
|
checkErr(test_db.Create(&widg_A).Error)
|
||||||
|
@ -155,12 +154,12 @@ func DummyPopulateDB(test_db *gorm.DB) {
|
||||||
checkErr(test_db.Model(&ScenarioA).Association("SimulationModels").Append(&SimulationModelB).Error)
|
checkErr(test_db.Model(&ScenarioA).Association("SimulationModels").Append(&SimulationModelB).Error)
|
||||||
|
|
||||||
// Scenario HM Dashboards
|
// Scenario HM Dashboards
|
||||||
checkErr(test_db.Model(&ScenarioA).Association("Dashboards").Append(&dab_A).Error)
|
checkErr(test_db.Model(&ScenarioA).Association("Dashboards").Append(&DashboardA).Error)
|
||||||
checkErr(test_db.Model(&ScenarioA).Association("Dashboards").Append(&dab_B).Error)
|
checkErr(test_db.Model(&ScenarioA).Association("Dashboards").Append(&DashboardB).Error)
|
||||||
|
|
||||||
// Dashboard HM Widget
|
// Dashboard HM Widget
|
||||||
checkErr(test_db.Model(&dab_A).Association("Widgets").Append(&widg_A).Error)
|
checkErr(test_db.Model(&DashboardA).Association("Widgets").Append(&widg_A).Error)
|
||||||
checkErr(test_db.Model(&dab_A).Association("Widgets").Append(&widg_B).Error)
|
checkErr(test_db.Model(&DashboardA).Association("Widgets").Append(&widg_B).Error)
|
||||||
|
|
||||||
// SimulationModel HM Signals
|
// SimulationModel HM Signals
|
||||||
checkErr(test_db.Model(&SimulationModelA).Association("InputMapping").Append(&InSignalA).Error)
|
checkErr(test_db.Model(&SimulationModelA).Association("InputMapping").Append(&InSignalA).Error)
|
||||||
|
|
|
@ -139,7 +139,7 @@ type Widget struct {
|
||||||
// Locked state of widget
|
// Locked state of widget
|
||||||
IsLocked bool `gorm:"default:false"`
|
IsLocked bool `gorm:"default:false"`
|
||||||
// Custom properties of widget as JSON string
|
// Custom properties of widget as JSON string
|
||||||
CustomProperties string
|
CustomProperties postgres.Jsonb
|
||||||
// ID of dashboard to which widget belongs
|
// ID of dashboard to which widget belongs
|
||||||
DashboardID uint
|
DashboardID uint
|
||||||
// Files that belong to widget (for example images)
|
// Files that belong to widget (for example images)
|
||||||
|
@ -170,6 +170,7 @@ type File struct {
|
||||||
FileData []byte `gorm:"column:FileData"`
|
FileData []byte `gorm:"column:FileData"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Credentials type (not for DB)
|
||||||
type credentials struct {
|
type credentials struct {
|
||||||
Username string `json:"username"`
|
Username string `json:"username"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
|
|
|
@ -39,38 +39,38 @@ type SimulatorResponse struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type DashboardResponse struct {
|
type DashboardResponse struct {
|
||||||
ID uint `json:"ID"`
|
ID uint `json:"id"`
|
||||||
Name string `json:"Name"`
|
Name string `json:"name"`
|
||||||
Grid int `json:"Grid"`
|
Grid int `json:"grid"`
|
||||||
ScenarioID uint `json:"ScenarioID"`
|
ScenarioID uint `json:"scenarioID"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type WidgetResponse struct {
|
type WidgetResponse struct {
|
||||||
ID uint `json:"ID"`
|
ID uint `json:"id"`
|
||||||
Name string `json:"Name"`
|
Name string `json:"name"`
|
||||||
Type string `json:"Type"`
|
Type string `json:"type"`
|
||||||
Width uint `json:"Width"`
|
Width uint `json:"width"`
|
||||||
Height uint `json:"Height"`
|
Height uint `json:"height"`
|
||||||
MinWidth uint `json:"MinWidth"`
|
MinWidth uint `json:"minWidth"`
|
||||||
MinHeight uint `json:"MinHeight"`
|
MinHeight uint `json:"minHeight"`
|
||||||
X int `json:"X"`
|
X int `json:"x"`
|
||||||
Y int `json:"Y"`
|
Y int `json:"y"`
|
||||||
Z int `json:"Z"`
|
Z int `json:"z"`
|
||||||
DashboardID uint `json:"DashboardID"`
|
DashboardID uint `json:"dashboardID"`
|
||||||
IsLocked bool `json:"IsLocked"`
|
IsLocked bool `json:"isLocked"`
|
||||||
CustomProperties string `json:"CustomProperties"`
|
CustomProperties postgres.Jsonb `json:"customProperties"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type FileResponse struct {
|
type FileResponse struct {
|
||||||
Name string `json:"Name"`
|
Name string `json:"name"`
|
||||||
ID uint `json:"ID"`
|
ID uint `json:"id"`
|
||||||
Type string `json:"Type"`
|
Type string `json:"type"`
|
||||||
Size uint `json:"Size"`
|
Size uint `json:"size"`
|
||||||
H uint `json:"ImageHeight"`
|
H uint `json:"imageHeight"`
|
||||||
W uint `json:"ImageWidth"`
|
W uint `json:"imageWidth"`
|
||||||
Date string `json:"Date"`
|
Date string `json:"date"`
|
||||||
WidgetID uint `json:"WidgetID"`
|
WidgetID uint `json:"widgetID"`
|
||||||
SimulationModelID uint `json:"SimulationModelID"`
|
SimulationModelID uint `json:"simulationModelID"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type SignalResponse struct {
|
type SignalResponse struct {
|
||||||
|
|
|
@ -338,3 +338,14 @@ var InSignalCUpdated_response = SignalResponse{
|
||||||
Unit: InSignalCUpdated.Unit,
|
Unit: InSignalCUpdated.Unit,
|
||||||
SimulationModelID: InSignalCUpdated.SimulationModelID,
|
SimulationModelID: InSignalCUpdated.SimulationModelID,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Dashboards
|
||||||
|
|
||||||
|
var DashboardA = Dashboard{ID: 1, Name: "Dashboard_A", Grid: 15, ScenarioID: 1}
|
||||||
|
var DashboardA_response = DashboardResponse{ID: DashboardA.ID, Name: DashboardA.Name, Grid: DashboardA.Grid, ScenarioID: DashboardA.ScenarioID}
|
||||||
|
var DashboardB = Dashboard{ID: 2, Name: "Dashboard_B", Grid: 10, ScenarioID: 1}
|
||||||
|
var DashboardB_response = DashboardResponse{ID: DashboardB.ID, Name: DashboardB.Name, Grid: DashboardB.Grid, ScenarioID: DashboardB.ScenarioID}
|
||||||
|
var DashboardC = Dashboard{ID: 3, Name: "Dashboard_C", Grid: 25, ScenarioID: 1}
|
||||||
|
var DashboardC_response = DashboardResponse{ID: DashboardC.ID, Name: DashboardC.Name, Grid: DashboardC.Grid, ScenarioID: DashboardC.ScenarioID}
|
||||||
|
var DashboardCUpdated = Dashboard{ID: DashboardC.ID, Name: "Dashboard_Cupdated", Grid: 24, ScenarioID: DashboardC.ScenarioID}
|
||||||
|
var DashboardCUpdated_response = DashboardResponse{ID: DashboardCUpdated.ID, Name: DashboardCUpdated.Name, Grid: DashboardCUpdated.Grid, ScenarioID: DashboardCUpdated.ScenarioID}
|
||||||
|
|
|
@ -56,7 +56,7 @@ func getDashboards(c *gin.Context) {
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Tags dashboards
|
// @Tags dashboards
|
||||||
// @Param inputDab body common.DashboardResponse true "Dashboard to be added incl. ID of Scenario"
|
// @Param inputDab body common.ResponseMsgDashboard true "Dashboard to be added incl. ID of Scenario"
|
||||||
// @Success 200 "OK."
|
// @Success 200 "OK."
|
||||||
// @Failure 401 "Unauthorized Access"
|
// @Failure 401 "Unauthorized Access"
|
||||||
// @Failure 403 "Access forbidden."
|
// @Failure 403 "Access forbidden."
|
||||||
|
@ -65,8 +65,8 @@ func getDashboards(c *gin.Context) {
|
||||||
// @Router /dashboards [post]
|
// @Router /dashboards [post]
|
||||||
func addDashboard(c *gin.Context) {
|
func addDashboard(c *gin.Context) {
|
||||||
|
|
||||||
var newDab Dashboard
|
var newDabData common.ResponseMsgDashboard
|
||||||
err := c.BindJSON(&newDab)
|
err := c.BindJSON(&newDabData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errormsg := "Bad request. Error binding form data to JSON: " + err.Error()
|
errormsg := "Bad request. Error binding form data to JSON: " + err.Error()
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
c.JSON(http.StatusBadRequest, gin.H{
|
||||||
|
@ -75,6 +75,12 @@ func addDashboard(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var newDab Dashboard
|
||||||
|
newDab.ID = newDabData.Dashboard.ID
|
||||||
|
newDab.Grid = newDabData.Dashboard.Grid
|
||||||
|
newDab.ScenarioID = newDabData.Dashboard.ScenarioID
|
||||||
|
newDab.Name = newDabData.Dashboard.Name
|
||||||
|
|
||||||
ok, _ := scenario.CheckPermissions(c, common.Create, "body", int(newDab.ScenarioID))
|
ok, _ := scenario.CheckPermissions(c, common.Create, "body", int(newDab.ScenarioID))
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
|
@ -96,7 +102,7 @@ func addDashboard(c *gin.Context) {
|
||||||
// @Tags dashboards
|
// @Tags dashboards
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Param inputDab body common.DashboardResponse true "Dashboard to be updated"
|
// @Param inputDab body common.ResponseMsgDashboard true "Dashboard to be updated"
|
||||||
// @Success 200 "OK."
|
// @Success 200 "OK."
|
||||||
// @Failure 401 "Unauthorized Access"
|
// @Failure 401 "Unauthorized Access"
|
||||||
// @Failure 403 "Access forbidden."
|
// @Failure 403 "Access forbidden."
|
||||||
|
@ -111,7 +117,7 @@ func updateDashboard(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var modifiedDab Dashboard
|
var modifiedDab common.ResponseMsgDashboard
|
||||||
err := c.BindJSON(&modifiedDab)
|
err := c.BindJSON(&modifiedDab)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errormsg := "Bad request. Error binding form data to JSON: " + err.Error()
|
errormsg := "Bad request. Error binding form data to JSON: " + err.Error()
|
||||||
|
@ -121,7 +127,7 @@ func updateDashboard(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = d.update(modifiedDab)
|
err = d.update(modifiedDab.Dashboard)
|
||||||
if common.ProvideErrorResponse(c, err) == false {
|
if common.ProvideErrorResponse(c, err) == false {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"message": "OK.",
|
"message": "OK.",
|
||||||
|
|
|
@ -46,7 +46,7 @@ func (d *Dashboard) addToScenario() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Dashboard) update(modifiedDab Dashboard) error {
|
func (d *Dashboard) update(modifiedDab common.DashboardResponse) error {
|
||||||
|
|
||||||
db := common.GetDB()
|
db := common.GetDB()
|
||||||
|
|
||||||
|
|
|
@ -10,84 +10,16 @@ import (
|
||||||
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/user"
|
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/user"
|
||||||
)
|
)
|
||||||
|
|
||||||
var token string
|
|
||||||
|
|
||||||
type credentials struct {
|
|
||||||
Username string `json:"username"`
|
|
||||||
Password string `json:"password"`
|
|
||||||
}
|
|
||||||
|
|
||||||
var cred = credentials{
|
|
||||||
Username: "User_A",
|
|
||||||
Password: "abc123",
|
|
||||||
}
|
|
||||||
|
|
||||||
var msgOK = common.ResponseMsg{
|
|
||||||
Message: "OK.",
|
|
||||||
}
|
|
||||||
|
|
||||||
var dabA = common.DashboardResponse{
|
|
||||||
ID: 1,
|
|
||||||
Name: "Dashboard_A",
|
|
||||||
Grid: 15,
|
|
||||||
ScenarioID: 1,
|
|
||||||
}
|
|
||||||
|
|
||||||
var dabB = common.DashboardResponse{
|
|
||||||
ID: 2,
|
|
||||||
Name: "Dashboard_B",
|
|
||||||
Grid: 15,
|
|
||||||
ScenarioID: 1,
|
|
||||||
}
|
|
||||||
|
|
||||||
var dabC = common.Dashboard{
|
|
||||||
ID: 3,
|
|
||||||
Name: "Dashboard_C",
|
|
||||||
Grid: 99,
|
|
||||||
ScenarioID: 1,
|
|
||||||
}
|
|
||||||
|
|
||||||
var dabCupdated = common.Dashboard{
|
|
||||||
ID: dabC.ID,
|
|
||||||
Name: "Dashboard_CUpdated",
|
|
||||||
ScenarioID: dabC.ScenarioID,
|
|
||||||
Grid: dabC.Grid,
|
|
||||||
}
|
|
||||||
|
|
||||||
var dabC_response = common.DashboardResponse{
|
|
||||||
ID: dabC.ID,
|
|
||||||
Name: dabC.Name,
|
|
||||||
Grid: dabC.Grid,
|
|
||||||
ScenarioID: dabC.ScenarioID,
|
|
||||||
}
|
|
||||||
|
|
||||||
var dabC_responseUpdated = common.DashboardResponse{
|
|
||||||
ID: dabCupdated.ID,
|
|
||||||
Name: dabCupdated.Name,
|
|
||||||
Grid: dabCupdated.Grid,
|
|
||||||
ScenarioID: dabCupdated.ScenarioID,
|
|
||||||
}
|
|
||||||
|
|
||||||
var myDashboards = []common.DashboardResponse{
|
|
||||||
dabA,
|
|
||||||
dabB,
|
|
||||||
}
|
|
||||||
|
|
||||||
var msgDashboards = common.ResponseMsgDashboards{
|
|
||||||
Dashboards: myDashboards,
|
|
||||||
}
|
|
||||||
|
|
||||||
var msgDab = common.ResponseMsgDashboard{
|
|
||||||
Dashboard: dabC_response,
|
|
||||||
}
|
|
||||||
|
|
||||||
var msgDabupdated = common.ResponseMsgDashboard{
|
|
||||||
Dashboard: dabC_responseUpdated,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test /dashboards endpoints
|
// Test /dashboards endpoints
|
||||||
func TestEndpoints(t *testing.T) {
|
func TestEndpoints(t *testing.T) {
|
||||||
|
|
||||||
|
var token string
|
||||||
|
|
||||||
|
var myDashboards = []common.DashboardResponse{common.DashboardA_response, common.DashboardB_response}
|
||||||
|
var msgDashboards = common.ResponseMsgDashboards{Dashboards: myDashboards}
|
||||||
|
var msgDab = common.ResponseMsgDashboard{Dashboard: common.DashboardC_response}
|
||||||
|
var msgDabupdated = common.ResponseMsgDashboard{Dashboard: common.DashboardCUpdated_response}
|
||||||
|
|
||||||
db := common.DummyInitDB()
|
db := common.DummyInitDB()
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
common.DummyPopulateDB(db)
|
common.DummyPopulateDB(db)
|
||||||
|
@ -103,40 +35,11 @@ func TestEndpoints(t *testing.T) {
|
||||||
|
|
||||||
RegisterDashboardEndpoints(api.Group("/dashboards"))
|
RegisterDashboardEndpoints(api.Group("/dashboards"))
|
||||||
|
|
||||||
credjson, err := json.Marshal(cred)
|
credjson, _ := json.Marshal(common.CredUser)
|
||||||
if err != nil {
|
msgOKjson, _ := json.Marshal(common.MsgOK)
|
||||||
panic(err)
|
msgDashboardsjson, _ := json.Marshal(msgDashboards)
|
||||||
}
|
msgDabjson, _ := json.Marshal(msgDab)
|
||||||
|
msgDabupdatedjson, _ := json.Marshal(msgDabupdated)
|
||||||
msgOKjson, err := json.Marshal(msgOK)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
msgDashboardsjson, err := json.Marshal(msgDashboards)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
msgDabjson, err := json.Marshal(msgDab)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
msgDabupdatedjson, err := json.Marshal(msgDabupdated)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
dabCjson, err := json.Marshal(dabC)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
dabCupdatedjson, err := json.Marshal(dabCupdated)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
token = common.AuthenticateForTest(t, router, "/api/authenticate", "POST", credjson, 200)
|
token = common.AuthenticateForTest(t, router, "/api/authenticate", "POST", credjson, 200)
|
||||||
|
|
||||||
|
@ -144,13 +47,13 @@ func TestEndpoints(t *testing.T) {
|
||||||
common.TestEndpoint(t, router, token, "/api/dashboards?scenarioID=1", "GET", nil, 200, msgDashboardsjson)
|
common.TestEndpoint(t, router, token, "/api/dashboards?scenarioID=1", "GET", nil, 200, msgDashboardsjson)
|
||||||
|
|
||||||
// test POST dashboards
|
// test POST dashboards
|
||||||
common.TestEndpoint(t, router, token, "/api/dashboards", "POST", dabCjson, 200, msgOKjson)
|
common.TestEndpoint(t, router, token, "/api/dashboards", "POST", msgDabjson, 200, msgOKjson)
|
||||||
|
|
||||||
// test GET dashboards/:dashboardID to check if previous POST worked correctly
|
// test GET dashboards/:dashboardID to check if previous POST worked correctly
|
||||||
common.TestEndpoint(t, router, token, "/api/dashboards/3", "GET", nil, 200, msgDabjson)
|
common.TestEndpoint(t, router, token, "/api/dashboards/3", "GET", nil, 200, msgDabjson)
|
||||||
|
|
||||||
// test PUT dashboards/:dashboardID
|
// test PUT dashboards/:dashboardID
|
||||||
common.TestEndpoint(t, router, token, "/api/dashboards/3", "PUT", dabCupdatedjson, 200, msgOKjson)
|
common.TestEndpoint(t, router, token, "/api/dashboards/3", "PUT", msgDabupdatedjson, 200, msgOKjson)
|
||||||
common.TestEndpoint(t, router, token, "/api/dashboards/3", "GET", nil, 200, msgDabupdatedjson)
|
common.TestEndpoint(t, router, token, "/api/dashboards/3", "GET", nil, 200, msgDabupdatedjson)
|
||||||
|
|
||||||
// test DELETE dashboards/:dashboardID
|
// test DELETE dashboards/:dashboardID
|
||||||
|
|
Loading…
Add table
Reference in a new issue