Beispiel #1
0
    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
                )
            )
Beispiel #2
0
    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
                )
            )
Beispiel #3
0
    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)
Beispiel #4
0
    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)
Beispiel #5
0
    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)
Beispiel #6
0
    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)
Beispiel #7
0
    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)
Beispiel #8
0
    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)
Beispiel #9
0
 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)
Beispiel #10
0
 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)