Skip to content

Commit

Permalink
test: add tests for search API session/client handling #258
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Nov 25, 2021
1 parent 75bb983 commit b7dea73
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
46 changes: 46 additions & 0 deletions test/datagateway_api/icat/test_icat_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import json
from unittest.mock import mock_open, patch

from icat.client import Client
import pytest

from datagateway_api.src.common.config import APIConfig
from datagateway_api.src.datagateway_api.icat.icat_client_pool import ICATClient


Expand All @@ -10,6 +15,47 @@ def test_init(self):

assert not test_icat_client.autoLogout

@pytest.mark.parametrize(
"client_use, expected_url, expected_check_cert",
[
pytest.param(
"datagateway_api",
"https://localhost:8181/ICATService/ICAT?wsdl",
False,
id="DataGateway API Usage",
),
pytest.param(
"search_api",
"https://localhost.testdomain:8181/ICATService/ICAT?wsdl",
True,
id="Search API Usage",
),
],
)
def test_client_use(
self, test_config_data, client_use, expected_url, expected_check_cert,
):
with patch("builtins.open", mock_open(read_data=json.dumps(test_config_data))):
api_config = APIConfig.load("test/path")

class MockClient:
def __init__(url, checkCert=True): # noqa
print(f"URL: {url}, Cert: {checkCert}")
# Would've preferred to assign these values to self but this didn't
# seem to be possible
Client.url = f"{url}/ICATService/ICAT?wsdl"
Client.checkCert = checkCert

with patch(
"datagateway_api.src.common.config.Config.config", api_config,
):
with patch(
"icat.client.Client.__init__", side_effect=MockClient.__init__,
):
test_icat_client = ICATClient(client_use)
assert test_icat_client.url == expected_url
assert test_icat_client.checkCert == expected_check_cert

def test_clean_up(self):
test_icat_client = ICATClient()
assert id(test_icat_client) in Client.Register
Expand Down
21 changes: 21 additions & 0 deletions test/search_api/test_session_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from datagateway_api.src.datagateway_api.icat.icat_client_pool import ICATClient
from datagateway_api.src.search_api.session_handler import (
client_manager,
SessionHandler,
)


class TestSessionHandler:
def test_session_handler_class(self):
assert isinstance(SessionHandler.client, ICATClient)

def test_client_manager_decorator(self):
@client_manager
def manage_client():
pass

# Checks that the decorator assigns a session ID, checking one is not present
# before the decorator runs
assert not SessionHandler.client.sessionId
manage_client()
assert SessionHandler.client.sessionId

0 comments on commit b7dea73

Please sign in to comment.