Skip to content

Commit

Permalink
test: add tests for search API error signalling #296
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Feb 14, 2022
1 parent 3a5a3e8 commit 5193af1
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions test/search_api/test_error_handling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import pytest

from datagateway_api.src.common.exceptions import (
BadRequestError,
FilterError,
MissingRecordError,
SearchAPIError,
)
from datagateway_api.src.search_api.helpers import search_api_error_handling


class TestErrorHandling:
@pytest.mark.parametrize(
"raised_exception, expected_exception, status_code",
[
pytest.param(BadRequestError, BadRequestError, 400, id="Bad request error"),
pytest.param(FilterError, FilterError, 400, id="Invalid filter"),
pytest.param(
MissingRecordError, MissingRecordError, 404, id="Missing record",
),
pytest.param(SearchAPIError, SearchAPIError, 500, id="Search API error"),
pytest.param(TypeError, BadRequestError, 400, id="Type error"),
pytest.param(ValueError, BadRequestError, 400, id="Value error"),
pytest.param(AttributeError, BadRequestError, 400, id="Attribute error"),
],
)
def test_valid_error_raised(
self, raised_exception, expected_exception, status_code,
):
@search_api_error_handling
def raise_exception():
raise raised_exception()

try:
raise_exception()
except Exception as e:
assert isinstance(e.args[0], dict)
assert e.status_code == status_code
assert list(e.args[0]["error"].keys()) == ["statusCode", "name", "message"]

with pytest.raises(expected_exception):
raise_exception()

0 comments on commit 5193af1

Please sign in to comment.