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_extract_placeholders(): message = "the quick {color} fox {verb} over the lazy {animal}" assert OpenEOApiErrorSpecHelper.extract_placeholders(message) == { "color", "verb", "animal" }