package gather import ( "encoding/json" "testing" "github.com/google/go-cmp/cmp" dto "github.com/prometheus/client_model/go" ) func TestMetrics(t *testing.T) { cases := []struct { name string ms []Metrics }{ { name: "empty", ms: make([]Metrics, 0), }, { name: "single", ms: []Metrics{ { Timestamp: 12345, Tags: map[string]string{ "b": "B", "a": "A", "c": "C", }, Fields: map[string]interface{}{ "x": 12.3, "y": "a long string", }, Type: dto.MetricType_SUMMARY, }, }, }, { name: "multiple", ms: []Metrics{ { Timestamp: 12345, Tags: map[string]string{ "b": "B", "a": "A", "c": "C", }, Fields: map[string]interface{}{ "x": 12.3, "y": "a long string", }, Type: dto.MetricType_SUMMARY, }, { Timestamp: 12345, Tags: map[string]string{ "b": "B2", "a": "A2", "c": "C2", }, Fields: map[string]interface{}{ "x": 12.5, "y": "a long string2", }, Type: dto.MetricType_GAUGE, }, }, }, } for _, c := range cases { b, err := json.Marshal(c.ms) if err != nil { t.Fatalf("error in marshaling metrics: %v", err) } result := make([]Metrics, 0) err = json.Unmarshal(b, &result) if err != nil { t.Fatalf("error in unmarshaling metrics: b: %s, %v", string(b), err) } if diff := cmp.Diff(c.ms, result, nil); diff != "" { t.Fatalf("unmarshaling metrics is incorrect, want %v, got %v", c.ms, result) } } }