Skip to content

Commit

Permalink
#150: Add tests for ICAT distinct filters
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Nov 11, 2020
1 parent 3c350ee commit 90f3b4b
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions test/icat/filters/test_distinct_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import pytest

from datagateway_api.common.exceptions import FilterError
from datagateway_api.common.icat.filters import PythonICATDistinctFieldFilter


class TestICATDistinctFilter:
def test_valid_str_field_input(self, icat_query):
test_filter = PythonICATDistinctFieldFilter("name")
test_filter.apply_filter(icat_query)

assert (
icat_query.conditions == {"name": "!= null"}
and icat_query.aggregate == "DISTINCT"
)

def test_valid_list_fields_input(self, icat_query):
test_filter = PythonICATDistinctFieldFilter(["doi", "name", "title"])
test_filter.apply_filter(icat_query)

assert (
icat_query.conditions
== {"doi": "!= null", "name": "!= null", "title": "!= null"}
and icat_query.aggregate == "DISTINCT"
)

def test_invalid_field(self, icat_query):
test_filter = PythonICATDistinctFieldFilter("my_new_field")
with pytest.raises(FilterError):
test_filter.apply_filter(icat_query)

def test_distinct_aggregate_added(self, icat_query):
test_filter = PythonICATDistinctFieldFilter("id")
test_filter.apply_filter(icat_query)

assert icat_query.aggregate == "DISTINCT"

@pytest.mark.parametrize("existing_aggregate", ["COUNT", "AVG", "SUM"])
def test_existing_aggregate_appended(self, icat_query, existing_aggregate):
icat_query.setAggregate(existing_aggregate)

test_filter = PythonICATDistinctFieldFilter("name")
test_filter.apply_filter(icat_query)

assert icat_query.aggregate == f"{existing_aggregate}:DISTINCT"

0 comments on commit 90f3b4b

Please sign in to comment.