Skip to content

Commit 5efb1cf

Browse files
committed
add try/except case to test_timestamp_filename. Also use mock datetime.
1 parent d587763 commit 5efb1cf

File tree

1 file changed

+37
-13
lines changed

1 file changed

+37
-13
lines changed

tests/test_utils.py

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import warnings
55
from contextlib import contextmanager
66
from datetime import datetime
7+
from unittest import mock
78

89
import matplotlib
910
import numpy as np
@@ -114,7 +115,7 @@ def test_get_full_version_unexpected(monkeypatch):
114115
assert get_full_version() == __version__ + ".x"
115116

116117

117-
def test_timestamp_filename():
118+
def test_timestamp_filename(caplog):
118119
with tempfile.TemporaryDirectory() as tmpdir_name:
119120
filepath = os.path.join(tmpdir_name, "test_file.name")
120121
base, ext = os.path.splitext(filepath)
@@ -123,21 +124,44 @@ def test_timestamp_filename():
123124
with open(filepath, "w") as f:
124125
f.write("Test file")
125126

126-
# Case 1: move=False should return the new file name with apended timestamp.
127-
renamed_file = timestamp_filename(filepath, move=False)
128-
timestamp = datetime.now().strftime("%y%m%d_%H%M")
129-
assert renamed_file.startswith(f"{base}_{timestamp}") and renamed_file.endswith(
130-
f"{ext}"
131-
)
127+
# Mock datetime to return a fixed timestamp.
128+
mock_datetime_value = datetime(1707, 4, 15, 12, 0, 0)
129+
mock_timestamp = mock_datetime_value.strftime("%y%m%d_%H%M%S")
130+
131+
with mock.patch("aspire.utils.misc.datetime") as mock_datetime:
132+
mock_datetime.now.return_value = mock_datetime_value
133+
mock_datetime.strftime = datetime.strftime
134+
135+
# Case 1: move=False should return the new file name with appended timestamp.
136+
renamed_file = timestamp_filename(filepath, move=False)
137+
assert renamed_file == f"{base}_{mock_timestamp}{ext}"
138+
139+
# Case 2: move=True (default) should rename file on disk.
140+
with caplog.at_level(logging.INFO):
141+
renamed_file = timestamp_filename(filepath)
142+
143+
# Check log for renaming operation.
144+
assert f"Renaming {filepath} as {renamed_file}" in caplog.text
145+
146+
# Check that the original file no longer exists.
147+
assert not os.path.exists(filepath)
148+
149+
# Check that the new file exists on disk with the expected name.
150+
assert os.path.exists(renamed_file)
132151

133-
# Case 2: move=True (default) should rename file on disk.
134-
renamed_file = timestamp_filename(filepath)
152+
# Case 3: Test when the file does not exist.
153+
non_existent_file = os.path.join(tmpdir_name, "non_existent_file.name")
154+
with caplog.at_level(logging.WARNING):
155+
result = timestamp_filename(non_existent_file)
135156

136-
# Check that the original file no longer exists.
137-
assert not os.path.exists(filepath)
157+
# Check that None is returned since the file doesn't exist.
158+
assert result is None
138159

139-
# Check that the new file exists on disk with the expected name.
140-
assert os.path.exists(renamed_file)
160+
# Check log for the warning about file not found.
161+
assert (
162+
f"File '{non_existent_file}' not found, could not rename."
163+
in caplog.text
164+
)
141165

142166

143167
def test_power_set():

0 commit comments

Comments
 (0)