MCPcopy
hub / github.com/grafana/dskit / TestLimitsMap_SetAndString

Function TestLimitsMap_SetAndString

flagext/map_test.go:76–158  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

74}
75
76func TestLimitsMap_SetAndString(t *testing.T) {
77 t.Run("numeric", func(t *testing.T) {
78 tc := map[string]struct {
79 input string
80 expected map[string]float64
81 error string
82 }{
83
84 "set without error": {
85 input: `{"key1":10,"key2":20}`,
86 expected: map[string]float64{"key1": 10, "key2": 20},
87 },
88 "set with parsing error": {
89 input: `{"key1": 10, "key2": 20`,
90 error: "unexpected end of JSON input",
91 },
92 "set with validation error": {
93 input: `{"key1": -10, "key2": 20}`,
94 error: "value cannot be negative",
95 },
96 "set with incompatible value type": {
97 input: `{"key1": "abc", "key2": "def"}`,
98 error: "json: cannot unmarshal string into Go value of type float64",
99 },
100 }
101
102 for name, tt := range tc {
103 t.Run("numeric/"+name, func(t *testing.T) {
104 lm := NewLimitsMap(fakeFloat64Validator)
105 err := lm.Set(tt.input)
106 if tt.error != "" {
107 require.Error(t, err)
108 require.Equal(t, tt.error, err.Error())
109 } else {
110 require.NoError(t, err)
111 require.Equal(t, tt.expected, lm.Read())
112 require.Equal(t, tt.input, lm.String())
113 }
114 })
115 }
116 })
117
118 t.Run("string", func(t *testing.T) {
119 tc := map[string]struct {
120 input string
121 expected map[string]string
122 error string
123 }{
124
125 "set without error": {
126 input: `{"key1":"abc","key2":"def"}`,
127 expected: map[string]string{"key1": "abc", "key2": "def"},
128 },
129 "set with parsing error": {
130 input: `{"key1": "abc", "key2": "def`,
131 error: "unexpected end of JSON input",
132 },
133 "set with validation error": {

Callers

nothing calls this directly

Calls 7

NewLimitsMapFunction · 0.85
RunMethod · 0.80
SetMethod · 0.65
ReadMethod · 0.65
StringMethod · 0.65
ErrorMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected