-
Notifications
You must be signed in to change notification settings - Fork 0
/
chart_test.go
175 lines (168 loc) Β· 7.17 KB
/
chart_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
package gobarchar
import (
"net/http"
"strings"
"testing"
)
func TestGenerateBarChart(t *testing.T) {
testCases := []struct {
name string
queryParams string
expected string
}{
{
name: "No 'sort' parameter",
queryParams: "A=10&B=20&C=15",
expected: strings.TrimSpace(`
A 10 βββββββββββββ
B 20 βββββββββββββββββββββββββ
C 15 βββββββββββββββββββ
Avg. 15 βββββββββββββββββββ
Total 45 βββββββββββββββββββββββββ
`),
},
{
name: "Sort by ascending",
queryParams: "A=10&B=20&C=15&sort=asc",
expected: strings.TrimSpace(`
A 10 βββββββββββββ
C 15 βββββββββββββββββββ
B 20 βββββββββββββββββββββββββ
Avg. 15 βββββββββββββββββββ
Total 45 βββββββββββββββββββββββββ
`),
},
{
name: "Sort by descending",
queryParams: "A=10&B=20&C=15&sort=desc",
expected: strings.TrimSpace(`
B 20 βββββββββββββββββββββββββ
C 15 βββββββββββββββββββ
A 10 βββββββββββββ
Avg. 15 βββββββββββββββββββ
Total 45 βββββββββββββββββββββββββ
`),
},
{
name: "Invalid value for 'sort' parameter",
queryParams: "A=10&B=20&C=15&sort=invalid",
expected: strings.TrimSpace(`
A 10 βββββββββββββ
B 20 βββββββββββββββββββββββββ
C 15 βββββββββββββββββββ
Avg. 15 βββββββββββββββββββ
Total 45 βββββββββββββββββββββββββ
`),
},
{
name: "Several input values without 'sort' parameter",
queryParams: "2012=8&2013=6&2014=8&2015=14&2016=8&2017=6&2018=0&2019=24&2020=17&2021=21&2022=17&2023=13",
expected: strings.TrimSpace(`
2012 8 βββββββββ
2013 6 βββββββ
2014 8 βββββββββ
2015 14 βββββββββββββββ
2016 8 βββββββββ
2017 6 βββββββ
2018 0 β
2019 24 βββββββββββββββββββββββββ
2020 17 ββββββββββββββββββ
2021 21 ββββββββββββββββββββββ
2022 17 ββββββββββββββββββ
2023 13 ββββββββββββββ
Avg. 11.83 βββββββββββββ
Total 142 βββββββββββββββββββββββββ
`),
},
{
name: "Keep HTML entity '%20' when no 'spaces' parameter",
queryParams: "Year%202024=10&Year%202023=8",
expected: strings.TrimSpace(`
Year%202024 10 βββββββββββββββββββββββββ
Year%202023 8 ββββββββββββββββββββ
Avg. 9 βββββββββββββββββββββββ
Total 18 βββββββββββββββββββββββββ
`),
},
{
name: "Replace HTML entity '%20' with spaces",
queryParams: "Year%202024=10&Year%202023=8&spaces=yes",
expected: strings.TrimSpace(`
Year 2024 10 βββββββββββββββββββββββββ
Year 2023 8 ββββββββββββββββββββ
Avg. 9 βββββββββββββββββββββββ
Total 18 βββββββββββββββββββββββββ
`),
},
{
name: "Invalid value for 'spaces' parameter",
queryParams: "Year%202024=10&Year%202023=8&spaces=invalid",
expected: strings.TrimSpace(`
Year%202024 10 βββββββββββββββββββββββββ
Year%202023 8 ββββββββββββββββββββ
Avg. 9 βββββββββββββββββββββββ
Total 18 βββββββββββββββββββββββββ
`),
},
{
name: "Add 'title' parameter with literal spaces",
queryParams: "A=10&B=20&C=15&title=A descriptive title",
expected: strings.TrimSpace(`
A descriptive title
A 10 βββββββββββββ
B 20 βββββββββββββββββββββββββ
C 15 βββββββββββββββββββ
Avg. 15 βββββββββββββββββββ
Total 45 βββββββββββββββββββββββββ
`),
},
{
name: "Add 'title' parameter with HTML entity spaces",
queryParams: "A=10&B=20&C=15&title=A%20descriptive%20title",
expected: strings.TrimSpace(`
A descriptive title
A 10 βββββββββββββ
B 20 βββββββββββββββββββββββββ
C 15 βββββββββββββββββββ
Avg. 15 βββββββββββββββββββ
Total 45 βββββββββββββββββββββββββ
`),
},
{
name: "Add 'title' parameter as first parameter",
queryParams: "title=A descriptive title&A=10&B=20&C=15",
expected: strings.TrimSpace(`
A descriptive title
A 10 βββββββββββββ
B 20 βββββββββββββββββββββββββ
C 15 βββββββββββββββββββ
Avg. 15 βββββββββββββββββββ
Total 45 βββββββββββββββββββββββββ
`),
},
{
name: "Add 'title' parameter with other parameters",
queryParams: "A=10&B=20&C=15&sort=desc&title=A descriptive title",
expected: strings.TrimSpace(`
A descriptive title
B 20 βββββββββββββββββββββββββ
C 15 βββββββββββββββββββ
A 10 βββββββββββββ
Avg. 15 βββββββββββββββββββ
Total 45 βββββββββββββββββββββββββ
`),
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
r, err := http.NewRequest("GET", "/?"+testCase.queryParams, nil)
if err != nil {
t.Fatal(err)
}
actual := strings.TrimSpace(createBarChart(r))
if actual != testCase.expected {
t.Errorf("Unexpected output:\nExpected:\n%s\nGot:\n%s", testCase.expected, actual)
}
})
}
}