Nav apraksta

test_exceptions.py 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import pytest
  2. from ocpp.exceptions import (
  3. FormatViolationError,
  4. ProtocolError,
  5. TypeConstraintViolationError,
  6. )
  7. from ocpp.messages import Call, validate_payload
  8. def test_exception_with_error_details():
  9. exception = ProtocolError("Some error", {"key": "value"})
  10. assert exception.description == "Some error"
  11. assert exception.details == {"key": "value"}
  12. def test_exception_without_error_details():
  13. exception = ProtocolError()
  14. assert exception.description == "Payload for Action is incomplete"
  15. assert exception.details == {}
  16. def test_exception_show_triggered_message_type_constraint():
  17. # chargePointVendor should be a string, not an integer,
  18. # so this should raise a TypeConstraintViolationError
  19. call = Call(
  20. unique_id=123456,
  21. action="BootNotification",
  22. payload={"chargePointVendor": 1, "chargePointModel": "SingleSocketCharger"},
  23. )
  24. ocpp_message = (
  25. "'ocpp_message': <Call - unique_id=123456, action=BootNotification, "
  26. "payload={'chargePointVendor': 1, 'chargePointModel': 'SingleSocketCharger'}"
  27. )
  28. with pytest.raises(TypeConstraintViolationError) as exception_info:
  29. validate_payload(call, "1.6")
  30. assert ocpp_message in str(exception_info.value)
  31. def test_exception_show_triggered_message_format():
  32. # The payload is syntactically incorrect, should trigger a FormatViolationError
  33. call = Call(
  34. unique_id=123457,
  35. action="BootNotification",
  36. payload={"syntactically": "incorrect"},
  37. )
  38. ocpp_message = (
  39. "'ocpp_message': <Call - unique_id=123457, action=BootNotification, "
  40. "payload={'syntactically': 'incorrect'}"
  41. )
  42. with pytest.raises(FormatViolationError) as exception_info:
  43. validate_payload(call, "1.6")
  44. assert ocpp_message in str(exception_info.value)