Skip to content

Commit

Permalink
Add some mocked unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TorecLuik committed Jan 9, 2024
1 parent 03e3dcf commit 3effaa6
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 19 deletions.
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ find = {} # Scan the project directory with the default parameters

[tool.setuptools_scm]

[project.optional-dependencies]
test = [
"mock"
]

[project.urls]
"Homepage" = "https://github.com/NL-BioImaging/omero-slurm-client"
Expand Down
142 changes: 123 additions & 19 deletions tests/unit/test_slurm_client.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,127 @@
from omero_slurm_client import SlurmClient
import pytest
import mock
from mock import patch


def test_slurm_client_init():
# Test default initialization
slurm_client = SlurmClient()
assert isinstance(slurm_client, SlurmClient)
assert slurm_client.slurm_data_path == SlurmClient._DEFAULT_SLURM_DATA_PATH
assert slurm_client.slurm_images_path == SlurmClient._DEFAULT_SLURM_IMAGES_PATH
# Add more assertions for other default values

# Test custom initialization
slurm_client_custom = SlurmClient(
host="custom_host",
slurm_data_path="/custom/data/path",
slurm_images_path="/custom/images/path",
# Add more custom parameters
)
assert slurm_client_custom.host == "custom_host"
assert slurm_client_custom.slurm_data_path == "/custom/data/path"
assert slurm_client_custom.slurm_images_path == "/custom/images/path"
# Add more assertions for other custom values
@patch('omero_slurm_client.slurm_client.Connection.run')
@patch('omero_slurm_client.slurm_client.Connection.put')
def test_slurm_client_connection(mconn_put, mconn_run):
# GIVEN
# Create a SlurmClient instance and assign the mock Connection instance to its super attribute
slurm_client = SlurmClient("localhost", 8022, "slurm",
slurm_data_path="/path/to/data",
slurm_images_path="/path/to/images",
slurm_converters_path="/path/to/converters",
init_slurm=False)
# WHEN
slurm_client.run("echo hello world")
slurm_client.put("file.txt", "/remote/path")

# THEN
mconn_run.assert_called_with('echo hello world')
mconn_put.assert_called_once()


@pytest.mark.parametrize("slurm_model_repos, expected_slurm_model_images", [
({}, {}),
({"workflow1": "https://github.com/example/workflow1"},
{"workflow1": "dockerhub.com/image1"}),
])
@patch('omero_slurm_client.slurm_client.Connection.open')
@patch('omero_slurm_client.slurm_client.Connection.run')
@patch('omero_slurm_client.slurm_client.Connection.put')
@patch('omero_slurm_client.slurm_client.requests_cache.CachedSession')
def test_init_workflows(mock_CachedSession,
_mock_Connection_put,
_mock_Connection_run,
_mock_Connection_open,
slurm_model_repos, expected_slurm_model_images):
# GIVEN
wf_image = "dockerhub.com/image1"
json_descriptor = {"container-image": {"image": wf_image}}
github_session = mock_CachedSession.return_value
github_response = mock.Mock()
github_response.json.return_value = json_descriptor
github_session.get.return_value = github_response
slurm_client = SlurmClient("localhost", 8022, "slurm",
slurm_data_path="/path/to/data",
slurm_images_path="/path/to/images",
slurm_converters_path="/path/to/converters",
slurm_model_repos=slurm_model_repos)

# WHEN
slurm_client.init_workflows()
# THEN
assert slurm_client.slurm_model_images == expected_slurm_model_images


@patch('omero_slurm_client.slurm_client.requests_cache.CachedSession')
@patch('omero_slurm_client.slurm_client.Connection.run')
@patch('omero_slurm_client.slurm_client.Connection.put')
def test_init_workflows_force_update(_mock_Connection_put,
_mock_Connection_run,
mock_CachedSession):
# GIVEN
wf_name = "workflow1"
wf_repo = "https://github.com/example/workflow1"
wf_image = "dockerhub.com/image1"
json_descriptor = {"container-image": {"image": wf_image}}
github_session = mock_CachedSession.return_value
github_response = mock.Mock()
github_response.json.return_value = json_descriptor
github_session.get.return_value = github_response

slurm_client = SlurmClient("localhost", 8022, "slurm",
slurm_data_path="/path/to/data",
slurm_images_path="/path/to/images",
slurm_converters_path="/path/to/converters",
slurm_model_repos={wf_name: wf_repo})

# WHEN
slurm_client.init_workflows(force_update=True)

# THEN
mock_CachedSession.assert_called()
github_session.get.assert_called_with(
wf_repo+"/raw/master/descriptor.json")
assert slurm_client.slurm_model_images[wf_name] == wf_image


def test_invalid_workflow_repo():
# GIVEN
# THEN
with pytest.raises(ValueError,
match="Error while pulling descriptor file"):
# WHEN
invalid_url = "https://github.com/this-is-an-invalid-url/wf"
SlurmClient(
host="localhost",
port=8022,
user="slurm",
slurm_data_path="/path/to/data",
slurm_images_path="/path/to/images",
slurm_converters_path="/path/to/converters",
slurm_model_repos={
"workflow1": invalid_url},
slurm_script_repo="https://github.com/nl-bioimaging/slurm-scripts",
)


def test_invalid_workflow_url():
# GIVEN
# THEN
with pytest.raises(ValueError, match="Invalid GitHub URL"):
# WHEN
invalid_url = "https://this-is-an-invalid-url/wf"
SlurmClient(
host="localhost",
port=8022,
user="slurm",
slurm_data_path="/path/to/data",
slurm_images_path="/path/to/images",
slurm_converters_path="/path/to/converters",
slurm_model_repos={
"workflow1": invalid_url},
slurm_script_repo="https://github.com/nl-bioimaging/slurm-scripts",
)

0 comments on commit 3effaa6

Please sign in to comment.