tests for result wrapping
This commit is contained in:
@@ -145,6 +145,7 @@ def test_login_logout(session, credentials):
|
||||
assert status.api_server_error.message == "Nutzer oder Passwort falsch."
|
||||
|
||||
|
||||
@pytest.mark.api_con_required
|
||||
def test_assert_login_while_logged_out(session):
|
||||
assert session.session_token is None
|
||||
assert session._creds is not None
|
||||
|
||||
@@ -7,7 +7,7 @@ import pytest
|
||||
from pydantic import ValidationError
|
||||
|
||||
from delta_barth import errors
|
||||
from delta_barth.constants import DEFAULT_API_ERR_CODE
|
||||
from delta_barth.constants import DEFAULT_API_ERR_CODE, DEFAULT_INTERNAL_ERR_CODE
|
||||
from delta_barth.types import DelBarApiError, Status
|
||||
|
||||
|
||||
@@ -76,18 +76,18 @@ def test_status_handler_api_error():
|
||||
assert new_err.api_server_error == api_err
|
||||
|
||||
|
||||
def test_status_handler_unwrap():
|
||||
def test_status_handler_raise_for_status():
|
||||
status_hdlr = errors.StatusHandler()
|
||||
|
||||
# success: should not raise
|
||||
err_status = status_hdlr.SUCCESS
|
||||
assert status_hdlr.unwrap(err_status) is None
|
||||
assert status_hdlr.raise_for_status(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)
|
||||
status_hdlr.raise_for_status(err_status)
|
||||
except errors.UDataProcessingError as err:
|
||||
descr = str(err)
|
||||
assert err_descr in descr
|
||||
@@ -103,7 +103,7 @@ def test_status_handler_unwrap():
|
||||
)
|
||||
with pytest.raises(errors.UInternalError):
|
||||
try:
|
||||
status_hdlr.unwrap(err_status)
|
||||
status_hdlr.raise_for_status(err_status)
|
||||
except errors.UInternalError as err:
|
||||
descr = str(err)
|
||||
assert description in descr
|
||||
@@ -115,7 +115,7 @@ def test_status_handler_unwrap():
|
||||
err_status = status_hdlr.api_error(error=api_err)
|
||||
with pytest.raises(errors.UApiError):
|
||||
try:
|
||||
status_hdlr.unwrap(err_status)
|
||||
status_hdlr.raise_for_status(err_status)
|
||||
except errors.UApiError as err:
|
||||
descr = str(err)
|
||||
assert description in descr
|
||||
@@ -151,6 +151,7 @@ def test_result_wrapper_class():
|
||||
)
|
||||
assert twrapper.status == errors.STATUS_HANDLER.SUCCESS
|
||||
assert twrapper.result == test_result
|
||||
assert twrapper.unwrap() == test_result
|
||||
assert twrapper.status.code != error_code
|
||||
# test for no result
|
||||
test_result = errors.NotSet()
|
||||
@@ -165,3 +166,65 @@ def test_result_wrapper_class():
|
||||
assert twrapper.status.code == error_code
|
||||
with pytest.raises(errors.WAccessResultDespiteError):
|
||||
twrapper.result
|
||||
with pytest.raises(errors.UInternalError):
|
||||
twrapper.unwrap()
|
||||
# test other error code spans
|
||||
with pytest.raises(ValueError):
|
||||
error_code = DEFAULT_INTERNAL_ERR_CODE - 1
|
||||
twrapper: errors.ResultWrapper[int] = errors.ResultWrapper(
|
||||
result=test_result,
|
||||
exception=exception,
|
||||
code_on_error=52, # not possible because of lower bound
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
error_code = DEFAULT_API_ERR_CODE + 1
|
||||
twrapper: errors.ResultWrapper[int] = errors.ResultWrapper(
|
||||
result=test_result,
|
||||
exception=exception,
|
||||
code_on_error=402, # not possible because of upper bound
|
||||
)
|
||||
|
||||
|
||||
def test_wrap_result():
|
||||
MESSAGE = "Test case wrapped function decorator"
|
||||
error_code = 103
|
||||
|
||||
@errors.wrap_result(error_code)
|
||||
def test_func_1() -> None:
|
||||
raise ValueError(MESSAGE)
|
||||
|
||||
res = test_func_1()
|
||||
assert isinstance(res, errors.ResultWrapper)
|
||||
assert res.status.code == error_code
|
||||
assert res.status.description == "ValueError"
|
||||
assert res.status.message == MESSAGE
|
||||
assert res.status.api_server_error is None
|
||||
with pytest.raises(errors.UInternalError):
|
||||
res.unwrap()
|
||||
|
||||
@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
|
||||
res = test_func_2("2", "0")
|
||||
with pytest.raises(errors.WAccessResultDespiteError):
|
||||
res.result
|
||||
with pytest.raises(errors.UInternalError):
|
||||
res.unwrap()
|
||||
assert res.status.code == error_code
|
||||
assert res.status.description == "ZeroDivisionError"
|
||||
# failure 2
|
||||
res = test_func_2("2", "test")
|
||||
with pytest.raises(errors.WAccessResultDespiteError):
|
||||
res.result
|
||||
with pytest.raises(errors.UInternalError):
|
||||
res.unwrap()
|
||||
assert res.status.code == error_code
|
||||
assert res.status.description == "ValueError"
|
||||
|
||||
Reference in New Issue
Block a user