| 153 | } |
| 154 | |
| 155 | func TestSampleJSON(t *testing.T) { |
| 156 | input := []struct { |
| 157 | plain string |
| 158 | value Sample |
| 159 | }{ |
| 160 | { |
| 161 | plain: `{"metric":{"__name__":"test_metric"},"value":[1234.567,"123.1"]}`, |
| 162 | value: Sample{ |
| 163 | Metric: Metric{ |
| 164 | MetricNameLabel: "test_metric", |
| 165 | }, |
| 166 | Value: 123.1, |
| 167 | Timestamp: 1234567, |
| 168 | }, |
| 169 | }, |
| 170 | } |
| 171 | |
| 172 | for _, test := range input { |
| 173 | b, err := json.Marshal(test.value) |
| 174 | if err != nil { |
| 175 | t.Error(err) |
| 176 | continue |
| 177 | } |
| 178 | |
| 179 | if string(b) != test.plain { |
| 180 | t.Errorf("encoding error: expected %q, got %q", test.plain, b) |
| 181 | continue |
| 182 | } |
| 183 | |
| 184 | var sv Sample |
| 185 | err = json.Unmarshal(b, &sv) |
| 186 | if err != nil { |
| 187 | t.Error(err) |
| 188 | continue |
| 189 | } |
| 190 | |
| 191 | if !reflect.DeepEqual(sv, test.value) { |
| 192 | t.Errorf("decoding error: expected %v, got %v", test.value, sv) |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | func TestVectorJSON(t *testing.T) { |
| 198 | input := []struct { |