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, set()) 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, set()) 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, set()) self.assertEquals(err.inconsistent_fields, set()) self.assertIn('required', str(err)) self.assertNotIn('invalid', str(err)) self.assertNotIn('inconsistent', 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_address_validation(self): # Test valid address. address = Address( line1='address_line1', line2='address_line2', postal_code='75000', city_name='Paris', country_code='US', subdivision_code=None) self.assertEquals(address.valid, True) # Test required fields at validation. address = Address( line1=None, postal_code=None, city_name=None, country_code=None) self.assertEquals(address.valid, False) with self.assertRaises(InvalidAddress) as expt: address.validate() err = expt.exception self.assertEquals( err.required_fields, set(['line1', 'postal_code', 'city_name', 'country_code'])) self.assertEquals(err.invalid_fields, set()) self.assertEquals(err.inconsistent_fields, set()) self.assertIn('required', str(err)) self.assertNotIn('invalid', str(err)) self.assertNotIn('inconsistent', str(err)) # Test post-normalization validation of invalid country and subdivision # codes. address = Address( line1='Dummy street', postal_code='12345', city_name='Dummy city') self.assertEquals(address.valid, False) address.country_code = 'invalid-code' address.subdivision_code = 'stupid-code' with self.assertRaises(InvalidAddress) as expt: address.validate() err = expt.exception self.assertEquals(err.required_fields, set()) self.assertEquals( err.invalid_fields, set(['country_code', 'subdivision_code'])) self.assertEquals(err.inconsistent_fields, set()) self.assertNotIn('required', str(err)) self.assertIn('invalid', str(err)) self.assertNotIn('inconsistent', str(err)) # Mix invalid and required fields in post-normalization validation. address = Address( line1='Dummy street', postal_code='12345', city_name='Dummy city') self.assertEquals(address.valid, False) address.country_code = None address.subdivision_code = 'stupid-code' with self.assertRaises(InvalidAddress) as expt: address.validate() err = expt.exception self.assertEquals(err.required_fields, set(['country_code'])) self.assertEquals(err.invalid_fields, set(['subdivision_code'])) self.assertEquals(err.inconsistent_fields, set()) self.assertIn('required', str(err)) self.assertIn('invalid', str(err)) self.assertNotIn('inconsistent', str(err)) # Test post-normalization validation of inconsistent country and # subdivision codes. address = Address( line1='Dummy street', postal_code='12345', city_name='Dummy city') self.assertEquals(address.valid, False) address.country_code = 'FR' address.subdivision_code = 'US-CA' with self.assertRaises(InvalidAddress) as expt: address.validate() err = expt.exception self.assertEquals(err.required_fields, set()) self.assertEquals(err.invalid_fields, set()) 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))
def test_address_validation(self): # Test valid address. address = Address(line1='address_line1', line2='address_line2', postal_code='75000', city_name='Paris', country_code='US', subdivision_code=None) self.assertEquals(address.valid, True) # Test required fields at validation. address = Address(line1=None, postal_code=None, city_name=None, country_code=None) self.assertEquals(address.valid, False) with self.assertRaises(InvalidAddress) as expt: address.validate() err = expt.exception self.assertEquals( err.required_fields, set(['line1', 'postal_code', 'city_name', '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)) # Test post-normalization validation of invalid country and subdivision # codes. address = Address(line1='Dummy street', postal_code='12345', city_name='Dummy city') self.assertEquals(address.valid, False) address.country_code = 'invalid-code' address.subdivision_code = 'stupid-code' with self.assertRaises(InvalidAddress) as expt: address.validate() err = expt.exception self.assertEquals(err.required_fields, set()) self.assertEquals(err.invalid_fields, { 'country_code': 'invalid-code', 'subdivision_code': 'stupid-code' }) self.assertEquals(err.inconsistent_fields, set()) self.assertNotIn('required', str(err)) self.assertIn('invalid', str(err)) self.assertNotIn('inconsistent', str(err)) # Mix invalid and required fields in post-normalization validation. address = Address(line1='Dummy street', postal_code='12345', city_name='Dummy city') self.assertEquals(address.valid, False) address.country_code = None address.subdivision_code = 'stupid-code' with self.assertRaises(InvalidAddress) as expt: address.validate() err = expt.exception self.assertEquals(err.required_fields, set(['country_code'])) self.assertEquals(err.invalid_fields, {'subdivision_code': 'stupid-code'}) self.assertEquals(err.inconsistent_fields, set()) self.assertIn('required', str(err)) self.assertIn('invalid', str(err)) self.assertNotIn('inconsistent', str(err)) # Test post-normalization validation of inconsistent country and # subdivision codes. address = Address(line1='Dummy street', postal_code='12345', city_name='Dummy city') self.assertEquals(address.valid, False) address.country_code = 'FR' address.subdivision_code = 'US-CA' with self.assertRaises(InvalidAddress) as expt: address.validate() 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))
def test_address_validation(self) -> None: # Test valid address. address = Address( line1="address_line1", line2="address_line2", postal_code="75000", city_name="Paris", country_code="US", subdivision_code=None, ) assert address.valid is True # Test required fields at validation. address = Address(line1=None, postal_code=None, city_name=None, country_code=None) assert address.valid is False with pytest.raises(InvalidAddress) as expt: address.validate() err = expt.value assert err.required_fields == { "line1", "postal_code", "city_name", "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) # Test post-normalization validation of invalid country and subdivision # codes. address = Address(line1="Dummy street", postal_code="12345", city_name="Dummy city") assert address.valid is False address.country_code = "invalid-code" address.subdivision_code = "stupid-code" with pytest.raises(InvalidAddress) as expt: address.validate() err = expt.value assert err.required_fields == set() assert err.invalid_fields == { "country_code": "invalid-code", "subdivision_code": "stupid-code", } assert err.inconsistent_fields == set() assert "required" not in str(err) assert "invalid" in str(err) assert "inconsistent" not in str(err) # Mix invalid and required fields in post-normalization validation. address = Address(line1="Dummy street", postal_code="12345", city_name="Dummy city") assert address.valid is False address.country_code = None address.subdivision_code = "stupid-code" with pytest.raises(InvalidAddress) as expt: address.validate() err = expt.value assert err.required_fields == {"country_code"} assert err.invalid_fields == {"subdivision_code": "stupid-code"} assert err.inconsistent_fields == set() assert "required" in str(err) assert "invalid" in str(err) assert "inconsistent" not in str(err) # Test post-normalization validation of inconsistent country and # subdivision codes. address = Address(line1="Dummy street", postal_code="12345", city_name="Dummy city") assert address.valid is False address.country_code = "FR" address.subdivision_code = "US-CA" with pytest.raises(InvalidAddress) as expt: address.validate() 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)