Skip to content

Commit

Permalink
fix: fix parameters.value WHERE filter with between operator #270
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Feb 24, 2022
1 parent 598bf9f commit 3fe8dfe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
15 changes: 11 additions & 4 deletions datagateway_api/src/search_api/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,19 @@ def apply_filter(self, query):
# matters):
# {"Parameter": {"value": ["numericValue", "stringValue", "dateTimeValue"]}} # noqa: B950
if field_name == "value":
if isinstance(self.value, (int, float)):
# If the value is a list, extract the first value to determine which
# parameter value type should be used
if self.operation == "between" and isinstance(self.value, list):
filter_value = self.value[0]
else:
filter_value = self.value

if isinstance(filter_value, (int, float)):
icat_field_name = icat_field_name[0]
elif isinstance(self.value, datetime):
elif isinstance(filter_value, datetime):
icat_field_name = icat_field_name[2]
elif isinstance(self.value, str):
if DateHandler.is_str_a_date(self.value):
elif isinstance(filter_value, str):
if DateHandler.is_str_a_date(filter_value):
icat_field_name = icat_field_name[2]
else:
icat_field_name = icat_field_name[1]
Expand Down
8 changes: 8 additions & 0 deletions test/search_api/filters/test_search_api_where_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ class TestSearchAPIWhereFilter:
id="Numeric (float) parameter value (mapping that maps to multiple ICAT"
"fields)",
),
pytest.param(
SearchAPIWhereFilter("parameters.value", [20, 30], "between"),
"Document",
"SELECT o FROM Investigation o JOIN o.parameters AS p WHERE"
" p.numericValue between '20' and '30'",
id="Numeric (int) parameter value with between operator (mapping that"
" maps to multiple ICAT fields)",
),
pytest.param(
SearchAPIWhereFilter("parameters.value", ["test"], "eq"),
"Document",
Expand Down

0 comments on commit 3fe8dfe

Please sign in to comment.