Skip to content

Commit f162f55

Browse files
committed
Additional test cases for run.py:
- Additional tests to cover the creation of the folders - Test to ensure a job folder is created when the directory is empty - Test to ensure the correct job folder is created if there are existing folders - Test to ensure job folder is created correctly when non job folders are present - Test to ensure job folder is created with mixed job folders and other folders - Test to ensure job folders with the wrong suffix are ignored
1 parent 155d68b commit f162f55

File tree

2 files changed

+132
-23
lines changed

2 files changed

+132
-23
lines changed

CodeEntropy/run.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import logging
22
import os
33
import pickle
4-
import re
54

65
import MDAnalysis as mda
76
from MDAnalysis.analysis.base import AnalysisFromFunction
@@ -52,33 +51,38 @@ def create_job_folder():
5251
folders.
5352
"""
5453
# Get the current working directory
55-
base_dir = os.getcwd()
56-
57-
# List all folders in the base directory
58-
existing_folders = [
59-
f for f in os.listdir(base_dir) if os.path.isdir(os.path.join(base_dir, f))
60-
]
61-
62-
# Filter folders that match the pattern 'jobXXX'
63-
job_folders = [f for f in existing_folders if re.match(r"job\d{3}", f)]
64-
65-
# Determine the next job number
66-
if job_folders:
67-
max_job_number = max(
68-
[int(re.search(r"\d{3}", f).group()) for f in job_folders]
69-
)
70-
next_job_number = max_job_number + 1
71-
else:
54+
current_dir = os.getcwd()
55+
56+
# Get a list of existing folders that start with "job"
57+
existing_folders = [f for f in os.listdir(current_dir) if f.startswith("job")]
58+
59+
# Extract numbers from existing folder names
60+
job_numbers = []
61+
for folder in existing_folders:
62+
try:
63+
# Assuming folder names are in the format "jobXXX"
64+
job_number = int(folder[3:]) # Get the number part after "job"
65+
job_numbers.append(job_number)
66+
except ValueError:
67+
continue # Ignore any folder names that don't follow the pattern
68+
69+
# If no folders exist, start with job001
70+
if not job_numbers:
7271
next_job_number = 1
72+
else:
73+
next_job_number = max(job_numbers) + 1
7374

74-
# Format the new job folder name
75+
# Create the new job folder name
7576
new_job_folder = f"job{next_job_number:03d}"
76-
new_job_folder_path = os.path.join(base_dir, new_job_folder)
7777

78-
# Create the new job folder
79-
os.makedirs(new_job_folder_path, exist_ok=True)
78+
# Create the full path to the new folder
79+
new_folder_path = os.path.join(current_dir, new_job_folder)
80+
81+
# Create the directory
82+
os.makedirs(new_folder_path, exist_ok=True)
8083

81-
return new_job_folder_path
84+
# Return the path of the newly created folder
85+
return new_folder_path
8286

8387
def run_entropy_workflow(self):
8488
"""

tests/test_CodeEntropy/test_run.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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

Comments
 (0)