|
| 1 | +from collections.abc import Iterator, Mapping |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from hyperbrowser.exceptions import HyperbrowserError |
| 6 | +from hyperbrowser.mapping_utils import read_string_key_mapping |
| 7 | + |
| 8 | + |
| 9 | +class _BrokenKeysMapping(Mapping[object, object]): |
| 10 | + def __getitem__(self, key: object) -> object: |
| 11 | + _ = key |
| 12 | + return "value" |
| 13 | + |
| 14 | + def __iter__(self) -> Iterator[object]: |
| 15 | + return iter(()) |
| 16 | + |
| 17 | + def __len__(self) -> int: |
| 18 | + return 0 |
| 19 | + |
| 20 | + def keys(self): # type: ignore[override] |
| 21 | + raise RuntimeError("broken keys") |
| 22 | + |
| 23 | + |
| 24 | +class _BrokenValueMapping(Mapping[object, object]): |
| 25 | + def __getitem__(self, key: object) -> object: |
| 26 | + _ = key |
| 27 | + raise RuntimeError("broken value read") |
| 28 | + |
| 29 | + def __iter__(self) -> Iterator[object]: |
| 30 | + return iter(("field",)) |
| 31 | + |
| 32 | + def __len__(self) -> int: |
| 33 | + return 1 |
| 34 | + |
| 35 | + |
| 36 | +class _HyperbrowserValueFailureMapping(Mapping[object, object]): |
| 37 | + def __getitem__(self, key: object) -> object: |
| 38 | + _ = key |
| 39 | + raise HyperbrowserError("custom value read failure") |
| 40 | + |
| 41 | + def __iter__(self) -> Iterator[object]: |
| 42 | + return iter(("field",)) |
| 43 | + |
| 44 | + def __len__(self) -> int: |
| 45 | + return 1 |
| 46 | + |
| 47 | + |
| 48 | +def _read_mapping(mapping_value): |
| 49 | + return read_string_key_mapping( |
| 50 | + mapping_value, |
| 51 | + expected_mapping_error="expected mapping", |
| 52 | + read_keys_error="failed keys", |
| 53 | + non_string_key_error_builder=lambda key: f"non-string key: {type(key).__name__}", |
| 54 | + read_value_error_builder=lambda key_display: ( |
| 55 | + f"failed value for '{key_display}'" |
| 56 | + ), |
| 57 | + key_display=lambda key: key, |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +def test_read_string_key_mapping_returns_dict(): |
| 62 | + assert _read_mapping({"field": "value"}) == {"field": "value"} |
| 63 | + |
| 64 | + |
| 65 | +def test_read_string_key_mapping_rejects_non_mappings(): |
| 66 | + with pytest.raises(HyperbrowserError, match="expected mapping"): |
| 67 | + _read_mapping(["value"]) |
| 68 | + |
| 69 | + |
| 70 | +def test_read_string_key_mapping_wraps_key_iteration_failures(): |
| 71 | + with pytest.raises(HyperbrowserError, match="failed keys") as exc_info: |
| 72 | + _read_mapping(_BrokenKeysMapping()) |
| 73 | + |
| 74 | + assert isinstance(exc_info.value.original_error, RuntimeError) |
| 75 | + |
| 76 | + |
| 77 | +def test_read_string_key_mapping_rejects_non_string_keys(): |
| 78 | + with pytest.raises(HyperbrowserError, match="non-string key: int"): |
| 79 | + _read_mapping({1: "value"}) |
| 80 | + |
| 81 | + |
| 82 | +def test_read_string_key_mapping_wraps_value_read_failures(): |
| 83 | + with pytest.raises( |
| 84 | + HyperbrowserError, match="failed value for 'field'" |
| 85 | + ) as exc_info: |
| 86 | + _read_mapping(_BrokenValueMapping()) |
| 87 | + |
| 88 | + assert isinstance(exc_info.value.original_error, RuntimeError) |
| 89 | + |
| 90 | + |
| 91 | +def test_read_string_key_mapping_preserves_hyperbrowser_value_failures(): |
| 92 | + with pytest.raises(HyperbrowserError, match="custom value read failure") as exc_info: |
| 93 | + _read_mapping(_HyperbrowserValueFailureMapping()) |
| 94 | + |
| 95 | + assert exc_info.value.original_error is None |
| 96 | + |
| 97 | + |
| 98 | +def test_read_string_key_mapping_falls_back_for_unreadable_key_display(): |
| 99 | + with pytest.raises( |
| 100 | + HyperbrowserError, match="failed value for '<unreadable key>'" |
| 101 | + ): |
| 102 | + read_string_key_mapping( |
| 103 | + _BrokenValueMapping(), |
| 104 | + expected_mapping_error="expected mapping", |
| 105 | + read_keys_error="failed keys", |
| 106 | + non_string_key_error_builder=lambda key: ( |
| 107 | + f"non-string key: {type(key).__name__}" |
| 108 | + ), |
| 109 | + read_value_error_builder=lambda key_display: ( |
| 110 | + f"failed value for '{key_display}'" |
| 111 | + ), |
| 112 | + key_display=lambda key: key.encode("utf-8"), |
| 113 | + ) |
0 commit comments