Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty Where Filter Input on Count Endpoints #326

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 80 additions & 69 deletions datagateway_api/src/search_api/query_filter_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,87 +103,98 @@ def get_where_filter(where_filter_input, entity_name):
:type entity_name: :class:`str`
:return: The list of `NestedWhereFilters` and/ or `SearchAPIWhereFilter` objects
created
:raises FilterError: If the where filter input is not provided as an object
"""
if not isinstance(where_filter_input, dict):
raise FilterError(
"Bad where filter input, please ensure that it is provided as an "
"object",
)

where_filters = []
if (
list(where_filter_input.keys())[0] == "and"
or list(where_filter_input.keys())[0] == "or"
):
log.debug("and/or operators found: %s", list(where_filter_input.keys())[0])
boolean_operator = list(where_filter_input.keys())[0]
conditions = list(where_filter_input.values())[0]
conditional_where_filters = []

for condition in conditions:
# Could be nested AND/OR
where_filter = {
"filter": {"where": condition},
}
conditional_where_filters.extend(
SearchAPIQueryFilterFactory.get_query_filter(
where_filter, entity_name,
),
if where_filter_input:
if (
list(where_filter_input.keys())[0] == "and"
or list(where_filter_input.keys())[0] == "or"
):
log.debug(
"and/or operators found: %s", list(where_filter_input.keys())[0],
)
boolean_operator = list(where_filter_input.keys())[0]
conditions = list(where_filter_input.values())[0]
conditional_where_filters = []

for condition in conditions:
# Could be nested AND/OR
where_filter = {
"filter": {"where": condition},
}
conditional_where_filters.extend(
SearchAPIQueryFilterFactory.get_query_filter(
where_filter, entity_name,
),
)

nested = NestedWhereFilters(
conditional_where_filters[:-1],
conditional_where_filters[-1],
boolean_operator,
SearchAPIQuery(entity_name),
)
where_filters.append(nested)
elif list(where_filter_input.keys())[0] == "text":
log.debug("Text operator found within JSON where object")
try:
entity_class = getattr(search_api_models, entity_name)
except AttributeError as e:
raise SearchAPIError(
f"No text operator fields have been defined for {entity_name}"
f", {e.args}",
nested = NestedWhereFilters(
conditional_where_filters[:-1],
conditional_where_filters[-1],
boolean_operator,
SearchAPIQuery(entity_name),
)
where_filters.append(nested)
elif list(where_filter_input.keys())[0] == "text":
log.debug("Text operator found within JSON where object")
try:
entity_class = getattr(search_api_models, entity_name)
except AttributeError as e:
raise SearchAPIError(
f"No text operator fields have been defined for {entity_name}"
f", {e.args}",
)
VKTB marked this conversation as resolved.
Show resolved Hide resolved

or_conditional_filters = []
field_names = entity_class._text_operator_fields
log.debug(
"Text operators found for PaNOSC %s: %s", entity_name, field_names,
)
if not field_names:
# No text operator fields present, simply log and move on, we should
# ignore text operator queries on entities where `_text_operator_fields`
# is empty (meaning they are not present in the origina PaNOSC data
# model)
log.info(
"No text operator fields found for PaNOSC entity %s, will"
" ignore",
entity_name,
or_conditional_filters = []
field_names = entity_class._text_operator_fields
log.debug(
"Text operators found for PaNOSC %s: %s", entity_name, field_names,
)
else:
for field_name in field_names:
or_conditional_filters.append(
{field_name: {"like": where_filter_input["text"]}},
if not field_names:
# No text operator fields present, simply log and move on, we should
# ignore text operator queries on entities where
# `_text_operator_fields` is empty (meaning they are not present in
# the origina PaNOSC data model)
log.info(
"No text operator fields found for PaNOSC entity %s, will"
" ignore",
entity_name,
)
else:
for field_name in field_names:
or_conditional_filters.append(
{field_name: {"like": where_filter_input["text"]}},
)

where_filter = {
"filter": {"where": {"or": or_conditional_filters}},
}
where_filters.extend(
SearchAPIQueryFilterFactory.get_query_filter(
where_filter, entity_name,
where_filter = {
"filter": {"where": {"or": or_conditional_filters}},
}
where_filters.extend(
SearchAPIQueryFilterFactory.get_query_filter(
where_filter, entity_name,
),
)
else:
log.info(
"Basic where filter found, extracting field, value and operation",
)
filter_data = SearchAPIQueryFilterFactory.get_condition_values(
where_filter_input,
)
where_filters.append(
SearchAPIWhereFilter(
field=filter_data[0],
value=filter_data[1],
operation=filter_data[2],
),
)
else:
log.info("Basic where filter found, extracting field, value and operation")
filter_data = SearchAPIQueryFilterFactory.get_condition_values(
where_filter_input,
)
where_filters.append(
SearchAPIWhereFilter(
field=filter_data[0],
value=filter_data[1],
operation=filter_data[2],
),
)

return where_filters

Expand Down
9 changes: 1 addition & 8 deletions test/search_api/endpoints/test_count_dataset_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ class TestSearchAPICountDatasetFilesEndpoint:
"{}",
{"count": 56},
id="Basic /datasets/{pid}/files/count request",
# Skipped because empty dict for filter doesn't work on where
marks=pytest.mark.skip,
),
pytest.param(
"0-8401-1070-7",
Expand All @@ -40,12 +38,7 @@ class TestSearchAPICountDatasetFilesEndpoint:
id="Count dataset files with filter to return zero count",
),
pytest.param(
"unknown pid",
"{}",
{"count": 0},
id="Non-existent dataset pid",
# Skipped because empty dict for filter doesn't work on where
marks=pytest.mark.skip,
"unknown pid", "{}", {"count": 0}, id="Non-existent dataset pid",
),
],
)
Expand Down
16 changes: 2 additions & 14 deletions test/search_api/endpoints/test_count_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,16 @@ class TestSearchAPICountEndpoint:
"endpoint_name, request_filter, expected_json",
[
pytest.param(
"datasets",
"{}",
{"count": 479},
id="Basic /datasets/count request",
# Skipped because empty dict for filter doesn't work on where
marks=pytest.mark.skip,
"datasets", "{}", {"count": 479}, id="Basic /datasets/count request",
),
pytest.param(
"documents",
"{}",
{"count": 239},
id="Basic /documents/count request",
# Skipped because empty dict for filter doesn't work on where
marks=pytest.mark.skip,
"documents", "{}", {"count": 239}, id="Basic /documents/count request",
),
pytest.param(
"instruments",
"{}",
{"count": 14},
id="Basic /instruments/count request",
# Skipped because empty dict for filter doesn't work on where
marks=pytest.mark.skip,
),
pytest.param(
"datasets",
Expand Down
1 change: 1 addition & 0 deletions test/search_api/test_search_api_query_filter_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2063,6 +2063,7 @@ def test_valid_filter_input_with_all_filters(
},
id="Unsupported skip filter in scope of include filter",
),
pytest.param({"filter": {"where": []}}, id="Bad where filter input"),
pytest.param(
{"filter": {"where": {"isPublic": {"lt": True}}}},
id="Unsupported operator in where filter with boolean value",
Expand Down