* EDITED*
I would like to test what will happen if external API will return 500 status code.
main.py
@app.route("/<a>/<b>", methods=["GET"])
def repo_info(a: str, b: str) -> Union[Response, str]:
info = some_func(a, b)
result = create_result_dict(some_func)
return Response(
response=json.dumps(result, ensure_ascii=False),
status=200,
mimetype="application/json",
@app.errorhandler(500)
def server_error(e):
logging.exception(f"An error occurred during a request: {e}.")
return Response(
response="An internal error occurred. See logs for full stacktrace.",
status=500,
)
my_module.py
def some_func(a: str, b: str) -> Dict[str, str]:
return json.loads(
(requests.get(f"https://api.github.com/repos/{a}/{b}")).text
)
I tried around this code but feel like headless chicken:
from flask import Response
import pytest
import requests
from unittest.mock import patch
from requests.exceptions import HTTPError
@patch.object(my_module, "some_func")
def test_some_func(mocked):
mocked.return_value = HTTPError()
result = my_module.some_func()
with pytest.raises(HTTPError):
result == mocked
Also HTTPError
doesn't takes arguments, how can I pass information, that I would like 500 status code?