-
Notifications
You must be signed in to change notification settings - Fork 2
/
metric_test.go
129 lines (122 loc) · 2.72 KB
/
metric_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
package snitch_test
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/xjewer/snitch"
"github.com/xjewer/snitch/lib/config"
)
func Test_makeMetrics(t *testing.T) {
const Prefix = "test"
type testCase struct {
value []config.Key
err error
expectation []*snitch.Metric
}
cases := []testCase{
{
value: []config.Key{
{Key: "key1.$1.$5", Count: true, Timing: "$2", Delimiter: "| "},
},
err: nil,
expectation: []*snitch.Metric{
snitch.NewMetric([]snitch.KeyPath{
snitch.NewKeyPath("test", 0, false),
snitch.NewKeyPath("key1", 0, false),
snitch.NewKeyPath("", 1, true),
snitch.NewKeyPath("", 5, true),
}, true,
true,
2,
"| ",
),
},
},
{
value: []config.Key{
{Key: "key2.path.test", Count: false, Timing: "$1", Delimiter: " "},
},
err: nil,
expectation: []*snitch.Metric{
snitch.NewMetric([]snitch.KeyPath{
snitch.NewKeyPath("test", 0, false),
snitch.NewKeyPath("key2", 0, false),
snitch.NewKeyPath("path", 0, false),
snitch.NewKeyPath("test", 0, false),
}, false,
true,
1,
" ",
),
},
},
{
value: []config.Key{
{Key: "key2.path.test", Count: false, Timing: "t", Delimiter: " "},
},
err: snitch.ErrEmptyVarName,
expectation: []*snitch.Metric{},
},
{
value: []config.Key{
{Key: "--$d", Count: true, Delimiter: " -- "},
},
err: nil,
expectation: []*snitch.Metric{
snitch.NewMetric([]snitch.KeyPath{
snitch.NewKeyPath("test", 0, false),
snitch.NewKeyPath("--$d", 0, false),
}, true,
false,
0,
" -- ",
),
},
},
{
value: []config.Key{
{Key: "key2.path.$y", Count: true, Delimiter: ", "},
},
err: errors.New("strconv.Atoi: parsing \"y\": invalid syntax"),
expectation: []*snitch.Metric{},
},
}
for i, tc := range cases {
t.Run(fmt.Sprint(i), func(t *testing.T) {
a := assert.New(t)
result, err := snitch.MakeMetrics(tc.value, Prefix)
a.Equal(tc.expectation, result)
if tc.err != nil {
a.EqualError(tc.err, err.Error())
} else {
a.Nil(err)
}
})
}
}
func Test_getVarName(t *testing.T) {
type testCase struct {
value string
err error
expectation int
}
cases := []testCase{
{"$1", nil, 1},
{"$", snitch.ErrEmptyVarName, 0},
{"", snitch.ErrEmptyVarName, 0},
{"$rr", errors.New("strconv.Atoi: parsing \"rr\": invalid syntax"), 0},
}
for i, tc := range cases {
t.Run(fmt.Sprint(i), func(t *testing.T) {
a := assert.New(t)
result, err := snitch.GetVarName(tc.value)
a.Equal(tc.expectation, result)
if tc.err != nil {
a.EqualError(tc.err, err.Error())
} else {
a.Nil(err)
}
})
}
}