Skip to content

Commit

Permalink
refactor: remove unused function and place functionality within `Sear…
Browse files Browse the repository at this point in the history
…chAPIWhereFilter.apply_filter()` #260

- This commit also has fixes for tests relevant to this change
  • Loading branch information
MRichards99 committed Dec 21, 2021
1 parent 40320f2 commit 4f16776
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 21 deletions.
10 changes: 0 additions & 10 deletions datagateway_api/src/datagateway_api/filter_order_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,3 @@ def manage_icat_filters(self, filters, query):
self.merge_python_icat_limit_skip_filters()
self.clear_python_icat_order_filters()
self.apply_filters(query)

def add_query_to_where_filters(self, search_api_query):
"""
TODO
"""
for filter_ in self.filters:
if isinstance(filter_, NestedWhereFilters):
NestedWhereFilters.set_search_api_query(
filter_, search_api_query,
)
1 change: 0 additions & 1 deletion datagateway_api/src/search_api/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def get_search(endpoint_name, entity_name, filters):

filter_handler = FilterOrderHandler()
filter_handler.add_filters(filters)
filter_handler.add_query_to_where_filters(query)
filter_handler.apply_filters(query)

log.debug("Python ICAT Query: %s", query.icat_query.query)
Expand Down
1 change: 1 addition & 0 deletions datagateway_api/src/search_api/nested_where_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def __init__(self, lhs, rhs, joining_operator, search_api_query=None):
NestedWhereFilters.set_search_api_query(self, search_api_query)

def apply_filter(self, query):
NestedWhereFilters.set_search_api_query(self, query)
query.icat_query.query.setConditionsByString(str(self))

@staticmethod
Expand Down
7 changes: 6 additions & 1 deletion datagateway_api/src/search_api/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from datagateway_api.src.search_api.session_handler import SessionHandler


# TODO - Not sure if this should inherit from `ICATQuery`?
class SearchAPIQuery:
def __init__(self, panosc_entity_name):
self.panosc_entity_name = panosc_entity_name
Expand All @@ -12,3 +11,9 @@ def __init__(self, panosc_entity_name):
]

self.icat_query = ICATQuery(SessionHandler.client, self.icat_entity_name)

def __repr__(self):
return (
f"PaNOSC Entity Name: {self.panosc_entity_name}, ICAT Entity Name:"
f" {self.icat_entity_name}, ICAT Query: {str(self.icat_query.query)}"
)
21 changes: 14 additions & 7 deletions test/search_api/filters/test_search_api_where_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,21 @@ def test_valid_apply_where_filter(self, filter_input, entity_name, expected_quer
[
pytest.param(
NestedWhereFilters(
[], [SearchAPIWhereFilter("name", "SANS2D", "like")], "and",
[],
[SearchAPIWhereFilter("name", "SANS2D", "like")],
"and",
SearchAPIQuery("Instrument"),
),
"Instrument",
"SELECT o FROM Instrument o WHERE (o.name like '%SANS2D%')",
id="Nested input with single filter",
),
pytest.param(
NestedWhereFilters(
[], [SearchAPIWhereFilter("facility", "ISIS", "like")], "or",
[],
[SearchAPIWhereFilter("facility", "ISIS", "like")],
"or",
SearchAPIQuery("Instrument"),
),
"Instrument",
"SELECT o FROM Instrument o JOIN o.facility AS f WHERE (f.name like"
Expand All @@ -142,6 +148,7 @@ def test_valid_apply_where_filter(self, filter_input, entity_name, expected_quer
[SearchAPIWhereFilter("summary", "My Test Summary", "eq")],
[SearchAPIWhereFilter("title", "Test title", "eq")],
"or",
SearchAPIQuery("Document"),
),
"Document",
"SELECT o FROM Investigation o WHERE (o.summary = 'My Test Summary' or"
Expand All @@ -153,6 +160,7 @@ def test_valid_apply_where_filter(self, filter_input, entity_name, expected_quer
[SearchAPIWhereFilter("summary", "My Test Summary", "eq")],
[SearchAPIWhereFilter("keywords", "Test keyword", "eq")],
"and",
SearchAPIQuery("Document"),
),
"Document",
"SELECT o FROM Investigation o JOIN o.keywords AS s1 WHERE (o.summary ="
Expand All @@ -169,6 +177,7 @@ def test_valid_apply_where_filter(self, filter_input, entity_name, expected_quer
),
],
"and",
SearchAPIQuery("Dataset"),
),
"Dataset",
"SELECT o FROM Dataset o JOIN o.sample AS s1 JOIN s1.parameters AS s2"
Expand All @@ -193,6 +202,7 @@ def test_valid_apply_where_filter(self, filter_input, entity_name, expected_quer
),
],
"and",
SearchAPIQuery("Document"),
),
"Document",
"SELECT o FROM Investigation o WHERE ((o.summary = 'My Test Summary' or"
Expand All @@ -205,13 +215,10 @@ def test_valid_apply_where_filter(self, filter_input, entity_name, expected_quer
def test_valid_apply_nested_filters(
self, filter_input, entity_name, expected_query,
):
filter_handler = FilterOrderHandler()
filter_handler.add_filter(filter_input)
test_query = SearchAPIQuery(entity_name)
filter_handler.add_query_to_where_filters(test_query)

filter_handler = FilterOrderHandler()
filter_handler.add_filter(filter_input)
filter_handler.apply_filters(test_query)

print(f"JPQL Query: {str(test_query.icat_query.query)}")

assert str(test_query.icat_query.query) == expected_query
2 changes: 0 additions & 2 deletions test/search_api/test_nested_where_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ def test_str_filters(self, lhs, rhs, joining_operator, expected_where_clause):
def test_search_api_filters(
self, lhs, rhs, joining_operator, query, expected_where_clause,
):
# TODO - Is creating clients causing this to be slow? Test once session handler
# work merged
test_nest = NestedWhereFilters(lhs, rhs, joining_operator, query)
where_clause = str(test_nest)
assert where_clause == expected_where_clause
Expand Down
16 changes: 16 additions & 0 deletions test/search_api/test_search_api_query_filter_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
SearchAPIWhereFilter,
)
from datagateway_api.src.search_api.nested_where_filters import NestedWhereFilters
from datagateway_api.src.search_api.query import SearchAPIQuery
from datagateway_api.src.search_api.query_filter_factory import (
SearchAPIQueryFilterFactory,
)
Expand Down Expand Up @@ -86,6 +87,9 @@ def test_valid_where_filter_text_operator(
assert repr(filters[0].lhs) == repr(expected_lhs)
assert repr(filters[0].rhs) == repr(expected_rhs)
assert filters[0].joining_operator == expected_joining_operator
assert repr(filters[0].search_api_query) == repr(
SearchAPIQuery(test_entity_name),
)

@pytest.mark.parametrize(
"test_request_filter, test_entity_name, expected_lhs, expected_rhs"
Expand Down Expand Up @@ -312,6 +316,9 @@ def test_valid_where_filter_with_and_boolean_operator(
assert repr(filters[0].lhs) == repr(expected_lhs)
assert repr(filters[0].rhs) == repr(expected_rhs)
assert filters[0].joining_operator == expected_joining_operator
assert repr(filters[0].search_api_query) == repr(
SearchAPIQuery(test_entity_name),
)

@pytest.mark.parametrize(
"test_request_filter, test_entity_name, expected_lhs, expected_rhs"
Expand Down Expand Up @@ -535,6 +542,9 @@ def test_valid_where_filter_with_or_boolean_operator(
assert repr(filters[0].lhs) == repr(expected_lhs)
assert repr(filters[0].rhs) == repr(expected_rhs)
assert filters[0].joining_operator == expected_joining_operator
assert repr(filters[0].search_api_query) == repr(
SearchAPIQuery(test_entity_name),
)

@pytest.mark.parametrize(
"test_request_filter, test_entity_name, expected_lhs, expected_rhs"
Expand Down Expand Up @@ -876,6 +886,9 @@ def test_valid_where_filter_with_nested_and_boolean_operator(
assert repr(filters[0].lhs) == repr(expected_lhs)
assert repr(filters[0].rhs) == repr(expected_rhs)
assert filters[0].joining_operator == expected_joining_operator
assert repr(filters[0].search_api_query) == repr(
SearchAPIQuery(test_entity_name),
)

@pytest.mark.parametrize(
"test_request_filter, test_entity_name, expected_lhs, expected_rhs"
Expand Down Expand Up @@ -1217,6 +1230,9 @@ def test_valid_where_filter_with_nested_or_boolean_operator(
assert repr(filters[0].lhs) == repr(expected_lhs)
assert repr(filters[0].rhs) == repr(expected_rhs)
assert filters[0].joining_operator == expected_joining_operator
assert repr(filters[0].search_api_query) == repr(
SearchAPIQuery(test_entity_name),
)

@pytest.mark.parametrize(
"test_request_filter, test_entity_name, expected_length"
Expand Down

0 comments on commit 4f16776

Please sign in to comment.