From 46cb5c3bd72cea6f34f9b62084d17275941d72a2 Mon Sep 17 00:00:00 2001 From: Matthew Richards Date: Fri, 3 Dec 2021 10:43:05 +0000 Subject: [PATCH] test: fix tests for `NestedWhereFilters` #259 --- datagateway_api/src/search_api/nested_where_filters.py | 7 ++++--- test/search_api/test_nested_where_filters.py | 9 ++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/datagateway_api/src/search_api/nested_where_filters.py b/datagateway_api/src/search_api/nested_where_filters.py index 5fe1248e..82b02f89 100644 --- a/datagateway_api/src/search_api/nested_where_filters.py +++ b/datagateway_api/src/search_api/nested_where_filters.py @@ -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): """ @@ -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)}" diff --git a/test/search_api/test_nested_where_filters.py b/test/search_api/test_nested_where_filters.py index c9ae6c64..4746b389 100644 --- a/test/search_api/test_nested_where_filters.py +++ b/test/search_api/test_nested_where_filters.py @@ -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( @@ -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", ), ],