Skip to content

Commit

Permalink
fix: fix the lack of trailing zero in modify_file_path and update t…
Browse files Browse the repository at this point in the history
…ests
  • Loading branch information
furkan-bilgin committed Dec 21, 2024
1 parent e565c6e commit 6c132e8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
9 changes: 6 additions & 3 deletions src/enrgdaq/utils/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from datetime import datetime


def modify_file_path(file_path: str, add_date: bool, tag: str | None) -> str:
def modify_file_path(
file_path: str, add_date: bool, tag: str | None, date: datetime | None = None
) -> str:
"""
Modifies a file path by adding a date and/or a tag.
Expand All @@ -20,9 +22,10 @@ def modify_file_path(file_path: str, add_date: bool, tag: str | None) -> str:
"""

if add_date:
now = datetime.now()
if date is None:
date = datetime.now()
sep = os.path.sep
file_dir = f"{now.year}{sep}{now.month}{sep}{now.day}"
file_dir = f"{date.year}{sep}{date.month:02d}{sep}{date.day:02d}"
file_path = os.path.join(file_dir, file_path)
if tag is not None:
head, tail = os.path.splitext(file_path)
Expand Down
15 changes: 10 additions & 5 deletions src/tests/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ def test_modify_file_path(self):
file_path = "test.csv"
add_date = True
tag = "tag"
sep = os.path.sep
expected_file_path = f"{datetime.now().year}{sep}{datetime.now().month}{sep}{datetime.now().day}{sep}test_tag.csv"
actual_file_path = modify_file_path(file_path, add_date, tag)

self.assertEqual(expected_file_path, actual_file_path)
now = datetime.now()
test_dates = [
(datetime(2023, 10, 20), "2023/10/20/test_tag.csv"),
(datetime(2023, 1, 2), "2023/01/02/test_tag.csv"),
(None, f"{now.year}/{now.month:02d}/{now.day:02d}/test_tag.csv"),
]
for date, expected_file_path in test_dates:
expected_file_path = expected_file_path.replace("/", os.path.sep)
actual_file_path = modify_file_path(file_path, add_date, tag, date)
self.assertEqual(expected_file_path, actual_file_path)

@patch("enrgdaq.daq.jobs.store.csv.modify_file_path", return_value="test.csv")
@patch("builtins.open", new_callable=mock_open)
Expand Down

0 comments on commit 6c132e8

Please sign in to comment.