-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
04761dc
commit c87c09b
Showing
2 changed files
with
87 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from unittest.mock import MagicMock, patch | ||
|
||
|
||
def test_create_conda_env(): | ||
# Define test parameters | ||
computer_name = "localhost" | ||
env_name = "test_env" | ||
pip_packages = ["numpy", "pandas"] | ||
conda_deps = ["scipy"] | ||
python_version = "3.8" | ||
shell = "posix" | ||
|
||
# Mock the computer and related objects | ||
mock_computer = MagicMock() | ||
mock_computer.label = computer_name | ||
mock_user = MagicMock() | ||
mock_user.email = "[email protected]" | ||
mock_authinfo = MagicMock() | ||
mock_transport = MagicMock() | ||
mock_scheduler = MagicMock() | ||
|
||
mock_authinfo.get_transport.return_value = mock_transport | ||
mock_computer.get_authinfo.return_value = mock_authinfo | ||
mock_authinfo.computer.get_scheduler.return_value = mock_scheduler | ||
|
||
# Mock successful transport behavior | ||
mock_transport.exec_command_wait.return_value = ( | ||
0, # retval | ||
"Environment setup is complete.\n", # stdout | ||
"", # stderr | ||
) | ||
|
||
# Patch `load_computer` and `User.collection.get_default` to return mocked objects | ||
with ( | ||
patch("aiida.orm.utils.loaders.load_computer", return_value=mock_computer), | ||
patch("aiida.orm.User.collection.get_default", return_value=mock_user), | ||
): | ||
from aiida_pythonjob.utils import create_conda_env | ||
|
||
# Call the function | ||
success, message = create_conda_env( | ||
computer=computer_name, | ||
name=env_name, | ||
pip=pip_packages, | ||
conda={"dependencies": conda_deps}, | ||
python_version=python_version, | ||
shell=shell, | ||
) | ||
|
||
# Assertions for successful case | ||
assert success is True | ||
assert message == "Environment setup is complete." | ||
|
||
# Validate that exec_command_wait was called with the generated script | ||
mock_transport.exec_command_wait.assert_called_once() | ||
called_script = mock_transport.exec_command_wait.call_args[0][0] | ||
assert f"conda create -y -n {env_name} python={python_version}" in called_script | ||
assert "pip install numpy pandas" in called_script | ||
|
||
mock_transport.close() |