|
14 | 14 | package model |
15 | 15 |
|
16 | 16 | import ( |
| 17 | + "errors" |
| 18 | + "strings" |
17 | 19 | "testing" |
18 | 20 |
|
19 | 21 | "github.com/google/go-cmp/cmp" |
20 | 22 | "github.com/google/go-cmp/cmp/cmpopts" |
21 | 23 | dto "github.com/prometheus/client_model/go" |
| 24 | + "github.com/stretchr/testify/require" |
22 | 25 | "google.golang.org/protobuf/proto" |
| 26 | + "gopkg.in/yaml.v2" |
23 | 27 | ) |
24 | 28 |
|
25 | 29 | func testMetric(t testing.TB) { |
@@ -89,6 +93,115 @@ func BenchmarkMetric(b *testing.B) { |
89 | 93 | } |
90 | 94 | } |
91 | 95 |
|
| 96 | +func TestValidationScheme(t *testing.T) { |
| 97 | + var scheme ValidationScheme |
| 98 | + require.Equal(t, UnsetValidation, scheme) |
| 99 | +} |
| 100 | + |
| 101 | +func TestValidationScheme_String(t *testing.T) { |
| 102 | + for _, tc := range []struct { |
| 103 | + name string |
| 104 | + scheme ValidationScheme |
| 105 | + want string |
| 106 | + }{ |
| 107 | + { |
| 108 | + name: "Unset", |
| 109 | + scheme: UnsetValidation, |
| 110 | + want: `unset`, |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "Legacy", |
| 114 | + scheme: LegacyValidation, |
| 115 | + want: "legacy", |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "UTF8", |
| 119 | + scheme: UTF8Validation, |
| 120 | + want: "utf8", |
| 121 | + }, |
| 122 | + } { |
| 123 | + t.Run(tc.name, func(t *testing.T) { |
| 124 | + require.Equal(t, tc.want, tc.scheme.String()) |
| 125 | + }) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func TestValidationScheme_MarshalYAML(t *testing.T) { |
| 130 | + for _, tc := range []struct { |
| 131 | + name string |
| 132 | + scheme ValidationScheme |
| 133 | + want string |
| 134 | + }{ |
| 135 | + { |
| 136 | + name: "Unset", |
| 137 | + scheme: UnsetValidation, |
| 138 | + want: `""`, |
| 139 | + }, |
| 140 | + { |
| 141 | + name: "Legacy", |
| 142 | + scheme: LegacyValidation, |
| 143 | + want: "legacy", |
| 144 | + }, |
| 145 | + { |
| 146 | + name: "UTF8", |
| 147 | + scheme: UTF8Validation, |
| 148 | + want: "utf8", |
| 149 | + }, |
| 150 | + } { |
| 151 | + t.Run(tc.name, func(t *testing.T) { |
| 152 | + marshaled, err := yaml.Marshal(tc.scheme) |
| 153 | + require.NoError(t, err) |
| 154 | + require.Equal(t, tc.want, strings.TrimSpace(string(marshaled))) |
| 155 | + }) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +func TestValidationScheme_UnmarshalYAML(t *testing.T) { |
| 160 | + for _, tc := range []struct { |
| 161 | + name string |
| 162 | + input string |
| 163 | + want ValidationScheme |
| 164 | + wantError error |
| 165 | + }{ |
| 166 | + { |
| 167 | + name: "Unset empty input", |
| 168 | + input: ``, |
| 169 | + want: UnsetValidation, |
| 170 | + }, |
| 171 | + { |
| 172 | + name: "Unset quoted input", |
| 173 | + input: `""`, |
| 174 | + want: UnsetValidation, |
| 175 | + }, |
| 176 | + { |
| 177 | + name: "Legacy", |
| 178 | + input: `legacy`, |
| 179 | + want: LegacyValidation, |
| 180 | + }, |
| 181 | + { |
| 182 | + name: "UTF8", |
| 183 | + input: `utf8`, |
| 184 | + want: UTF8Validation, |
| 185 | + }, |
| 186 | + { |
| 187 | + name: "Invalid", |
| 188 | + input: `invalid`, |
| 189 | + wantError: errors.New(`unrecognized ValidationScheme: "invalid"`), |
| 190 | + }, |
| 191 | + } { |
| 192 | + t.Run(tc.name, func(t *testing.T) { |
| 193 | + scheme := UnsetValidation |
| 194 | + err := yaml.Unmarshal([]byte(tc.input), &scheme) |
| 195 | + if tc.wantError == nil { |
| 196 | + require.NoError(t, err) |
| 197 | + require.Equal(t, tc.want, scheme) |
| 198 | + } else { |
| 199 | + require.EqualError(t, err, tc.wantError.Error()) |
| 200 | + } |
| 201 | + }) |
| 202 | + } |
| 203 | +} |
| 204 | + |
92 | 205 | func TestMetricNameIsLegacyValid(t *testing.T) { |
93 | 206 | scenarios := []struct { |
94 | 207 | mn LabelValue |
|
0 commit comments