From 7e89e295bd2a4c48a932fdec03bdc21b04a40fce Mon Sep 17 00:00:00 2001 From: Matthew Richards Date: Fri, 13 Aug 2021 07:27:27 +0000 Subject: [PATCH] #241: Add tests for ping endpoint --- test/db/endpoints/test_ping_db.py | 24 ++++++++++++++++++++++++ test/icat/endpoints/test_ping_icat.py | 26 ++++++++++++++++++++++++++ test/test_backends.py | 1 + 3 files changed, 51 insertions(+) create mode 100644 test/db/endpoints/test_ping_db.py create mode 100644 test/icat/endpoints/test_ping_icat.py diff --git a/test/db/endpoints/test_ping_db.py b/test/db/endpoints/test_ping_db.py new file mode 100644 index 00000000..96c4588b --- /dev/null +++ b/test/db/endpoints/test_ping_db.py @@ -0,0 +1,24 @@ +from unittest.mock import patch + +import pytest +from sqlalchemy.exc import SQLAlchemyError + +from datagateway_api.common.backends import create_backend +from datagateway_api.common.constants import Constants +from datagateway_api.common.exceptions import DatabaseError + + +class TestICATPing: + def test_valid_ping(self, flask_test_app_db): + test_response = flask_test_app_db.get("/ping") + + assert test_response.json == Constants.PING_OK_RESPONSE + + def test_invalid_ping(self): + with patch( + "sqlalchemy.engine.reflection.Inspector.get_table_names", + side_effect=SQLAlchemyError("Mocked Exception"), + ): + with pytest.raises(DatabaseError): + backend = create_backend("db") + backend.ping() diff --git a/test/icat/endpoints/test_ping_icat.py b/test/icat/endpoints/test_ping_icat.py new file mode 100644 index 00000000..d18d15e1 --- /dev/null +++ b/test/icat/endpoints/test_ping_icat.py @@ -0,0 +1,26 @@ +from unittest.mock import patch + +from icat.exception import ICATError +import pytest + +from datagateway_api.common.backends import create_backend +from datagateway_api.common.constants import Constants +from datagateway_api.common.exceptions import PythonICATError +from datagateway_api.common.icat.icat_client_pool import create_client_pool + + +class TestICATPing: + def test_valid_ping(self, flask_test_app_icat): + test_response = flask_test_app_icat.get("/ping") + + assert test_response.json == Constants.PING_OK_RESPONSE + + def test_invalid_ping(self): + with patch( + "icat.client.Client.getEntityNames", + side_effect=ICATError("Mocked Exception"), + ): + with pytest.raises(PythonICATError): + backend = create_backend("python_icat") + client_pool = create_client_pool() + backend.ping(client_pool=client_pool) diff --git a/test/test_backends.py b/test/test_backends.py index 36637365..d2d82948 100644 --- a/test/test_backends.py +++ b/test/test_backends.py @@ -43,6 +43,7 @@ class DummyBackend(Backend): facilitycycle_id = "facilitycycle_id" id_ = "id_" + assert d.ping() is None assert d.login(credentials) is None assert d.get_session_details(session_id) is None assert d.refresh(session_id) is None