Debian Codegraph @

tests/test_exceptions.py

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