revise widget testing

This commit is contained in:
Sonja Happ 2019-07-25 13:25:46 +02:00
parent 65f7a9b493
commit 92eb455e5a
6 changed files with 169 additions and 193 deletions

View file

@ -131,10 +131,9 @@ func DummyPopulateDB(test_db *gorm.DB) {
checkErr(test_db.Create(&FileC).Error)
checkErr(test_db.Create(&FileD).Error)
widg_A := Widget{Name: "Widget_A"}
widg_B := Widget{Name: "Widget_B"}
checkErr(test_db.Create(&widg_A).Error)
checkErr(test_db.Create(&widg_B).Error)
// Widgets
checkErr(test_db.Create(&WidgetA).Error)
checkErr(test_db.Create(&WidgetB).Error)
// Associations between models
// For `belongs to` use the model with id=1
@ -155,8 +154,8 @@ func DummyPopulateDB(test_db *gorm.DB) {
checkErr(test_db.Model(&ScenarioA).Association("Dashboards").Append(&DashboardB).Error)
// Dashboard HM Widget
checkErr(test_db.Model(&DashboardA).Association("Widgets").Append(&widg_A).Error)
checkErr(test_db.Model(&DashboardA).Association("Widgets").Append(&widg_B).Error)
checkErr(test_db.Model(&DashboardA).Association("Widgets").Append(&WidgetA).Error)
checkErr(test_db.Model(&DashboardA).Association("Widgets").Append(&WidgetB).Error)
// SimulationModel HM Signals
checkErr(test_db.Model(&SimulationModelA).Association("InputMapping").Append(&InSignalA).Error)
@ -173,8 +172,8 @@ func DummyPopulateDB(test_db *gorm.DB) {
checkErr(test_db.Model(&SimulatorA).Association("SimulationModels").Append(&SimulationModelB).Error)
// Widget HM Files
checkErr(test_db.Model(&widg_A).Association("Files").Append(&FileA).Error)
checkErr(test_db.Model(&widg_A).Association("Files").Append(&FileB).Error)
checkErr(test_db.Model(&WidgetA).Association("Files").Append(&FileA).Error)
checkErr(test_db.Model(&WidgetA).Association("Files").Append(&FileB).Error)
}
// Erase tables and glose the testdb

View file

@ -209,19 +209,19 @@ type WidgetSerializer struct {
func (self *WidgetSerializer) Response() WidgetResponse {
response := WidgetResponse{
ID: self.ID,
Name: self.Name,
Type: self.Type,
Width: self.Width,
Height: self.Height,
MinWidth: self.MinWidth,
MinHeight: self.MinHeight,
X: self.X,
Y: self.Y,
Z: self.Z,
DashboardID: self.DashboardID,
IsLocked: self.IsLocked,
//CustomProperties
ID: self.ID,
Name: self.Name,
Type: self.Type,
Width: self.Width,
Height: self.Height,
MinWidth: self.MinWidth,
MinHeight: self.MinHeight,
X: self.X,
Y: self.Y,
Z: self.Z,
DashboardID: self.DashboardID,
IsLocked: self.IsLocked,
CustomProperties: self.CustomProperties,
}
return response
}

View file

@ -410,3 +410,117 @@ var FileD = File{
WidgetID: 0,
SimulationModelID: 1,
}
// Widgets
var customPropertiesA = json.RawMessage(`{"property1" : "testValue1A", "property2" : "testValue2A", "property3" : 42}`)
var customPropertiesB = json.RawMessage(`{"property1" : "testValue1B", "property2" : "testValue2B", "property3" : 43}`)
var customPropertiesC = json.RawMessage(`{"property1" : "testValue1C", "property2" : "testValue2C", "property3" : 44}`)
var WidgetA = Widget{
Name: "Widget_A",
Type: "graph",
Width: 100,
Height: 50,
MinHeight: 40,
MinWidth: 80,
X: 10,
Y: 10,
Z: 10,
IsLocked: false,
CustomProperties: postgres.Jsonb{customPropertiesA},
DashboardID: 1,
}
var WidgetA_response = WidgetResponse{
ID: 1,
Name: WidgetA.Name,
Type: WidgetA.Type,
Width: WidgetA.Width,
Height: WidgetA.Height,
MinWidth: WidgetA.MinWidth,
MinHeight: WidgetA.MinHeight,
X: WidgetA.X,
Y: WidgetA.Y,
Z: WidgetA.Z,
IsLocked: WidgetA.IsLocked,
CustomProperties: WidgetA.CustomProperties,
DashboardID: WidgetA.DashboardID,
}
var WidgetB = Widget{
Name: "Widget_B",
Type: "slider",
Width: 200,
Height: 20,
MinHeight: 10,
MinWidth: 50,
X: 100,
Y: -40,
Z: 0,
IsLocked: false,
CustomProperties: postgres.Jsonb{customPropertiesB},
DashboardID: 1,
}
var WidgetB_response = WidgetResponse{
ID: 2,
Name: WidgetB.Name,
Type: WidgetB.Type,
Width: WidgetB.Width,
Height: WidgetB.Height,
MinWidth: WidgetB.MinWidth,
MinHeight: WidgetB.MinHeight,
X: WidgetB.X,
Y: WidgetB.Y,
Z: WidgetB.Z,
IsLocked: WidgetB.IsLocked,
CustomProperties: WidgetB.CustomProperties,
DashboardID: WidgetB.DashboardID,
}
var WidgetC = Widget{
Name: "Widget_C",
Type: "bargraph",
Height: 30,
Width: 100,
MinHeight: 20,
MinWidth: 50,
X: 11,
Y: 12,
Z: 13,
IsLocked: false,
CustomProperties: postgres.Jsonb{customPropertiesC},
DashboardID: 1,
}
var WidgetC_response = WidgetResponse{
ID: 3,
Name: WidgetC.Name,
Type: WidgetC.Type,
Width: WidgetC.Width,
Height: WidgetC.Height,
MinWidth: WidgetC.MinWidth,
MinHeight: WidgetC.MinHeight,
X: WidgetC.X,
Y: WidgetC.Y,
Z: WidgetC.Z,
IsLocked: WidgetC.IsLocked,
CustomProperties: WidgetC.CustomProperties,
DashboardID: WidgetC.DashboardID,
}
var WidgetCUpdated_response = WidgetResponse{
ID: 3,
Name: "Widget_CUpdated",
Type: WidgetC.Type,
Height: 35,
Width: 110,
MinHeight: WidgetC.MinHeight,
MinWidth: WidgetC.MinWidth,
X: WidgetC.X,
Y: WidgetC.Y,
Z: WidgetC.Z,
IsLocked: WidgetC.IsLocked,
CustomProperties: WidgetC.CustomProperties,
DashboardID: WidgetC.DashboardID,
}

View file

@ -55,7 +55,7 @@ func getWidgets(c *gin.Context) {
// @Accept json
// @Produce json
// @Tags widgets
// @Param inputWidget body common.WidgetResponse true "Widget to be added incl. ID of dashboard"
// @Param inputWidget body common.ResponseMsgWidget true "Widget to be added incl. ID of dashboard"
// @Success 200 "OK."
// @Failure 401 "Unauthorized Access"
// @Failure 403 "Access forbidden."
@ -64,8 +64,8 @@ func getWidgets(c *gin.Context) {
// @Router /widgets [post]
func addWidget(c *gin.Context) {
var newWidget Widget
err := c.BindJSON(&newWidget)
var newWidgetData common.ResponseMsgWidget
err := c.BindJSON(&newWidgetData)
if err != nil {
errormsg := "Bad request. Error binding form data to JSON: " + err.Error()
c.JSON(http.StatusBadRequest, gin.H{
@ -74,6 +74,20 @@ func addWidget(c *gin.Context) {
return
}
var newWidget Widget
newWidget.Name = newWidgetData.Widget.Name
newWidget.Type = newWidgetData.Widget.Type
newWidget.Height = newWidgetData.Widget.Height
newWidget.Width = newWidgetData.Widget.Width
newWidget.MinHeight = newWidgetData.Widget.MinHeight
newWidget.MinWidth = newWidgetData.Widget.MinWidth
newWidget.X = newWidgetData.Widget.X
newWidget.Y = newWidgetData.Widget.Y
newWidget.Z = newWidgetData.Widget.Z
newWidget.CustomProperties = newWidgetData.Widget.CustomProperties
newWidget.IsLocked = newWidgetData.Widget.IsLocked
newWidget.DashboardID = newWidgetData.Widget.DashboardID
ok, _ := dashboard.CheckPermissions(c, common.Create, "body", int(newWidget.DashboardID))
if !ok {
return
@ -94,7 +108,7 @@ func addWidget(c *gin.Context) {
// @Tags widgets
// @Accept json
// @Produce json
// @Param inputWidget body common.WidgetResponse true "Widget to be updated"
// @Param inputWidget body common.ResponseMsgWidget true "Widget to be updated"
// @Success 200 "OK."
// @Failure 401 "Unauthorized Access"
// @Failure 403 "Access forbidden."
@ -109,7 +123,7 @@ func updateWidget(c *gin.Context) {
return
}
var modifiedWidget Widget
var modifiedWidget common.ResponseMsgWidget
err := c.BindJSON(&modifiedWidget)
if err != nil {
errormsg := "Bad request. Error binding form data to JSON: " + err.Error()
@ -119,7 +133,7 @@ func updateWidget(c *gin.Context) {
return
}
err = w.update(modifiedWidget)
err = w.update(modifiedWidget.Widget)
if common.ProvideErrorResponse(c, err) == false {
c.JSON(http.StatusOK, gin.H{
"message": "OK.",

View file

@ -46,7 +46,7 @@ func (w *Widget) addToDashboard() error {
return err
}
func (w *Widget) update(modifiedWidget Widget) error {
func (w *Widget) update(modifiedWidget common.WidgetResponse) error {
db := common.GetDB()
err := db.Model(w).Updates(map[string]interface{}{

View file

@ -10,138 +10,16 @@ import (
"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 wdgA = common.WidgetResponse{
ID: 1,
Name: "Widget_A",
Type: "",
Height: 0,
Width: 0,
MinHeight: 0,
MinWidth: 0,
X: 0,
Y: 0,
Z: 0,
IsLocked: false,
CustomProperties: "",
DashboardID: 1,
}
var wdgB = common.WidgetResponse{
ID: 2,
Name: "Widget_B",
Type: "",
Height: 0,
Width: 0,
MinHeight: 0,
MinWidth: 0,
X: 0,
Y: 0,
Z: 0,
IsLocked: false,
CustomProperties: "",
DashboardID: 1,
}
var wdgC = common.Widget{
ID: 3,
Name: "Widget_C",
Type: "",
Height: 30,
Width: 100,
MinHeight: 20,
MinWidth: 50,
X: 11,
Y: 12,
Z: 13,
IsLocked: false,
CustomProperties: "",
DashboardID: 1,
}
var wdgCupdated = common.Widget{
ID: wdgC.ID,
Name: "Widget_CUpdated",
Type: wdgC.Type,
Height: wdgC.Height,
Width: wdgC.Width,
MinHeight: wdgC.MinHeight,
MinWidth: wdgC.MinWidth,
X: wdgC.X,
Y: wdgC.Y,
Z: wdgC.Z,
IsLocked: wdgC.IsLocked,
CustomProperties: wdgC.CustomProperties,
DashboardID: wdgC.DashboardID,
}
var wdgC_response = common.WidgetResponse{
ID: wdgC.ID,
Name: wdgC.Name,
Type: wdgC.Type,
Height: wdgC.Height,
Width: wdgC.Width,
MinHeight: wdgC.MinHeight,
MinWidth: wdgC.MinWidth,
X: wdgC.X,
Y: wdgC.Y,
Z: wdgC.Z,
IsLocked: wdgC.IsLocked,
CustomProperties: wdgC.CustomProperties,
DashboardID: wdgC.DashboardID,
}
var wdgC_responseUpdated = common.WidgetResponse{
ID: wdgC.ID,
Name: "Widget_CUpdated",
Type: wdgC.Type,
Height: wdgC.Height,
Width: wdgC.Width,
MinHeight: wdgC.MinHeight,
MinWidth: wdgC.MinWidth,
X: wdgC.X,
Y: wdgC.Y,
Z: wdgC.Z,
IsLocked: wdgC.IsLocked,
CustomProperties: wdgC.CustomProperties,
DashboardID: wdgC.DashboardID,
}
var myWidgets = []common.WidgetResponse{
wdgA,
wdgB,
}
var msgWidgets = common.ResponseMsgWidgets{
Widgets: myWidgets,
}
var msgWdg = common.ResponseMsgWidget{
Widget: wdgC_response,
}
var msgWdgupdated = common.ResponseMsgWidget{
Widget: wdgC_responseUpdated,
}
// Test /widgets endpoints
func TestWidgetEndpoints(t *testing.T) {
var token string
var myWidgets = []common.WidgetResponse{common.WidgetA_response, common.WidgetB_response}
var msgWidgets = common.ResponseMsgWidgets{Widgets: myWidgets}
var msgWdg = common.ResponseMsgWidget{Widget: common.WidgetC_response}
var msgWdgupdated = common.ResponseMsgWidget{Widget: common.WidgetCUpdated_response}
db := common.DummyInitDB()
defer db.Close()
common.DummyPopulateDB(db)
@ -157,40 +35,11 @@ func TestWidgetEndpoints(t *testing.T) {
RegisterWidgetEndpoints(api.Group("/widgets"))
credjson, err := json.Marshal(cred)
if err != nil {
panic(err)
}
msgOKjson, err := json.Marshal(msgOK)
if err != nil {
panic(err)
}
msgWidgetsjson, err := json.Marshal(msgWidgets)
if err != nil {
panic(err)
}
msgWdgjson, err := json.Marshal(msgWdg)
if err != nil {
panic(err)
}
msgWdgupdatedjson, err := json.Marshal(msgWdgupdated)
if err != nil {
panic(err)
}
wdgCjson, err := json.Marshal(wdgC)
if err != nil {
panic(err)
}
wdgCupdatedjson, err := json.Marshal(wdgCupdated)
if err != nil {
panic(err)
}
credjson, _ := json.Marshal(common.CredUser)
msgOKjson, _ := json.Marshal(common.MsgOK)
msgWidgetsjson, _ := json.Marshal(msgWidgets)
msgWdgjson, _ := json.Marshal(msgWdg)
msgWdgupdatedjson, _ := json.Marshal(msgWdgupdated)
token = common.AuthenticateForTest(t, router, "/api/authenticate", "POST", credjson, 200)
@ -198,13 +47,13 @@ func TestWidgetEndpoints(t *testing.T) {
common.TestEndpoint(t, router, token, "/api/widgets?dashboardID=1", "GET", nil, 200, msgWidgetsjson)
// test POST widgets
common.TestEndpoint(t, router, token, "/api/widgets", "POST", wdgCjson, 200, msgOKjson)
common.TestEndpoint(t, router, token, "/api/widgets", "POST", msgWdgjson, 200, msgOKjson)
// test GET widgets/:widgetID to check if previous POST worked correctly
common.TestEndpoint(t, router, token, "/api/widgets/3", "GET", nil, 200, msgWdgjson)
// test PUT widgets/:widgetID
common.TestEndpoint(t, router, token, "/api/widgets/3", "PUT", wdgCupdatedjson, 200, msgOKjson)
common.TestEndpoint(t, router, token, "/api/widgets/3", "PUT", msgWdgupdatedjson, 200, msgOKjson)
common.TestEndpoint(t, router, token, "/api/widgets/3", "GET", nil, 200, msgWdgupdatedjson)
// test DELETE widgets/:widgetID