Skip to content

Commit

Permalink
make LHS and RHS support lists of WHERE filters #259
Browse files Browse the repository at this point in the history
- Relevant for request filter inputs that come with 3 or more
- Test cases have been added to cover this as well as edge cases such as lists with a single filter in them
  • Loading branch information
MRichards99 committed Dec 2, 2021
1 parent ea5deb0 commit 31c07ab
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
16 changes: 9 additions & 7 deletions datagateway_api/src/search_api/nested_where_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ def __init__(self, lhs, rhs, joining_operator):
:type joining_operator: :class:`str`
"""

# Ensure each side is in a list for consistency for string conversion
if not isinstance(lhs, list):
lhs = [lhs]
if not isinstance(rhs, list):
rhs = [rhs]

self.lhs = lhs
self.rhs = rhs
self.joining_operator = f" {joining_operator} "
Expand All @@ -34,14 +40,10 @@ def __str__(self):
# If neither side contains `None`, we should continue as normal
pass

for i in range(len(boolean_algebra_list)):
# Convert inputs to JPQL-ready
boolean_algebra_list[i] = str(boolean_algebra_list[i])

# TODO - LHS and RHS need to be able to deal with a list of
# `SearchAPIWhereFilters`
# If either side contains a list of WHERE filter objects, flatten the conditions
conditions = [str(m) for n in (i for i in boolean_algebra_list) for m in n]

return f"({self.joining_operator.join(boolean_algebra_list)})"
return f"({self.joining_operator.join(conditions)})"

def __repr__(self):
return f"LHS: {repr(self.lhs)}, RHS: {repr(self.rhs)}, Operator: {repr(self.joining_operator)}"
39 changes: 39 additions & 0 deletions test/search_api/test_nested_where_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,50 @@ def test_str_filters(self, lhs, rhs, joining_operator, expected_where_clause):
"(o.name = 'test name' OR o.id = '3')",
id="(o.name = 'test name' OR o.id = '3')",
),
pytest.param(
[SearchAPIWhereFilter("name", "test name", "eq", "and")],
SearchAPIWhereFilter("id", 3, "eq", "and"),
"OR",
"(o.name = 'test name' OR o.id = '3')",
id="Single filter list in LHS",
),
pytest.param(
[SearchAPIWhereFilter("name", "test name", "eq", "and")],
[SearchAPIWhereFilter("id", 3, "eq", "and")],
"OR",
"(o.name = 'test name' OR o.id = '3')",
id="Single filter list in LHS and RHS",
),
pytest.param(
[
SearchAPIWhereFilter("name", "test name", "eq", "and"),
SearchAPIWhereFilter("id", 10, "lt"),
],
[SearchAPIWhereFilter("id", 3, "gt", "and")],
"AND",
"(o.name = 'test name' AND o.id < '10' AND o.id > '3')",
id="Multiple filters on LHS",
),
pytest.param(
[
SearchAPIWhereFilter("name", "test name", "eq", "and"),
SearchAPIWhereFilter("id", 10, "lt"),
],
[
SearchAPIWhereFilter("id", 3, "gt", "and"),
SearchAPIWhereFilter("doi", "Test DOI", "like"),
],
"AND",
"(o.name = 'test name' AND o.id < '10' AND o.id > '3')",
id="Multiple filters on LHS and RHS",
),
],
)
def test_search_api_filters(
self, lhs, rhs, joining_operator, 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)
where_clause = str(test_nest)
assert where_clause == expected_where_clause
Expand Down

0 comments on commit 31c07ab

Please sign in to comment.