Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix499 correct sizeu value #500

Merged
merged 4 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions API/models/entity_attributes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package models

import (
"math"
"p3/repository"
u "p3/utils"
"strings"
Expand Down Expand Up @@ -156,7 +157,7 @@ func setCorridorColor(attributes map[string]any) {
}
}

// Check if sizeU and height are coherents
// Check if sizeU and height are consistent
func checkSizeUAndHeight(attributes map[string]any) *u.Error {
if attributes["sizeU"] == nil || attributes["height"] == nil {
return nil
Expand All @@ -169,15 +170,22 @@ func checkSizeUAndHeight(attributes map[string]any) *u.Error {
Message: err.Error(),
}
}
height := attributes["height"]
h := sizeU * RACKUNIT
height, err := u.GetFloat(attributes["height"])
if err != nil {
return &u.Error{
Type: u.ErrBadFormat,
Message: err.Error(),
}
}

s := height / RACKUNIT
switch heightUnit := attributes["heightUnit"]; heightUnit {
case "cm":
h *= 100
s /= 100
case "mm":
h *= 1000
s /= 1000
}
if height == h {
if sizeU == math.Ceil(s) {
return nil
} else {
return &u.Error{
Expand Down
2 changes: 1 addition & 1 deletion API/models/schemas/device_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"$ref": "refs/types.json#/definitions/float"
},
"sizeU": {
"$ref": "refs/types.json#/definitions/float"
"type": "integer"
},
"color": {
"type": "string",
Expand Down
3 changes: 2 additions & 1 deletion CLI/controllers/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"cli/models"
test_utils "cli/test"
"maps"
"math"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -43,7 +44,7 @@ func TestApplyTemplateOfTypeDeviceWorks(t *testing.T) {

test_utils.MockGetObjTemplate(mockAPI, template)

sizeU := int((float64(template["sizeWDHmm"].([]any)[2].(int)) / 1000) / models.RACKUNIT)
sizeU := int(math.Ceil((float64(template["sizeWDHmm"].([]any)[2].(int)) / 1000) / models.RACKUNIT))
err := controller.ApplyTemplate(attributes, device, models.DEVICE)
assert.Nil(t, err)

Expand Down
8 changes: 4 additions & 4 deletions CLI/controllers/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestUpdateDeviceSizeUmm(t *testing.T) {
}
mockDataUpdate := map[string]any{
"attributes": map[string]any{
"sizeU": float64(1),
"sizeU": 1,
"height": 44.45,
},
}
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestUpdateDeviceSizeUcm(t *testing.T) {
}
mockDataUpdate := map[string]any{
"attributes": map[string]any{
"sizeU": float64(1),
"sizeU": 1,
"height": 4.445,
},
}
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestUpdateDeviceheightmm(t *testing.T) {
}
mockDataUpdate := map[string]any{
"attributes": map[string]any{
"sizeU": float64(1),
"sizeU": 1,
"height": 44.45,
},
}
Expand Down Expand Up @@ -176,7 +176,7 @@ func TestUpdateDeviceheightcm(t *testing.T) {
}
mockDataUpdate := map[string]any{
"attributes": map[string]any{
"sizeU": float64(1),
"sizeU": 1,
"height": 4.445,
},
}
Expand Down
5 changes: 3 additions & 2 deletions CLI/models/attributes_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package models
import (
"cli/utils"
"fmt"
"math"
)

// Compute coherent sizeU or height according to given data
Expand Down Expand Up @@ -49,7 +50,7 @@ func ComputeSizeUAndHeight(obj, data map[string]any) error {
default:
return fmt.Errorf(errMsg)
}
newAttrs["sizeU"] = sizeU
newAttrs["sizeU"] = int(math.Ceil(sizeU))
}
return nil
}
Expand All @@ -61,7 +62,7 @@ func SetDeviceSizeUFromTemplate(deviceAttrs, tmpl map[string]any, tmplHeight any
if height, err := utils.GetFloat(tmplHeight); err != nil {
return err
} else {
deviceAttrs["sizeU"] = int((height / 1000) / RACKUNIT)
deviceAttrs["sizeU"] = int(math.Ceil((height / 1000) / RACKUNIT))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion CLI/models/attributes_device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestSetDeviceSizeUFromTemplateWorks(t *testing.T) {
}
err := models.SetDeviceSizeUFromTemplate(deviceAttrs, input, any(10000))
assert.Nil(t, err)
assert.Equal(t, 224, deviceAttrs["sizeU"])
assert.Equal(t, 225, deviceAttrs["sizeU"])
}

// endregion
Loading