|
| 1 | +import json |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from mdagent.tools.base_tools.analysis_tools.rdf_tool import RDFTool |
| 6 | + |
| 7 | +# TODO add dcd files in testing file for testing |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture(scope="module") |
| 11 | +def rdf_input_good_string(): |
| 12 | + return """ |
| 13 | + { |
| 14 | + "trajectory_fileid": "rec0_142404", |
| 15 | + "topology_fileid": "top_sim0_142401", |
| 16 | + "stride": 2, |
| 17 | + "selections": ["protein", "water"] |
| 18 | + } |
| 19 | + """ |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture(scope="module") |
| 23 | +def rdf_input_wrong_string_1(): |
| 24 | + return """ |
| 25 | + { |
| 26 | + "topology_fileid": "top_sim0_142401", |
| 27 | + "stride": 2, |
| 28 | + "selections": [["protein", "water", "lipid"]] |
| 29 | + } |
| 30 | + """ |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture(scope="module") |
| 34 | +def rdf_input_wrong_string_2(): |
| 35 | + return """ |
| 36 | + { |
| 37 | + "trajectory_fileid": "rec0_142404", |
| 38 | + "topology_fileid": "top_sim0_142401", |
| 39 | + "stride": "half", |
| 40 | + "selections": [["protein", "water", "lipid"]] |
| 41 | + } |
| 42 | + """ |
| 43 | + |
| 44 | + |
| 45 | +@pytest.fixture(scope="module") |
| 46 | +def rdf_input_wrong_string_3(): |
| 47 | + return """ |
| 48 | + { |
| 49 | + "trajectory_fileid": "rec0_142404Wrong", |
| 50 | + "topology_fileid": "top_sim0_142401", |
| 51 | + "stride": 2, |
| 52 | + "selections": [["protein", "water"]] |
| 53 | + } |
| 54 | + """ |
| 55 | + |
| 56 | + |
| 57 | +def test_rdf_tool_init(get_registry): |
| 58 | + registry = get_registry("raw", False) |
| 59 | + tool = RDFTool(path_registry=registry) |
| 60 | + assert tool.name == "RDFTool" |
| 61 | + assert tool.path_registry == registry |
| 62 | + |
| 63 | + |
| 64 | +class MockRegistryResponse: |
| 65 | + # mock json() method always returns a specific testing dictionary |
| 66 | + dictionary = { |
| 67 | + "rec0_142404": "files/records/TRAJ_sim0123456_142404.dcd", |
| 68 | + "top_sim0_142401": "files/records/TOP_sim0123456_142401.pdb", |
| 69 | + } |
| 70 | + |
| 71 | + @staticmethod |
| 72 | + def get_mapped_path(self, fileid): |
| 73 | + return self.dictionary[fileid] |
| 74 | + |
| 75 | + def list_path_names(self): |
| 76 | + return "rec0_142404", "top_sim0_142401" |
| 77 | + |
| 78 | + |
| 79 | +def test_rdf_tool_validation( |
| 80 | + monkeypatch, |
| 81 | + rdf_input_good_string, |
| 82 | + rdf_input_wrong_string_1, |
| 83 | + rdf_input_wrong_string_2, |
| 84 | + rdf_input_wrong_string_3, |
| 85 | + get_registry, |
| 86 | +): |
| 87 | + registry = get_registry("raw", False) |
| 88 | + tool = RDFTool(path_registry=registry) |
| 89 | + |
| 90 | + def mock_get_mapped_path(fileid): |
| 91 | + return MockRegistryResponse.dictionary[fileid] |
| 92 | + |
| 93 | + def mock_list_path_names(): |
| 94 | + return "rec0_142404", "top_sim0_142401" |
| 95 | + |
| 96 | + monkeypatch.setattr(tool.path_registry, "get_mapped_path", mock_get_mapped_path) |
| 97 | + monkeypatch.setattr(tool.path_registry, "list_path_names", mock_list_path_names) |
| 98 | + # assert that a valueerror was raised |
| 99 | + with pytest.raises(ValueError) as error: |
| 100 | + inputs = tool.validate_input(json.loads(rdf_input_wrong_string_1)) |
| 101 | + assert str(error.value) == "Missing Inputs: Trajectory file ID is required" |
| 102 | + inputs = tool.validate_input(json.loads(rdf_input_wrong_string_2)) |
| 103 | + assert str(error.value) == ( |
| 104 | + "Incorrect Inputs: Stride must be an integer " |
| 105 | + "or None for default value of 1" |
| 106 | + ) |
| 107 | + inputs = tool.validate_input(json.loads(rdf_input_wrong_string_3)) |
| 108 | + assert str(error.value) == "Trajectory File ID not in path registry" |
| 109 | + |
| 110 | + inputs = tool.validate_input(json.loads(rdf_input_good_string)) |
| 111 | + |
| 112 | + assert inputs["trajectory_fileid"] == "rec0_142404" |
| 113 | + assert inputs["topology_fileid"] == "top_sim0_142401" |
| 114 | + assert inputs["stride"] == 2 |
| 115 | + # assert inputs["selections"] == ["protein", "water"] |
0 commit comments