def test_postal_code_normalization(self) -> None: address = Address( line1="10, avenue des Champs Elysées", postal_code=" - f- - - 75008 - ", city_name="Paris", country_code="FR", ) assert address.postal_code == "F-75008" address = Address( line1="10, avenue des Champs Elysées", postal_code="-- aAA 77b - - - --___--- sd- fs - df" "sd--$^$^$^---fsf -sd xd --", city_name="Paris", country_code="FR", ) assert address.postal_code == "AAA 77B-SD-FS-DFSD-FSF-SD XD" address = Address( line1="10, avenue des Champs Elysées", postal_code="J/PPB1>6/_", city_name="Paris", country_code="FR", ) assert address.postal_code == "JPPB16" address = Address( line1="10, avenue des Champs Elysées", postal_code=" * * * aAA 77b - -", city_name="Paris", country_code="FR", ) assert address.postal_code == "AAA 77B"
def test_postal_code_normalization(self): address = Address(line1='10, avenue des Champs Elysées', postal_code=' - f- - - 75008 - ', city_name='Paris', country_code='FR') self.assertEqual(address.postal_code, 'F-75008') address = Address( line1='10, avenue des Champs Elysées', postal_code='-- aAA 77b - - - --___--- sd- fs - df' 'sd--$^$^$^---fsf -sd xd --', city_name='Paris', country_code='FR') self.assertEqual(address.postal_code, 'AAA 77B-SD-FS-DFSD-FSF-SD XD') address = Address(line1='10, avenue des Champs Elysées', postal_code='J/PPB1>6/_', city_name='Paris', country_code='FR') self.assertEqual(address.postal_code, 'JPPB16') address = Address(line1='10, avenue des Champs Elysées', postal_code=' * * * aAA 77b - -', city_name='Paris', country_code='FR') self.assertEqual(address.postal_code, 'AAA 77B')
def test_country_subdivision_reconciliation(self): # Perfect, already normalized country and subdivision. address1 = Address(line1='1273 Pale San Vitores Road', postal_code='96913', city_name='Tamuning', country_code='GU', subdivision_code='US-GU') # Non-normalized country. address2 = Address(line1='1273 Pale San Vitores Road', postal_code='96913', city_name='Tamuning', country_code='US', subdivision_code='US-GU') # Country only, from which we guess the subdivision. address3 = Address(line1='1273 Pale San Vitores Road', postal_code='96913', city_name='Tamuning', country_code='GU') # Subdivision only, from which we derive the country. address4 = Address(line1='1273 Pale San Vitores Road', postal_code='96913', city_name='Tamuning', subdivision_code='US-GU') for address in [address1, address2, address3, address4]: self.assertEqual(address.line1, '1273 Pale San Vitores Road') self.assertEqual(address.line2, None) self.assertEqual(address.postal_code, '96913') self.assertEqual(address.city_name, 'Tamuning') self.assertEqual(address.country_code, 'GU') self.assertEqual(address.subdivision_code, 'US-GU')
def test_city_override_by_subdivision(self) -> None: Address( line1="2 King Edward Street", postal_code="EC1A 1HQ", city_name="London, City of", subdivision_code="GB-LND", ) with pytest.raises(InvalidAddress) as expt: Address( line1="2 King Edward Street", postal_code="EC1A 1HQ", city_name="Paris", subdivision_code="GB-LND", ) err = expt.value assert err.required_fields == set() assert err.invalid_fields == {} assert err.inconsistent_fields == {("city_name", "subdivision_code")} assert "required" not in str(err) assert "invalid" not in str(err) assert "inconsistent" in str(err) # Make sure no error is raised when using replace_city_name=False address = Address( line1="2 King Edward Street", postal_code="EC1A 1HQ", city_name="Paris", subdivision_code="GB-LND", replace_city_name=False, ) assert address.city_name == "Paris"
def test_invalid_code_normalization(self) -> None: # Invalid country and subdivision codes are normalized to None. address = Address( line1="10, avenue des Champs Elysées", postal_code="75008", city_name="Paris", subdivision_code="42", ) assert address.country_code is None assert address.subdivision_code is None assert address.valid is False with pytest.raises(InvalidAddress) as expt: address.validate() err = expt.value assert err.required_fields == {"country_code"} assert err.invalid_fields == {} assert err.inconsistent_fields == set() assert "required" in str(err) assert "invalid" not in str(err) assert "inconsistent" not in str(err) address = Address( line1="10, avenue des Champs Elysées", postal_code="75008", city_name="Paris", country_code="MARS", ) assert address.country_code is None assert address.subdivision_code is None assert address.valid is False with pytest.raises(InvalidAddress) as expt: address.validate() err = expt.value assert err.required_fields == {"country_code"} assert err.invalid_fields == {} assert err.inconsistent_fields == set() assert "required" in str(err) assert "invalid" not in str(err) assert "inconsistent" not in str(err) address = Address( line1="10, avenue des Champs Elysées", postal_code="75008", city_name="Paris", country_code="MARS", subdivision_code="42", ) assert address.country_code is None assert address.subdivision_code is None assert address.valid is False with pytest.raises(InvalidAddress) as expt: address.validate() err = expt.value assert err.required_fields == {"country_code"} assert err.invalid_fields == {} assert err.inconsistent_fields == set() assert "required" in str(err) assert "invalid" not in str(err) assert "inconsistent" not in str(err)
def test_invalid_code_normalization(self): # Invalid country and subdivision codes are normalized to None. address = Address(line1='10, avenue des Champs Elysées', postal_code='75008', city_name='Paris', subdivision_code='42') self.assertEquals(address.country_code, None) self.assertEquals(address.subdivision_code, None) self.assertEquals(address.valid, False) with self.assertRaises(InvalidAddress) as expt: address.validate() err = expt.exception self.assertEquals(err.required_fields, set(['country_code'])) self.assertEquals(err.invalid_fields, dict()) self.assertEquals(err.inconsistent_fields, set()) self.assertIn('required', str(err)) self.assertNotIn('invalid', str(err)) self.assertNotIn('inconsistent', str(err)) address = Address(line1='10, avenue des Champs Elysées', postal_code='75008', city_name='Paris', country_code='MARS') self.assertEquals(address.country_code, None) self.assertEquals(address.subdivision_code, None) self.assertEquals(address.valid, False) with self.assertRaises(InvalidAddress) as expt: address.validate() err = expt.exception self.assertEquals(err.required_fields, set(['country_code'])) self.assertEquals(err.invalid_fields, dict()) self.assertEquals(err.inconsistent_fields, set()) self.assertIn('required', str(err)) self.assertNotIn('invalid', str(err)) self.assertNotIn('inconsistent', str(err)) address = Address(line1='10, avenue des Champs Elysées', postal_code='75008', city_name='Paris', country_code='MARS', subdivision_code='42') self.assertEquals(address.country_code, None) self.assertEquals(address.subdivision_code, None) self.assertEquals(address.valid, False) with self.assertRaises(InvalidAddress) as expt: address.validate() err = expt.exception self.assertEquals(err.required_fields, set(['country_code'])) self.assertEquals(err.invalid_fields, dict()) self.assertEquals(err.inconsistent_fields, set()) self.assertIn('required', str(err)) self.assertNotIn('invalid', str(err)) self.assertNotIn('inconsistent', str(err))
def test_subdivision_derived_fields(self) -> None: address = Address( line1="31, place du Théatre", postal_code="59000", city_name="Lille", subdivision_code="FR-59", ) assert address.subdivision == subdivisions.get(code="FR-59") assert address.subdivision_code == "FR-59" assert address.subdivision_name == "Nord" assert address.subdivision_type_name == "Metropolitan department" assert address.subdivision_type_id == "metropolitan_department" assert address.metropolitan_department == subdivisions.get( code="FR-59") assert address.metropolitan_department_area_code == "FR-59" assert address.metropolitan_department_name == "Nord" assert address.metropolitan_department_type_name == "Metropolitan department" assert address.metropolitan_region == subdivisions.get(code="FR-HDF") assert address.metropolitan_region_area_code == "FR-HDF" assert address.metropolitan_region_name == "Hauts-de-France" assert address.metropolitan_region_type_name == "Metropolitan region" assert address.country == countries.get(alpha_2="FR") assert address.country_code == "FR" assert address.country_name == "France"
def test_subdivision_type_id_collision(self) -> None: # The subdivision metadata IDs we derived from subdivision types should # not collide with Address class internals. simple_address = Address( line1="10, avenue des Champs Elysées", postal_code="75008", city_name="Paris", country_code="FR", ) # Check each subdivision metadata. for subdiv in subdivisions: # XXX ISO 3166-2 reuse the country type as subdivisions. # We really need to add proper support for these cases, as we did # for cities. if subdivision_type_id(subdiv) in ["country"]: continue for metadata_id in subdivision_metadata(subdiv): # Check collision with any atrribute defined on Address class. if metadata_id in Address.SUBDIVISION_METADATA_WHITELIST: assert hasattr(simple_address, metadata_id) else: assert not hasattr(simple_address, metadata_id)
def test_dict_access(self): address = Address(line1='10, avenue des Champs Elysées', postal_code='75008', city_name='Paris', country_code='FR') self.assertSequenceEqual( set([ 'line1', 'line2', 'postal_code', 'city_name', 'country_code', 'subdivision_code', ]), set(address.keys())) self.assertEquals(len(address), 6) self.assertEquals( set([ None, '10, avenue des Champs Elysées', '75008', 'Paris', 'FR' ]), set(address.values())) self.assertEquals( { 'line1': '10, avenue des Champs Elysées', 'line2': None, 'postal_code': '75008', 'city_name': 'Paris', 'country_code': 'FR', 'subdivision_code': None, }, dict(address.items())) for key in address: self.assertEquals(getattr(address, key), address[key])
def test_subdivision_derived_fields(self): address = Address(line1='31, place du Théatre', postal_code='59000', city_name='Lille', subdivision_code='FR-59') self.assertEquals(address.subdivision, subdivisions.get(code='FR-59')) self.assertEquals(address.subdivision_code, 'FR-59') self.assertEquals(address.subdivision_name, 'Nord') self.assertEquals(address.subdivision_type_name, 'Metropolitan department') self.assertEquals(address.subdivision_type_id, 'metropolitan_department') self.assertEquals(address.metropolitan_department, subdivisions.get(code='FR-59')) self.assertEquals(address.metropolitan_department_area_code, 'FR-59') self.assertEquals(address.metropolitan_department_name, 'Nord') self.assertEquals(address.metropolitan_department_type_name, 'Metropolitan department') self.assertEquals(address.metropolitan_region, subdivisions.get(code='FR-HDF')) self.assertEquals(address.metropolitan_region_area_code, 'FR-HDF') self.assertEquals(address.metropolitan_region_name, 'Hauts-de-France') self.assertEquals(address.metropolitan_region_type_name, 'Metropolitan region') self.assertEquals(address.country, countries.get(alpha_2='FR')) self.assertEquals(address.country_code, 'FR') self.assertEquals(address.country_name, 'France')
def test_dict_access(self) -> None: address = Address( line1="10, avenue des Champs Elysées", postal_code="75008", city_name="Paris", country_code="FR", ) assert { "line1", "line2", "postal_code", "city_name", "country_code", "subdivision_code", } == set(address) assert len(address) == 6 assert {"10, avenue des Champs Elysées", "75008", "Paris", "FR", None} == set(address.values()) assert { "line1": "10, avenue des Champs Elysées", "line2": None, "postal_code": "75008", "city_name": "Paris", "country_code": "FR", "subdivision_code": None, } == dict(address.items()) for key in address: assert getattr(address, key) == address[key]
def test_emptiness(self) -> None: address = Address() assert address.empty is True assert not address address.line1 = "10, avenue des Champs Elysées" assert address.empty is False assert address
def test_blank_line_swap(self): address = Address(line1='', line2='10, avenue des Champs Elysées', postal_code='75008', city_name='Paris', country_code='FR') self.assertEquals(address.line1, '10, avenue des Champs Elysées') self.assertEquals(address.line2, None)
def test_unknown_field(self) -> None: # Test constructor. with pytest.raises(KeyError): Address(bad_field="Blah blah blah") # Test item setter. address = random_address() with pytest.raises(KeyError): address["bad_field"] = "Blah blah blah"
def test_blank_string_normalization(self): address = Address(line1='10, avenue des Champs Elysées', line2='', postal_code='75008', city_name='Paris', country_code='FR', subdivision_code='') self.assertEquals(address.line2, None) self.assertEquals(address.subdivision_code, None)
def test_unknown_field(self): # Test constructor. with self.assertRaises(KeyError): Address(bad_field='Blah blah blah') # Test item setter. address = random_address() with self.assertRaises(KeyError): address['bad_field'] = 'Blah blah blah'
def test_emptiness(self): address = Address() self.assertTrue(address.empty) self.assertFalse(address) self.assertTrue(not address) address.line1 = '10, avenue des Champs Elysées' self.assertFalse(address.empty) self.assertTrue(address) self.assertFalse(not address)
def test_blank_line_swap(self) -> None: address = Address( line1="", line2="10, avenue des Champs Elysées", postal_code="75008", city_name="Paris", country_code="FR", ) assert address.line1 == "10, avenue des Champs Elysées" assert address.line2 is None
def test_city_override_by_subdivision(self): Address(line1='2 King Edward Street', postal_code='EC1A 1HQ', city_name='London, City of', subdivision_code='GB-LND') with self.assertRaises(InvalidAddress) as expt: Address(line1='2 King Edward Street', postal_code='EC1A 1HQ', city_name='Paris', subdivision_code='GB-LND') err = expt.exception self.assertEquals(err.required_fields, set()) self.assertEquals(err.invalid_fields, dict()) self.assertEquals(err.inconsistent_fields, set([('city_name', 'subdivision_code')])) self.assertNotIn('required', str(err)) self.assertNotIn('invalid', str(err)) self.assertIn('inconsistent', str(err))
def test_unicode_mess(self): address = Address(line1='ब ♎ 1F: ̹ƶώ㎂🐎🐙💊 ꧲⋉ ⦼ Ė꧵┵', line2='⫇⻛⋯ ǖ╶🐎🐙💊ᵞᚘ⎢ ⚗ ⑆ ͋ụ 0 ⇚ � ῐ ', postal_code='3☾Ă⻛🐎🐙💊ȁ�ƈDŽ✒', city_name='Į🐎🐙💊❤Ệ▋', country_code='FR') self.assertIsNotNone(address.line1) self.assertIsNotNone(address.line2) self.assertIsNotNone(address.postal_code) self.assertIsNotNone(address.city_name)
def test_country_subdivision_validation(self) -> None: Address( line1="10, avenue des Champs Elysées", postal_code="75008", city_name="Paris", country_code="FR", subdivision_code="FR-75", ) with pytest.raises(InvalidAddress) as expt: Address( line1="10, avenue des Champs Elysées", postal_code="75008", city_name="Paris", country_code="FR", subdivision_code="BE-BRU", ) err = expt.value assert err.required_fields == set() assert err.invalid_fields == {} assert err.inconsistent_fields == {("country_code", "subdivision_code") } assert "required" not in str(err) assert "invalid" not in str(err) assert "inconsistent" in str(err) with pytest.raises(InvalidAddress) as expt: Address( line1="10, avenue des Champs Elysées", postal_code="75008", city_name="Paris", country_code="FR", subdivision_code="US-GU", ) err = expt.value assert err.required_fields == set() assert err.invalid_fields == {} assert err.inconsistent_fields == {("country_code", "subdivision_code") } assert "required" not in str(err) assert "invalid" not in str(err) assert "inconsistent" in str(err)
def test_blank_string_normalization(self) -> None: address = Address( line1="10, avenue des Champs Elysées", line2="", postal_code="75008", city_name="Paris", country_code="FR", subdivision_code="", ) assert address.line2 is None assert address.subdivision_code is None
def test_default_values(self): address = Address(line1='10, avenue des Champs Elysées', postal_code='75008', city_name='Paris', country_code='FR') self.assertEquals(address.line1, '10, avenue des Champs Elysées') self.assertEquals(address.line2, None) self.assertEquals(address.postal_code, '75008') self.assertEquals(address.city_name, 'Paris') self.assertEquals(address.country_code, 'FR') self.assertEquals(address.subdivision_code, None)
def test_unicode_mess(self) -> None: address = Address( line1="ब ♎ 1F: ̹ƶώ㎂🐎🐙💊 ꧲⋉ ⦼ Ė꧵┵", line2="⫇⻛⋯ ǖ╶🐎🐙💊ᵞᚘ⎢ ⚗ ⑆ ͋ụ 0 ⇚ � ῐ ", postal_code="3☾Ă⻛🐎🐙💊ȁ�ƈDŽ✒", city_name="Į🐎🐙💊❤Ệ▋", country_code="FR", ) assert address.line1 is not None assert address.line2 is not None assert address.postal_code is not None assert address.city_name is not None
def test_default_values(self) -> None: address = Address( line1="10, avenue des Champs Elysées", postal_code="75008", city_name="Paris", country_code="FR", ) assert address.line1 == "10, avenue des Champs Elysées" assert address.line2 is None assert address.postal_code == "75008" assert address.city_name == "Paris" assert address.country_code == "FR" assert address.subdivision_code is None
def test_non_string_field_value(self): # Test constructor. with self.assertRaises(TypeError): Address(line1=Decimal()) # Test attribute setter. address = random_address() with self.assertRaises(TypeError): address.line1 = Decimal() # Test item setter. with self.assertRaises(TypeError): address['line1'] = Decimal()
def test_non_string_field_value(self) -> None: # Test constructor. with pytest.raises(TypeError): Address(line1=Decimal()) # type: ignore # Test attribute setter. address = random_address() with pytest.raises(TypeError): address.line1 = Decimal() # type: ignore # Test item setter. with pytest.raises(TypeError): address["line1"] = Decimal()
def test_space_normalization(self): address = Address(line1=' 10, avenue des \n Champs Elysées ', line2=' ', postal_code=' F 75008 ', city_name=' Paris City ', country_code=' fr ', subdivision_code=' fR-75 ') self.assertEqual(address.line1, '10, avenue des Champs Elysées') self.assertEqual(address.line2, None) self.assertEqual(address.postal_code, 'F 75008') self.assertEqual(address.city_name, 'Paris City') self.assertEqual(address.country_code, 'FR') self.assertEqual(address.subdivision_code, 'FR-75')
def test_subdivision_derived_country(self): address = Address(line1='Senate House', line2='Tyndall Avenue', postal_code='BS8 1TH', city_name='Bristol', subdivision_code='GB-ENG') self.assertEquals(address.subdivision, subdivisions.get(code='GB-ENG')) self.assertEquals(address.subdivision_code, 'GB-ENG') self.assertEquals(address.subdivision_name, 'England') self.assertEquals(address.subdivision_type_name, 'Country') self.assertEquals(address.subdivision_type_id, 'country') self.assertEquals(address.country_code, 'GB')
def test_country_subdivision_validation(self): Address(line1='10, avenue des Champs Elysées', postal_code='75008', city_name='Paris', country_code='FR', subdivision_code='FR-75') with self.assertRaises(InvalidAddress) as expt: Address(line1='10, avenue des Champs Elysées', postal_code='75008', city_name='Paris', country_code='FR', subdivision_code='BE-BRU') err = expt.exception self.assertEquals(err.required_fields, set()) self.assertEquals(err.invalid_fields, dict()) self.assertEquals(err.inconsistent_fields, set([('country_code', 'subdivision_code')])) self.assertNotIn('required', str(err)) self.assertNotIn('invalid', str(err)) self.assertIn('inconsistent', str(err)) with self.assertRaises(InvalidAddress) as expt: Address(line1='10, avenue des Champs Elysées', postal_code='75008', city_name='Paris', country_code='FR', subdivision_code='US-GU') err = expt.exception self.assertEquals(err.required_fields, set()) self.assertEquals(err.invalid_fields, dict()) self.assertEquals(err.inconsistent_fields, set([('country_code', 'subdivision_code')])) self.assertNotIn('required', str(err)) self.assertNotIn('invalid', str(err)) self.assertIn('inconsistent', str(err))