| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package funcapi |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/stretchr/testify/assert" |
| 9 | "github.com/stretchr/testify/require" |
| 10 | ) |
| 11 | |
| 12 | func TestColumnBuildColumn_AllFields(t *testing.T) { |
| 13 | maxVal := 99.5 |
| 14 | col := Column{ |
| 15 | Index: 3, |
| 16 | Name: "CPU", |
| 17 | Type: FieldTypeInteger, |
| 18 | Units: "ms", |
| 19 | Visualization: FieldVisualBar, |
| 20 | Sort: FieldSortDescending, |
| 21 | Sortable: true, |
| 22 | Sticky: true, |
| 23 | Summary: FieldSummarySum, |
| 24 | Filter: FieldFilterRange, |
| 25 | FullWidth: true, |
| 26 | Wrap: true, |
| 27 | DefaultExpandedFilter: true, |
| 28 | UniqueKey: true, |
| 29 | Visible: true, |
| 30 | Max: &maxVal, |
| 31 | PointerTo: "details", |
| 32 | Dummy: true, |
| 33 | ValueOptions: ValueOptions{ |
| 34 | Transform: FieldTransformDuration, |
| 35 | DecimalPoints: 2, |
| 36 | DefaultValue: 0, |
| 37 | }, |
| 38 | } |
| 39 | |
| 40 | result := col.BuildColumn() |
| 41 | |
| 42 | assert.Equal(t, 3, result["index"]) |
| 43 | assert.Equal(t, "CPU", result["name"]) |
| 44 | assert.Equal(t, "integer", result["type"]) |
| 45 | assert.Equal(t, "bar", result["visualization"]) |
| 46 | assert.Equal(t, "descending", result["sort"]) |
| 47 | assert.Equal(t, true, result["sortable"]) |
| 48 | assert.Equal(t, true, result["sticky"]) |
| 49 | assert.Equal(t, "sum", result["summary"]) |
| 50 | assert.Equal(t, "range", result["filter"]) |
| 51 | assert.Equal(t, true, result["full_width"]) |
| 52 | assert.Equal(t, true, result["wrap"]) |
| 53 | assert.Equal(t, true, result["default_expanded_filter"]) |
| 54 | assert.Equal(t, true, result["unique_key"]) |
| 55 | assert.Equal(t, true, result["visible"]) |
| 56 | assert.Equal(t, "ms", result["units"]) |
| 57 | assert.Equal(t, maxVal, result["max"]) |
| 58 | assert.Equal(t, "details", result["pointer_to"]) |
| 59 | assert.Equal(t, true, result["dummy"]) |
| 60 | |
| 61 | valueOpts, ok := result["value_options"].(map[string]any) |
| 62 | require.True(t, ok, "value_options should be a map") |
| 63 | assert.Equal(t, "duration", valueOpts["transform"]) |
| 64 | assert.Equal(t, 2, valueOpts["decimal_points"]) |
| 65 | assert.Equal(t, 0, valueOpts["default_value"]) |
| 66 | assert.Equal(t, "ms", valueOpts["units"]) |
| 67 | } |
| 68 | |
| 69 | func TestColumnBuildColumn_NoOptionalFields(t *testing.T) { |
| 70 | col := Column{ |
| 71 | Index: 0, |
| 72 | Name: "Query", |
| 73 | Type: FieldTypeString, |
| 74 | ValueOptions: ValueOptions{ |
| 75 | Transform: FieldTransformNone, |
| 76 | }, |
| 77 | } |
| 78 | |
| 79 | result := col.BuildColumn() |
| 80 | |
| 81 | _, hasUnits := result["units"] |
| 82 | _, hasMax := result["max"] |
| 83 | _, hasPointer := result["pointer_to"] |
| 84 | _, hasDummy := result["dummy"] |
| 85 | assert.False(t, hasUnits, "units should be omitted when empty") |
| 86 | assert.False(t, hasMax, "max should be omitted when nil") |
| 87 | assert.False(t, hasPointer, "pointer_to should be omitted when empty") |
| 88 | assert.False(t, hasDummy, "dummy should be omitted when false") |
| 89 | |
| 90 | valueOpts, ok := result["value_options"].(map[string]any) |
| 91 | require.True(t, ok, "value_options should be a map") |
| 92 | _, hasValueUnits := valueOpts["units"] |
| 93 | assert.False(t, hasValueUnits, "value_options.units should be omitted when Units empty") |
| 94 | } |