-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
128 lines (115 loc) · 2.76 KB
/
config_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
package main
import (
"errors"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)
var testConfig = config{LocalizedTimezones: []string{
"Pacific/Auckland",
"Pacific/Chatham",
}}
var testConfigYaml, _ = yaml.Marshal(testConfig)
func TestLoadConfig(t *testing.T) {
c, err := loadConfig("config_test.yaml")
if assert.NoError(t, err) {
require.IsType(t, config{}, c)
require.EqualValues(
t, config{
LocalizedTimezones: []string{
"Pacific/Auckland",
},
TimeOfUse: []timeOfUse{
{
Name: "electricity_price",
Description: "Electricity price",
Timezone: "Pacific/Auckland",
Labels: map[string]string{"provider": "Power Co"},
DefaultValue: 0.1106,
TimeWindows: []timeWindow{{
Value: 0.2423,
Start: "07:00",
End: "21:00",
startHour: 7,
endHour: 21,
}},
},
}}, c,
)
}
}
func TestConfigWatcher(t *testing.T) {
f, err := os.CreateTemp("", "config_test.*.yaml")
if err != nil {
t.Fatal(err)
}
defer os.Remove(f.Name())
go configWatcher(f.Name())
time.Sleep(10 * time.Millisecond) // Give the watcher time to start
assert.Equal(t, config{}, liveConfig)
err = os.WriteFile(f.Name(), testConfigYaml, 0644)
if err != nil {
t.Fatal(err)
}
time.Sleep(10 * time.Millisecond) // Give the watcher time to update
assert.Equal(t, testConfig, liveConfig)
}
func TestConfigInit(t *testing.T) {
f, err := os.CreateTemp("", "")
if err != nil {
t.Fatal(err)
}
defer os.Remove(f.Name())
os.WriteFile(f.Name(), testConfigYaml, 0644)
t.Setenv("CONFIG_FILE", f.Name())
configInit()
assert.Equal(t, testConfig, liveConfig)
}
func TestparseWindowTimes(t *testing.T) {
testCases := map[string]struct {
input string
expectedH int
expectedM int
err error
}{
"standard": {
input: "13:00",
expectedH: 13,
expectedM: 0,
err: nil,
},
"multiple units": {
input: "15:30",
expectedH: 15,
expectedM: 30,
err: nil,
},
"unsupported second resolution": {
input: "15:30:00",
expectedH: 0,
expectedM: 0,
err: errors.New(`Invalid time format. Must be hh:mm. Got: "15:30:00"`),
},
"duration": {
input: "19h13m",
expectedH: 0,
expectedM: 0,
err: errors.New(`Invalid time format. Must be hh:mm. Got: "19h13m"`),
},
"empty string": {
input: "",
expectedH: 0,
expectedM: 0,
err: errors.New(`Invalid time format. Must be hh:mm. Got: ""`),
},
}
for name, tc := range testCases {
actualH, actualM, err := parseWindowTimes(tc.input)
assert.Equal(t, tc.expectedH, actualH, name)
assert.Equal(t, tc.expectedM, actualM, name)
assert.Equal(t, tc.err, err, name)
}
}