Skip to content

Commit

Permalink
Merge pull request #295 from ral-facilities/implement-limit-and-skip-…
Browse files Browse the repository at this point in the history
…filters

Implement Limit and Skip filters
  • Loading branch information
VKTB authored Dec 6, 2021
2 parents b3b797c + 7310707 commit dd23cce
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 6 deletions.
17 changes: 12 additions & 5 deletions datagateway_api/src/datagateway_api/icat/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,21 @@ def apply_filter(self, query):


class PythonICATSkipFilter(SkipFilter):
def __init__(self, skip_value):
def __init__(self, skip_value, filter_use="datagateway_api"):
super().__init__(skip_value)
self.filter_use = filter_use

def apply_filter(self, query):
icat_properties = get_icat_properties(
Config.config.datagateway_api.icat_url,
Config.config.datagateway_api.icat_check_cert,
)
if self.filter_use == "datagateway_api":
icat_properties = get_icat_properties(
Config.config.datagateway_api.icat_url,
Config.config.datagateway_api.icat_check_cert,
)
else:
icat_properties = get_icat_properties(
Config.config.search_api.icat_url,
Config.config.search_api.icat_check_cert,
)
icat_set_limit(query, self.skip_value, icat_properties["maxEntities"])


Expand Down
2 changes: 1 addition & 1 deletion datagateway_api/src/search_api/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def apply_filter(self, query):

class SearchAPISkipFilter(PythonICATSkipFilter):
def __init__(self, skip_value):
super().__init__(skip_value)
super().__init__(skip_value, filter_use="search_api")

def apply_filter(self, query):
return super().apply_filter(query)
Expand Down
22 changes: 22 additions & 0 deletions test/search_api/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from icat.client import Client
from icat.query import Query
import pytest

from datagateway_api.src.common.config import Config


@pytest.fixture(scope="package")
def icat_client():
client = Client(
Config.config.search_api.icat_url,
checkCert=Config.config.search_api.icat_check_cert,
)
client.login(
Config.config.test_mechanism, Config.config.test_user_credentials.dict(),
)
return client


@pytest.fixture()
def icat_query(icat_client):
return Query(icat_client, "Investigation")
28 changes: 28 additions & 0 deletions test/search_api/test_limit_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import pytest

from datagateway_api.src.common.exceptions import FilterError
from datagateway_api.src.search_api.filters import SearchAPILimitFilter


class TestSearchAPILimitFilter:
@pytest.mark.parametrize(
"limit_value",
[
pytest.param(10, id="typical"),
pytest.param(0, id="low boundary"),
pytest.param(9999, id="high boundary"),
],
)
def test_valid_limit_value(self, icat_query, limit_value):
test_filter = SearchAPILimitFilter(limit_value)
test_filter.apply_filter(icat_query)

assert icat_query.limit == (0, limit_value)

@pytest.mark.parametrize(
"limit_value",
[pytest.param(-50, id="extreme invalid"), pytest.param(-1, id="boundary")],
)
def test_invalid_limit_value(self, icat_query, limit_value):
with pytest.raises(FilterError):
SearchAPILimitFilter(limit_value)
31 changes: 31 additions & 0 deletions test/search_api/test_skip_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pytest

from datagateway_api.src.common.config import Config
from datagateway_api.src.common.exceptions import FilterError
from datagateway_api.src.common.helpers import get_icat_properties
from datagateway_api.src.search_api.filters import SearchAPISkipFilter


class TestSearchAPISkipFilter:
@pytest.mark.parametrize(
"skip_value", [pytest.param(10, id="typical"), pytest.param(0, id="boundary")],
)
def test_valid_skip_value(self, icat_query, skip_value):
test_filter = SearchAPISkipFilter(skip_value)
test_filter.apply_filter(icat_query)

assert icat_query.limit == (
skip_value,
get_icat_properties(
Config.config.search_api.icat_url,
Config.config.search_api.icat_check_cert,
)["maxEntities"],
)

@pytest.mark.parametrize(
"skip_value",
[pytest.param(-375, id="extreme invalid"), pytest.param(-1, id="boundary")],
)
def test_invalid_skip_value(self, icat_query, skip_value):
with pytest.raises(FilterError):
SearchAPISkipFilter(skip_value)

0 comments on commit dd23cce

Please sign in to comment.