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