Files
kopia/internal/metricid/id_mapping_test.go
Jarek Kowalski c2cc25c504 feat(repository): added internal/metricid package (#2626)
This manages mapping of metric names to IDs which allows efficient JSON
representation of counter values for each set of metrics where
only values are in the index order.

This will be used in the telemetry protocol and for storing counters in
the repository.

Added test that ensures all metrics registered in a repository have
the corresponding mapping.
2022-12-05 04:41:42 +00:00

55 lines
1.2 KiB
Go

package metricid_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/kopia/kopia/internal/metricid"
)
func TestMapToSlice(t *testing.T) {
m := metricid.NewMapping(map[string]int{
"a": 1,
"b": 2,
"d": 4,
})
cases := []struct {
input map[string]string
want []string
}{
{nil, []string{"", "", "", ""}},
{map[string]string{"a": "foo", "b": "bar"}, []string{"foo", "bar", "", ""}},
{map[string]string{"a": "foo", "b": "bar", "d": "baz"}, []string{"foo", "bar", "", "baz"}},
// 'c' is dropped since it's not in the mapping
{map[string]string{"a": "foo", "b": "bar", "c": "qux", "d": "baz"}, []string{"foo", "bar", "", "baz"}},
}
for _, tc := range cases {
require.Equal(t, tc.want, metricid.MapToSlice(m, tc.input))
}
}
func TestSliceToMap(t *testing.T) {
m := metricid.NewMapping(map[string]int{
"a": 1,
"b": 2,
"c": 3,
})
cases := []struct {
input []int
want map[string]int
}{
{[]int{3}, map[string]int{"a": 3}},
{[]int{3, 4, 5}, map[string]int{"a": 3, "b": 4, "c": 5}},
{[]int{3, 4, 5, 6, 7}, map[string]int{"a": 3, "b": 4, "c": 5}},
}
for _, tc := range cases {
require.Equal(t, tc.want, metricid.SliceToMap(m, tc.input))
}
}