Skip to content

Commit

Permalink
Refine generate_filename method and test
Browse files Browse the repository at this point in the history
  • Loading branch information
akuny committed Jan 16, 2024
1 parent 171cba1 commit 490f0f8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
11 changes: 9 additions & 2 deletions nad_ch/domain/entities.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import datetime
import os
import re


class Entity:
Expand Down Expand Up @@ -41,7 +43,12 @@ def __repr__(self):
(created: {self.created_at}; updated: {self.updated_at})"

@staticmethod
def generate_filename(provider: DataProvider) -> str:
def generate_filename(file_path: str, provider: DataProvider) -> str:
s = re.sub(r'\W+', '_', provider.name)
s = s.lower()
s = s.strip('_')
formatted_provider_name = re.sub(r'_+', '_', s)
datetime_str = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"{provider.name}_{datetime_str}.zip"
_, file_extension = os.path.splitext(file_path)
filename = f"{formatted_provider_name}_{datetime_str}{file_extension}"
return filename
2 changes: 1 addition & 1 deletion nad_ch/use_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def ingest_data_submission(
return

try:
filename = DataSubmission.generate_filename(provider)
filename = DataSubmission.generate_filename(file_path, provider)
ctx.storage.upload(file_path, filename)

submission = DataSubmission(filename, provider)
Expand Down
12 changes: 12 additions & 0 deletions tests/domain/test_entities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import datetime
from nad_ch.domain.entities import DataProvider, DataSubmission


def test_data_submission_generates_filename():
provider = DataProvider("Some Provider")
filename = DataSubmission.generate_filename("someupload.zip", provider)
todays_date = datetime.datetime.now().strftime("%Y%m%d")
print(filename)
assert filename.startswith("some_provider")
assert todays_date in filename
assert filename.endswith(".zip")

0 comments on commit 490f0f8

Please sign in to comment.