Skip to content

Commit

Permalink
test: fix tests for NestedWhereFilters #259
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Dec 3, 2021
1 parent 31c07ab commit 46cb5c3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
7 changes: 4 additions & 3 deletions datagateway_api/src/search_api/nested_where_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __init__(self, lhs, rhs, joining_operator):

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

def __str__(self):
"""
Expand All @@ -35,15 +35,16 @@ def __str__(self):
"""
boolean_algebra_list = [self.lhs, self.rhs]
try:
boolean_algebra_list.remove(None)
boolean_algebra_list.remove([None])
except ValueError:
# If neither side contains `None`, we should continue as normal
pass

# 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]
operator = f" {self.joining_operator} "

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

def __repr__(self):
return f"LHS: {repr(self.lhs)}, RHS: {repr(self.rhs)}, Operator: {repr(self.joining_operator)}"
9 changes: 6 additions & 3 deletions test/search_api/test_nested_where_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ 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", None, "AND", "(A)", id="LHS (A) w/ misc. AND"),
pytest.param("A", None, "OR", "(A)", id="LHS (A) w/ misc. OR"),
pytest.param([], "A", "AND", "(A)", id="RHS (A) w/ misc. AND"),
pytest.param([], "A", "OR", "(A)", id="RHS (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 Expand Up @@ -114,7 +116,8 @@ def test_str_filters(self, lhs, rhs, joining_operator, expected_where_clause):
SearchAPIWhereFilter("doi", "Test DOI", "like"),
],
"AND",
"(o.name = 'test name' AND o.id < '10' AND o.id > '3')",
"(o.name = 'test name' AND o.id < '10' AND o.id > '3' AND o.doi like"
" '%Test DOI%')",
id="Multiple filters on LHS and RHS",
),
],
Expand Down

0 comments on commit 46cb5c3

Please sign in to comment.