168 lines
5.3 KiB
Python
168 lines
5.3 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import asdict
|
|
from typing import cast
|
|
|
|
import pytest
|
|
from pydantic import ValidationError
|
|
|
|
from delta_barth import errors
|
|
from delta_barth.constants import DEFAULT_API_ERR_CODE
|
|
from delta_barth.types import DelBarApiError, Status
|
|
|
|
|
|
def test_status_handler_parsing():
|
|
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
|
|
parsed_pipe_states = asdict(parsed_pipe_states)
|
|
|
|
for err in predef_errs:
|
|
dopt_err = cast(Status, parsed_pipe_states[err[0]])
|
|
assert isinstance(dopt_err, Status)
|
|
assert dopt_err.code == err[1]
|
|
assert dopt_err.description == err[2]
|
|
assert dopt_err.message == ""
|
|
|
|
status_hdlr._parse_data_pipe_states()
|
|
|
|
|
|
def test_status_handler_internal():
|
|
DESCRIPTION = "test case"
|
|
MESSAGE = "an error occurred"
|
|
ERR_CODE = 101
|
|
|
|
status_hdlr = errors.StatusHandler()
|
|
new_err = status_hdlr.error(
|
|
description=DESCRIPTION,
|
|
message=MESSAGE,
|
|
code=ERR_CODE,
|
|
)
|
|
assert new_err.code == ERR_CODE
|
|
assert new_err.description == DESCRIPTION
|
|
assert new_err.message == MESSAGE
|
|
# failure cases
|
|
err_code = 50 # default lower bound: 100
|
|
with pytest.raises(ValueError):
|
|
new_err = status_hdlr.error(
|
|
description=DESCRIPTION,
|
|
message=MESSAGE,
|
|
code=err_code,
|
|
)
|
|
err_code = 500 # default upper bound: 400
|
|
with pytest.raises(ValueError):
|
|
new_err = status_hdlr.error(
|
|
description=DESCRIPTION,
|
|
message=MESSAGE,
|
|
code=err_code,
|
|
)
|
|
|
|
|
|
def test_status_handler_api_error():
|
|
MESSAGE = "an error occurred"
|
|
api_err = DelBarApiError(status_code=401, message="test case")
|
|
assert api_err.status_code == 401
|
|
assert api_err.message == "test case"
|
|
|
|
status_hdlr = errors.StatusHandler()
|
|
new_err = status_hdlr.api_error(error=api_err)
|
|
assert new_err.code == DEFAULT_API_ERR_CODE
|
|
assert "API-Server" in new_err.description
|
|
assert new_err.message != MESSAGE
|
|
assert new_err.api_server_error is not None
|
|
assert new_err.api_server_error == api_err
|
|
|
|
|
|
def test_status_handler_unwrap():
|
|
status_hdlr = errors.StatusHandler()
|
|
|
|
# success: should not raise
|
|
err_status = status_hdlr.SUCCESS
|
|
assert status_hdlr.unwrap(err_status) is None
|
|
# data related errors (predefined)
|
|
err_status = status_hdlr.pipe_states.BAD_QUALITY
|
|
err_descr = err_status.description
|
|
with pytest.raises(errors.UDataProcessingError):
|
|
try:
|
|
status_hdlr.unwrap(err_status)
|
|
except errors.UDataProcessingError as err:
|
|
descr = str(err)
|
|
assert err_descr in descr
|
|
raise err
|
|
# internal error, not data-related
|
|
description = "test case"
|
|
message = "an error occurred"
|
|
err_code = 101
|
|
err_status = status_hdlr.error(
|
|
description=description,
|
|
message=message,
|
|
code=err_code,
|
|
)
|
|
with pytest.raises(errors.UInternalError):
|
|
try:
|
|
status_hdlr.unwrap(err_status)
|
|
except errors.UInternalError as err:
|
|
descr = str(err)
|
|
assert description in descr
|
|
raise err
|
|
# external API error
|
|
api_err = DelBarApiError(status_code=401, message="test case", code="1234")
|
|
description = "Kommunikation mit dem API-Server aufgetreten"
|
|
msg = "Bitte beachten Sie die"
|
|
err_status = status_hdlr.api_error(error=api_err)
|
|
with pytest.raises(errors.UApiError):
|
|
try:
|
|
status_hdlr.unwrap(err_status)
|
|
except errors.UApiError as err:
|
|
descr = str(err)
|
|
assert description in descr
|
|
assert msg in descr
|
|
raise err
|
|
|
|
|
|
def test_status_handler_exception_parsing():
|
|
status_hdlr = errors.StatusHandler()
|
|
test_message = "Test Exception"
|
|
test_code = 110
|
|
exception = ValueError(test_message)
|
|
status = status_hdlr.exception_to_error(exception, test_code)
|
|
assert status.code == test_code
|
|
assert status.description == exception.__class__.__name__
|
|
assert status.message == test_message
|
|
|
|
|
|
def test_not_set():
|
|
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():
|
|
test_result = 10
|
|
error_code = 146
|
|
twrapper: errors.ResultWrapper[int] = errors.ResultWrapper(
|
|
result=test_result,
|
|
exception=None,
|
|
code_on_error=error_code,
|
|
)
|
|
assert twrapper.status == errors.STATUS_HANDLER.SUCCESS
|
|
assert twrapper.result == test_result
|
|
assert twrapper.status.code != error_code
|
|
# test for no result
|
|
test_result = errors.NotSet()
|
|
test_message = "Test of error message"
|
|
exception = ValueError(test_message)
|
|
twrapper: errors.ResultWrapper[int] = errors.ResultWrapper(
|
|
result=test_result,
|
|
exception=exception,
|
|
code_on_error=error_code,
|
|
)
|
|
assert twrapper.status != errors.STATUS_HANDLER.SUCCESS
|
|
assert twrapper.status.code == error_code
|
|
with pytest.raises(errors.WAccessResultDespiteError):
|
|
twrapper.result
|