forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenergy_metrics_test.go
155 lines (146 loc) Β· 3.14 KB
/
energy_metrics_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
package core
import (
"testing"
)
func isEqualFloat64(a, b *float64) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
return *a == *b
}
func TestEnergyMetrics(t *testing.T) {
f := func(f float64) *float64 { return &f }
type tcStep = struct {
kWh, greenShare float64
effPrice, effCo2 *float64
}
tc := []struct {
title string
steps []tcStep
totalWh, solarPercentage float64
price, pricePerKWh, co2PerKWh *float64
}{
{
"initial state",
[]tcStep{},
0, 0, nil, nil, nil,
},
{
"energy value",
[]tcStep{
{0.1, 0, nil, nil},
{0.2, 0, nil, nil},
},
200, 0, nil, nil, nil,
},
{
"ignore lower energy value",
[]tcStep{
{0.2, 0, nil, nil},
{0.1, 0, nil, nil},
},
200, 0, nil, nil, nil,
},
{
"half solar",
[]tcStep{
{0.1, 1, nil, nil},
{0.2, 0, nil, nil},
},
200, 50, nil, nil, nil,
},
{
"only solar",
[]tcStep{
{0.1, 1, nil, nil},
{0.2, 1, nil, nil},
},
200, 100, nil, nil, nil,
},
{
"static price",
[]tcStep{
{1, 0, f(0.5), nil},
},
1000, 0, f(0.5), f(0.5), nil,
},
{
"dynamic price",
[]tcStep{
{1, 0, f(1), nil},
{2, 0, f(0), nil},
},
2000, 0, f(1), f(0.5), nil,
},
{
"dynamic price",
[]tcStep{
{2, 0, f(1), nil},
{4, 0, f(0), nil},
},
4000, 0, f(2), f(0.5), nil,
},
{
"static co2",
[]tcStep{
{1, 0, nil, f(500)},
},
1000, 0, nil, nil, f(500),
},
{
"dynamic co2",
[]tcStep{
{1, 0, nil, f(1000)},
{2, 0, nil, f(0)},
},
2000, 0, nil, nil, f(500),
},
{
"grid only, half, full solar, half, grid only",
[]tcStep{
{1, 0, f(2), f(200)},
{2, 0.5, f(1), f(50)},
{3, 1, f(0), f(0)},
{4, 0.5, f(1), f(50)},
{5, 0, f(2), f(200)},
},
5000, 40, f(6), f(1.2), f(100),
},
}
for _, tc := range tc {
s := NewEnergyMetrics()
for _, tc := range tc.steps {
s.SetEnvironment(tc.greenShare, tc.effPrice, tc.effCo2)
s.Update(tc.kWh)
}
if s.TotalWh() != tc.totalWh {
t.Errorf("%s: TotalWh was incorrect, got: %.3f, want: %.3f.", tc.title, s.TotalWh(), tc.totalWh)
}
if s.SolarPercentage() != tc.solarPercentage {
t.Errorf("%s: SolarPercentage was incorrect, got: %.3f, want: %.3f.", tc.title, s.SolarPercentage(), tc.solarPercentage)
}
price := s.Price()
if !isEqualFloat64(price, tc.price) {
t.Errorf("%s: Price was incorrect, got: %v, want: %v.", tc.title, *price, *tc.price)
}
pricePerKWh := s.PricePerKWh()
if !isEqualFloat64(pricePerKWh, tc.pricePerKWh) {
t.Errorf("%s: PricePerKWh was incorrect, got: %v, want: %v.", tc.title, *pricePerKWh, *tc.pricePerKWh)
}
co2PerKWh := s.Co2PerKWh()
if !isEqualFloat64(co2PerKWh, tc.co2PerKWh) {
t.Errorf("%s: Co2PerKWh was incorrect, got: %v, want: %v.", tc.title, *co2PerKWh, *tc.co2PerKWh)
}
}
// reset
s := NewEnergyMetrics()
s.SetEnvironment(1, f(1), f(1))
s.Update(1)
s.Reset()
if s.TotalWh() != 0 || s.SolarPercentage() != 0 || s.Co2PerKWh() != nil || s.Price() != nil || s.PricePerKWh() != nil {
t.Errorf("Metrics not properly reset %+v", s)
}
}