forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsite_test.go
145 lines (136 loc) Β· 3.24 KB
/
site_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
package core
import (
"testing"
"github.com/evcc-io/evcc/util"
)
func TestSitePower(t *testing.T) {
tc := []struct {
maxGrid, grid, battery, site float64
}{
{0, 0, 0, 0}, // silent night
{0, 0, 1, 1}, // battery discharging
{0, 0, -1, -1}, // battery charging -> negative result cannot occur in reality
{0, 1, 0, 1}, // grid import
{0, 1, 1, 2}, // grid import + battery discharging
{0, -1, 0, -1}, // grid export
{0, -1, -1, -2}, // grid export + battery charging
{0, 1, -1, 0}, // grid import + battery charging -> should not happen
{0.5, 1, -1, 1}, // grid import + DC battery charging
}
log := util.NewLogger("foo")
for _, tc := range tc {
res := sitePower(log, tc.maxGrid, tc.grid, tc.battery, 0)
if res != tc.site {
t.Errorf("sitePower wanted %.f, got %.f", tc.site, res)
}
}
}
func TestGreenShare(t *testing.T) {
tc := []struct {
title string
grid, pv, battery, home, lp float64
greenShareTotal, greenShareHome, greenShareLoadpoints float64
}{
{
"half grid, half pv, green home",
1000, 1000, 0, 1000, 1000,
0.5, 1, 0,
},
{
"half grid, half pv, no home",
1000, 1000, 0, 0, 2000,
0.5, 1, 0.5,
},
{
"half grid, half pv, no lp",
2500, 2500, 0, 5000, 0,
0.5, 0.5, 0,
},
{
"full pv",
0, 5000, 0, 1000, 4000,
1, 1, 1,
},
{
"full grid",
5000, 0, 0, 1000, 4000,
0, 0, 0,
},
{
"half grid, half battery, green home",
1000, 0, 1000, 1000, 1000,
0.5, 1, 0,
},
{
"half grid, half battery, no home",
1000, 0, 1000, 0, 2000,
0.5, 1, 0.5,
},
{
"half grid, half battery, no lp",
1000, 0, 1000, 2000, 0,
0.5, 0.5, 0,
},
{
"full pv, pv export",
-5000, 10000, 0, 1000, 4000,
1, 1, 1,
},
{
"full pv, pv export, no lp",
-5000, 10000, 0, 5000, 0,
1, 1, 1,
},
{
"full pv, pv export, battery charge",
-2500, 10000, -2500, 1000, 4000,
1, 1, 1,
},
{
"full grid, battery charge",
3000, 0, -1000, 1000, 1000,
0, 0, 0,
},
{
"full grid, battery charge, no lp",
2000, 0, -1000, 1000, 0,
0, 0, 0,
},
{
"half grid, half pv, battery charge, no lp",
1000, 1000, -1000, 1000, 0,
0.5, 1, 0,
},
{
"half grid, half pv, battery charge, home, lp",
1000, 1000, -1000, 500, 500,
0.5, 1, 0,
},
{
"pv ac limited, battery charge & grid import",
1000, 3000, -1000, 1000, 2000,
0.75, 1, 0.5,
},
}
for _, tc := range tc {
t.Logf(tc.title)
s := &Site{
gridPower: tc.grid,
pvPower: tc.pv,
batteryPower: tc.battery,
}
totalPower := tc.grid + tc.pv + max(0, tc.battery)
greenShareTotal := s.greenShare(0, totalPower)
if greenShareTotal != tc.greenShareTotal {
t.Errorf("greenShareTotal wanted %.3f, got %.3f", tc.greenShareTotal, greenShareTotal)
}
greenShareHome := s.greenShare(0, tc.home)
if greenShareHome != tc.greenShareHome {
t.Errorf("greenShareHome wanted %.3f, got %.3f", tc.greenShareHome, greenShareHome)
}
greenShareLoadpoints := s.greenShare(tc.home+max(0, -tc.battery), totalPower)
if greenShareLoadpoints != tc.greenShareLoadpoints {
t.Errorf("greenShareLoadpoints wanted %.3f, got %.3f", tc.greenShareLoadpoints, greenShareLoadpoints)
}
}
}