refactor error tests

This commit is contained in:
Florian Förster 2025-03-12 07:05:30 +01:00
parent 2262cfe45f
commit 78d72df015

View File

@ -11,10 +11,14 @@ from delta_barth.constants import DEFAULT_API_ERR_CODE, DEFAULT_INTERNAL_ERR_COD
from delta_barth.types import DelBarApiError, Status
def test_status_handler_parsing():
@pytest.fixture(scope="module")
def status_hdlr() -> errors.StatusHandler:
return errors.StatusHandler()
def test_status_handler_parsing(status_hdlr):
predef_errs = errors.DATA_PIPELINE_STATUS_DESCR
status_hdlr = errors.StatusHandler()
assert status_hdlr.pipe_states is not None
parsed_pipe_states = status_hdlr.pipe_states
assert parsed_pipe_states.SUCCESS == status_hdlr.SUCCESS
@ -30,12 +34,11 @@ def test_status_handler_parsing():
status_hdlr._parse_data_pipe_states()
def test_status_handler_internal():
def test_status_handler_InternalErrors_Success(status_hdlr):
DESCRIPTION = "test case"
MESSAGE = "an error occurred"
ERR_CODE = 101
status_hdlr = errors.StatusHandler()
new_err = status_hdlr.error(
description=DESCRIPTION,
message=MESSAGE,
@ -45,23 +48,29 @@ def test_status_handler_internal():
assert new_err.description == DESCRIPTION
assert new_err.message == MESSAGE
# failure cases
err_code = 50 # default lower bound: 100
def test_status_handler_InternalErrors_CodeOutOfBounds(status_hdlr):
DESCRIPTION = "test case"
MESSAGE = "an error occurred"
err_code = DEFAULT_INTERNAL_ERR_CODE - 1
with pytest.raises(ValueError):
new_err = status_hdlr.error(
_ = status_hdlr.error(
description=DESCRIPTION,
message=MESSAGE,
code=err_code,
)
err_code = 500 # default upper bound: 400
err_code = DEFAULT_API_ERR_CODE + 1
with pytest.raises(ValueError):
new_err = status_hdlr.error(
_ = status_hdlr.error(
description=DESCRIPTION,
message=MESSAGE,
code=err_code,
)
def test_status_handler_api_error():
def test_status_handler_ApiError(status_hdlr):
MESSAGE = "an error occurred"
api_err = DelBarApiError(status_code=401, message="test case")
assert api_err.status_code == 401
@ -76,12 +85,13 @@ def test_status_handler_api_error():
assert new_err.api_server_error == api_err
def test_status_handler_raise_for_status():
status_hdlr = errors.StatusHandler()
def test_status_handler_raise_for_status_Success(status_hdlr):
# success: should not raise
err_status = status_hdlr.SUCCESS
assert status_hdlr.raise_for_status(err_status) is None
def test_status_handler_raise_for_status_PredefinedErrors(status_hdlr):
# data related errors (predefined)
err_status = status_hdlr.pipe_states.BAD_QUALITY
err_descr = err_status.description
@ -92,6 +102,9 @@ def test_status_handler_raise_for_status():
descr = str(err)
assert err_descr in descr
raise err
def test_status_handler_raise_for_status_InternalErrors(status_hdlr):
# internal error, not data-related
description = "test case"
message = "an error occurred"
@ -108,6 +121,9 @@ def test_status_handler_raise_for_status():
descr = str(err)
assert description in descr
raise err
def test_status_handler_raise_for_status_ExternalApiErrors(status_hdlr):
# external API error
api_err = DelBarApiError(status_code=401, message="test case", code="1234")
description = "Kommunikation mit dem API-Server aufgetreten"
@ -123,6 +139,7 @@ def test_status_handler_raise_for_status():
raise err
@pytest.mark.new
def test_status_handler_exception_parsing():
status_hdlr = errors.StatusHandler()
test_message = "Test Exception"
@ -134,14 +151,15 @@ def test_status_handler_exception_parsing():
assert status.message == test_message
def test_not_set():
def test_NotSet():
not_set = errors.NotSet()
# using slots, dynamic attribute generation should not be possible
with pytest.raises(AttributeError):
not_set.test = "try to set value" # type: ignore
def test_result_wrapper_class():
def test_result_wrapper_class_Success():
# successful operation
test_result = 10
error_code = 146
twrapper: errors.ResultWrapper[int] = errors.ResultWrapper(
@ -153,7 +171,11 @@ def test_result_wrapper_class():
assert twrapper.result == test_result
assert twrapper.unwrap() == test_result
assert twrapper.status.code != error_code
def test_result_wrapper_class_Failure():
# test for no result
error_code = 146
test_result = errors.NotSet()
test_message = "Test of error message"
exception = ValueError(test_message)
@ -168,25 +190,31 @@ def test_result_wrapper_class():
twrapper.result
with pytest.raises(errors.UInternalError):
twrapper.unwrap()
def test_result_wrapper_class_WrongCodeSpans():
# test other error code spans
test_result = errors.NotSet()
test_message = "Test of error message"
exception = ValueError(test_message)
with pytest.raises(ValueError):
error_code = DEFAULT_INTERNAL_ERR_CODE - 1
twrapper: errors.ResultWrapper[int] = errors.ResultWrapper(
_: errors.ResultWrapper[int] = errors.ResultWrapper(
result=test_result,
exception=exception,
code_on_error=52, # not possible because of lower bound
code_on_error=error_code, # not possible because of lower bound
)
with pytest.raises(ValueError):
error_code = DEFAULT_API_ERR_CODE + 1
twrapper: errors.ResultWrapper[int] = errors.ResultWrapper(
_: errors.ResultWrapper[int] = errors.ResultWrapper(
result=test_result,
exception=exception,
code_on_error=402, # not possible because of upper bound
code_on_error=error_code, # not possible because of upper bound
)
def test_wrap_result():
def test_wrap_result_None():
MESSAGE = "Test case wrapped function decorator"
error_code = 103
@ -203,16 +231,27 @@ def test_wrap_result():
with pytest.raises(errors.UInternalError):
res.unwrap()
def test_wrap_result_NotNone_Success():
error_code = 103
@errors.wrap_result(error_code)
def test_func_2(x: str, y: str) -> int:
return int(int(x) / int(y))
# success
res = test_func_2("2", "1")
assert res.result == 2
assert res.unwrap() == 2
assert res.status == errors.STATUS_HANDLER.SUCCESS
# failure 1
def test_wrap_result_NotNone_FailureZeroDvisionError():
error_code = 103
@errors.wrap_result(error_code)
def test_func_2(x: str, y: str) -> int:
return int(int(x) / int(y))
res = test_func_2("2", "0")
with pytest.raises(errors.WAccessResultDespiteError):
res.result
@ -220,7 +259,15 @@ def test_wrap_result():
res.unwrap()
assert res.status.code == error_code
assert res.status.description == "ZeroDivisionError"
# failure 2
def test_wrap_result_NotNone_FailureValueError():
error_code = 103
@errors.wrap_result(error_code)
def test_func_2(x: str, y: str) -> int:
return int(int(x) / int(y))
res = test_func_2("2", "test")
with pytest.raises(errors.WAccessResultDespiteError):
res.result