|
| 1 | +"""Backward-compat tests for the deprecated ``GeoPostalArea`` alias. |
| 2 | +
|
| 3 | +AdCP 3.1.0-rc.10 restructured postal-area targeting: the inline |
| 4 | +``{system, values}`` ``GeoPostalArea`` shape was replaced by the |
| 5 | +``PostalArea`` union of a native per-country arm (``{country, system, |
| 6 | +values}``) and a legacy arm (``{system, values}`` with country-fused tokens |
| 7 | +like ``us_zip``). ``GeoPostalArea`` is retained as a deprecated alias to the |
| 8 | +legacy arm so old import and construction code keeps working. |
| 9 | +
|
| 10 | +These tests exercise the public surface (``from adcp.types import ...``) and |
| 11 | +the public ``PostalArea`` / ``TargetingOverlay`` types — the wire contract, |
| 12 | +not the generated class layout. |
| 13 | +""" |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import warnings |
| 18 | + |
| 19 | +import pytest |
| 20 | + |
| 21 | +from adcp.types import PostalArea, TargetingOverlay |
| 22 | + |
| 23 | +# The 11 legacy country-fused tokens that the removed GeoPostalArea accepted. |
| 24 | +LEGACY_TOKENS = [ |
| 25 | + "us_zip", |
| 26 | + "us_zip_plus_four", |
| 27 | + "gb_outward", |
| 28 | + "gb_full", |
| 29 | + "ca_fsa", |
| 30 | + "ca_full", |
| 31 | + "de_plz", |
| 32 | + "fr_code_postal", |
| 33 | + "au_postcode", |
| 34 | + "ch_plz", |
| 35 | + "at_plz", |
| 36 | +] |
| 37 | + |
| 38 | + |
| 39 | +def _geo_postal_area_cls() -> type: |
| 40 | + """Access the deprecated alias without raising the DeprecationWarning.""" |
| 41 | + import adcp.types |
| 42 | + |
| 43 | + with warnings.catch_warnings(): |
| 44 | + warnings.simplefilter("ignore", DeprecationWarning) |
| 45 | + return adcp.types.GeoPostalArea |
| 46 | + |
| 47 | + |
| 48 | +def test_geo_postal_area_import_still_works(): |
| 49 | + """``from adcp.types import GeoPostalArea`` does not raise ImportError.""" |
| 50 | + import adcp.types |
| 51 | + |
| 52 | + with warnings.catch_warnings(): |
| 53 | + warnings.simplefilter("ignore", DeprecationWarning) |
| 54 | + # Attribute access exercises the same PEP 562 ``__getattr__`` path as |
| 55 | + # a ``from adcp.types import GeoPostalArea`` statement. |
| 56 | + assert adcp.types.GeoPostalArea is not None |
| 57 | + |
| 58 | + |
| 59 | +def test_geo_postal_area_access_emits_deprecation_warning(): |
| 60 | + """Accessing the alias emits a DeprecationWarning naming the migration.""" |
| 61 | + import adcp.types |
| 62 | + |
| 63 | + with pytest.warns(DeprecationWarning, match="GeoPostalArea is deprecated"): |
| 64 | + alias = adcp.types.GeoPostalArea |
| 65 | + |
| 66 | + assert alias is not None |
| 67 | + |
| 68 | + |
| 69 | +def test_geo_postal_area_warning_names_postalarea_migration(): |
| 70 | + """The warning points migrators at the PostalArea native arm.""" |
| 71 | + import adcp.types |
| 72 | + |
| 73 | + with pytest.warns(DeprecationWarning) as record: |
| 74 | + _ = adcp.types.GeoPostalArea |
| 75 | + |
| 76 | + message = str(record[0].message) |
| 77 | + assert "PostalArea" in message |
| 78 | + assert "backward compatibility" in message |
| 79 | + assert "future major" in message |
| 80 | + |
| 81 | + |
| 82 | +def test_old_construction_shape_still_works(): |
| 83 | + """Constructing the old ``{system, values}`` shape succeeds.""" |
| 84 | + geo_postal_area = _geo_postal_area_cls() |
| 85 | + |
| 86 | + area = geo_postal_area(system="us_zip", values=["10001", "10002"]) |
| 87 | + |
| 88 | + dumped = area.model_dump(mode="json") |
| 89 | + assert dumped == {"system": "us_zip", "values": ["10001", "10002"]} |
| 90 | + |
| 91 | + |
| 92 | +@pytest.mark.parametrize("token", LEGACY_TOKENS) |
| 93 | +def test_every_legacy_token_constructs(token: str): |
| 94 | + """All 11 removed-type fused tokens still construct via the alias.""" |
| 95 | + geo_postal_area = _geo_postal_area_cls() |
| 96 | + |
| 97 | + area = geo_postal_area(system=token, values=["X1"]) |
| 98 | + assert area.model_dump(mode="json") == {"system": token, "values": ["X1"]} |
| 99 | + |
| 100 | + |
| 101 | +def test_constructed_value_validates_against_postalarea_union_legacy_arm(): |
| 102 | + """The constructed legacy value validates through the PostalArea union. |
| 103 | +
|
| 104 | + Passing the constructed instance preserves the legacy arm — the union |
| 105 | + resolves to the legacy ``{system, values}`` shape rather than injecting a |
| 106 | + spurious ``country``. |
| 107 | + """ |
| 108 | + geo_postal_area = _geo_postal_area_cls() |
| 109 | + |
| 110 | + area = geo_postal_area(system="gb_outward", values=["SW1A"]) |
| 111 | + |
| 112 | + validated = PostalArea.model_validate(area) |
| 113 | + # Legacy arm round-trips faithfully with no injected country field. |
| 114 | + assert validated.model_dump(mode="json") == { |
| 115 | + "system": "gb_outward", |
| 116 | + "values": ["SW1A"], |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | +def test_legacy_value_accepted_where_geo_postal_areas_used(): |
| 121 | + """Legacy postal areas are accepted in ``TargetingOverlay.geo_postal_areas``.""" |
| 122 | + geo_postal_area = _geo_postal_area_cls() |
| 123 | + |
| 124 | + overlay = TargetingOverlay( |
| 125 | + geo_postal_areas=[ |
| 126 | + geo_postal_area(system="us_zip", values=["10001"]), |
| 127 | + geo_postal_area(system="gb_outward", values=["SW1A"]), |
| 128 | + ] |
| 129 | + ) |
| 130 | + |
| 131 | + dumped = overlay.model_dump(mode="json", exclude_none=True)["geo_postal_areas"] |
| 132 | + assert dumped == [ |
| 133 | + {"system": "us_zip", "values": ["10001"]}, |
| 134 | + {"system": "gb_outward", "values": ["SW1A"]}, |
| 135 | + ] |
| 136 | + |
| 137 | + |
| 138 | +def test_unknown_attribute_still_raises_attribute_error(): |
| 139 | + """The shim does not swallow genuinely missing attributes.""" |
| 140 | + import adcp.types |
| 141 | + |
| 142 | + with pytest.raises(AttributeError): |
| 143 | + _ = adcp.types.DefinitelyNotAPublicType |
0 commit comments