Skip to content

Commit

Permalink
#141: Fix bug when a count endpoint with distinct filter should get …
Browse files Browse the repository at this point in the history
…0 results but caused an IndexError

- Test case to cover this edge case also added in this commit
  • Loading branch information
MRichards99 committed Apr 13, 2021
1 parent 1b7cb60 commit 44b707a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
11 changes: 6 additions & 5 deletions datagateway_api/common/icat/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,13 @@ def execute_query(self, client, return_json_formattable=False):
log.info("Query results will be returned in a JSON format")
data = []

if self.query.manual_count:
# Manually count the number of results
data.append(len(query_result))
return data

for result in query_result:
if self.query.manual_count:
# Manually count the number of results
data.append(len(query_result))
break
elif distinct_query:
if distinct_query:
# When multiple attributes are given in a distinct filter, Python
# ICAT returns the results in a nested list. This doesn't happen
# when a single attribute is given, so the result is encased in a
Expand Down
17 changes: 14 additions & 3 deletions test/icat/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import pytest

from datagateway_api.common.date_handler import DateHandler
from datagateway_api.common.exceptions import FilterError, PythonICATError
from datagateway_api.common.exceptions import PythonICATError
from datagateway_api.common.icat.filters import (
PythonICATSkipFilter,
PythonICATWhereFilter,
)
from datagateway_api.common.icat.query import ICATQuery


def prepare_icat_data_for_assertion(data, remove_id=False):
def prepare_icat_data_for_assertion(data, remove_id=False, remove_visit_id=False):
"""
Remove meta attributes from ICAT data. Meta attributes contain data about data
creation/modification, and should be removed to ensure correct assertion values
Expand All @@ -38,6 +38,8 @@ def prepare_icat_data_for_assertion(data, remove_id=False):
# meta_attributes is immutable
if remove_id:
entity.pop("id")
if remove_visit_id:
entity.pop("visitId")

assertable_data.append(entity)

Expand Down Expand Up @@ -172,7 +174,6 @@ def test_invalid_query_creation(self, icat_client):
],
id="Query with included entity",
),
# pytest.param(id="Query with included entity"), # facility?
pytest.param(
{
"title": "like '%Test data for the Python ICAT Backend on"
Expand Down Expand Up @@ -260,6 +261,16 @@ def test_invalid_query_creation(self, icat_client):
[1],
id="Multiple distinct fields on count query",
),
pytest.param(
{"title": "like '%Unknown testing data for DG API%'",},
"DISTINCT",
None,
["title", "name"],
True,
True,
[0],
id="Multiple distinct fields on count query to return 0 matches",
),
],
)
@pytest.mark.usefixtures("single_investigation_test_data")
Expand Down

0 comments on commit 44b707a

Please sign in to comment.