forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsavings_test.go
226 lines (196 loc) Β· 4.9 KB
/
savings_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
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
package core
import (
"math"
"testing"
"time"
"github.com/benbjohnson/clock"
)
func assertEnergy(t *testing.T, s *Savings, total, self, percentage float64) {
if !compareWithTolerane(s.TotalCharged(), total) {
t.Errorf("TotalCharged was incorrect, got: %.3f, want: %.3f.", s.TotalCharged(), total)
}
if !compareWithTolerane(s.selfConsumptionCharged, self) {
t.Errorf("ChargedSelfConsumption was incorrect, got: %.3f, want: %.3f.", s.selfConsumptionCharged, self)
}
if int(s.SelfConsumptionPercent()) != int(percentage) {
t.Errorf("SelfConsumptionPercent was incorrect, got: %.1f, want: %.1f.", s.SelfConsumptionPercent(), percentage)
}
}
func assertPrices(t *testing.T, s *Savings, effectivePrice, savingsAmount float64) {
if !compareWithTolerane(s.EffectivePrice(), effectivePrice) {
t.Errorf("EffectivePrice was incorrect, got: %.3f, want: %.3f.", s.EffectivePrice(), effectivePrice)
}
if !compareWithTolerane(s.SavingsAmount(), savingsAmount) {
t.Errorf("SavingsAmount was incorrect, got: %.3f, want: %.3f.", s.SavingsAmount(), savingsAmount)
}
}
func compareWithTolerane(a, b float64) bool {
tolerance := 0.001
diff := math.Abs(a - b)
return diff < tolerance
}
type StubPublisher struct{}
func (p StubPublisher) publish(key string, val interface{}) {}
func TestSavingsWithChangingEnergySources(t *testing.T) {
p := StubPublisher{}
clck := clock.NewMock()
s := &Savings{
clock: clck,
started: clck.Now(),
updated: clck.Now(),
}
tc := []struct {
title string
grid, pv, battery, charge float64
total, self, percentage float64
}{
{"half grid, half pv",
2500, 2500, 0, 5000,
5, 2.5, 50},
{"full pv",
0, 5000, 0, 5000,
10, 7.5, 75},
{"full grid",
5000, 0, 0, 5000,
15, 7.5, 50},
{"half grid, half battery",
2500, 0, 2500, 5000,
20, 10, 50},
{"full pv, pv export",
-5000, 10000, 0, 5000,
25, 15, 60},
{"full pv, pv export, battery charge",
-2500, 10000, -2500, 5000,
30, 20, 66},
{"double charge speed, full grid",
10000, 0, 0, 10000,
40, 20, 50},
}
s.Update(p, 0, 0, 0, 0)
for _, tc := range tc {
t.Logf("%+v", tc)
clck.Add(time.Hour)
s.Update(p, tc.grid, tc.pv, tc.battery, tc.charge)
assertEnergy(t, s, tc.total, tc.self, tc.percentage)
}
}
func TestSavingsWithDifferentTimespans(t *testing.T) {
p := StubPublisher{}
clck := clock.NewMock()
s := &Savings{
clock: clck,
started: clck.Now(),
updated: clck.Now(),
}
type tcStep = struct {
dt time.Duration
grid, pv, battery, charge float64
}
tc := []struct {
title string
steps []tcStep
total, self, percentage float64
}{
{"10 second not charging, full grid",
[]tcStep{
{10 * time.Second, 1000, 0, 0, 0},
},
0, 0, 0, // 0Wh
},
{"10 second 11kW charging, full grid",
[]tcStep{
{10 * time.Second, 0, 0, 0, 11000},
},
0.030556, 0, 0, // 30,555Wh
},
{"10 second 11kW charging, full grid",
[]tcStep{
{10 * time.Second, 0, 0, 0, 11000},
},
0.061111, 0, 0, // 61,111Wh
},
{"5x 2 second 11kW charging, full grid",
[]tcStep{
{2 * time.Second, 0, 0, 0, 11000},
{2 * time.Second, 0, 0, 0, 11000},
{2 * time.Second, 0, 0, 0, 11000},
{2 * time.Second, 0, 0, 0, 11000},
{2 * time.Second, 0, 0, 0, 11000},
},
0.092, 0, 0, // 91,666Wh
},
{"30 min 11kW charging, full grid",
[]tcStep{
{30 * time.Minute, 0, 0, 0, 11000},
},
5.592, 0, 0, // 5561,111Wh
},
{"4 hours 11kW charging, full pv",
[]tcStep{
{4 * time.Hour, 0, 11000, 0, 11000},
},
49.592, 44, 88,
},
}
s.Update(p, 0, 0, 0, 0)
for _, tc := range tc {
t.Logf("%+v", tc)
for _, tc := range tc.steps {
clck.Add(tc.dt)
s.Update(p, tc.grid, tc.pv, tc.battery, tc.charge)
}
assertEnergy(t, s, tc.total, tc.self, tc.percentage)
}
}
func TestEffectiveEnergyPriceAndSavingsAmount(t *testing.T) {
p := StubPublisher{}
clck := clock.NewMock()
type tcStep = struct {
dt time.Duration
grid, pv, battery, charge float64
}
tc := []struct {
title string
steps []tcStep
effectivePrice, savingsAmount float64
}{
{"1 hour, 10kW, full grid",
[]tcStep{
{time.Hour, 10000, 0, 0, 10000},
},
0.3, 0,
},
{"1 hour, 10kW, full pv",
[]tcStep{
{time.Hour, 0, 10000, 0, 10000},
},
0.08, 2.2,
},
{"1 hour, 10kW, full battery",
[]tcStep{
{time.Hour, 0, 0, 10000, 10000},
},
0.08, 2.2,
},
{"1 hour, 10kW, half grid, half pv",
[]tcStep{
{time.Hour, 5000, 0, 5000, 10000},
},
0.19, 1.1,
},
}
for _, tc := range tc {
t.Logf("%+v", tc)
s := &Savings{
clock: clck,
started: clck.Now(),
updated: clck.Now(),
}
s.Update(p, 0, 0, 0, 0)
for _, tc := range tc.steps {
clck.Add(tc.dt)
s.Update(p, tc.grid, tc.pv, tc.battery, tc.charge)
}
assertPrices(t, s, tc.effectivePrice, tc.savingsAmount)
}
}