github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/util/conv_test.go (about) 1 package util 2 3 import ( 4 "reflect" 5 "testing" 6 "time" 7 8 "github.com/prometheus/common/model" 9 ) 10 11 func TestRoundToMilliseconds(t *testing.T) { 12 tests := []struct { 13 name string 14 from time.Time 15 through time.Time 16 wantFrom model.Time 17 wantThrough model.Time 18 }{ 19 { 20 "0", 21 time.Unix(0, 0), 22 time.Unix(0, 1), 23 model.Time(0), 24 model.Time(1), 25 }, 26 { 27 "equal", 28 time.Unix(0, time.Millisecond.Nanoseconds()), 29 time.Unix(0, time.Millisecond.Nanoseconds()), 30 model.Time(1), 31 model.Time(1), 32 }, 33 { 34 "exact", 35 time.Unix(0, time.Millisecond.Nanoseconds()), 36 time.Unix(0, 2*time.Millisecond.Nanoseconds()), 37 model.Time(1), 38 model.Time(2), 39 }, 40 { 41 "rounding", 42 time.Unix(0, time.Millisecond.Nanoseconds()+10), 43 time.Unix(0, 2*time.Millisecond.Nanoseconds()+10), 44 model.Time(1), 45 model.Time(3), 46 }, 47 { 48 "rounding large number in nanoseconds", 49 time.Unix(0, 1643958368442000064), 50 time.Unix(0, 1643958368443000064), 51 model.Time(1643958368442), 52 model.Time(1643958368444), 53 }, 54 { 55 "already rounded large number in nanoseconds", 56 time.Unix(0, 1643958368442000000), 57 time.Unix(0, 1643958368443000000), 58 model.Time(1643958368442), 59 model.Time(1643958368443), 60 }, 61 } 62 for _, tt := range tests { 63 t.Run(tt.name, func(t *testing.T) { 64 from, through := RoundToMilliseconds(tt.from, tt.through) 65 if !reflect.DeepEqual(from, tt.wantFrom) { 66 t.Errorf("RoundToMilliseconds() from = %v, want %v", from, tt.wantFrom) 67 } 68 if !reflect.DeepEqual(through, tt.wantThrough) { 69 t.Errorf("RoundToMilliseconds() through = %v, want %v", through, tt.wantThrough) 70 } 71 }) 72 } 73 } 74 75 func TestModelLabelSetToMap(t *testing.T) { 76 77 tests := []struct { 78 name string 79 m model.LabelSet 80 want map[string]string 81 }{ 82 { 83 "nil", 84 nil, 85 map[string]string{}, 86 }, 87 { 88 "one", 89 model.LabelSet{model.LabelName("foo"): model.LabelValue("bar")}, 90 map[string]string{"foo": "bar"}, 91 }, 92 { 93 "two", 94 model.LabelSet{ 95 model.LabelName("foo"): model.LabelValue("bar"), 96 model.LabelName("buzz"): model.LabelValue("fuzz"), 97 }, 98 map[string]string{ 99 "foo": "bar", 100 "buzz": "fuzz", 101 }, 102 }, 103 } 104 for _, tt := range tests { 105 t.Run(tt.name, func(t *testing.T) { 106 if got := ModelLabelSetToMap(tt.m); !reflect.DeepEqual(got, tt.want) { 107 t.Errorf("ModelLabelSetToMap() = %v, want %v", got, tt.want) 108 } 109 }) 110 } 111 } 112 113 func TestMapToModelLabelSet(t *testing.T) { 114 tests := []struct { 115 name string 116 args map[string]string 117 want model.LabelSet 118 }{ 119 {"nil", nil, model.LabelSet{}}, 120 { 121 "one", 122 map[string]string{"foo": "bar"}, 123 model.LabelSet{model.LabelName("foo"): model.LabelValue("bar")}, 124 }, 125 { 126 "two", 127 map[string]string{ 128 "foo": "bar", 129 "buzz": "fuzz", 130 }, 131 model.LabelSet{ 132 model.LabelName("foo"): model.LabelValue("bar"), 133 model.LabelName("buzz"): model.LabelValue("fuzz"), 134 }, 135 }, 136 } 137 for _, tt := range tests { 138 t.Run(tt.name, func(t *testing.T) { 139 if got := MapToModelLabelSet(tt.args); !reflect.DeepEqual(got, tt.want) { 140 t.Errorf("MapToModelLabelSet() = %v, want %v", got, tt.want) 141 } 142 }) 143 } 144 }