-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#150: Add tests for ICAT distinct filters
- Loading branch information
1 parent
3c350ee
commit 90f3b4b
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |