forked from thomaspoignant/go-feature-flag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_test.go
182 lines (175 loc) · 5.33 KB
/
common_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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package exporter_test
import (
"fmt"
"os"
"testing"
"text/template"
"github.com/stretchr/testify/assert"
"github.com/thomaspoignant/go-feature-flag/exporter"
)
func Test_ParseTemplate(t *testing.T) {
type args struct {
template string
defaultTemplate string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Invalid template",
args: args{
template: "{{ .Kind}};{{ .ContextKind}};{{ .UserKey}};{{ .CreationDate}};{{ .Key}};{{ .Variation}};" +
"{{ .Value}};{{ .Default}",
defaultTemplate: "{{ .Kind}};{{ .ContextKind}};{{ .UserKey}};{{ .CreationDate}};{{ .Key}};{{ .Variation}};" +
"{{ .Value}};{{ .Default}}\n",
},
wantErr: true,
},
{
name: "Valid template",
args: args{
template: "{{ .Kind}};{{ .ContextKind}};{{ .UserKey}};{{ .CreationDate}};{{ .Key}};{{ .Variation}};",
defaultTemplate: "{{ .Kind}};{{ .ContextKind}};{{ .UserKey}};{{ .CreationDate}};{{ .Key}};{{ .Variation}};" +
"{{ .Value}};{{ .Default}}\n",
},
wantErr: false,
},
{
name: "empty template use default",
args: args{
template: "",
defaultTemplate: "{{ .Kind}};{{ .ContextKind}};{{ .UserKey}};{{ .CreationDate}};{{ .Key}};{{ .Variation}};" +
"{{ .Value}};{{ .Default}}\n",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defaultT, _ := template.New("random-name").Parse(tt.args.defaultTemplate)
assert.NotPanics(t, func() { exporter.ParseTemplate("random-name", tt.args.template, tt.args.defaultTemplate) })
got := exporter.ParseTemplate("random-name", tt.args.template, tt.args.defaultTemplate)
if tt.wantErr {
assert.Equal(t, defaultT, got, "If template invalid we should use default template")
return
}
assert.NotEqual(t, defaultT, got, "We should not have the same template")
})
}
}
func TestComputeFilename(t *testing.T) {
hostname, _ := os.Hostname()
type args struct {
template *template.Template
format string
}
tests := []struct {
name string
args args
want string
wantErr assert.ErrorAssertionFunc
}{
{
name: "Nothing to template",
args: args{
template: exporter.ParseTemplate("filenameFormat", "flag-variation", "flag-variation"),
format: "json",
},
want: "flag-variation",
wantErr: assert.NoError,
},
{
name: "With extension",
args: args{
template: exporter.ParseTemplate("filenameFormat", "flag-variation.{{ .Format }}", "flag-variation"),
format: "json",
},
want: "flag-variation.json",
wantErr: assert.NoError,
},
{
name: "Multiple templates",
args: args{
template: exporter.ParseTemplate("filenameFormat", "flag-variation-{{ .Hostname}}.{{ .Format}}", "flag-variation"),
format: "json",
},
want: "flag-variation-" + hostname + ".json",
wantErr: assert.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := exporter.ComputeFilename(tt.args.template, tt.args.format)
if !tt.wantErr(t, err, fmt.Sprintf("ComputeFilename(%v, %v)", tt.args.template, tt.args.format)) {
return
}
assert.Equalf(t, tt.want, got, "ComputeFilename(%v, %v)", tt.args.template, tt.args.format)
})
}
}
func TestFormatEventInCSV(t *testing.T) {
type args struct {
csvTemplate *template.Template
event exporter.FeatureEvent
}
tests := []struct {
name string
args args
want string
wantErr assert.ErrorAssertionFunc
}{
{
name: "valid",
args: args{
csvTemplate: exporter.ParseTemplate("exporterExample", exporter.DefaultCsvTemplate, exporter.DefaultCsvTemplate),
event: exporter.FeatureEvent{
Kind: "feature", ContextKind: "anonymousUser", UserKey: "ABCD", CreationDate: 1617970547, Key: "random-key",
Variation: "Default", Value: "YO", Default: false, Source: "SERVER",
},
},
want: "feature;anonymousUser;ABCD;1617970547;random-key;Default;YO;false;SERVER\n",
wantErr: assert.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := exporter.FormatEventInCSV(tt.args.csvTemplate, tt.args.event)
if !tt.wantErr(t, err, fmt.Sprintf("FormatEventInCSV(%v, %v)", tt.args.csvTemplate, tt.args.event)) {
return
}
assert.Equalf(t, tt.want, string(got), "FormatEventInCSV(%v, %v)", tt.args.csvTemplate, tt.args.event)
})
}
}
func TestFormatEventInJSON(t *testing.T) {
type args struct {
event exporter.FeatureEvent
}
tests := []struct {
name string
args args
want string
wantErr assert.ErrorAssertionFunc
}{
{
name: "valid",
args: args{event: exporter.FeatureEvent{
Kind: "feature", ContextKind: "anonymousUser", UserKey: "ABCD", CreationDate: 1617970547, Key: "random-key",
Variation: "Default", Value: "YO", Default: false, Source: "SERVER",
}},
want: "{\"kind\":\"feature\",\"contextKind\":\"anonymousUser\",\"userKey\":\"ABCD\",\"creationDate\":1617970547,\"key\":\"random-key\",\"variation\":\"Default\",\"value\":\"YO\",\"default\":false,\"version\":\"\",\"source\":\"SERVER\"}\n",
wantErr: assert.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := exporter.FormatEventInJSON(tt.args.event)
if !tt.wantErr(t, err, fmt.Sprintf("FormatEventInJSON(%v)", tt.args.event)) {
return
}
assert.Equalf(t, tt.want, string(got), "FormatEventInJSON(%v)", tt.args.event)
})
}
}