| 60 | } |
| 61 | |
| 62 | func Test_TimeSeriesV2ToLabels(t *testing.T) { |
| 63 | symbols := []string{"", "__name__", "test_metric", "job", "prometheus", "instance", "server-1"} |
| 64 | b := &labels.ScratchBuilder{} |
| 65 | |
| 66 | tests := []struct { |
| 67 | desc string |
| 68 | ts *TimeSeriesV2 |
| 69 | isErr bool |
| 70 | expectedLabels labels.Labels |
| 71 | }{ |
| 72 | { |
| 73 | desc: "Success", |
| 74 | ts: &TimeSeriesV2{ |
| 75 | LabelsRefs: []uint32{1, 2, 3, 4, 5, 6}, |
| 76 | }, |
| 77 | expectedLabels: labels.FromStrings("__name__", "test_metric", "job", "prometheus", "instance", "server-1"), |
| 78 | }, |
| 79 | { |
| 80 | desc: "Odd length of the label refs", |
| 81 | ts: &TimeSeriesV2{ |
| 82 | LabelsRefs: []uint32{1, 2, 3}, |
| 83 | }, |
| 84 | isErr: true, |
| 85 | }, |
| 86 | { |
| 87 | desc: "Label name refs out of ranges", |
| 88 | ts: &TimeSeriesV2{ |
| 89 | LabelsRefs: []uint32{1, 2, 10, 4}, |
| 90 | }, |
| 91 | isErr: true, |
| 92 | }, |
| 93 | { |
| 94 | desc: "Label value refs out of ranges", |
| 95 | ts: &TimeSeriesV2{ |
| 96 | LabelsRefs: []uint32{1, 2, 3, 10}, |
| 97 | }, |
| 98 | isErr: true, |
| 99 | }, |
| 100 | } |
| 101 | |
| 102 | for _, test := range tests { |
| 103 | t.Run(test.desc, func(t *testing.T) { |
| 104 | lbs, err := test.ts.ToLabels(b, symbols) |
| 105 | if test.isErr { |
| 106 | require.Error(t, err) |
| 107 | } else { |
| 108 | require.Equal(t, test.expectedLabels.String(), lbs.String()) |
| 109 | } |
| 110 | }) |
| 111 | } |
| 112 | } |