Skip to content

Commit

Permalink
reorganized tool-existing related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
qcampbel committed Jan 4, 2024
1 parent 16b69dc commit e47a85c
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 75 deletions.
83 changes: 8 additions & 75 deletions tests/test_fxns.py → tests/test_analysis_fxns.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@

import pytest

from mdagent.tools.base_tools import (
CleaningTools,
SimulationFunctions,
VisFunctions,
get_pdb,
)
from mdagent.tools.base_tools.analysis_tools import VisFunctions
from mdagent.tools.base_tools.analysis_tools.plot_tools import plot_data, process_csv
from mdagent.utils import PathRegistry

Expand All @@ -33,34 +28,12 @@ def path_to_cif():
os.chdir(original_cwd)


@pytest.fixture
def cleaning_fxns():
return CleaningTools()


# Test simulation tools
@pytest.fixture
def sim_fxns():
return SimulationFunctions()


# Test visualization tools
@pytest.fixture
def vis_fxns():
return VisFunctions()


# Test MD utility tools
@pytest.fixture
def fibronectin():
return "fibronectin pdb"


@pytest.fixture
def get_registry():
return PathRegistry()


# Plot tools
def test_process_csv():
mock_csv_content = "Time,Value1,Value2\n1,10,20\n2,15,25"
mock_reader = MagicMock()
Expand Down Expand Up @@ -114,6 +87,12 @@ def test_plot_data():
assert "All plots failed due to non-numeric data." in str(excinfo.value)


# Visualization tools
@pytest.fixture
def vis_fxns():
return VisFunctions()


@pytest.mark.skip(reason="molrender is not pip installable")
def test_run_molrender(path_to_cif, vis_fxns):
result = vis_fxns.run_molrender(path_to_cif)
Expand All @@ -123,49 +102,3 @@ def test_run_molrender(path_to_cif, vis_fxns):
def test_create_notebook(path_to_cif, vis_fxns, get_registry):
result = vis_fxns.create_notebook(path_to_cif, get_registry)
assert result == "Visualization Complete"


def test_add_hydrogens_and_remove_water(path_to_cif, cleaning_fxns, get_registry):
result = cleaning_fxns._add_hydrogens_and_remove_water(path_to_cif, get_registry)
assert "Cleaned File" in result # just want to make sur the function ran


@patch("os.path.exists")
@patch("os.listdir")
def test_extract_parameters_path(mock_listdir, mock_exists, sim_fxns, get_registry):
# Test when parameters.json exists
mock_exists.return_value = True
assert sim_fxns._extract_parameters_path() == "simulation_parameters_summary.json"
mock_exists.assert_called_once_with("simulation_parameters_summary.json")
mock_exists.reset_mock() # Reset the mock for the next scenario

# Test when parameters.json does not exist, but some_parameters.json does
mock_exists.return_value = False
mock_listdir.return_value = ["some_parameters.json", "other_file.txt"]
assert sim_fxns._extract_parameters_path() == "some_parameters.json"

# Test when no appropriate file exists
mock_listdir.return_value = ["other_file.json", "other_file.txt"]
with pytest.raises(ValueError) as e:
sim_fxns._extract_parameters_path()
assert str(e.value) == "No parameters.json file found in directory."


@patch(
"builtins.open",
new_callable=mock_open,
read_data='{"param1": "value1", "param2": "value2"}',
)
@patch("json.load")
def test_setup_simulation_from_json(mock_json_load, mock_file_open, sim_fxns):
# Define the mock behavior for json.load
mock_json_load.return_value = {"param1": "value1", "param2": "value2"}
params = sim_fxns._setup_simulation_from_json("test_file.json")
mock_file_open.assert_called_once_with("test_file.json", "r")
mock_json_load.assert_called_once()
assert params == {"param1": "value1", "param2": "value2"}


def test_getpdb(fibronectin, get_registry):
name = get_pdb(fibronectin, get_registry)
assert name.endswith(".pdb")
52 changes: 52 additions & 0 deletions tests/test_preprocess_fxns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import os

import pytest

from mdagent.tools.base_tools.preprocess_tools import CleaningTools, get_pdb
from mdagent.utils import PathRegistry

# Test functions in preprocess tools


@pytest.fixture
def get_registry():
return PathRegistry()


@pytest.fixture
def path_to_cif():
# Save original working directory
original_cwd = os.getcwd()

# Change current working directory to the directory where the CIF file is located
tests_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(tests_dir)

# Yield the filename only
filename_only = "3pqr.cif"
yield filename_only

# Restore original working directory after the test is done
os.chdir(original_cwd)


# test get pdb file
@pytest.fixture
def fibronectin():
return "fibronectin pdb"


def test_getpdb(fibronectin, get_registry):
name = get_pdb(fibronectin, get_registry)
assert name.endswith(".pdb")


# test cleaning functions
@pytest.fixture
def cleaning_fxns():
return CleaningTools()


def test_add_hydrogens_and_remove_water(path_to_cif, cleaning_fxns, get_registry):
result = cleaning_fxns._add_hydrogens_and_remove_water(path_to_cif, get_registry)
assert "Cleaned File" in result # just want to make sur the function ran
48 changes: 48 additions & 0 deletions tests/test_sim_fxns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from unittest.mock import mock_open, patch

import pytest

from mdagent.tools.base_tools.simulation_tools import SimulationFunctions

# Test functions in simulation tools


@pytest.fixture
def sim_fxns():
return SimulationFunctions()


@patch("os.path.exists")
@patch("os.listdir")
def test_extract_parameters_path(mock_listdir, mock_exists, sim_fxns):
# Test when parameters.json exists
mock_exists.return_value = True
assert sim_fxns._extract_parameters_path() == "simulation_parameters_summary.json"
mock_exists.assert_called_once_with("simulation_parameters_summary.json")
mock_exists.reset_mock() # Reset the mock for the next scenario

# Test when parameters.json does not exist, but some_parameters.json does
mock_exists.return_value = False
mock_listdir.return_value = ["some_parameters.json", "other_file.txt"]
assert sim_fxns._extract_parameters_path() == "some_parameters.json"

# Test when no appropriate file exists
mock_listdir.return_value = ["other_file.json", "other_file.txt"]
with pytest.raises(ValueError) as e:
sim_fxns._extract_parameters_path()
assert str(e.value) == "No parameters.json file found in directory."


@patch(
"builtins.open",
new_callable=mock_open,
read_data='{"param1": "value1", "param2": "value2"}',
)
@patch("json.load")
def test_setup_simulation_from_json(mock_json_load, mock_file_open, sim_fxns):
# Define the mock behavior for json.load
mock_json_load.return_value = {"param1": "value1", "param2": "value2"}
params = sim_fxns._setup_simulation_from_json("test_file.json")
mock_file_open.assert_called_once_with("test_file.json", "r")
mock_json_load.assert_called_once()
assert params == {"param1": "value1", "param2": "value2"}
Empty file added tests/test_util_tools.py
Empty file.

0 comments on commit e47a85c

Please sign in to comment.