Skip to content

Commit

Permalink
add functionality to deal with an empty RHS #259
Browse files Browse the repository at this point in the history
- Relevant when a filter input has a boolean operator but there's only one condition in that list. Makes the boolean operator redundant but is still an edge case we should support
- Additional test cases have been added to cover this
  • Loading branch information
MRichards99 committed Dec 2, 2021
1 parent df323fe commit ea5deb0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 16 additions & 1 deletion datagateway_api/src/search_api/nested_where_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,28 @@ def __init__(self, lhs, rhs, joining_operator):

self.lhs = lhs
self.rhs = rhs
self.joining_operator = f" {joining_operator} "

def __str__(self):
"""
Join the condition on the left with the one on the right with the boolean
operator
"""
return f"({str(self.lhs)} {self.joining_operator} {str(self.rhs)})"
boolean_algebra_list = [self.lhs, self.rhs]
try:
boolean_algebra_list.remove(None)
except ValueError:
# 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`

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

def __repr__(self):
return f"LHS: {repr(self.lhs)}, RHS: {repr(self.rhs)}, Operator: {repr(self.joining_operator)}"
2 changes: 2 additions & 0 deletions test/search_api/test_nested_where_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class TestNestedWhereFilters:
@pytest.mark.parametrize(
"lhs, rhs, joining_operator, expected_where_clause",
[
pytest.param("A", None, "AND", "(A)", id="(A) w/ misc. AND"),
pytest.param("A", None, "OR", "(A)", id="(A) w/ misc. OR"),
pytest.param("A", "B", "AND", "(A AND B)", id="(A AND B)"),
pytest.param("A", "B", "OR", "(A OR B)", id="(A OR B)"),
pytest.param(
Expand Down

0 comments on commit ea5deb0

Please sign in to comment.