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