|
23 | 23 | class TestGatherProcessingJob(unittest.TestCase): |
24 | 24 | """Tests methods in GatherProcessingJob class.""" |
25 | 25 |
|
| 26 | + @patch("pathlib.Path.exists") |
| 27 | + @patch("builtins.open", new_callable=mock_open, read_data='{"data_processes": []}') |
| 28 | + @patch("json.load") |
| 29 | + def test_load_existing_processing_exists_and_valid( |
| 30 | + self, mock_json_load: MagicMock, mock_file_open: MagicMock, mock_exists: MagicMock |
| 31 | + ): |
| 32 | + """Tests load_existing_processing when file exists and is valid.""" |
| 33 | + mock_exists.return_value = True |
| 34 | + existing_processing_dict = { |
| 35 | + "data_processes": [ |
| 36 | + { |
| 37 | + "start_date_time": "2024-09-09T01:02:03", |
| 38 | + "end_date_time": "2024-09-10T01:02:03", |
| 39 | + "process_type": "Image atlas alignment", |
| 40 | + "experimenters": ["Existing Experimenter"], |
| 41 | + "stage": "Processing", |
| 42 | + "code": {"url": "www.example.com/existing"}, |
| 43 | + } |
| 44 | + ] |
| 45 | + } |
| 46 | + mock_json_load.return_value = existing_processing_dict |
| 47 | + |
| 48 | + example_processing = Processing(data_processes=[]) |
| 49 | + example_settings = JobSettings(output_directory="example", processing=example_processing) |
| 50 | + job = GatherProcessingJob(settings=example_settings) |
| 51 | + |
| 52 | + result = job.load_existing_processing() |
| 53 | + |
| 54 | + self.assertIsNotNone(result) |
| 55 | + self.assertIsInstance(result, Processing) |
| 56 | + self.assertEqual(len(result.data_processes), 1) |
| 57 | + mock_exists.assert_called_once() |
| 58 | + mock_json_load.assert_called_once() |
| 59 | + |
| 60 | + @patch("pathlib.Path.exists") |
| 61 | + def test_load_existing_processing_does_not_exist(self, mock_exists: MagicMock): |
| 62 | + """Tests load_existing_processing when file does not exist.""" |
| 63 | + mock_exists.return_value = False |
| 64 | + |
| 65 | + example_processing = Processing(data_processes=[]) |
| 66 | + example_settings = JobSettings(output_directory="example", processing=example_processing) |
| 67 | + job = GatherProcessingJob(settings=example_settings) |
| 68 | + |
| 69 | + result = job.load_existing_processing() |
| 70 | + |
| 71 | + self.assertIsNone(result) |
| 72 | + mock_exists.assert_called_once() |
| 73 | + |
| 74 | + @patch("pathlib.Path.exists") |
| 75 | + @patch("builtins.open", new_callable=mock_open) |
| 76 | + @patch("json.load") |
| 77 | + @patch("logging.error") |
| 78 | + def test_load_existing_processing_invalid_schema( |
| 79 | + self, mock_log_error: MagicMock, mock_json_load: MagicMock, mock_file_open: MagicMock, mock_exists: MagicMock |
| 80 | + ): |
| 81 | + """Tests load_existing_processing when file exists but doesn't match Processing schema.""" |
| 82 | + mock_exists.return_value = True |
| 83 | + mock_json_load.return_value = {"invalid_field": "value"} |
| 84 | + |
| 85 | + example_processing = Processing(data_processes=[]) |
| 86 | + example_settings = JobSettings(output_directory="example", processing=example_processing) |
| 87 | + job = GatherProcessingJob(settings=example_settings) |
| 88 | + |
| 89 | + with self.assertRaises(Exception): |
| 90 | + job.load_existing_processing() |
| 91 | + |
| 92 | + mock_log_error.assert_called_once() |
| 93 | + |
26 | 94 | @patch("builtins.open", new_callable=mock_open) |
27 | 95 | @patch("json.dump") |
28 | 96 | def test_run_job(self, mock_json_dump: MagicMock, mock_file_open: MagicMock): |
29 | | - """Tests run_job method.""" |
| 97 | + """Tests run_job method with example processing data.""" |
30 | 98 | # noinspection PyArgumentList |
31 | 99 | example_processing = Processing( |
32 | 100 | data_processes=[ |
@@ -58,6 +126,55 @@ def test_run_job(self, mock_json_dump: MagicMock, mock_file_open: MagicMock): |
58 | 126 | mock_file_open.assert_has_calls(calls=[call(Path("example") / "processing.json", "w")]) |
59 | 127 | mock_json_dump.assert_called_once() |
60 | 128 |
|
| 129 | + @patch("aind_metadata_mapper.gather_processing_job.GatherProcessingJob.load_existing_processing") |
| 130 | + @patch("builtins.open", new_callable=mock_open) |
| 131 | + @patch("json.dump") |
| 132 | + @patch("logging.error") |
| 133 | + def test_run_job_merge_fails_raises_error( |
| 134 | + self, |
| 135 | + mock_log_error: MagicMock, |
| 136 | + mock_json_dump: MagicMock, |
| 137 | + mock_file_open: MagicMock, |
| 138 | + mock_load_existing: MagicMock, |
| 139 | + ): |
| 140 | + """Tests run_job raises exception when merge fails.""" |
| 141 | + existing_processing = Processing( |
| 142 | + data_processes=[ |
| 143 | + DataProcess( |
| 144 | + start_date_time=datetime(2024, 9, 9, 1, 2, 3), |
| 145 | + end_date_time=datetime(2024, 9, 10, 1, 2, 3), |
| 146 | + process_type=ProcessName.IMAGE_ATLAS_ALIGNMENT, |
| 147 | + experimenters=["Existing Experimenter"], |
| 148 | + stage=ProcessStage.PROCESSING, |
| 149 | + code=Code(url="www.example.com/existing_process"), |
| 150 | + ) |
| 151 | + ] |
| 152 | + ) |
| 153 | + mock_load_existing.return_value = existing_processing |
| 154 | + with patch.object(Processing, "__add__", side_effect=Exception("Merge failed")): |
| 155 | + new_processing = Processing( |
| 156 | + data_processes=[ |
| 157 | + DataProcess( |
| 158 | + start_date_time=datetime(2024, 10, 10, 1, 2, 3), |
| 159 | + end_date_time=datetime(2024, 10, 11, 1, 2, 3), |
| 160 | + process_type=ProcessName.COMPRESSION, |
| 161 | + experimenters=["AIND Scientific Computing"], |
| 162 | + stage=ProcessStage.PROCESSING, |
| 163 | + code=Code(url="www.example.com/ephys_compression"), |
| 164 | + ) |
| 165 | + ] |
| 166 | + ) |
| 167 | + example_settings = JobSettings(output_directory="example", processing=new_processing) |
| 168 | + job = GatherProcessingJob(settings=example_settings) |
| 169 | + |
| 170 | + with self.assertRaises(Exception) as context: |
| 171 | + job.run_job() |
| 172 | + |
| 173 | + self.assertIn("Merge failed", str(context.exception)) |
| 174 | + mock_log_error.assert_called_once() |
| 175 | + self.assertIn("Failed to merge existing processing.json", str(mock_log_error.call_args)) |
| 176 | + mock_json_dump.assert_not_called() |
| 177 | + |
61 | 178 |
|
62 | 179 | if __name__ == "__main__": |
63 | 180 | unittest.main() |
0 commit comments