Skip to content

Commit

Permalink
fix tests for metrics_api_scaler; add new tests for value_by_path; fi…
Browse files Browse the repository at this point in the history
…x []interface edge case for value_by_path

Signed-off-by: Friedrich Albert Kyuri <[email protected]>
  • Loading branch information
Friedrich Albert Kyuri committed Feb 15, 2024
1 parent b3c9f14 commit 611a92b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
20 changes: 8 additions & 12 deletions pkg/scalers/metrics_api_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,32 +128,28 @@ func TestGetValueFromResponse(t *testing.T) {
input []byte
key string
format APIFormat
expectVal interface{}
expectVal float64
expectErr bool
}{
{name: "integer", input: []byte(`{"components":[{"id": "82328e93e", "tasks": 32, "str": "64", "k":"1k","wrong":"NaN"}],"count":2.43}`), key: "count", format: JSONFormat, expectVal: 2.43},
{name: "string", input: []byte(`{"components":[{"id": "82328e93e", "tasks": 32, "str": "64", "k":"1k","wrong":"NaN"}],"count":2.43}`), key: "components.0.str", format: JSONFormat, expectVal: 64},
{name: "{}.[].{}", input: []byte(`{"components":[{"id": "82328e93e", "tasks": 32, "str": "64", "k":"1k","wrong":"NaN"}],"count":2.43}`), key: "components.0.tasks", format: JSONFormat, expectVal: 32},
{name: "invalid data", input: []byte(`{"components":[{"id": "82328e93e", "tasks": 32, "str": "64", "k":"1k","wrong":"NaN"}],"count":2.43}`), key: "components.0.wrong", format: JSONFormat, expectErr: true},
{name: "integer", input: []byte(`components: [{id: "82328e93e", tasks: 32, str: "64", k: "1k", wrong: "NaN"}] count: 2.43`), key: "count", format: YAMLFormat, expectVal: 2.43},
{name: "string", input: []byte(`components: [{id: "82328e93e", tasks: 32, str: "64", k: "1k", wrong: "NaN"}] count: 2.43`), key: "components.0.str", format: YAMLFormat, expectVal: 64},
{name: "{}.[].{}", input: []byte(`components: [{id: "82328e93e", tasks: 32, str: "64", k: "1k", wrong: "NaN"}] count: 2.43`), key: "components.0.tasks", format: YAMLFormat, expectVal: 32},
{name: "invalid data", input: []byte(`components: [{id: "82328e93e", tasks: 32, str: "64", k: "1k", wrong: "NaN"}] count: 2.43`), key: "components.0.wrong", format: YAMLFormat, expectErr: true},
{name: "integer", input: []byte(`{components: [{id: 82328e93e, tasks: 32, str: '64', k: 1k, wrong: NaN}], count: 2.43}`), key: "count", format: YAMLFormat, expectVal: 2.43},
{name: "string", input: []byte(`{components: [{id: 82328e93e, tasks: 32, str: '64', k: 1k, wrong: NaN}], count: 2.43}`), key: "components.0.str", format: YAMLFormat, expectVal: 64},
{name: "{}.[].{}", input: []byte(`{components: [{id: 82328e93e, tasks: 32, str: '64', k: 1k, wrong: NaN}], count: 2.43}`), key: "components.0.tasks", format: YAMLFormat, expectVal: 32},
{name: "invalid data", input: []byte(`{components: [{id: 82328e93e, tasks: 32, str: '64', k: 1k, wrong: NaN}], count: 2.43}`), key: "components.0.wrong", format: YAMLFormat, expectErr: true},
}

for _, tc := range testCases {
t.Run(string(tc.format)+": "+tc.name, func(t *testing.T) {
v, err := GetValueFromResponse(tc.input, tc.key, tc.format)

if tc.expectErr && err == nil {
t.Error("Expected error but got success")
} else if !tc.expectErr && err != nil {
t.Error("Expected success but got error:", err)
if tc.expectErr {
assert.Error(t, err)
}

if !tc.expectErr && v != tc.expectVal {
t.Errorf("Expected %v, got %v", tc.expectVal, v)
}
assert.EqualValues(t, tc.expectVal, v)
})
}
}
Expand Down
12 changes: 9 additions & 3 deletions pkg/util/value_by_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@ func GetValueByPath(data map[string]interface{}, path string) (interface{}, erro
return nil, fmt.Errorf("key '%s' not found in path '%s'", key, path)
}

switch v := val.(type) {
switch typedValue := val.(type) {
case map[interface{}]interface{}:
// Convert map[interface{}]interface{} to map[string]interface{}
current = make(map[string]interface{})
for k, v := range v {
for k, v := range typedValue {
current[fmt.Sprintf("%v", k)] = v
}
case []interface{}:
// Convert map[interface{}]interface{} to map[string]interface{}
current = make(map[string]interface{})
for k, v := range typedValue {
current[fmt.Sprintf("%v", k)] = v
}
case map[string]interface{}:
current = v
current = typedValue
default:
// Reached the final value
return val, nil
Expand Down
11 changes: 11 additions & 0 deletions pkg/util/value_by_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ func TestGetValueByPath(t *testing.T) {
expected: nil,
wantErr: true,
},
{
name: "Interface slice",
input: map[string]interface{}{
"some": []interface{}{
1, 2, 3,
},
},
path: "some.0",
expected: 1,
wantErr: false,
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 611a92b

Please sign in to comment.