forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcc_test.go
441 lines (413 loc) Β· 16.1 KB
/
mcc_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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package charger
import (
"bytes"
"io"
"net/http"
"reflect"
"strings"
"testing"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
)
// HTTP testing appproach from http://hassansin.github.io/Unit-Testing-http-client-in-Go
type roundTripFunc func(req *http.Request) *http.Response
// RoundTrip .
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req), nil
}
// apiResponse helps to map an API Call to a test response
type apiResponse struct {
apiCall string
apiResponse string
}
// NewTestClient returns *http.Client with Transport replaced to avoid making real calls
func NewTestClient(fn roundTripFunc) *http.Client {
return &http.Client{
Transport: fn,
}
}
// NewTestMobileConnect .
func NewTestMobileConnect(t *testing.T, responses []apiResponse) *MobileConnect {
mcc := &MobileConnect{
Helper: request.NewHelper(util.NewLogger("foo")),
uri: "http://192.0.2.2:502",
password: "none",
token: "token",
tokenExpiry: time.Now().Add(10 * time.Minute),
}
mcc.Client = NewTestClient(func(req *http.Request) *http.Response {
// Each method may have multiple API calls, so we need to finde the proper
// response string for the currently invoked call
var responseString string
for _, s := range responses {
if strings.Contains("/"+s.apiCall, req.URL.Path) {
responseString = s.apiResponse
}
}
return &http.Response{
StatusCode: http.StatusOK,
// Send response to be tested
Body: io.NopCloser(bytes.NewBufferString(responseString)),
// Must be set to non-nil value or it panics
Header: make(http.Header),
}
})
return mcc
}
func TestMobileConnectLogin(t *testing.T) {
tests := []struct {
name string
responses []apiResponse
password string
wantErr bool
}{
// test cases for software version 2914
{"login - success", []apiResponse{{mccAPILogin, "{\n \"token\": \"1234567890._abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n}"}}, "password", false},
{"login - wrong password", []apiResponse{{mccAPILogin, "{\n \"error\": \"wrong password\"\n}"}}, "wrong", true},
{"login - bad return", []apiResponse{{mccAPILogin, "{{\n \"error\": \"wrong password\"\n}"}}, "wrong", true},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
mcc := NewTestMobileConnect(t, tc.responses)
if err := mcc.login(); (err != nil) != tc.wantErr {
t.Errorf("MobileConnect.login() error = %v, wantErr %v", err, tc.wantErr)
}
})
}
}
func TestMobileConnectRefresh(t *testing.T) {
tests := []struct {
name string
responses []apiResponse
wantErr bool
}{
// test cases for software version 2914
{"refresh - success", []apiResponse{{mccAPIRefresh, "{\n \"token\": \"1234567890._abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n}"}}, false},
{"refresh - wrong password", []apiResponse{{mccAPIRefresh, "{\n \"error\": \"signature mismatch: OP-gWPOgQ9fdKujMgRNHkeH4WHqYrHe3Z2RqVXeUEuw1\"\n}"}}, true},
{"refresh - bad return", []apiResponse{{mccAPIRefresh, "{{\n \"error\": \"\"\n}"}}, true},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
mcc := NewTestMobileConnect(t, tc.responses)
if err := mcc.refresh(); (err != nil) != tc.wantErr {
t.Errorf("MobileConnect.login() error = %v, wantErr %v", err, tc.wantErr)
}
})
}
}
func TestMobileConnectStatus(t *testing.T) {
tests := []struct {
name string
responses []apiResponse
want api.ChargeStatus
wantErr bool
}{
// test cases for software version 2914
{"home plug - Unexpected API response", []apiResponse{{mccAPIChargeState, "abc"}}, api.StatusNone, true},
{"home plug - Unplugged", []apiResponse{{mccAPIChargeState, "0\n"}}, api.StatusA, false},
{"home plug - Connecting", []apiResponse{{mccAPIChargeState, "1\n"}}, api.StatusB, false},
{"home plug - Error", []apiResponse{{mccAPIChargeState, "2\n"}}, api.StatusF, false},
{"home plug - Established", []apiResponse{{mccAPIChargeState, "3\n"}}, api.StatusB, false},
{"home plug - Paused", []apiResponse{{mccAPIChargeState, "4\n"}}, api.StatusB, false},
{"home plug - Active", []apiResponse{{mccAPIChargeState, "5\n"}}, api.StatusC, false},
{"home plug - Finished", []apiResponse{{mccAPIChargeState, "6\n"}}, api.StatusB, false},
{"home plug - Unexpected status value", []apiResponse{{mccAPIChargeState, "10\n"}}, api.StatusNone, true},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
mcc := NewTestMobileConnect(t, tc.responses)
got, err := mcc.Status()
if (err != nil) != tc.wantErr {
t.Errorf("MobileConnect.Status() error = %v, wantErr %v", err, tc.wantErr)
return
}
if !reflect.DeepEqual(got, tc.want) {
t.Errorf("MobileConnect.Status() = %v, want %v", got, tc.want)
}
})
}
}
func TestMobileConnectEnabled(t *testing.T) {
tests := []struct {
name string
responses []apiResponse
want bool
wantErr bool
}{
// test cases for software version 2914
{"home plug - Unexpected API response", []apiResponse{{mccAPIChargeState, "abc"}}, false, true},
{"home plug - Unplugged", []apiResponse{{mccAPIChargeState, "0\n"}}, false, false},
{"home plug - Connecting", []apiResponse{{mccAPIChargeState, "1\n"}}, false, false},
{"home plug - Error", []apiResponse{{mccAPIChargeState, "2\n"}}, false, false},
{"home plug - Established", []apiResponse{{mccAPIChargeState, "3\n"}}, false, false},
{"home plug - Paused", []apiResponse{{mccAPIChargeState, "4\n"}}, true, false},
{"home plug - Active", []apiResponse{{mccAPIChargeState, "5\n"}}, true, false},
{"home plug - Finished", []apiResponse{{mccAPIChargeState, "6\n"}}, true, false},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
mcc := NewTestMobileConnect(t, tc.responses)
got, err := mcc.Enabled()
if (err != nil) != tc.wantErr {
t.Errorf("MobileConnect.Enabled() error = %v, wantErr %v", err, tc.wantErr)
return
}
if got != tc.want {
t.Errorf("MobileConnect.Enabled() = %v, want %v", got, tc.want)
}
})
}
}
func TestMobileConnectMaxCurrent(t *testing.T) {
tests := []struct {
name string
responses []apiResponse
current int64
wantErr bool
}{
// test cases for software version 2914
{
"home plug - success min value",
[]apiResponse{
{mccAPICurrentCableInformation, "\"{\\\"carCable\\\":5,\\\"gridCable\\\":8,\\\"hwfpMaxLimit\\\":32,\\\"maxValue\\\":10,\\\"minValue\\\":6,\\\"value\\\":10}\""},
{mccAPISetCurrentLimit, "\"OK\"\n"},
},
6, false,
},
{
"home plug - success max value",
[]apiResponse{
{mccAPICurrentCableInformation, "\"{\\\"carCable\\\":5,\\\"gridCable\\\":8,\\\"hwfpMaxLimit\\\":32,\\\"maxValue\\\":10,\\\"minValue\\\":6,\\\"value\\\":10}\""},
{mccAPISetCurrentLimit, "\"OK\"\n"},
},
10, false,
},
{
"home plug - error value too small",
[]apiResponse{
{mccAPICurrentCableInformation, "\"{\\\"carCable\\\":5,\\\"gridCable\\\":8,\\\"hwfpMaxLimit\\\":32,\\\"maxValue\\\":10,\\\"minValue\\\":6,\\\"value\\\":10}\""},
},
0, true,
},
{
"home plug - error value too big",
[]apiResponse{
{mccAPICurrentCableInformation, "\"{\\\"carCable\\\":5,\\\"gridCable\\\":8,\\\"hwfpMaxLimit\\\":32,\\\"maxValue\\\":10,\\\"minValue\\\":6,\\\"value\\\":10}\""},
},
16, true,
},
{
"home plug - 1st API success but 2nd API error",
[]apiResponse{
{mccAPICurrentCableInformation, "\"{\\\"carCable\\\":5,\\\"gridCable\\\":8,\\\"hwfpMaxLimit\\\":32,\\\"maxValue\\\":10,\\\"minValue\\\":6,\\\"value\\\":10}\""},
{mccAPISetCurrentLimit, "Unexpected response"},
},
10, true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
mcc := NewTestMobileConnect(t, tc.responses)
if err := mcc.MaxCurrent(tc.current); (err != nil) != tc.wantErr {
t.Errorf("MobileConnect.MaxCurrent() error = %v, wantErr %v", err, tc.wantErr)
}
})
}
}
func TestMobileConnectCurrentPower(t *testing.T) {
tests := []struct {
name string
responses []apiResponse
want float64
wantErr bool
}{
// test cases for software version 2914
{
"home plug - charging",
[]apiResponse{
{mccAPIEnergy, "\"{\\n \\\"L1\\\": {\\n \\\"Ampere\\\": 9.9000000000000004,\\n \\\"Power\\\": 2308,\\n \\\"Volts\\\": 230.5\\n },\\n \\\"L2\\\": {\\n \\\"Ampere\\\": 0,\\n \\\"Power\\\": 0,\\n \\\"Volts\\\": 13.700000000000001\\n },\\n \\\"L3\\\": {\\n \\\"Ampere\\\": 0,\\n \\\"Power\\\": 0,\\n \\\"Volts\\\": 13.9\\n }\\n}\\n\""},
},
2308, false,
},
{
"3 phase low power - charging",
[]apiResponse{
{mccAPIEnergy, "\"{\\n \\\"L1\\\": {\\n \\\"Ampere\\\": 0.5,\\n \\\"Power\\\": 7,\\n \\\"Volts\\\": 244.40000000000001\\n },\\n \\\"L2\\\": {\\n \\\"Ampere\\\": 0.5,\\n \\\"Power\\\": 0,\\n \\\"Volts\\\": 242.10000000000002\\n },\\n \\\"L3\\\": {\\n \\\"Ampere\\\": 0.5,\\n \\\"Power\\\": 1,\\n \\\"Volts\\\": 242.30000000000001\\n }\\n}\\n\""},
},
8, false,
},
{
"no data response",
[]apiResponse{
{mccAPIEnergy, "\"\"\n"},
},
0, false,
},
{
"home plug - error response",
[]apiResponse{
{mccAPIEnergy, "\"{\\n \\\"L1\\\": {\\n \\\"Ampere\\\": 0,\\n \\\"Power\\\": 0,\\n \\\"Volts\\\": 246.60000000000002\\n },\\n \\\"L2\\\": {\\n \\\"Ampere\\\": 0,\\n \\\"Power\\\": 0,\\n \\\"Volts\\\": 16.800000000000001\\n },\\n \\\"L3\\\": {\\n \\\"Ampere\\\": 0,\\n \\\"Power\\\": 0,\\n \\\"Volts\\\": 16.300000000000001\\n }\\n}\\n\""},
},
0, false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
mcc := NewTestMobileConnect(t, tc.responses)
got, err := mcc.CurrentPower()
if (err != nil) != tc.wantErr {
t.Errorf("MobileConnect.CurrentPower() error = %v, wantErr %v", err, tc.wantErr)
return
}
if got != tc.want {
t.Errorf("MobileConnect.CurrentPower() = %v, want %v", got, tc.want)
}
})
}
}
func TestMobileConnectChargedEnergy(t *testing.T) {
tests := []struct {
name string
responses []apiResponse
want float64
wantErr bool
}{
// test cases for software version 2914
{
"valid response",
[]apiResponse{
{mccAPICurrentSession, "\"{\\n \\\"account\\\": \\\"PRIVATE\\\",\\n \\\"chargingRate\\\": 0,\\n \\\"chargingType\\\": \\\"AC\\\",\\n \\\"clockSrc\\\": \\\"NTP\\\",\\n \\\"costs\\\": 0,\\n \\\"currency\\\": \\\"\\\",\\n \\\"departTime\\\": \\\"\\\",\\n \\\"duration\\\": 30789,\\n \\\"endOfChargeTime\\\": \\\"\\\",\\n \\\"endSoc\\\": 0,\\n \\\"endTime\\\": \\\"\\\",\\n \\\"energySumKwh\\\": 18.832000000000001,\\n \\\"evChargingRatekW\\\": 0,\\n \\\"evTargetSoc\\\": -1,\\n \\\"evVasAvailability\\\": false,\\n \\\"pcid\\\": \\\"\\\",\\n \\\"powerRange\\\": 0,\\n \\\"selfEnergy\\\": 0,\\n \\\"sessionId\\\": 13,\\n \\\"soc\\\": -1,\\n \\\"solarEnergyShare\\\": 0,\\n \\\"startSoc\\\": 0,\\n \\\"startTime\\\": \\\"2020-04-15T10:07:22+02:00\\\",\\n \\\"totalRange\\\": 0,\\n \\\"vehicleBrand\\\": \\\"\\\",\\n \\\"vehicleModel\\\": \\\"\\\",\\n \\\"whitelist\\\": false\\n}\\n\""},
},
18.832000000000001, false,
},
{
"no data response",
[]apiResponse{
{mccAPICurrentSession, "\"\"\n"},
},
0, false,
},
{
"error response",
[]apiResponse{
{mccAPICurrentSession, "invalidjson"},
},
0, true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
mcc := NewTestMobileConnect(t, tc.responses)
got, err := mcc.ChargedEnergy()
if (err != nil) != tc.wantErr {
t.Errorf("MobileConnect.ChargedEnergy() error = %v, wantErr %v", err, tc.wantErr)
return
}
if got != tc.want {
t.Errorf("MobileConnect.ChargedEnergy() = %v, want %v", got, tc.want)
}
})
}
}
func TestMobileConnectChargingTime(t *testing.T) {
tests := []struct {
name string
responses []apiResponse
want time.Duration
wantErr bool
}{
{
"valid response",
[]apiResponse{
{mccAPICurrentSession, "\"{\\n \\\"account\\\": \\\"PRIVATE\\\",\\n \\\"chargingRate\\\": 0,\\n \\\"chargingType\\\": \\\"AC\\\",\\n \\\"clockSrc\\\": \\\"NTP\\\",\\n \\\"costs\\\": 0,\\n \\\"currency\\\": \\\"\\\",\\n \\\"departTime\\\": \\\"\\\",\\n \\\"duration\\\": 30789,\\n \\\"endOfChargeTime\\\": \\\"\\\",\\n \\\"endSoc\\\": 0,\\n \\\"endTime\\\": \\\"\\\",\\n \\\"energySumKwh\\\": 18.832000000000001,\\n \\\"evChargingRatekW\\\": 0,\\n \\\"evTargetSoc\\\": -1,\\n \\\"evVasAvailability\\\": false,\\n \\\"pcid\\\": \\\"\\\",\\n \\\"powerRange\\\": 0,\\n \\\"selfEnergy\\\": 0,\\n \\\"sessionId\\\": 13,\\n \\\"soc\\\": -1,\\n \\\"solarEnergyShare\\\": 0,\\n \\\"startSoc\\\": 0,\\n \\\"startTime\\\": \\\"2020-04-15T10:07:22+02:00\\\",\\n \\\"totalRange\\\": 0,\\n \\\"vehicleBrand\\\": \\\"\\\",\\n \\\"vehicleModel\\\": \\\"\\\",\\n \\\"whitelist\\\": false\\n}\\n\""},
},
30789 * time.Second, false,
},
{
"no data response",
[]apiResponse{
{mccAPICurrentSession, "\"\"\n"},
},
0, false,
},
{
"error response",
[]apiResponse{
{mccAPICurrentSession, "invalidjson"},
},
0, true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
mcc := NewTestMobileConnect(t, tc.responses)
got, err := mcc.ChargingTime()
if (err != nil) != tc.wantErr {
t.Errorf("MobileConnect.ChargingTime() error = %v, wantErr %v", err, tc.wantErr)
return
}
if got != tc.want {
t.Errorf("MobileConnect.ChargingTime() = %v, want %v", got, tc.want)
}
})
}
}
func TestMobileConnectCurrents(t *testing.T) {
tests := []struct {
name string
responses []apiResponse
wantL1, wantL2, wantL3 float64
wantErr bool
}{
// test cases for software version 2914
{
"home plug - charging",
[]apiResponse{
{mccAPIEnergy, "\"{\\n \\\"L1\\\": {\\n \\\"Ampere\\\": 9.9000000000000004,\\n \\\"Power\\\": 2308,\\n \\\"Volts\\\": 230.5\\n },\\n \\\"L2\\\": {\\n \\\"Ampere\\\": 0,\\n \\\"Power\\\": 0,\\n \\\"Volts\\\": 13.700000000000001\\n },\\n \\\"L3\\\": {\\n \\\"Ampere\\\": 0,\\n \\\"Power\\\": 0,\\n \\\"Volts\\\": 13.9\\n }\\n}\\n\""},
},
9.9000000000000004, 0, 0, false,
},
{
"3 phase low power - charging",
[]apiResponse{
{mccAPIEnergy, "\"{\\n \\\"L1\\\": {\\n \\\"Ampere\\\": 0.5,\\n \\\"Power\\\": 7,\\n \\\"Volts\\\": 244.40000000000001\\n },\\n \\\"L2\\\": {\\n \\\"Ampere\\\": 0.5,\\n \\\"Power\\\": 0,\\n \\\"Volts\\\": 242.10000000000002\\n },\\n \\\"L3\\\": {\\n \\\"Ampere\\\": 0.5,\\n \\\"Power\\\": 1,\\n \\\"Volts\\\": 242.30000000000001\\n }\\n}\\n\""},
},
0.5, 0.5, 0.5, false,
},
{
"no data response",
[]apiResponse{
{mccAPIEnergy, "\"\"\n"},
},
0, 0, 0, false,
},
{
"home plug - error response",
[]apiResponse{
{mccAPIEnergy, "\"{\\n \\\"L1\\\": {\\n \\\"Ampere\\\": 0,\\n \\\"Power\\\": 0,\\n \\\"Volts\\\": 246.60000000000002\\n },\\n \\\"L2\\\": {\\n \\\"Ampere\\\": 0,\\n \\\"Power\\\": 0,\\n \\\"Volts\\\": 16.800000000000001\\n },\\n \\\"L3\\\": {\\n \\\"Ampere\\\": 0,\\n \\\"Power\\\": 0,\\n \\\"Volts\\\": 16.300000000000001\\n }\\n}\\n\""},
},
0, 0, 0, false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
mcc := NewTestMobileConnect(t, tc.responses)
gotL1, gotL2, gotL3, err := mcc.Currents()
if (err != nil) != tc.wantErr {
t.Errorf("MobileConnect.Currents() error = %v, wantErr %v", err, tc.wantErr)
return
}
if gotL1 != tc.wantL1 {
t.Errorf("MobileConnect.Currents() = %v, want %v", gotL1, tc.wantL1)
}
if gotL2 != tc.wantL2 {
t.Errorf("MobileConnect.Currents() = %v, want %v", gotL2, tc.wantL2)
}
if gotL3 != tc.wantL3 {
t.Errorf("MobileConnect.Currents() = %v, want %v", gotL3, tc.wantL3)
}
})
}
}