From ea5deb0bbeebe6955d9cf7a05d6fba2ff03d46fc Mon Sep 17 00:00:00 2001 From: Matthew Richards Date: Thu, 2 Dec 2021 19:26:42 +0000 Subject: [PATCH] add functionality to deal with an empty RHS #259 - 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 --- .../src/search_api/nested_where_filters.py | 17 ++++++++++++++++- test/search_api/test_nested_where_filters.py | 2 ++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/datagateway_api/src/search_api/nested_where_filters.py b/datagateway_api/src/search_api/nested_where_filters.py index 1f1521a2..fa822194 100644 --- a/datagateway_api/src/search_api/nested_where_filters.py +++ b/datagateway_api/src/search_api/nested_where_filters.py @@ -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)}" diff --git a/test/search_api/test_nested_where_filters.py b/test/search_api/test_nested_where_filters.py index 37acbdfc..4e25ec06 100644 --- a/test/search_api/test_nested_where_filters.py +++ b/test/search_api/test_nested_where_filters.py @@ -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(