|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from hiero_sdk_python.response_code import ResponseCode |
| 6 | + |
| 7 | + |
| 8 | +pytestmark = pytest.mark.unit |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.parametrize( |
| 12 | + ("code", "expected_name"), |
| 13 | + [ |
| 14 | + # Previously swapped with each other (356 <-> 357). |
| 15 | + (356, "SERVICE_ENDPOINTS_EXCEEDED_LIMIT"), |
| 16 | + (357, "INVALID_IPV4_ADDRESS"), |
| 17 | + # Previously carried a misspelled or non-canonical name. |
| 18 | + (344, "INVALID_GOSSIP_CA_CERTIFICATE"), |
| 19 | + (363, "PENDING_AIRDROP_ID_LIST_TOO_LONG"), |
| 20 | + (369, "INVALID_TOKEN_IN_PENDING_AIRDROP"), |
| 21 | + # Previously missing entirely, so the network's code surfaced as UNKNOWN_CODE_113. |
| 22 | + (113, "RECEIVER_SIG_REQUIRED"), |
| 23 | + ], |
| 24 | +) |
| 25 | +def test_code_resolves_to_canonical_proto_name(code: int, expected_name: str) -> None: |
| 26 | + """Each corrected code resolves to the name used in response_code.proto.""" |
| 27 | + member = ResponseCode(code) |
| 28 | + assert member.name == expected_name |
| 29 | + assert member is ResponseCode[expected_name] |
| 30 | + |
| 31 | + |
| 32 | +@pytest.mark.parametrize( |
| 33 | + ("deprecated_name", "canonical_name", "value"), |
| 34 | + [ |
| 35 | + ("INVALID_GOSSIP_CAE_CERTIFICATE", "INVALID_GOSSIP_CA_CERTIFICATE", 344), |
| 36 | + ("MAX_PENDING_AIRDROP_ID_EXCEEDED", "PENDING_AIRDROP_ID_LIST_TOO_LONG", 363), |
| 37 | + ("INVALID_TOKEN_ID_PENDING_AIRDROP", "INVALID_TOKEN_IN_PENDING_AIRDROP", 369), |
| 38 | + ], |
| 39 | +) |
| 40 | +def test_renamed_codes_keep_working_deprecated_alias(deprecated_name: str, canonical_name: str, value: int) -> None: |
| 41 | + """The old names still resolve, so existing user code keeps working.""" |
| 42 | + alias = getattr(ResponseCode, deprecated_name) |
| 43 | + assert alias == value |
| 44 | + assert alias is getattr(ResponseCode, canonical_name) |
| 45 | + # An alias is reachable by name but is not a member in its own right. |
| 46 | + assert deprecated_name in ResponseCode.__members__ |
| 47 | + assert deprecated_name not in {member.name for member in ResponseCode} |
| 48 | + |
| 49 | + |
| 50 | +def test_unknown_code_still_falls_back() -> None: |
| 51 | + """The _missing_ hook is unaffected by the corrected members.""" |
| 52 | + unknown = ResponseCode(9999) |
| 53 | + assert unknown.name == "UNKNOWN_CODE_9999" |
| 54 | + assert unknown.is_unknown |
| 55 | + assert not ResponseCode.SUCCESS.is_unknown |
0 commit comments