feat: add more test cases

This commit is contained in:
tonysken 2026-07-24 20:16:06 +01:00
parent 4120c207ed
commit 612d34eb00
No known key found for this signature in database
GPG key ID: 67D2B9EB6131A6CE

View file

@ -19,10 +19,8 @@ func TestConvertBigMetricFromSchema(t *testing.T) {
} }
var schema map[string]interface{} var schema map[string]interface{}
err := json.Unmarshal([]byte(SCHEMA_JSON), &schema) if err := json.Unmarshal([]byte(SCHEMA_JSON), &schema); err != nil {
if err != nil { fmt.Println("Error unmarshaling SCHEMA_JSON:", err)
fmt.Println("Error:", err)
return
} }
scenarios := []scenario{ scenarios := []scenario{
@ -76,49 +74,115 @@ func TestConvertBigMetricFromSchema(t *testing.T) {
}, },
}, },
}, },
// { {
// name: "unknown schema type is a no-op", name: "direct string schema: converts int64 bytes at path",
// data: map[string]interface{}{ data: map[string]interface{}{
// "limit": int64(2048), "limit": int64(2048),
// }, },
// path: ".limit", path: ".limit",
// schema: 42, schema: "bytes",
// expected: map[string]interface{}{ expected: map[string]interface{}{
// "limit": int64(2048), "limit": "2.000 KB",
// }, },
// }, },
// { {
// name: "string schema: errors when value at path is not int64", name: "direct string schema: converts int64 nanoseconds at path",
// data: map[string]interface{}{ data: map[string]interface{}{
// "limit": "not a number", "cpu": int64(1500000),
// }, },
// path: ".limit", path: ".cpu",
// schema: "bytes", schema: "nanoseconds",
// expected: map[string]interface{}{"limit": "not a number"}, expected: map[string]interface{}{
// expectedErr: "Can't convert string to int64", "cpu": "1.500 ms",
// }, },
// { },
// name: "[]string schema: errors when value at path is not []int64", {
// data: map[string]interface{}{ name: "direct string schema: keeps small bytes value without unit suffix",
// "percpu_usage": []string{"a", "b"}, data: map[string]interface{}{
// }, "limit": int64(500),
// path: ".percpu_usage", },
// schema: []string{"nanoseconds", "nanoseconds"}, path: ".limit",
// expected: map[string]interface{}{"percpu_usage": []string{"a", "b"}}, schema: "bytes",
// expectedErr: "Can't convert []string to []int64", expected: map[string]interface{}{
// }, "limit": "500",
// { },
// name: "string schema: errors when path does not exist in data", },
// data: map[string]interface{}{}, {
// path: ".missing", name: "direct []string schema: converts []int64 at path",
// schema: "bytes", data: map[string]interface{}{
// expected: map[string]interface{}{}, "percpu_usage": []int64{1500000, 2500000000},
// expectedErr: "Unable to find the key", },
// }, path: ".percpu_usage",
schema: []string{"nanoseconds", "nanoseconds"},
expected: map[string]interface{}{
"percpu_usage": []string{"1.500 ms", "2.500 s"},
},
},
{
name: "map schema: recurses into single-key nested map",
data: map[string]interface{}{
"outer": map[string]interface{}{
"inner": int64(4096),
},
},
path: "",
schema: map[string]interface{}{
"outer": map[string]interface{}{
"inner": "bytes",
},
},
expected: map[string]interface{}{
"outer": map[string]interface{}{
"inner": "4.000 KB",
},
},
},
{
name: "unknown schema type is a no-op",
data: map[string]interface{}{
"limit": int64(2048),
},
path: ".limit",
schema: 42,
expected: map[string]interface{}{
"limit": int64(2048),
},
},
{
name: "string schema: errors when value at path is not int64",
data: map[string]interface{}{
"limit": "not a number",
},
path: ".limit",
schema: "bytes",
expected: map[string]interface{}{"limit": "not a number"},
expectedErr: "Can't convert string to int64",
},
{
name: "[]string schema: errors when value at path is not []int64",
data: map[string]interface{}{
"percpu_usage": []string{"a", "b"},
},
path: ".percpu_usage",
schema: []string{"nanoseconds", "nanoseconds"},
expected: map[string]interface{}{"percpu_usage": []string{"a", "b"}},
expectedErr: "Can't convert []string to []int64",
},
{
name: "string schema: errors when path does not exist in data",
data: map[string]interface{}{},
path: ".missing",
schema: "bytes",
expected: map[string]interface{}{},
expectedErr: "Unable to find the key",
},
} }
for _, s := range scenarios { for _, s := range scenarios {
t.Run(s.name, func(t *testing.T) { t.Run(s.name, func(t *testing.T) {
if m, ok := s.schema.(map[string]interface{}); ok && m == nil {
t.Skip("scenario depends on SCHEMA_JSON which failed to parse")
}
data := s.data data := s.data
err := convertBigMetricFromSchema(&data, s.path, s.schema) err := convertBigMetricFromSchema(&data, s.path, s.schema)
if s.expectedErr != "" { if s.expectedErr != "" {