-
Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathtest_charge_point.py
410 lines (361 loc) · 13.8 KB
/
test_charge_point.py
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
from dataclasses import asdict
import pytest
from ocpp.charge_point import (
camel_to_snake_case,
remove_nones,
serialize_as_dict,
snake_to_camel_case,
)
from ocpp.messages import Call
from ocpp.routing import after, create_route_map, on
from ocpp.v16 import ChargePoint as cp_16
from ocpp.v16.call import BootNotification, GetConfiguration, MeterValues
from ocpp.v16.call_result import BootNotification as BootNotificationResult
from ocpp.v16.datatypes import MeterValue, SampledValue
from ocpp.v16.enums import Action, RegistrationStatus
from ocpp.v201 import ChargePoint as cp_201
from ocpp.v201.call import GetVariables as v201GetVariables
from ocpp.v201.call import SetNetworkProfile as v201SetNetworkProfile
from ocpp.v201.datatypes import (
ComponentType,
EVSEType,
GetVariableDataType,
NetworkConnectionProfileType,
VariableType,
)
from ocpp.v201.enums import OCPPInterfaceType, OCPPTransportType, OCPPVersionType
def test_getters_should_not_be_called_during_routemap_setup():
class ChargePoint(cp_201):
@property
def foo(self):
raise RuntimeError("this will be raised")
try:
ChargePoint("blah", None)
except RuntimeError as e:
assert str(e) == "this will be raised"
pytest.fail("Getter was called during ChargePoint creation")
def test_multiple_classes_with_same_name_for_handler():
class ChargerA(cp_201):
@on(Action.Heartbeat)
def heartbeat(self, **kwargs):
pass
class ChargerB(cp_201):
@on(Action.Heartbeat)
def heartbeat(self, **kwargs):
pass
A = ChargerA("A", None)
B = ChargerB("B", None)
route_mapA = create_route_map(A)
route_mapB = create_route_map(B)
assert route_mapA["Heartbeat"] != route_mapB["Heartbeat"]
@pytest.mark.parametrize(
"test_input,expected",
[
({"transactionId": "74563478"}, {"transaction_id": "74563478"}),
({"fullSoC": 100}, {"full_soc": 100}),
({"responderURL": "foo.com"}, {"responder_url": "foo.com"}),
({"url": "foo.com"}, {"url": "foo.com"}),
({"ocppCSMSURL": "foo.com"}, {"ocpp_csms_url": "foo.com"}),
({"InvalidURL": "foo.com"}, {"invalid_url": "foo.com"}),
({"evMinV2XEnergyRequest": 200}, {"ev_min_v2x_energy_request": 200}),
({"v2xChargingCtrlr": 200}, {"v2x_charging_ctrlr": 200}),
({"webSocketPingInterval": 200}, {"web_socket_ping_interval": 200}),
({"signV2GCertificate": 200}, {"sign_v2g_certificate": 200}),
(
{"v2gCertificateInstallationEnabled": 200},
{"v2g_certificate_installation_enabled": 200},
),
],
)
def test_camel_to_snake_case(test_input, expected):
result = camel_to_snake_case(test_input)
assert result == expected
@pytest.mark.parametrize(
"test_input,expected",
[
({"transaction_id": "74563478"}, {"transactionId": "74563478"}),
({"full_soc": 100}, {"fullSoC": 100}),
({"soc_limit_reached": 200}, {"SoCLimitReached": 200}),
({"ev_min_v2x_energy_request": 200}, {"evMinV2XEnergyRequest": 200}),
({"v2x_charging_ctrlr": 200}, {"v2xChargingCtrlr": 200}),
({"responder_url": "foo.com"}, {"responderURL": "foo.com"}),
({"url": "foo.com"}, {"url": "foo.com"}),
({"ocpp_csms_url": "foo.com"}, {"ocppCSMSURL": "foo.com"}),
({"invalid_url": "foo.com"}, {"invalidURL": "foo.com"}),
({"web_socket_ping_interval": 200}, {"webSocketPingInterval": 200}),
({"sign_v2g_certificate": 200}, {"signV2GCertificate": 200}),
(
{"v2g_certificate_installation_enabled": 200},
{"v2gCertificateInstallationEnabled": 200},
),
],
)
def test_snake_to_camel_case(test_input, expected):
result = snake_to_camel_case(test_input)
assert result == expected
def test_remove_nones():
expected_payload = {"charge_point_model": "foo", "charge_point_vendor": "bar"}
payload = BootNotification(
charge_point_model="foo",
charge_point_vendor="bar",
charge_box_serial_number=None,
)
payload = asdict(payload)
assert expected_payload == remove_nones(payload)
def test_nested_remove_nones():
expected_payload = {
"configuration_slot": 1,
"connection_data": {
"ocpp_version": "OCPP20",
"ocpp_transport": "JSON",
"ocpp_csms_url": "wss://localhost:9000",
"message_timeout": 60,
"security_profile": 1,
"ocpp_interface": "Wired0",
},
}
connection_data = NetworkConnectionProfileType(
ocpp_version=OCPPVersionType.ocpp20,
ocpp_transport=OCPPTransportType.json,
ocpp_csms_url="wss://localhost:9000",
message_timeout=60,
security_profile=1,
ocpp_interface=OCPPInterfaceType.wired0,
vpn=None,
apn=None,
)
payload = v201SetNetworkProfile(
configuration_slot=1, connection_data=connection_data
)
payload = asdict(payload)
assert expected_payload == remove_nones(payload)
def test_nested_list_remove_nones():
expected_payload = {
"connector_id": 3,
"meter_value": [
{
"timestamp": "2017-08-17T07:08:06.186748+00:00",
"sampled_value": [
{
"value": "10",
"context": "Sample.Periodic",
"measurand": "Power.Active.Import",
"unit": "W",
},
{
"value": "50000",
"context": "Sample.Periodic",
"measurand": "Power.Active.Import",
"phase": "L1",
"unit": "W",
},
],
},
{
"timestamp": "2017-08-17T07:07:07.186748+00:00",
"sampled_value": [
{
"value": "10",
"context": "Sample.Periodic",
"measurand": "Power.Active.Import",
"unit": "W",
},
{
"value": "50000",
"context": "Sample.Periodic",
"measurand": "Power.Active.Import",
"phase": "L1",
"unit": "W",
},
],
},
],
"transaction_id": 5,
}
payload = MeterValues(
connector_id=3,
meter_value=[
MeterValue(
timestamp="2017-08-17T07:08:06.186748+00:00",
sampled_value=[
SampledValue(
value="10",
context="Sample.Periodic",
format=None,
measurand="Power.Active.Import",
phase=None,
location=None,
unit="W",
),
SampledValue(
value="50000",
context="Sample.Periodic",
format=None,
measurand="Power.Active.Import",
phase="L1",
location=None,
unit="W",
),
],
),
MeterValue(
timestamp="2017-08-17T07:07:07.186748+00:00",
sampled_value=[
SampledValue(
value="10",
context="Sample.Periodic",
format=None,
measurand="Power.Active.Import",
phase=None,
location=None,
unit="W",
),
SampledValue(
value="50000",
context="Sample.Periodic",
format=None,
measurand="Power.Active.Import",
phase="L1",
location=None,
unit="W",
),
],
),
],
transaction_id=5,
)
payload = asdict(payload)
assert expected_payload == remove_nones(payload)
def test_remove_nones_with_list_of_strings():
"""Make sure that `remove_nones()` doesn't crash when it encounters an
iterable other than a list or dict. See
https://github.com/mobilityhouse/ocpp/issues/289.
"""
payload = asdict(
GetConfiguration(key=["ClockAlignedDataInterval", "ConnectionTimeOut"])
)
assert remove_nones(payload) == {
"key": ["ClockAlignedDataInterval", "ConnectionTimeOut"]
}
def test_serialize_as_dict():
"""
Test recursively serializing a dataclasses as a dictionary.
"""
# Setup
expected = camel_to_snake_case(
{
"getVariableData": [
{
"component": {
"name": "Component",
"instance": None,
"evse": {
"id": 1,
"connectorId": None,
},
},
"variable": {
"name": "Variable",
"instance": None,
},
"attributeType": None,
}
],
"customData": None,
}
)
payload = v201GetVariables(
get_variable_data=[
GetVariableDataType(
component=ComponentType(
name="Component",
evse=EVSEType(id=1),
),
variable=VariableType(name="Variable"),
)
]
)
# Execute / Assert
assert serialize_as_dict(payload) == expected
@pytest.mark.asyncio
async def test_call_unique_id_added_to_handler_args_correctly(connection):
"""
This test ensures that the `call_unique_id` is getting passed to the
`on` and `after` handlers only if it is explicitly set in the handler signature.
To cover all possible cases, we define two chargers:
ChargerA:
`call_unique_id` not required on `on` handler but required on `after` handler.
ChargerB:
`call_unique_id` required on `on` handler but not required on `after` handler.
Each handler verifies a set of asserts to verify that the `call_unique_id`
is passed correctly.
To confirm that the handlers are actually being called and hence the asserts
are being ran, we introduce a set of counters that increase each time a specific
handler runs.
"""
charger_a_test_call_unique_id = "charger_a_1234"
charger_b_test_call_unique_id = "charger_b_5678"
payload_a = {"chargePointVendor": "foo_a", "chargePointModel": "bar_a"}
payload_b = {"chargePointVendor": "foo_b", "chargePointModel": "bar_b"}
class ChargerA(cp_16):
on_boot_notification_call_count = 0
after_boot_notification_call_count = 0
@on(Action.BootNotification)
def on_boot_notification(self, *args, **kwargs):
# call_unique_id should not be passed as arg nor kwarg
assert kwargs == camel_to_snake_case(payload_a)
assert args == ()
ChargerA.on_boot_notification_call_count += 1
return BootNotificationResult(
current_time="foo", interval=1, status=RegistrationStatus.accepted
)
@after(Action.BootNotification)
def after_boot_notification(self, call_unique_id, *args, **kwargs):
assert call_unique_id == charger_a_test_call_unique_id
assert kwargs == camel_to_snake_case(payload_a)
# call_unique_id should not be passed as arg
assert args == ()
ChargerA.after_boot_notification_call_count += 1
return BootNotificationResult(
current_time="foo", interval=1, status=RegistrationStatus.accepted
)
class ChargerB(cp_16):
on_boot_notification_call_count = 0
after_boot_notification_call_count = 0
@on(Action.BootNotification)
def on_boot_notification(self, call_unique_id, *args, **kwargs):
assert call_unique_id == charger_b_test_call_unique_id
assert kwargs == camel_to_snake_case(payload_b)
# call_unique_id should not be passed as arg
assert args == ()
ChargerB.on_boot_notification_call_count += 1
return BootNotificationResult(
current_time="foo", interval=1, status=RegistrationStatus.accepted
)
@after(Action.BootNotification)
def after_boot_notification(self, *args, **kwargs):
# call_unique_id should not be passed as arg nor kwarg
assert kwargs == camel_to_snake_case(payload_b)
assert args == ()
ChargerB.after_boot_notification_call_count += 1
return BootNotificationResult(
current_time="foo", interval=1, status=RegistrationStatus.accepted
)
charger_a = ChargerA("charger_a_id", connection)
charger_b = ChargerB("charger_b_id", connection)
msg_a = Call(
unique_id=charger_a_test_call_unique_id,
action=Action.BootNotification.value,
payload=payload_a,
)
await charger_a._handle_call(msg_a)
msg_b = Call(
unique_id=charger_b_test_call_unique_id,
action=Action.BootNotification.value,
payload=payload_b,
)
await charger_b._handle_call(msg_b)
assert ChargerA.on_boot_notification_call_count == 1
assert ChargerA.after_boot_notification_call_count == 1
assert ChargerB.on_boot_notification_call_count == 1
assert ChargerB.after_boot_notification_call_count == 1