-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for search API error signalling #296
- Loading branch information
1 parent
3a5a3e8
commit 5193af1
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |