Skip to content

Commit

Permalink
Merge pull request #251 from ral-facilities/increase-test-coverage
Browse files Browse the repository at this point in the history
Increase Test Coverage
  • Loading branch information
MRichards99 authored Aug 16, 2021
2 parents f48b471 + 9e0977d commit 87fbbeb
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 2 deletions.
14 changes: 14 additions & 0 deletions test/icat/filters/test_limit_filter.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from unittest.mock import patch

import pytest

from datagateway_api.common.exceptions import FilterError
from datagateway_api.common.filter_order_handler import FilterOrderHandler
from datagateway_api.common.icat.filters import (
icat_set_limit,
PythonICATLimitFilter,
PythonICATSkipFilter,
)
Expand Down Expand Up @@ -54,3 +57,14 @@ def test_limit_and_skip_merge_correctly(self, icat_query, skip_value, limit_valu
filter_handler.apply_filters(icat_query)

assert icat_query.limit == (skip_value, limit_value)

def test_invalid_icat_set_limit(self, icat_query):
"""
The validity of this function is tested when applying a limit filter, an explict
invalid test case is required to cover when invalid arguments are given
"""
with patch(
"icat.query.Query.setLimit", side_effect=TypeError("Mocked Exception"),
):
with pytest.raises(FilterError):
icat_set_limit(icat_query, 50, 50)
19 changes: 17 additions & 2 deletions test/icat/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from unittest.mock import patch

from icat.exception import ICATInternalError
import pytest

from datagateway_api.common.exceptions import BadRequestError
from datagateway_api.common.icat.helpers import get_icat_entity_name_as_camel_case
from datagateway_api.common.exceptions import BadRequestError, PythonICATError
from datagateway_api.common.icat.helpers import (
get_icat_entity_name_as_camel_case,
push_data_updates_to_icat,
)


class TestICATHelpers:
Expand Down Expand Up @@ -32,3 +38,12 @@ def test_valid_get_icat_entity_name_as_camel_case(
def test_invalid_get_icat_entity_name_as_camel_case(self, icat_client):
with pytest.raises(BadRequestError):
get_icat_entity_name_as_camel_case(icat_client, "UnknownEntityName")

def test_invalid_update_pushes(self, icat_client):
with patch(
"icat.entity.Entity.update",
side_effect=ICATInternalError("Mocked Exception"),
):
inv_entity = icat_client.new("investigation", name="Investigation A")
with pytest.raises(PythonICATError):
push_data_updates_to_icat(inv_entity)
17 changes: 17 additions & 0 deletions test/icat/test_icat_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from icat.client import Client

from datagateway_api.common.icat.icat_client_pool import ICATClient


class TestICATClient:
def test_init(self):
test_icat_client = ICATClient()
assert isinstance(test_icat_client, Client)

assert not test_icat_client.autoLogout

def test_clean_up(self):
test_icat_client = ICATClient()
assert id(test_icat_client) in Client.Register
test_icat_client.clean_up()
assert id(test_icat_client) not in Client.Register
11 changes: 11 additions & 0 deletions test/icat/test_session_handling.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from datetime import datetime
from unittest.mock import patch

from dateutil.tz import tzlocal
from icat.client import Client
import pytest

from datagateway_api.common.backends import create_backend
from datagateway_api.common.config import APIConfigOptions, config
from datagateway_api.common.date_handler import DateHandler
from datagateway_api.common.exceptions import AuthenticationError
from datagateway_api.common.icat.filters import PythonICATWhereFilter
from datagateway_api.common.icat.icat_client_pool import create_client_pool


class TestSessionHandling:
Expand Down Expand Up @@ -141,6 +145,13 @@ def test_invalid_login(

assert login_response.status_code == expected_response_code

def test_expired_session(self):
test_backend = create_backend("python_icat")
client_pool = create_client_pool()
with patch("icat.client.Client.getRemainingMinutes", return_value=-1):
with pytest.raises(AuthenticationError):
test_backend.get_session_details("session id", client_pool=client_pool)

def test_valid_logout(self, flask_test_app_icat):
client = Client(
config.get_config_value(APIConfigOptions.ICAT_URL),
Expand Down
24 changes: 24 additions & 0 deletions test/test_openapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from datetime import date, datetime

import pytest

from datagateway_api.src.resources.entities.entity_map import type_conversion


class TestOpenAPI:
@pytest.mark.parametrize(
"python_type, expected_type",
[
pytest.param(int, {"type": "integer"}, id="integer"),
pytest.param(float, {"type": "number", "format": "float"}, id="float"),
pytest.param(bool, {"type": "boolean"}, id="boolean"),
pytest.param(
datetime, {"type": "string", "format": "datetime"}, id="datetime",
),
pytest.param(date, {"type": "string", "format": "date"}, id="date"),
pytest.param(str, {"type": "string"}, id="string"),
],
)
def test_type_conversion(self, python_type, expected_type):
openapi_type = type_conversion(python_type)
assert openapi_type == expected_type
14 changes: 14 additions & 0 deletions test/test_query_filter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
from unittest.mock import patch

import pytest

from datagateway_api.common.exceptions import ApiError
from datagateway_api.common.filters import QueryFilter
from datagateway_api.common.query_filter_factory import QueryFilterFactory


class TestQueryFilter:
Expand All @@ -16,3 +22,11 @@ class DummyQueryFilter(QueryFilter):

assert qf.precedence is None
assert qf.apply_filter(apply_filter) is None

def test_invalid_query_filter_getter(self):
with patch(
"datagateway_api.common.config.config.get_config_value",
return_value="invalid_backend",
):
with pytest.raises(ApiError):
QueryFilterFactory.get_query_filter({"order": "id DESC"})

0 comments on commit 87fbbeb

Please sign in to comment.