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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
package config
import (
"os"
"path/filepath"
"strings"
"testing"
)
func write(t *testing.T, body string) string {
t.Helper()
path := filepath.Join(t.TempDir(), "config")
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
return path
}
func TestLoadMissingFileKeepsDefaults(t *testing.T) {
cfg, err := Load(filepath.Join(t.TempDir(), "absent"))
if err != nil {
t.Fatalf("missing file should not be an error: %v", err)
}
if cfg.Hours != 12 || cfg.Icons != "nerd" {
t.Fatalf("defaults not returned: %+v", cfg)
}
}
func TestLoadOverridesOnlyWhatIsSet(t *testing.T) {
cfg, err := Load(write(t, "hours=24\nicons=emoji\n"))
if err != nil {
t.Fatal(err)
}
if cfg.Hours != 24 {
t.Errorf("hours = %d, want 24", cfg.Hours)
}
if cfg.Icons != "emoji" {
t.Errorf("icons = %q, want emoji", cfg.Icons)
}
// Untouched keys must keep their defaults.
if cfg.GraphHeight != 5 || !cfg.Graph {
t.Errorf("unset keys lost their defaults: %+v", cfg)
}
}
func TestLoadCommentsAndBlanks(t *testing.T) {
cfg, err := Load(write(t, "\n# a comment\n\nhours=6 # trailing comment\n"))
if err != nil {
t.Fatal(err)
}
if cfg.Hours != 6 {
t.Fatalf("hours = %d, want 6 (trailing comment must be stripped)", cfg.Hours)
}
}
func TestLoadErrors(t *testing.T) {
for name, body := range map[string]string{
"no equals": "hours 12\n",
"unknown key": "colour=always\n",
"not a number": "hours=soon\n",
"not a bool": "graph=maybe\n",
} {
t.Run(name, func(t *testing.T) {
if _, err := Load(write(t, body)); err == nil {
t.Fatalf("expected an error for %q", body)
}
})
}
}
func TestPollenAllAndNone(t *testing.T) {
cfg, _ := Load(write(t, "pollen=all\n"))
if len(cfg.Pollen) != len(AllSpecies) {
t.Errorf("pollen=all gave %v", cfg.Pollen)
}
cfg, _ = Load(write(t, "pollen=none\n"))
if len(cfg.Pollen) != 0 {
t.Errorf("pollen=none gave %v", cfg.Pollen)
}
}
func TestValidateNamesTheOffender(t *testing.T) {
cfg := Default()
cfg.Columns = []string{"hour", "tempature"}
err := cfg.Validate()
if err == nil {
t.Fatal("expected an error for an unknown column")
}
if !strings.Contains(err.Error(), "tempature") {
t.Errorf("error must name the offending column, got: %v", err)
}
if !strings.Contains(err.Error(), "conditions") {
t.Errorf("error must list valid columns, got: %v", err)
}
}
func TestValidateRejectsBadEnums(t *testing.T) {
for name, mutate := range map[string]func(*Config){
"icons": func(c *Config) { c.Icons = "pictures" },
"color": func(c *Config) { c.Color = "sometimes" },
"units": func(c *Config) { c.Units = "furlongs" },
"hours": func(c *Config) { c.Hours = 0 },
"graph_height": func(c *Config) { c.GraphHeight = 1 },
"pollen": func(c *Config) { c.Pollen = []string{"oak"} },
} {
t.Run(name, func(t *testing.T) {
cfg := Default()
mutate(&cfg)
if err := cfg.Validate(); err == nil {
t.Fatalf("expected %s to be rejected", name)
}
})
}
}
func TestDefaultIsValid(t *testing.T) {
if err := Default().Validate(); err != nil {
t.Fatalf("the built-in default must be valid: %v", err)
}
}
func TestFieldsRequestsOnlySelectedColumns(t *testing.T) {
cfg := Default()
cfg.Columns = []string{"hour", "temp", "wind"}
got := strings.Join(cfg.Fields(), ",")
want := "temperature_2m,wind_speed_10m"
if got != want {
t.Fatalf("Fields() = %q, want %q", got, want)
}
}
func TestFieldsDeduplicates(t *testing.T) {
cfg := Default()
// icon and conditions both come from weather_code.
cfg.Columns = []string{"icon", "conditions"}
if got := cfg.Fields(); len(got) != 1 || got[0] != "weather_code" {
t.Fatalf("Fields() = %v, want one weather_code", got)
}
}
func TestWriteDefaultRoundTrips(t *testing.T) {
path := filepath.Join(t.TempDir(), "sub", "config")
want := Default()
want.Location = "Krakow"
if err := WriteDefault(path, want); err != nil {
t.Fatal(err)
}
got, err := Load(path)
if err != nil {
t.Fatalf("the file we generate must parse: %v", err)
}
if got.Location != "Krakow" || got.Hours != want.Hours || got.Icons != want.Icons {
t.Fatalf("round trip changed values:\n got %+v\nwant %+v", got, want)
}
if err := got.Validate(); err != nil {
t.Fatalf("the file we generate must validate: %v", err)
}
}
func TestCustomColumnDeclaration(t *testing.T) {
cfg, err := Load(write(t, `columns=hour,temp,birch
column.birch = air:birch_pollen
label.birch = brzoza
width.birch = 7
decimals.birch = 2
suffix.birch = g
`))
if err != nil {
t.Fatal(err)
}
cc, ok := cfg.Custom["birch"]
if !ok {
t.Fatal("birch was not declared")
}
if cc.Source != "air" || cc.Field != "birch_pollen" {
t.Errorf("source/field = %q/%q", cc.Source, cc.Field)
}
if cc.Label != "brzoza" || cc.Width != 7 || cc.Decimals != 2 || cc.Suffix != "g" {
t.Errorf("attributes not parsed: %+v", cc)
}
if err := cfg.Validate(); err != nil {
t.Fatalf("a complete declaration must validate: %v", err)
}
}
// Only the fields a selected column needs, split by which API serves them.
func TestCustomColumnsSplitFieldsByApi(t *testing.T) {
cfg, err := Load(write(t, `columns=hour,temp,birch,soil
column.birch = air:birch_pollen
column.soil = forecast:soil_temperature_0cm
`))
if err != nil {
t.Fatal(err)
}
fc := strings.Join(cfg.Fields(), ",")
if !strings.Contains(fc, "soil_temperature_0cm") || !strings.Contains(fc, "temperature_2m") {
t.Errorf("forecast fields = %q", fc)
}
if strings.Contains(fc, "birch_pollen") {
t.Errorf("an air field must not be asked of the forecast API: %q", fc)
}
if air := strings.Join(cfg.AirFields(), ","); air != "birch_pollen" {
t.Errorf("air fields = %q, want birch_pollen", air)
}
}
// No custom air column means no second request at all.
func TestNoAirFieldsWhenNoneDeclared(t *testing.T) {
if got := Default().AirFields(); len(got) != 0 {
t.Fatalf("AirFields() = %v, want empty", got)
}
}
func TestCustomColumnErrors(t *testing.T) {
for name, body := range map[string]string{
"no source": "column.x = birch_pollen\ncolumns=hour,x\n",
"unknown source": "column.x = weather:birch_pollen\ncolumns=hour,x\n",
"empty field": "column.x = air:\ncolumns=hour,x\n",
"bad width": "column.x = air:f\nwidth.x = wide\n",
"bad decimals": "column.x = air:f\ndecimals.x = 9\n",
} {
t.Run(name, func(t *testing.T) {
if _, err := Load(write(t, body)); err == nil {
t.Fatalf("expected an error for %q", body)
}
})
}
}
// Attributes without a declaration are a typo, not a silent no-op.
func TestAttributesWithoutDeclarationAreRejected(t *testing.T) {
cfg, err := Load(write(t, "label.birch = brzoza\n"))
if err != nil {
t.Fatal(err)
}
if err := cfg.Validate(); err == nil {
t.Fatal("label.birch without column.birch must be an error")
}
}
// Shadowing a built-in would make which column you get depend on lookup order.
func TestCustomColumnCannotShadowABuiltIn(t *testing.T) {
cfg, err := Load(write(t, "column.temp = air:birch_pollen\n"))
if err != nil {
t.Fatal(err)
}
err = cfg.Validate()
if err == nil || !strings.Contains(err.Error(), "built-in") {
t.Fatalf("expected a built-in clash error, got %v", err)
}
}
// An undeclared column name should say how to declare it.
func TestUnknownColumnSuggestsDeclaringIt(t *testing.T) {
cfg := Default()
cfg.Columns = []string{"hour", "birch"}
err := cfg.Validate()
if err == nil || !strings.Contains(err.Error(), "column.birch") {
t.Fatalf("error should show how to declare it, got %v", err)
}
}
func TestPollenExplicitTracksWhoChose(t *testing.T) {
cases := map[string]bool{
"pollen=grass,birch\n": true,
"pollen=all\n": false,
"pollen=none\n": false,
}
for body, want := range cases {
cfg, err := Load(write(t, body))
if err != nil {
t.Fatal(err)
}
if cfg.PollenExplicit != want {
t.Errorf("%q gave PollenExplicit=%v, want %v", body, cfg.PollenExplicit, want)
}
}
if Default().PollenExplicit {
t.Error("the default is not an explicit choice")
}
}
|