def test_error_code(error_code): """ Check implementation of given OpenEO error code. If no implemented exception: fail and print suggested implementation If there is an implementation (or multiple): check correctness (status code, ...) """ spec_helper = OpenEOApiErrorSpecHelper() exceptions = get_defined_exceptions().get(error_code, []) if len(exceptions) == 0: print('Suggested implementation:') print(spec_helper.generate_exception_class(error_code)) raise Exception( "No implemented exception class for error code {e!r}".format( e=error_code)) error_spec = spec_helper.get(error_code) for exception_cls in exceptions: # Check that OpenEO error code is in exception class name. # TODO: is this really necessary? assert error_code.lower() in exception_cls.__name__.lower() assert exception_cls.status_code == error_spec["http"] assert set(exception_cls._tags) == set(error_spec["tags"]) # Check placeholder usage in message placeholders_spec = spec_helper.extract_placeholders( error_spec["message"]) placeholders_actual = spec_helper.extract_placeholders( exception_cls.message) assert placeholders_spec == placeholders_actual assert exception_cls.message == error_spec["message"] using_default_init = exception_cls.__init__ is OpenEOApiException.__init__ if placeholders_actual and using_default_init: raise Exception( "Exception class {c} has placeholder in message but no custom __init__" .format(c=exception_cls.__name__))
def test_generate_exception_class(): spec = { "OutOfTea": { "description": "No more tea.", "message": "The {color} tea pot is empty.", "http": 418, "tags": ["foo"] } } src_lines = OpenEOApiErrorSpecHelper(spec).generate_exception_class( "OutOfTea").split("\n") assert "class OutOfTeaException(OpenEOApiException):" in src_lines assert " status_code = 418" in src_lines assert " code = 'OutOfTea'" in src_lines assert " message = 'The {color} tea pot is empty.'" in src_lines assert " def __init__(self, color: str):" in src_lines assert " super().__init__(self.message.format(color=color))"
def test_extract_placeholders(): message = "the quick {color} fox {verb} over the lazy {animal}" assert OpenEOApiErrorSpecHelper.extract_placeholders(message) == { "color", "verb", "animal" }
def test_unknown_error_codes(): from_spec = set(OpenEOApiErrorSpecHelper().get_error_codes()) defined = set(get_defined_exceptions().keys()) defined_but_not_in_spec = defined.difference(from_spec) assert defined_but_not_in_spec == set()
"http": 418, "tags": ["foo"] } } src_lines = OpenEOApiErrorSpecHelper(spec).generate_exception_class( "OutOfTea").split("\n") assert "class OutOfTeaException(OpenEOApiException):" in src_lines assert " status_code = 418" in src_lines assert " code = 'OutOfTea'" in src_lines assert " message = 'The {color} tea pot is empty.'" in src_lines assert " def __init__(self, color: str):" in src_lines assert " super().__init__(self.message.format(color=color))" @pytest.mark.parametrize("error_code", OpenEOApiErrorSpecHelper().get_error_codes()) def test_error_code(error_code): """ Check implementation of given OpenEO error code. If no implemented exception: fail and print suggested implementation If there is an implementation (or multiple): check correctness (status code, ...) """ spec_helper = OpenEOApiErrorSpecHelper() exceptions = get_defined_exceptions().get(error_code, []) if len(exceptions) == 0: print('Suggested implementation:') print(spec_helper.generate_exception_class(error_code)) raise Exception( "No implemented exception class for error code {e!r}".format( e=error_code))