|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +import tempfile |
| 4 | +import unittest |
| 5 | +from unittest.mock import patch |
| 6 | + |
| 7 | +from CodeEntropy.run import RunManager |
| 8 | + |
| 9 | + |
| 10 | +class TestRunManager(unittest.TestCase): |
| 11 | + """ |
| 12 | + Unit tests for the RunManager class. These tests verify the |
| 13 | + correct behavior of run manager. |
| 14 | + """ |
| 15 | + |
| 16 | + def setUp(self): |
| 17 | + """ |
| 18 | + Set up a temporary directory as the working directory before each test. |
| 19 | + """ |
| 20 | + self.test_dir = tempfile.mkdtemp(prefix="CodeEntropy_") |
| 21 | + self._orig_dir = os.getcwd() |
| 22 | + os.chdir(self.test_dir) |
| 23 | + |
| 24 | + def tearDown(self): |
| 25 | + """ |
| 26 | + Clean up by removing the temporary directory and restoring the original working |
| 27 | + directory. |
| 28 | + """ |
| 29 | + os.chdir(self._orig_dir) |
| 30 | + shutil.rmtree(self.test_dir) |
| 31 | + |
| 32 | + @patch("os.makedirs") |
| 33 | + @patch("os.listdir") |
| 34 | + def test_create_job_folder_empty_directory(self, mock_listdir, mock_makedirs): |
| 35 | + """ |
| 36 | + Test that 'job001' is created when the directory is initially empty. |
| 37 | + """ |
| 38 | + mock_listdir.return_value = [] |
| 39 | + new_folder_path = RunManager.create_job_folder() |
| 40 | + expected_path = os.path.join(self.test_dir, "job001") |
| 41 | + self.assertEqual(new_folder_path, expected_path) |
| 42 | + mock_makedirs.assert_called_once_with(expected_path, exist_ok=True) |
| 43 | + |
| 44 | + @patch("os.makedirs") |
| 45 | + @patch("os.listdir") |
| 46 | + def test_create_job_folder_with_existing_folders(self, mock_listdir, mock_makedirs): |
| 47 | + """ |
| 48 | + Test that the next sequential job folder (e.g., 'job004') is created when |
| 49 | + existing folders 'job001', 'job002', and 'job003' are present. |
| 50 | + """ |
| 51 | + mock_listdir.return_value = ["job001", "job002", "job003"] |
| 52 | + new_folder_path = RunManager.create_job_folder() |
| 53 | + expected_path = os.path.join(self.test_dir, "job004") |
| 54 | + self.assertEqual(new_folder_path, expected_path) |
| 55 | + mock_makedirs.assert_called_once_with(expected_path, exist_ok=True) |
| 56 | + |
| 57 | + @patch("os.makedirs") |
| 58 | + @patch("os.listdir") |
| 59 | + def test_create_job_folder_with_non_matching_folders( |
| 60 | + self, mock_listdir, mock_makedirs |
| 61 | + ): |
| 62 | + """ |
| 63 | + Test that 'job001' is created when the directory contains only non-job-related |
| 64 | + folders. |
| 65 | + """ |
| 66 | + mock_listdir.return_value = ["folderA", "another_one"] |
| 67 | + new_folder_path = RunManager.create_job_folder() |
| 68 | + expected_path = os.path.join(self.test_dir, "job001") |
| 69 | + self.assertEqual(new_folder_path, expected_path) |
| 70 | + mock_makedirs.assert_called_once_with(expected_path, exist_ok=True) |
| 71 | + |
| 72 | + @patch("os.makedirs") |
| 73 | + @patch("os.listdir") |
| 74 | + def test_create_job_folder_mixed_folder_names(self, mock_listdir, mock_makedirs): |
| 75 | + """ |
| 76 | + Test that the correct next job folder (e.g., 'job003') is created when both |
| 77 | + job and non-job folders exist in the directory. |
| 78 | + """ |
| 79 | + mock_listdir.return_value = ["job001", "abc", "job002", "random"] |
| 80 | + new_folder_path = RunManager.create_job_folder() |
| 81 | + expected_path = os.path.join(self.test_dir, "job003") |
| 82 | + self.assertEqual(new_folder_path, expected_path) |
| 83 | + mock_makedirs.assert_called_once_with(expected_path, exist_ok=True) |
| 84 | + |
| 85 | + @patch("os.makedirs") |
| 86 | + @patch("os.listdir") |
| 87 | + def test_create_job_folder_with_invalid_job_suffix( |
| 88 | + self, mock_listdir, mock_makedirs |
| 89 | + ): |
| 90 | + """ |
| 91 | + Test that invalid job folder names like 'jobABC' are ignored when determining |
| 92 | + the next job number. |
| 93 | + """ |
| 94 | + # Simulate existing folders, one of which is invalid |
| 95 | + mock_listdir.return_value = ["job001", "jobABC", "job002"] |
| 96 | + |
| 97 | + new_folder_path = RunManager.create_job_folder() |
| 98 | + expected_path = os.path.join(self.test_dir, "job003") |
| 99 | + |
| 100 | + self.assertEqual(new_folder_path, expected_path) |
| 101 | + mock_makedirs.assert_called_once_with(expected_path, exist_ok=True) |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + unittest.main() |
0 commit comments