| 1 |
from dataclasses import asdict |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from ocpp.charge_point import ( |
| 6 |
camel_to_snake_case, |
| 7 |
remove_nones, |
| 8 |
serialize_as_dict, |
| 9 |
snake_to_camel_case, |
| 10 |
) |
| 11 |
from ocpp.messages import Call |
| 12 |
from ocpp.routing import after, create_route_map, on |
| 13 |
from ocpp.v16 import ChargePoint as cp_16 |
| 14 |
from ocpp.v16.call import BootNotification, GetConfiguration, MeterValues |
| 15 |
from ocpp.v16.call_result import BootNotification as BootNotificationResult |
| 16 |
from ocpp.v16.datatypes import MeterValue, SampledValue |
| 17 |
from ocpp.v16.enums import Action, RegistrationStatus |
| 18 |
from ocpp.v201 import ChargePoint as cp_201 |
| 19 |
from ocpp.v201 import call_result, datatypes, enums |
| 20 |
from ocpp.v201.call import GetVariables as v201GetVariables |
| 21 |
from ocpp.v201.call import SetNetworkProfile as v201SetNetworkProfile |
| 22 |
from ocpp.v201.datatypes import ( |
| 23 |
ComponentType, |
| 24 |
EVSEType, |
| 25 |
GetVariableDataType, |
| 26 |
NetworkConnectionProfileType, |
| 27 |
VariableType, |
| 28 |
) |
| 29 |
from ocpp.v201.enums import ( |
| 30 |
OCPPInterfaceEnumType, |
| 31 |
OCPPTransportEnumType, |
| 32 |
OCPPVersionEnumType, |
| 33 |
) |
| 34 |
|
| 35 |
|
| 36 |
def test_getters_should_not_be_called_during_routemap_setup(): |
| 37 |
class ChargePoint(cp_201): |
| 38 |
@property |
| 39 |
def foo(self): |
| 40 |
raise RuntimeError("this will be raised") |
| 41 |
|
| 42 |
try: |
| 43 |
ChargePoint("blah", None) |
| 44 |
except RuntimeError as e: |
| 45 |
assert str(e) == "this will be raised" |
| 46 |
pytest.fail("Getter was called during ChargePoint creation") |
| 47 |
|
| 48 |
|
| 49 |
def test_multiple_classes_with_same_name_for_handler(): |
| 50 |
class ChargerA(cp_201): |
| 51 |
@on(Action.heartbeat) |
| 52 |
def heartbeat(self, **kwargs): |
| 53 |
pass |
| 54 |
|
| 55 |
class ChargerB(cp_201): |
| 56 |
@on(Action.heartbeat) |
| 57 |
def heartbeat(self, **kwargs): |
| 58 |
pass |
| 59 |
|
| 60 |
A = ChargerA("A", None) |
| 61 |
B = ChargerB("B", None) |
| 62 |
route_mapA = create_route_map(A) |
| 63 |
route_mapB = create_route_map(B) |
| 64 |
assert route_mapA["Heartbeat"] != route_mapB["Heartbeat"] |
| 65 |
|
| 66 |
|
| 67 |
@pytest.mark.parametrize( |
| 68 |
"test_input,expected", |
| 69 |
[ |
| 70 |
({"transactionId": "74563478"}, {"transaction_id": "74563478"}), |
| 71 |
({"fullSoC": 100}, {"full_soc": 100}), |
| 72 |
({"responderURL": "foo.com"}, {"responder_url": "foo.com"}), |
| 73 |
({"url": "foo.com"}, {"url": "foo.com"}), |
| 74 |
({"ocppCSMSURL": "foo.com"}, {"ocpp_csms_url": "foo.com"}), |
| 75 |
({"CSMSRootCertificate": "foo.com"}, {"csms_root_certificate": "foo.com"}), |
| 76 |
({"InvalidURL": "foo.com"}, {"invalid_url": "foo.com"}), |
| 77 |
({"evMinV2XEnergyRequest": 200}, {"ev_min_v2x_energy_request": 200}), |
| 78 |
({"v2xChargingCtrlr": 200}, {"v2x_charging_ctrlr": 200}), |
| 79 |
({"webSocketPingInterval": 200}, {"web_socket_ping_interval": 200}), |
| 80 |
({"signV2GCertificate": 200}, {"sign_v2g_certificate": 200}), |
| 81 |
( |
| 82 |
{"v2gCertificateInstallationEnabled": 200}, |
| 83 |
{"v2g_certificate_installation_enabled": 200}, |
| 84 |
), |
| 85 |
], |
| 86 |
) |
| 87 |
def test_camel_to_snake_case(test_input, expected): |
| 88 |
result = camel_to_snake_case(test_input) |
| 89 |
assert result == expected |
| 90 |
|
| 91 |
|
| 92 |
@pytest.mark.parametrize( |
| 93 |
"test_input,expected", |
| 94 |
[ |
| 95 |
({"transaction_id": "74563478"}, {"transactionId": "74563478"}), |
| 96 |
({"full_soc": 100}, {"fullSoC": 100}), |
| 97 |
({"soc_limit_reached": 200}, {"SoCLimitReached": 200}), |
| 98 |
({"ev_min_v2x_energy_request": 200}, {"evMinV2XEnergyRequest": 200}), |
| 99 |
({"v2x_charging_ctrlr": 200}, {"v2xChargingCtrlr": 200}), |
| 100 |
({"responder_url": "foo.com"}, {"responderURL": "foo.com"}), |
| 101 |
({"url": "foo.com"}, {"url": "foo.com"}), |
| 102 |
({"ocpp_csms_url": "foo.com"}, {"ocppCsmsUrl": "foo.com"}), |
| 103 |
({"csms_root_certificate": "foo.com"}, {"CSMSRootCertificate": "foo.com"}), |
| 104 |
({"invalid_url": "foo.com"}, {"invalidURL": "foo.com"}), |
| 105 |
({"web_socket_ping_interval": 200}, {"webSocketPingInterval": 200}), |
| 106 |
({"sign_v2g_certificate": 200}, {"signV2GCertificate": 200}), |
| 107 |
( |
| 108 |
{"v2g_certificate_installation_enabled": 200}, |
| 109 |
{"v2gCertificateInstallationEnabled": 200}, |
| 110 |
), |
| 111 |
], |
| 112 |
) |
| 113 |
def test_snake_to_camel_case(test_input, expected): |
| 114 |
result = snake_to_camel_case(test_input) |
| 115 |
assert result == expected |
| 116 |
|
| 117 |
|
| 118 |
def test_remove_nones(): |
| 119 |
expected_payload = {"charge_point_model": "foo", "charge_point_vendor": "bar"} |
| 120 |
|
| 121 |
payload = BootNotification( |
| 122 |
charge_point_model="foo", |
| 123 |
charge_point_vendor="bar", |
| 124 |
charge_box_serial_number=None, |
| 125 |
) |
| 126 |
payload = asdict(payload) |
| 127 |
|
| 128 |
assert expected_payload == remove_nones(payload) |
| 129 |
|
| 130 |
|
| 131 |
def test_nested_remove_nones(): |
| 132 |
expected_payload = { |
| 133 |
"configuration_slot": 1, |
| 134 |
"connection_data": { |
| 135 |
"ocpp_version": "OCPP20", |
| 136 |
"ocpp_transport": "JSON", |
| 137 |
"ocpp_csms_url": "wss://localhost:9000", |
| 138 |
"message_timeout": 60, |
| 139 |
"security_profile": 1, |
| 140 |
"ocpp_interface": "Wired0", |
| 141 |
}, |
| 142 |
} |
| 143 |
|
| 144 |
connection_data = NetworkConnectionProfileType( |
| 145 |
ocpp_version=OCPPVersionEnumType.ocpp20, |
| 146 |
ocpp_transport=OCPPTransportEnumType.json, |
| 147 |
ocpp_csms_url="wss://localhost:9000", |
| 148 |
message_timeout=60, |
| 149 |
security_profile=1, |
| 150 |
ocpp_interface=OCPPInterfaceEnumType.wired0, |
| 151 |
vpn=None, |
| 152 |
apn=None, |
| 153 |
) |
| 154 |
|
| 155 |
payload = v201SetNetworkProfile( |
| 156 |
configuration_slot=1, connection_data=connection_data |
| 157 |
) |
| 158 |
payload = asdict(payload) |
| 159 |
|
| 160 |
assert expected_payload == remove_nones(payload) |
| 161 |
|
| 162 |
|
| 163 |
def test_nested_list_remove_nones(): |
| 164 |
expected_payload = { |
| 165 |
"connector_id": 3, |
| 166 |
"meter_value": [ |
| 167 |
{ |
| 168 |
"timestamp": "2017-08-17T07:08:06.186748+00:00", |
| 169 |
"sampled_value": [ |
| 170 |
{ |
| 171 |
"value": "10", |
| 172 |
"context": "Sample.Periodic", |
| 173 |
"measurand": "Power.Active.Import", |
| 174 |
"unit": "W", |
| 175 |
}, |
| 176 |
{ |
| 177 |
"value": "50000", |
| 178 |
"context": "Sample.Periodic", |
| 179 |
"measurand": "Power.Active.Import", |
| 180 |
"phase": "L1", |
| 181 |
"unit": "W", |
| 182 |
}, |
| 183 |
], |
| 184 |
}, |
| 185 |
{ |
| 186 |
"timestamp": "2017-08-17T07:07:07.186748+00:00", |
| 187 |
"sampled_value": [ |
| 188 |
{ |
| 189 |
"value": "10", |
| 190 |
"context": "Sample.Periodic", |
| 191 |
"measurand": "Power.Active.Import", |
| 192 |
"unit": "W", |
| 193 |
}, |
| 194 |
{ |
| 195 |
"value": "50000", |
| 196 |
"context": "Sample.Periodic", |
| 197 |
"measurand": "Power.Active.Import", |
| 198 |
"phase": "L1", |
| 199 |
"unit": "W", |
| 200 |
}, |
| 201 |
], |
| 202 |
}, |
| 203 |
], |
| 204 |
"transaction_id": 5, |
| 205 |
} |
| 206 |
|
| 207 |
payload = MeterValues( |
| 208 |
connector_id=3, |
| 209 |
meter_value=[ |
| 210 |
MeterValue( |
| 211 |
timestamp="2017-08-17T07:08:06.186748+00:00", |
| 212 |
sampled_value=[ |
| 213 |
SampledValue( |
| 214 |
value="10", |
| 215 |
context="Sample.Periodic", |
| 216 |
format=None, |
| 217 |
measurand="Power.Active.Import", |
| 218 |
phase=None, |
| 219 |
location=None, |
| 220 |
unit="W", |
| 221 |
), |
| 222 |
SampledValue( |
| 223 |
value="50000", |
| 224 |
context="Sample.Periodic", |
| 225 |
format=None, |
| 226 |
measurand="Power.Active.Import", |
| 227 |
phase="L1", |
| 228 |
location=None, |
| 229 |
unit="W", |
| 230 |
), |
| 231 |
], |
| 232 |
), |
| 233 |
MeterValue( |
| 234 |
timestamp="2017-08-17T07:07:07.186748+00:00", |
| 235 |
sampled_value=[ |
| 236 |
SampledValue( |
| 237 |
value="10", |
| 238 |
context="Sample.Periodic", |
| 239 |
format=None, |
| 240 |
measurand="Power.Active.Import", |
| 241 |
phase=None, |
| 242 |
location=None, |
| 243 |
unit="W", |
| 244 |
), |
| 245 |
SampledValue( |
| 246 |
value="50000", |
| 247 |
context="Sample.Periodic", |
| 248 |
format=None, |
| 249 |
measurand="Power.Active.Import", |
| 250 |
phase="L1", |
| 251 |
location=None, |
| 252 |
unit="W", |
| 253 |
), |
| 254 |
], |
| 255 |
), |
| 256 |
], |
| 257 |
transaction_id=5, |
| 258 |
) |
| 259 |
|
| 260 |
payload = asdict(payload) |
| 261 |
assert expected_payload == remove_nones(payload) |
| 262 |
|
| 263 |
|
| 264 |
def test_remove_nones_with_list_of_strings(): |
| 265 |
"""Make sure that `remove_nones()` doesn't crash when it encounters an |
| 266 |
iterable other than a list or dict. See |
| 267 |
https://github.com/mobilityhouse/ocpp/issues/289. |
| 268 |
""" |
| 269 |
payload = asdict( |
| 270 |
GetConfiguration(key=["ClockAlignedDataInterval", "ConnectionTimeOut"]) |
| 271 |
) |
| 272 |
|
| 273 |
assert remove_nones(payload) == { |
| 274 |
"key": ["ClockAlignedDataInterval", "ConnectionTimeOut"] |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
def test_serialize_as_dict(): |
| 279 |
""" |
| 280 |
Test recursively serializing a dataclasses as a dictionary. |
| 281 |
""" |
| 282 |
# Setup |
| 283 |
expected = camel_to_snake_case( |
| 284 |
{ |
| 285 |
"getVariableData": [ |
| 286 |
{ |
| 287 |
"component": { |
| 288 |
"name": "Component", |
| 289 |
"instance": None, |
| 290 |
"evse": { |
| 291 |
"id": 1, |
| 292 |
"connectorId": None, |
| 293 |
}, |
| 294 |
}, |
| 295 |
"variable": { |
| 296 |
"name": "Variable", |
| 297 |
"instance": None, |
| 298 |
}, |
| 299 |
"attributeType": None, |
| 300 |
} |
| 301 |
], |
| 302 |
"customData": None, |
| 303 |
} |
| 304 |
) |
| 305 |
|
| 306 |
payload = v201GetVariables( |
| 307 |
get_variable_data=[ |
| 308 |
GetVariableDataType( |
| 309 |
component=ComponentType( |
| 310 |
name="Component", |
| 311 |
evse=EVSEType(id=1), |
| 312 |
), |
| 313 |
variable=VariableType(name="Variable"), |
| 314 |
) |
| 315 |
] |
| 316 |
) |
| 317 |
|
| 318 |
# Execute / Assert |
| 319 |
assert serialize_as_dict(payload) == expected |
| 320 |
|
| 321 |
|
| 322 |
def test_serialization_of_collection_of_multiple_elements(): |
| 323 |
"""This test validates that bug #635 is fixed. |
| 324 |
That bug incorrectly serialized payloads that contain a collection |
| 325 |
of elements. |
| 326 |
|
| 327 |
This test serializes a call_result.SetVariables that contains 2 |
| 328 |
SetVariableResultTypes. |
| 329 |
|
| 330 |
https://github.com/mobilityhouse/ocpp/issues/635 |
| 331 |
""" |
| 332 |
payload = call_result.SetVariables( |
| 333 |
set_variable_result=[ |
| 334 |
datatypes.SetVariableResultType( |
| 335 |
attribute_status=enums.SetVariableStatusEnumType.accepted, |
| 336 |
component={ |
| 337 |
"name": "TemperatureSensor", |
| 338 |
"instance": "First", |
| 339 |
"evse": {"id": 1, "connector_id": 1}, |
| 340 |
}, |
| 341 |
variable={"name": "DisplayUnit", "instance": "Main"}, |
| 342 |
attribute_type="Actual", |
| 343 |
attribute_status_info=None, |
| 344 |
), |
| 345 |
datatypes.SetVariableResultType( |
| 346 |
attribute_status="Accepted", |
| 347 |
component={"name": "TxCtrlr"}, |
| 348 |
variable={"name": "TxStopPoint"}, |
| 349 |
attribute_type="Actual", |
| 350 |
attribute_status_info=None, |
| 351 |
), |
| 352 |
], |
| 353 |
custom_data=None, |
| 354 |
) |
| 355 |
|
| 356 |
expected = { |
| 357 |
"custom_data": None, |
| 358 |
"set_variable_result": [ |
| 359 |
{ |
| 360 |
"attribute_status": "Accepted", |
| 361 |
"attribute_status_info": None, |
| 362 |
"attribute_type": "Actual", |
| 363 |
"component": { |
| 364 |
"evse": {"connector_id": 1, "id": 1}, |
| 365 |
"instance": "First", |
| 366 |
"name": "TemperatureSensor", |
| 367 |
}, |
| 368 |
"variable": {"instance": "Main", "name": "DisplayUnit"}, |
| 369 |
}, |
| 370 |
{ |
| 371 |
"attribute_status": "Accepted", |
| 372 |
"attribute_status_info": None, |
| 373 |
"attribute_type": "Actual", |
| 374 |
"component": {"name": "TxCtrlr"}, |
| 375 |
"variable": {"name": "TxStopPoint"}, |
| 376 |
}, |
| 377 |
], |
| 378 |
} |
| 379 |
# Execute / Assert |
| 380 |
assert serialize_as_dict(payload) == expected |
| 381 |
|
| 382 |
|
| 383 |
@pytest.mark.asyncio |
| 384 |
async def test_call_unique_id_added_to_handler_args_correctly(connection): |
| 385 |
""" |
| 386 |
This test ensures that the `call_unique_id` is getting passed to the |
| 387 |
`on` and `after` handlers only if it is explicitly set in the handler signature. |
| 388 |
|
| 389 |
To cover all possible cases, we define two chargers: |
| 390 |
|
| 391 |
ChargerA: |
| 392 |
`call_unique_id` not required on `on` handler but required on `after` handler. |
| 393 |
|
| 394 |
ChargerB: |
| 395 |
`call_unique_id` required on `on` handler but not required on `after` handler. |
| 396 |
|
| 397 |
Each handler verifies a set of asserts to verify that the `call_unique_id` |
| 398 |
is passed correctly. |
| 399 |
To confirm that the handlers are actually being called and hence the asserts |
| 400 |
are being ran, we introduce a set of counters that increase each time a specific |
| 401 |
handler runs. |
| 402 |
""" |
| 403 |
charger_a_test_call_unique_id = "charger_a_1234" |
| 404 |
charger_b_test_call_unique_id = "charger_b_5678" |
| 405 |
payload_a = {"chargePointVendor": "foo_a", "chargePointModel": "bar_a"} |
| 406 |
payload_b = {"chargePointVendor": "foo_b", "chargePointModel": "bar_b"} |
| 407 |
|
| 408 |
class ChargerA(cp_16): |
| 409 |
on_boot_notification_call_count = 0 |
| 410 |
after_boot_notification_call_count = 0 |
| 411 |
|
| 412 |
@on(Action.boot_notification) |
| 413 |
def on_boot_notification(self, *args, **kwargs): |
| 414 |
# call_unique_id should not be passed as arg nor kwarg |
| 415 |
assert kwargs == camel_to_snake_case(payload_a) |
| 416 |
assert args == () |
| 417 |
ChargerA.on_boot_notification_call_count += 1 |
| 418 |
return BootNotificationResult( |
| 419 |
current_time="foo", interval=1, status=RegistrationStatus.accepted |
| 420 |
) |
| 421 |
|
| 422 |
@after(Action.boot_notification) |
| 423 |
def after_boot_notification(self, call_unique_id, *args, **kwargs): |
| 424 |
assert call_unique_id == charger_a_test_call_unique_id |
| 425 |
assert kwargs == camel_to_snake_case(payload_a) |
| 426 |
# call_unique_id should not be passed as arg |
| 427 |
assert args == () |
| 428 |
ChargerA.after_boot_notification_call_count += 1 |
| 429 |
return BootNotificationResult( |
| 430 |
current_time="foo", interval=1, status=RegistrationStatus.accepted |
| 431 |
) |
| 432 |
|
| 433 |
class ChargerB(cp_16): |
| 434 |
on_boot_notification_call_count = 0 |
| 435 |
after_boot_notification_call_count = 0 |
| 436 |
|
| 437 |
@on(Action.boot_notification) |
| 438 |
def on_boot_notification(self, call_unique_id, *args, **kwargs): |
| 439 |
assert call_unique_id == charger_b_test_call_unique_id |
| 440 |
assert kwargs == camel_to_snake_case(payload_b) |
| 441 |
# call_unique_id should not be passed as arg |
| 442 |
assert args == () |
| 443 |
ChargerB.on_boot_notification_call_count += 1 |
| 444 |
return BootNotificationResult( |
| 445 |
current_time="foo", interval=1, status=RegistrationStatus.accepted |
| 446 |
) |
| 447 |
|
| 448 |
@after(Action.boot_notification) |
| 449 |
def after_boot_notification(self, *args, **kwargs): |
| 450 |
# call_unique_id should not be passed as arg nor kwarg |
| 451 |
assert kwargs == camel_to_snake_case(payload_b) |
| 452 |
assert args == () |
| 453 |
ChargerB.after_boot_notification_call_count += 1 |
| 454 |
return BootNotificationResult( |
| 455 |
current_time="foo", interval=1, status=RegistrationStatus.accepted |
| 456 |
) |
| 457 |
|
| 458 |
charger_a = ChargerA("charger_a_id", connection) |
| 459 |
charger_b = ChargerB("charger_b_id", connection) |
| 460 |
|
| 461 |
msg_a = Call( |
| 462 |
unique_id=charger_a_test_call_unique_id, |
| 463 |
action=Action.boot_notification.value, |
| 464 |
payload=payload_a, |
| 465 |
) |
| 466 |
await charger_a._handle_call(msg_a) |
| 467 |
|
| 468 |
msg_b = Call( |
| 469 |
unique_id=charger_b_test_call_unique_id, |
| 470 |
action=Action.boot_notification.value, |
| 471 |
payload=payload_b, |
| 472 |
) |
| 473 |
await charger_b._handle_call(msg_b) |
| 474 |
|
| 475 |
assert ChargerA.on_boot_notification_call_count == 1 |
| 476 |
assert ChargerA.after_boot_notification_call_count == 1 |
| 477 |
assert ChargerB.on_boot_notification_call_count == 1 |
| 478 |
assert ChargerB.after_boot_notification_call_count == 1 |