def run_parser(self, postcode, strict, incode_mandatory, expected): if inspect.isclass(expected) and issubclass(expected, Exception): try: parse_uk_postcode(postcode, strict, incode_mandatory) except expected: pass except Exception as e: m = '{!r} raised instead of expected {!r} for postcode={!r}, strict={!r} and' \ ' incode_mandatory={!r}' self.fail(m.format( e.__class__.__name__, expected.__name__, postcode, strict, incode_mandatory )) else: m = '{!r} exception not raised for postcode={!r}, strict={!r} and ' \ 'incode_mandatory={!r}' self.fail(m.format(expected.__name__, postcode, strict, incode_mandatory)) else: result = parse_uk_postcode(postcode, strict, incode_mandatory) m = 'Expected {!r} but got {!r} for postcode={!r}, strict={!r} and ' \ 'incode_mandatory={!r}' self.assertEquals( expected, result, m.format( expected, result, postcode, strict, incode_mandatory ) )
def test_invalid_postcode_error_is_value_error(self): """ Previous iterations of the code raised a ValueError when postcode was found to be invalid. New exceptions should be caught in the same way in case any code relies on it. """ with self.assertRaises(ValueError) as cm: parse_uk_postcode('xx0 2yr', True, True) self.assertEquals(cm.exception.__class__, InvalidPostcodeError)
def test_max_length_exceeded_error_is_value_error(self): """ Previous iterations of the code raised a ValueError when the postcode provided was too long. New exceptions should be caught in the same way in case any code relies on it. """ with self.assertRaises(ValueError) as cm: parse_uk_postcode('N16 8QSSS', True, True) self.assertEquals(cm.exception.__class__, MaxLengthExceededError)
def test_incode_not_found_error_is_value_error(self): """ Previous iterations of the code raised a ValueError exception when incode was expected but not found. New exceptions should be caught in the same way in case any code relies on it. """ with self.assertRaises(ValueError) as cm: parse_uk_postcode('N16', True, True) self.assertEquals(cm.exception.__class__, IncodeNotFoundError)
def test_all(self): for postcode, strict, incode_mandatory, required_result in test_data: try: actual_result = parse_uk_postcode(postcode, strict, incode_mandatory) except ValueError: actual_result = 'ValueError' self.assertEqual(actual_result, required_result)