Skip to content

Commit

Permalink
test: add tests to increase test coverage #259
Browse files Browse the repository at this point in the history
- `test_get_condition_values()` didn't resolve the partial branch coverage but I thought I'd commit it regardless because it might help us later on
  • Loading branch information
MRichards99 committed Dec 14, 2021
1 parent ed72451 commit e014af6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
4 changes: 2 additions & 2 deletions datagateway_api/src/common/base_query_filter_factory.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from abc import abstractstaticmethod
from abc import ABC, abstractstaticmethod


class QueryFilterFactory(object):
class QueryFilterFactory(ABC):
@abstractstaticmethod
def get_query_filter(request_filter, entity_name=None): # noqa: B902, N805
"""
Expand Down
22 changes: 22 additions & 0 deletions test/search_api/test_search_api_query_filter_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2004,3 +2004,25 @@ def test_valid_filter_input_with_all_filters(
def test_invalid_filter_input(self, test_request_filter):
with pytest.raises(FilterError):
SearchAPIQueryFilterFactory.get_query_filter(test_request_filter)

@pytest.mark.parametrize(
"filter_input, expected_return",
[
pytest.param(
{"property": "value"},
("property", "value", "eq"),
id="No operator specified",
),
pytest.param(
{"property": {"ne": "value"}},
("property", "value", "ne"),
id="Specific operator given in input",
),
],
)
def test_get_condition_values(self, filter_input, expected_return):
test_condition_values = SearchAPIQueryFilterFactory.get_condition_values(
filter_input,
)

assert test_condition_values == expected_return
15 changes: 15 additions & 0 deletions test/test_base_query_filter_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from datagateway_api.src.common.base_query_filter_factory import QueryFilterFactory


class TestBaseQueryFilterFactory:
def test_abstract_class(self):
QueryFilterFactory.__abstractmethods__ = set()

class DummyQueryFilterFactory(QueryFilterFactory):
pass

d = DummyQueryFilterFactory()

request_filter = "request_filter"

assert d.get_query_filter(request_filter) is None
14 changes: 13 additions & 1 deletion test/test_get_filters_from_query.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from datagateway_api.src.common.exceptions import FilterError
from datagateway_api.src.common.exceptions import ApiError, FilterError
from datagateway_api.src.common.helpers import get_filters_from_query_string
from datagateway_api.src.datagateway_api.database.filters import (
DatabaseDistinctFieldFilter,
Expand Down Expand Up @@ -52,3 +52,15 @@ def test_valid_multiple_filters(self, flask_test_app_db):
filters = get_filters_from_query_string("datagateway_api")

assert len(filters) == 2

def test_valid_search_api_filter(self, flask_test_app_db):
with flask_test_app_db:
flask_test_app_db.get('/?filter={"skip": 5, "limit": 10}')

filters = get_filters_from_query_string("search_api", "Dataset")

assert len(filters) == 2

def test_invalid_api_type(self):
with pytest.raises(ApiError):
get_filters_from_query_string("unknown_api")

0 comments on commit e014af6

Please sign in to comment.