Skip to content

Commit

Permalink
#211: Fix bug with nested-included distinct attributes
Browse files Browse the repository at this point in the history
- Also expands the unit test now those nested input values are possible to occur in the API
  • Loading branch information
MRichards99 committed Mar 19, 2021
1 parent 623a494 commit 8b31f5d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
11 changes: 9 additions & 2 deletions datagateway_api/common/icat/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,15 @@ def map_distinct_attributes_to_entity_names(self, distinct_fields, included_fiel
# range of a list with a single element
split_fields.insert(0, "base")

# If a key doesn't exist in the dictionary, create it and assign an empty
# list to it
# Check that only an entity name, and attribute name exist
# Code within loop is used for when `split_fields` =
# ['dataset', 'investigation', 'name'] for example
while len(split_fields) > 2:
# If a key doesn't exist in the dictionary, create it and assign an
# empty list to it
distinct_field_dict.setdefault(split_fields[0], [])
split_fields.pop(0)

distinct_field_dict.setdefault(split_fields[0], [])
distinct_field_dict[split_fields[0]].append(split_fields[-1])

Expand Down
25 changes: 23 additions & 2 deletions test/icat/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,34 @@ def test_json_format_execution_output(
"dataset",
{"base": ["id"]},
{"base": []},
id="Include filter used but no included attributes on distinct filter",
id="Include filter used but no included attributes on distinct filter,"
" no entity name match",
),
pytest.param(
"no match",
{"base": ["id"], "dataset": ["name"]},
{"base": [], "dataset": ["name"]},
id="Distinct filter contains included attributes, no entity name match",
),
pytest.param(
"dataset",
{"base": ["id"], "dataset": ["name"]},
{"base": ["name"], "dataset": ["name"]},
id="Distinct filter contains included attributes",
id="Distinct filter contains included attributes, entity name match",
),
pytest.param(
"dataset",
{"base": ["id"], "dataset": [], "investigation": ["name"]},
{"base": [], "dataset": [], "investigation": ["name"]},
id="Distinct filter contains nested included attributes, no entity name"
" match",
),
pytest.param(
"investigation",
{"base": ["id"], "dataset": [], "investigation": ["name"]},
{"base": ["name"], "dataset": [], "investigation": ["name"]},
id="Distinct filter contains nested included attributes, entity name"
" match",
),
],
)
Expand Down

0 comments on commit 8b31f5d

Please sign in to comment.