diff --git a/test/icat/filters/test_limit_filter.py b/test/icat/filters/test_limit_filter.py index 5599e22c..c9d02e9d 100644 --- a/test/icat/filters/test_limit_filter.py +++ b/test/icat/filters/test_limit_filter.py @@ -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, ) @@ -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) diff --git a/test/icat/test_helpers.py b/test/icat/test_helpers.py index dde50c69..c1963ca3 100644 --- a/test/icat/test_helpers.py +++ b/test/icat/test_helpers.py @@ -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: @@ -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) diff --git a/test/icat/test_icat_client.py b/test/icat/test_icat_client.py new file mode 100644 index 00000000..b68f3e19 --- /dev/null +++ b/test/icat/test_icat_client.py @@ -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 diff --git a/test/icat/test_session_handling.py b/test/icat/test_session_handling.py index 2d45463a..1392f962 100644 --- a/test/icat/test_session_handling.py +++ b/test/icat/test_session_handling.py @@ -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: @@ -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), diff --git a/test/test_openapi.py b/test/test_openapi.py new file mode 100644 index 00000000..bbf2cdf3 --- /dev/null +++ b/test/test_openapi.py @@ -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 diff --git a/test/test_query_filter.py b/test/test_query_filter.py index ec865390..7eddfcd1 100644 --- a/test/test_query_filter.py +++ b/test/test_query_filter.py @@ -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: @@ -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"})