Skip to content

Commit

Permalink
fix(cli) sizeU as int & correct value
Browse files Browse the repository at this point in the history
  • Loading branch information
Cedrok committed Jul 26, 2024
1 parent 60fa4fa commit f18b075
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
18 changes: 13 additions & 5 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 @@ -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
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

0 comments on commit f18b075

Please sign in to comment.