Serie: ocpp
python
59 righe
· Aggiornato 2025-10-10
test_exceptions.py
ocpp/homework/ocpp_many/tests/test_exceptions.py
import pytest
from ocpp.exceptions import (
FormatViolationError,
ProtocolError,
TypeConstraintViolationError,
)
from ocpp.messages import Call, _validate_payload
from ocpp.v16.enums import Action
def test_exception_with_error_details():
exception = ProtocolError("Some error", {"key": "value"})
assert exception.description == "Some error"
assert exception.details == {"key": "value"}
def test_exception_without_error_details():
exception = ProtocolError()
assert exception.description == "Payload for Action is incomplete"
assert exception.details == {}
def test_exception_show_triggered_message_type_constraint():
# chargePointVendor should be a string, not an integer,
# so this should raise a TypeConstraintViolationError
call = Call(
unique_id=123456,
action=Action.boot_notification,
payload={"chargePointVendor": 1, "chargePointModel": "SingleSocketCharger"},
)
ocpp_message = (
"'ocpp_message': <Call - unique_id=123456, action=BootNotification, "
"payload={'chargePointVendor': 1, 'chargePointModel': 'SingleSocketCharger'}"
)
with pytest.raises(TypeConstraintViolationError) as exception_info:
_validate_payload(call, "1.6")
assert ocpp_message in str(exception_info.value)
def test_exception_show_triggered_message_format():
# The payload is syntactically incorrect, should trigger a FormatViolationError
call = Call(
unique_id=123457,
action=Action.boot_notification,
payload={"syntactically": "incorrect"},
)
ocpp_message = (
"'ocpp_message': <Call - unique_id=123457, action=BootNotification, "
"payload={'syntactically': 'incorrect'}"
)
with pytest.raises(FormatViolationError) as exception_info:
_validate_payload(call, "1.6")
assert ocpp_message in str(exception_info.value)
Articoli correlati
ocpp
python
Aggiornato 2026-02-03
central_system.py
central_system.py — python source code from the ocpp learning materials (ocpp/homework/central_system.py).
Leggi l'articolo →
ocpp
python
Aggiornato 2026-02-03
charge_point.py
charge_point.py — python source code from the ocpp learning materials (ocpp/homework/ocpp/charge_point.py).
Leggi l'articolo →
ocpp
python
Aggiornato 2026-02-03
exceptions.py
exceptions.py — python source code from the ocpp learning materials (ocpp/homework/ocpp/exceptions.py).
Leggi l'articolo →
ocpp
python
Aggiornato 2026-02-03
messages.py
messages.py — python source code from the ocpp learning materials (ocpp/homework/ocpp/messages.py).
Leggi l'articolo →
ocpp
python
Aggiornato 2026-02-03
routing.py
routing.py — python source code from the ocpp learning materials (ocpp/homework/ocpp/routing.py).
Leggi l'articolo →
ocpp
python
Aggiornato 2026-02-03
__init__.py
__init__.py — python source code from the ocpp learning materials (ocpp/homework/ocpp/v16/__init__.py).
Leggi l'articolo →