from transform import ( clean_listing_address, extract_full_postcode, transform_property, ) class StubPostcodeIndex: def nearest(self, lat: float, lng: float) -> str: return "SW1A 9ZZ" def test_extract_full_postcode_normalizes_spacing() -> None: assert extract_full_postcode("10 Downing Street SW1A2AA") == "SW1A 2AA" assert extract_full_postcode("10 Downing Street, SW1A 2AA") == "SW1A 2AA" assert extract_full_postcode("Downing Street, Westminster") is None def test_clean_listing_address_removes_postcode_and_outcode_suffixes() -> None: assert clean_listing_address("10 Downing Street, SW1A 2AA") == "10 Downing Street" assert clean_listing_address("Hawthorne Road, Bromley, Kent, BR1") == ( "Hawthorne Road, Bromley, Kent" ) assert clean_listing_address("Kings Avenue, Bromley") == "Kings Avenue, Bromley" def test_rightmove_transform_prefers_postcode_from_display_address() -> None: prop = { "id": "123", "location": {"latitude": 51.5, "longitude": -0.1}, "price": {"amount": 750000, "displayPrices": []}, "propertySubType": "Terraced", "bedrooms": 3, "bathrooms": 1, "keyFeatures": [], "propertyUrl": "/properties/123", "displayAddress": "Flat 2, 10 Downing Street, SW1A 2AA", } result = transform_property(prop, "SW1A", StubPostcodeIndex()) assert result is not None assert result["Postcode"] == "SW1A 2AA" assert result["Postcode source"] == "address" assert result["Extracted postcode"] == "SW1A 2AA" assert result["Inferred postcode"] == "SW1A 9ZZ" assert result["Listing raw address"] == "Flat 2, 10 Downing Street, SW1A 2AA" assert result["Address per Property Register"] == "Flat 2, 10 Downing Street"