mirror of
https://git.rwth-aachen.de/acs/public/villas/web-backend-go/
synced 2025-03-30 00:00:12 +01:00
add testing for visualization endpoints
This commit is contained in:
parent
513ec3b2ee
commit
687e3863e4
7 changed files with 178 additions and 44 deletions
|
@ -93,6 +93,8 @@ test:backend:endpoints:
|
|||
- go test -v -args -dbhost=/var/run/postgresql
|
||||
- cd ../simulationmodel
|
||||
- go test -v -args -dbhost=/var/run/postgresql
|
||||
- cd ../visualization
|
||||
- go test -v -args -dbhost=/var/run/postgresql
|
||||
dependencies:
|
||||
- build:backend
|
||||
|
||||
|
|
|
@ -111,7 +111,7 @@ type Visualization struct {
|
|||
// Grid of visualization
|
||||
Grid int `gorm:"default:15"`
|
||||
// ID of simulation to which visualization belongs
|
||||
SimulationID uint `gorm:"not null"`
|
||||
SimulationID uint
|
||||
// Widgets that belong to visualization
|
||||
Widgets []Widget `gorm:"foreignkey:VisualizationID"`
|
||||
}
|
||||
|
|
|
@ -39,12 +39,14 @@ type SimulatorResponse struct {
|
|||
}
|
||||
|
||||
type VisualizationResponse struct {
|
||||
ID uint `json:"ID"`
|
||||
Name string `json:"Name"`
|
||||
Grid int `json:"Grid"`
|
||||
SimulationID uint `json:"SimulationID"`
|
||||
}
|
||||
|
||||
type WidgetResponse struct {
|
||||
ID uint `json:"ID"`
|
||||
Name string `json:"Name"`
|
||||
Type string `json:"Type"`
|
||||
Width uint `json:"Width"`
|
||||
|
@ -61,7 +63,7 @@ type WidgetResponse struct {
|
|||
|
||||
type FileResponse struct {
|
||||
Name string `json:"Name"`
|
||||
ID uint `json:"FileID"`
|
||||
ID uint `json:"ID"`
|
||||
Path string `json:"Path"`
|
||||
Type string `json:"Type"`
|
||||
Size uint `json:"Size"`
|
||||
|
@ -111,3 +113,11 @@ type ResponseMsgSimulationModel struct {
|
|||
type ResponseMsgSignals struct {
|
||||
Signals []SignalResponse `json:"signals"`
|
||||
}
|
||||
|
||||
type ResponseMsgVisualizations struct {
|
||||
Visualizations []VisualizationResponse `json:"visualizations"`
|
||||
}
|
||||
|
||||
type ResponseMsgVisualization struct {
|
||||
Visualization VisualizationResponse `json:"visualization"`
|
||||
}
|
||||
|
|
|
@ -176,6 +176,7 @@ func (self *VisualizationSerializer) Response() VisualizationResponse {
|
|||
Name: self.Name,
|
||||
Grid: self.Grid,
|
||||
SimulationID: self.SimulationID,
|
||||
ID: self.ID,
|
||||
}
|
||||
return response
|
||||
}
|
||||
|
@ -204,6 +205,7 @@ type WidgetSerializer struct {
|
|||
func (self *WidgetSerializer) Response() WidgetResponse {
|
||||
|
||||
response := WidgetResponse{
|
||||
ID: self.ID,
|
||||
Name: self.Name,
|
||||
Type: self.Type,
|
||||
Width: self.Width,
|
||||
|
|
|
@ -12,7 +12,6 @@ import (
|
|||
func RegisterSimulationModelEndpoints(r *gin.RouterGroup) {
|
||||
r.GET("", getSimulationModels)
|
||||
r.POST("", addSimulationModel)
|
||||
//r.POST("/:modelID", cloneSimulationModel)
|
||||
r.PUT("/:modelID", updateSimulationModel)
|
||||
r.GET("/:modelID", getSimulationModel)
|
||||
r.DELETE("/:modelID", deleteSimulationModel)
|
||||
|
@ -91,40 +90,6 @@ func addSimulationModel(c *gin.Context) {
|
|||
}
|
||||
}
|
||||
|
||||
func cloneSimulationModel(c *gin.Context) {
|
||||
|
||||
// modelID, err := routes.GetModelID(c)
|
||||
// if err != nil {
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// targetSimID, err := strconv.Atoi(c.PostForm("TargetSim"))
|
||||
// if err != nil {
|
||||
// errormsg := fmt.Sprintf("Bad request. No or incorrect format of target sim ID")
|
||||
// c.JSON(http.StatusBadRequest, gin.H{
|
||||
// "error": errormsg,
|
||||
// })
|
||||
// return
|
||||
// }
|
||||
|
||||
// TODO TO BE IMPLEMENTED
|
||||
// Check if target sim exists
|
||||
// Check if model exists
|
||||
|
||||
// Get all Signals of Model
|
||||
// Get Simulator of Model
|
||||
// Get Files of model
|
||||
|
||||
// Add new model object to DB and associate with target sim
|
||||
// Add new signal objects to DB and associate with new model object (careful with directions)
|
||||
// Associate Simulator with new Model object
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "Not implemented.",
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// updateSimulationModel godoc
|
||||
// @Summary Update a simulation model
|
||||
// @ID updateSimulationModel
|
||||
|
|
|
@ -13,7 +13,6 @@ func RegisterVisualizationEndpoints(r *gin.RouterGroup) {
|
|||
|
||||
r.GET("", getVisualizations)
|
||||
r.POST("", addVisualization)
|
||||
//r.POST("/:visualizationID", cloneVisualization)
|
||||
r.PUT("/:visualizationID", updateVisualization)
|
||||
r.GET("/:visualizationID", getVisualization)
|
||||
r.DELETE("/:visualizationID", deleteVisualization)
|
||||
|
@ -91,12 +90,6 @@ func addVisualization(c *gin.Context) {
|
|||
|
||||
}
|
||||
|
||||
func cloneVisualization(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "NOT implemented",
|
||||
})
|
||||
}
|
||||
|
||||
// updateVisualization godoc
|
||||
// @Summary Update a visualization
|
||||
// @ID updateVisualization
|
||||
|
|
162
routes/visualization/visualization_test.go
Normal file
162
routes/visualization/visualization_test.go
Normal file
|
@ -0,0 +1,162 @@
|
|||
package visualization
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common"
|
||||
"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 visA = common.VisualizationResponse{
|
||||
ID: 1,
|
||||
Name: "Visualization_A",
|
||||
Grid: 15,
|
||||
SimulationID: 1,
|
||||
}
|
||||
|
||||
var visB = common.VisualizationResponse{
|
||||
ID: 2,
|
||||
Name: "Visualization_B",
|
||||
Grid: 15,
|
||||
SimulationID: 1,
|
||||
}
|
||||
|
||||
var visC = common.Visualization{
|
||||
ID: 3,
|
||||
Name: "Visualization_C",
|
||||
Grid: 99,
|
||||
SimulationID: 1,
|
||||
}
|
||||
|
||||
var visCupdated = common.Visualization{
|
||||
ID: visC.ID,
|
||||
Name: "Visualization_CUpdated",
|
||||
SimulationID: visC.SimulationID,
|
||||
Grid: visC.Grid,
|
||||
}
|
||||
|
||||
var visC_response = common.VisualizationResponse{
|
||||
ID: visC.ID,
|
||||
Name: visC.Name,
|
||||
Grid: visC.Grid,
|
||||
SimulationID: visC.SimulationID,
|
||||
}
|
||||
|
||||
var visC_responseUpdated = common.VisualizationResponse{
|
||||
ID: visCupdated.ID,
|
||||
Name: visCupdated.Name,
|
||||
Grid: visCupdated.Grid,
|
||||
SimulationID: visCupdated.SimulationID,
|
||||
}
|
||||
|
||||
var myVisualizations = []common.VisualizationResponse{
|
||||
visA,
|
||||
visB,
|
||||
}
|
||||
|
||||
var msgVisualizations = common.ResponseMsgVisualizations{
|
||||
Visualizations: myVisualizations,
|
||||
}
|
||||
|
||||
var msgVis = common.ResponseMsgVisualization{
|
||||
Visualization: visC_response,
|
||||
}
|
||||
|
||||
var msgVisupdated = common.ResponseMsgVisualization{
|
||||
Visualization: visC_responseUpdated,
|
||||
}
|
||||
|
||||
// Test /models endpoints
|
||||
func TestSimulationModelEndpoints(t *testing.T) {
|
||||
|
||||
db := common.DummyInitDB()
|
||||
defer db.Close()
|
||||
common.DummyPopulateDB(db)
|
||||
|
||||
router := gin.Default()
|
||||
api := router.Group("/api")
|
||||
|
||||
// All endpoints require authentication except when someone wants to
|
||||
// login (POST /authenticate)
|
||||
user.VisitorAuthenticate(api.Group("/authenticate"))
|
||||
|
||||
api.Use(user.Authentication(true))
|
||||
|
||||
RegisterVisualizationEndpoints(api.Group("/visualizations"))
|
||||
|
||||
credjson, err := json.Marshal(cred)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
msgOKjson, err := json.Marshal(msgOK)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
msgVisualizationsjson, err := json.Marshal(msgVisualizations)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
msgVisjson, err := json.Marshal(msgVis)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
msgVisupdatedjson, err := json.Marshal(msgVisupdated)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
visCjson, err := json.Marshal(visC)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
visCupdatedjson, err := json.Marshal(visCupdated)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
token = common.AuthenticateForTest(t, router, "/api/authenticate", "POST", credjson, 200)
|
||||
|
||||
// test GET models
|
||||
common.TestEndpoint(t, router, token, "/api/visualizations?simulationID=1", "GET", nil, 200, string(msgVisualizationsjson))
|
||||
|
||||
// test POST models
|
||||
common.TestEndpoint(t, router, token, "/api/visualizations", "POST", visCjson, 200, string(msgOKjson))
|
||||
|
||||
// test GET models/:ModelID to check if previous POST worked correctly
|
||||
common.TestEndpoint(t, router, token, "/api/visualizations/3", "GET", nil, 200, string(msgVisjson))
|
||||
|
||||
// test PUT models/:ModelID
|
||||
common.TestEndpoint(t, router, token, "/api/visualizations/3", "PUT", visCupdatedjson, 200, string(msgOKjson))
|
||||
common.TestEndpoint(t, router, token, "/api/visualizations/3", "GET", nil, 200, string(msgVisupdatedjson))
|
||||
|
||||
// test DELETE models/:ModelID
|
||||
common.TestEndpoint(t, router, token, "/api/visualizations/3", "DELETE", nil, 200, string(msgOKjson))
|
||||
common.TestEndpoint(t, router, token, "/api/visualizations?simulationID=1", "GET", nil, 200, string(msgVisualizationsjson))
|
||||
|
||||
// TODO add testing for other return codes
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue