Skip to content

Commit

Permalink
added tests to writefilename and map path
Browse files Browse the repository at this point in the history
  • Loading branch information
Jgmedina95 committed Jan 9, 2024
1 parent b6962df commit 668ba24
Showing 1 changed file with 108 additions and 1 deletion.
109 changes: 108 additions & 1 deletion tests/test_fxns.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os
import warnings
from unittest.mock import MagicMock, mock_open, patch
Expand All @@ -11,7 +12,7 @@
get_pdb,
)
from mdagent.tools.base_tools.analysis_tools.plot_tools import plot_data, process_csv
from mdagent.utils import PathRegistry
from mdagent.utils import FileType, PathRegistry

warnings.filterwarnings("ignore", category=DeprecationWarning, module="pkg_resources")

Expand Down Expand Up @@ -169,3 +170,109 @@ def test_setup_simulation_from_json(mock_json_load, mock_file_open, sim_fxns):
def test_getpdb(fibronectin, get_registry):
name, _ = get_pdb(fibronectin, get_registry)
assert name.endswith(".pdb")


@pytest.fixture
def path_registry():
registry = PathRegistry()
registry.get_timestamp = lambda: "20240109"
return registry


def test_write_to_file():
path_registry = PathRegistry()

with patch("builtins.open", mock_open()):
file_name = path_registry.write_file_name(
FileType.PROTEIN,
protein_name="1XYZ",
description="testing",
file_format="pdb",
)
# assert file name starts and ends correctly
assert file_name.startswith("1XYZ")
assert file_name.endswith(".pdb")


def test_write_file_name_protein(path_registry):
file_name = path_registry.write_file_name(
FileType.PROTEIN, protein_name="1XYZ", description="testing", file_format="pdb"
)
assert file_name == "1XYZ_testing_20240109.pdb"


def test_write_file_name_simulation_with_conditions(path_registry):
file_name = path_registry.write_file_name(
FileType.SIMULATION,
type_of_sim="MD",
protein_file_id="1XYZ",
conditions="pH7",
time_stamp="20240109",
)
assert file_name == "MD_1XYZ_pH7_20240109"


def test_write_file_name_simulation_modified(path_registry):
file_name = path_registry.write_file_name(
FileType.SIMULATION, Sim_id="SIM456", modified=True, time_stamp="20240109"
)
assert file_name == "SIM456_MOD_20240109"


def test_write_file_name_simulation_default(path_registry):
file_name = path_registry.write_file_name(
FileType.SIMULATION,
type_of_sim="MD",
protein_file_id="123",
time_stamp="20240109",
)
assert file_name == "MD_123_20240109"


def test_write_file_name_record(path_registry):
file_name = path_registry.write_file_name(
FileType.RECORD, protein_file_id="123", Sim_id="SIM456", time_stamp="20240109"
)
assert file_name == "123_SIM456_20240109"


def test_map_path():
mock_json_data = {
"existing_name": {
"path": "existing/path",
"description": "Existing description",
}
}
new_path_dict = {"new_name": {"path": "new/path", "description": "New description"}}
updated_json_data = {**mock_json_data, **new_path_dict}

path_registry = PathRegistry()
path_registry.json_file_path = "dummy_json_file.json"

# Mocking os.path.exists to simulate the JSON file existence
with patch("os.path.exists", return_value=True):
# Mocking open for both reading and writing the JSON file
with patch(
"builtins.open", mock_open(read_data=json.dumps(mock_json_data))
) as mocked_file:
# Optionally, you can mock internal methods if needed
with patch.object(
path_registry, "_check_for_json", return_value=True
), patch.object(
path_registry, "_check_json_content", return_value=True
), patch.object(
path_registry, "_get_full_path", return_value="new/path"
): # Mocking _get_full_path
result = path_registry.map_path(
"new_name", "new/path", "New description"
)
# Aggregating all calls to write into a single string
written_data = "".join(
call.args[0] for call in mocked_file().write.call_args_list
)

# Comparing the aggregated data with the expected JSON data
assert json.loads(written_data) == updated_json_data

# Check the result message
assert result == "Path successfully mapped to name: new_name"

0 comments on commit 668ba24

Please sign in to comment.