Skip to content

Commit 1f21e41

Browse files
Add unit tests for alertspb ToProto and ParseTemplates (#7353)
* Add unit tests for alertspb ToProto and ParseTemplates Signed-off-by: Artem Muterko <artem@sopho.tech> * test: fix gofmt alignment in struct fields Signed-off-by: Artem Muterko <artem@sopho.tech> --------- Signed-off-by: Artem Muterko <artem@sopho.tech>
1 parent a1d8127 commit 1f21e41

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package alertspb
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestToProto(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
cfg string
13+
templates map[string]string
14+
user string
15+
}{
16+
{
17+
name: "empty config and no templates",
18+
cfg: "",
19+
templates: nil,
20+
user: "user-1",
21+
},
22+
{
23+
name: "config with templates",
24+
cfg: "route:\n receiver: default",
25+
templates: map[string]string{
26+
"slack.tmpl": "{{ define \"slack\" }}alert{{ end }}",
27+
},
28+
user: "user-2",
29+
},
30+
{
31+
name: "empty user",
32+
cfg: "global: {}",
33+
templates: nil,
34+
user: "",
35+
},
36+
}
37+
38+
for _, tt := range tests {
39+
t.Run(tt.name, func(t *testing.T) {
40+
result := ToProto(tt.cfg, tt.templates, tt.user)
41+
42+
assert.Equal(t, tt.user, result.User)
43+
assert.Equal(t, tt.cfg, result.RawConfig)
44+
45+
if tt.templates == nil {
46+
assert.Empty(t, result.Templates)
47+
} else {
48+
assert.Len(t, result.Templates, len(tt.templates))
49+
for _, tmpl := range result.Templates {
50+
expectedBody, ok := tt.templates[tmpl.Filename]
51+
assert.True(t, ok, "unexpected template filename: %s", tmpl.Filename)
52+
assert.Equal(t, expectedBody, tmpl.Body)
53+
}
54+
}
55+
})
56+
}
57+
}
58+
59+
func TestParseTemplates(t *testing.T) {
60+
tests := []struct {
61+
name string
62+
cfg AlertConfigDesc
63+
expected map[string]string
64+
}{
65+
{
66+
name: "no templates",
67+
cfg: AlertConfigDesc{Templates: nil},
68+
expected: map[string]string{},
69+
},
70+
{
71+
name: "single template",
72+
cfg: AlertConfigDesc{
73+
Templates: []*TemplateDesc{
74+
{Filename: "slack.tmpl", Body: "{{ define \"slack\" }}msg{{ end }}"},
75+
},
76+
},
77+
expected: map[string]string{
78+
"slack.tmpl": "{{ define \"slack\" }}msg{{ end }}",
79+
},
80+
},
81+
{
82+
name: "multiple templates",
83+
cfg: AlertConfigDesc{
84+
Templates: []*TemplateDesc{
85+
{Filename: "a.tmpl", Body: "body-a"},
86+
{Filename: "b.tmpl", Body: "body-b"},
87+
},
88+
},
89+
expected: map[string]string{
90+
"a.tmpl": "body-a",
91+
"b.tmpl": "body-b",
92+
},
93+
},
94+
}
95+
96+
for _, tt := range tests {
97+
t.Run(tt.name, func(t *testing.T) {
98+
result := ParseTemplates(tt.cfg)
99+
assert.Equal(t, tt.expected, result)
100+
})
101+
}
102+
}
103+
104+
func TestToProto_ParseTemplates_roundtrip(t *testing.T) {
105+
templates := map[string]string{
106+
"email.tmpl": "{{ define \"email\" }}hello{{ end }}",
107+
"slack.tmpl": "{{ define \"slack\" }}alert{{ end }}",
108+
}
109+
cfg := "route:\n receiver: default"
110+
user := "test-user"
111+
112+
proto := ToProto(cfg, templates, user)
113+
result := ParseTemplates(proto)
114+
115+
assert.Equal(t, templates, result)
116+
assert.Equal(t, cfg, proto.RawConfig)
117+
assert.Equal(t, user, proto.User)
118+
}

0 commit comments

Comments
 (0)