forked from chenjiandongx/mandodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
label_test.go
164 lines (155 loc) · 3.06 KB
/
label_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package mandodb
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestLabels_String(t *testing.T) {
cases := []struct {
lables LabelSet
expected string
}{
{
lables: LabelSet{
{
Name: "t1",
Value: "t1",
},
{
Name: "t2",
Value: "t2",
},
},
expected: "{t1=\"t1\", t2=\"t2\"}",
},
{
lables: LabelSet{},
expected: "{}",
},
{
lables: nil,
expected: "{}",
},
}
for _, c := range cases {
str := c.lables.String()
require.Equal(t, c.expected, str)
}
}
func TestLabels_Has(t *testing.T) {
tests := []struct {
input string
expected bool
}{
{
input: "foo",
expected: false,
},
{
input: "aaa",
expected: true,
},
}
labelsSet := LabelSet{
{
Name: "aaa",
Value: "111",
},
{
Name: "bbb",
Value: "222",
},
}
for i, test := range tests {
got := labelsSet.Has(test.input)
require.Equal(t, test.expected, got, "unexpected comparison result for test case %d", i)
}
}
func TestLabels_Hash(t *testing.T) {
lbls := LabelSet{
{Name: "foo", Value: "bar"},
{Name: "baz", Value: "qux"},
}
require.Equal(t, lbls.Hash(), lbls.Hash())
require.NotEqual(t, lbls.Hash(), LabelSet{lbls[1], lbls[0]}.Hash(), "unordered labels match.")
require.NotEqual(t, lbls.Hash(), LabelSet{lbls[0]}.Hash(), "different labels match.")
}
func TestLabels_WithoutEmpty(t *testing.T) {
for _, test := range []struct {
input LabelSet
expected LabelSet
}{
{
input: LabelSet{
{Name: "foo"},
{Name: "bar"},
},
expected: LabelSet{},
},
{
input: LabelSet{
{Name: "foo"},
{Name: "bar"},
{Name: "baz"},
},
expected: LabelSet{},
},
{
input: LabelSet{
{Name: "__name__", Value: "test"},
{Name: "hostname", Value: "localhost"},
{Name: "job", Value: "check"},
},
expected: LabelSet{
{Name: "__name__", Value: "test"},
{Name: "hostname", Value: "localhost"},
{Name: "job", Value: "check"},
},
},
{
input: LabelSet{
{Name: "__name__", Value: "test"},
{Name: "hostname", Value: "localhost"},
{Name: "bar"},
{Name: "job", Value: "check"},
},
expected: LabelSet{
{Name: "__name__", Value: "test"},
{Name: "hostname", Value: "localhost"},
{Name: "job", Value: "check"},
},
},
{
input: LabelSet{
{Name: "__name__", Value: "test"},
{Name: "foo"},
{Name: "hostname", Value: "localhost"},
{Name: "bar"},
{Name: "job", Value: "check"},
},
expected: LabelSet{
{Name: "__name__", Value: "test"},
{Name: "hostname", Value: "localhost"},
{Name: "job", Value: "check"},
},
},
{
input: LabelSet{
{Name: "__name__", Value: "test"},
{Name: "foo"},
{Name: "baz"},
{Name: "hostname", Value: "localhost"},
{Name: "bar"},
{Name: "job", Value: "check"},
},
expected: LabelSet{
{Name: "__name__", Value: "test"},
{Name: "hostname", Value: "localhost"},
{Name: "job", Value: "check"},
},
},
} {
t.Run("", func(t *testing.T) {
require.Equal(t, test.expected, test.input.filter())
})
}
}