Skip to content

Commit

Permalink
Merge pull request #335 from ral-facilities/test-coverage-search-api
Browse files Browse the repository at this point in the history
Increase Search API Code Coverage
  • Loading branch information
MRichards99 authored Feb 21, 2022
2 parents 8b613da + 20c43b6 commit e9deada
Show file tree
Hide file tree
Showing 10 changed files with 364 additions and 44 deletions.
2 changes: 1 addition & 1 deletion datagateway_api/src/search_api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def _is_panosc_entity_field_of_type_list(entity_field):
hasattr(entity_field_outer_type, "_name")
and entity_field_outer_type._name == "List"
):
is_list = True
is_list = True # pragma: py-37-code
# The `_name` `outer_type_` attribute was introduced in Python 3.7 so to check
# whether the field is of type list in Python 3.6, we are checking the type of its
# default value. We must ensure that any new list fields that get added in future
Expand Down
79 changes: 45 additions & 34 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ coverage = {extras = ["toml"], version = "^5.3"}
pytest-cov = "^2.10.1"
pytest-icdiff = "^0.5"
python-semantic-release = "^7.19.2"
coverage-conditional-plugin = "^0.5.0"

[tool.poetry.scripts]

Expand All @@ -69,6 +70,16 @@ source = ["datagateway_api"]
[tool.coverage.run]
branch = true
source = ["datagateway_api"]
plugins = ["coverage_conditional_plugin"]
omit = [
"*api_start_utils.py",
"*logger_setup.py",
"*main.py",
"*wsgi.py",
]

[tool.coverage.coverage_conditional_plugin.rules]
py-37-code = "sys_version_info > (3, 6)"

[tool.coverage.report]
show_missing = true
7 changes: 7 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,10 @@ def test_config_data():
def test_config(test_config_data):
with patch("builtins.open", mock_open(read_data=json.dumps(test_config_data))):
return APIConfig.load("test/path")


@pytest.fixture()
def test_config_without_search_api(test_config_data):
del test_config_data["search_api"]
with patch("builtins.open", mock_open(read_data=json.dumps(test_config_data))):
return APIConfig.load("test/path")
29 changes: 27 additions & 2 deletions test/search_api/filters/test_search_api_where_filter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

from datagateway_api.src.common.date_handler import DateHandler
from datagateway_api.src.common.filter_order_handler import FilterOrderHandler
from datagateway_api.src.search_api.filters import SearchAPIWhereFilter
from datagateway_api.src.search_api.nested_where_filters import NestedWhereFilters
Expand Down Expand Up @@ -145,8 +146,20 @@ class TestSearchAPIWhereFilter:
"Document",
"SELECT o FROM Investigation o JOIN o.parameters AS p WHERE"
" p.dateTimeValue = '2018-05-05T15:00:00.000Z'",
id="Datetime parameter value (mapping that maps to multiple ICAT"
" fields)",
id="Datetime (of type string) parameter value (mapping that maps to"
" multiple ICAT fields)",
),
pytest.param(
SearchAPIWhereFilter(
"parameters.value",
DateHandler.str_to_datetime_object("2018-05-05T15:00:00.000Z"),
"eq",
),
"Document",
"SELECT o FROM Investigation o JOIN o.parameters AS p WHERE"
" p.dateTimeValue = '2018-05-05 15:00:00+00:00'",
id="Datetime (of type date) parameter value (mapping that maps to"
" multiple ICAT fields)",
),
pytest.param(
SearchAPIWhereFilter("parameters.value", 20, "eq"),
Expand All @@ -164,6 +177,14 @@ class TestSearchAPIWhereFilter:
id="Numeric (float) parameter value (mapping that maps to multiple ICAT"
"fields)",
),
pytest.param(
SearchAPIWhereFilter("parameters.value", ["test"], "eq"),
"Document",
"SELECT o FROM Investigation o JOIN o.parameters AS p WHERE"
" p.stringValue = '['test']'",
id="Other type parameter value (mapping that maps to multiple ICAT"
"fields)",
),
],
)
def test_valid_apply_where_filter(self, filter_input, entity_name, expected_query):
Expand Down Expand Up @@ -281,3 +302,7 @@ def test_valid_apply_nested_filters(
filter_handler.apply_filters(test_query)

assert str(test_query.icat_query.query) == expected_query

def test_str_where_filter(self):
test_filter = SearchAPIWhereFilter("name", "WISH", "eq")
assert str(test_filter) == repr(test_filter)
6 changes: 5 additions & 1 deletion test/search_api/test_error_handling.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from datagateway_api.src.common.exceptions import (
ApiError,
BadRequestError,
FilterError,
MissingRecordError,
Expand All @@ -22,6 +23,7 @@ class TestErrorHandling:
pytest.param(TypeError, BadRequestError, 400, id="Type error"),
pytest.param(ValueError, BadRequestError, 400, id="Value error"),
pytest.param(AttributeError, BadRequestError, 400, id="Attribute error"),
pytest.param(ImportError, ImportError, 500, id="Import error"),
],
)
def test_valid_error_raised(
Expand All @@ -35,7 +37,9 @@ def raise_exception():
raise_exception()
except Exception as e:
assert isinstance(e.args[0], dict)
assert e.status_code == status_code
# Non-API defined exception won't have a `status_code` attribute
if isinstance(e, ApiError):
assert e.status_code == status_code
assert list(e.args[0]["error"].keys()) == ["statusCode", "name", "message"]

with pytest.raises(expected_exception):
Expand Down
Loading

0 comments on commit e9deada

Please sign in to comment.