Skip to content

Commit

Permalink
[PRMP-1063] blank bulk upload reports don't create a file
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyFlintNHS committed Oct 28, 2024
1 parent 22d9b68 commit aff278e
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 45 deletions.
105 changes: 60 additions & 45 deletions lambdas/services/bulk_upload_report_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,19 @@ def generate_success_report(self, ods_reports: list[OdsReport]):
[str(patient[0]), str(report.uploader_ods_code), str(patient[1])]
)

self.write_additional_report_items_to_csv(
file_name=file_name, headers=headers, rows_to_write=data_rows
)
if data_rows:
self.write_additional_report_items_to_csv(
file_name=file_name, headers=headers, rows_to_write=data_rows
)

logger.info("Uploading daily success report file to S3")
self.s3_service.upload_file(
s3_bucket_name=self.reports_bucket,
file_key=f"{self.s3_key_prefix}/{file_name}",
file_name=f"/tmp/{file_name}",
)
logger.info("Uploading daily success report file to S3")
self.s3_service.upload_file(
s3_bucket_name=self.reports_bucket,
file_key=f"{self.s3_key_prefix}/{file_name}",
file_name=f"/tmp/{file_name}",
)
else:
logger.info("No data to report for daily success report file")

def generate_suspended_report(self, ods_reports: list[OdsReport]):
file_name = (
Expand All @@ -186,16 +189,19 @@ def generate_suspended_report(self, ods_reports: list[OdsReport]):
[str(patient[0]), str(report.uploader_ods_code), str(patient[1])]
)

self.write_additional_report_items_to_csv(
file_name=file_name, headers=headers, rows_to_write=data_rows
)
if data_rows:
self.write_additional_report_items_to_csv(
file_name=file_name, headers=headers, rows_to_write=data_rows
)

logger.info("Uploading daily suspended report file to S3")
self.s3_service.upload_file(
s3_bucket_name=self.reports_bucket,
file_key=f"{self.s3_key_prefix}/{file_name}",
file_name=f"/tmp/{file_name}",
)
logger.info("Uploading daily suspended report file to S3")
self.s3_service.upload_file(
s3_bucket_name=self.reports_bucket,
file_key=f"{self.s3_key_prefix}/{file_name}",
file_name=f"/tmp/{file_name}",
)
else:
logger.info("No data to report for daily suspended report file")

def generate_deceased_report(self, ods_reports: list[OdsReport]):
file_name = (
Expand All @@ -220,16 +226,19 @@ def generate_deceased_report(self, ods_reports: list[OdsReport]):
]
)

self.write_additional_report_items_to_csv(
file_name=file_name, headers=headers, rows_to_write=data_rows
)
if data_rows:
self.write_additional_report_items_to_csv(
file_name=file_name, headers=headers, rows_to_write=data_rows
)

logger.info("Uploading daily deceased report file to S3")
self.s3_service.upload_file(
s3_bucket_name=self.reports_bucket,
file_key=f"{self.s3_key_prefix}/{file_name}",
file_name=f"/tmp/{file_name}",
)
logger.info("Uploading daily deceased report file to S3")
self.s3_service.upload_file(
s3_bucket_name=self.reports_bucket,
file_key=f"{self.s3_key_prefix}/{file_name}",
file_name=f"/tmp/{file_name}",
)
else:
logger.info("No data to report for daily deceased report file")

def generate_restricted_report(self, ods_reports: list[OdsReport]):
file_name = (
Expand All @@ -248,16 +257,19 @@ def generate_restricted_report(self, ods_reports: list[OdsReport]):
[str(patient[0]), str(report.uploader_ods_code), str(patient[1])]
)

self.write_additional_report_items_to_csv(
file_name=file_name, headers=headers, rows_to_write=data_rows
)
if data_rows:
self.write_additional_report_items_to_csv(
file_name=file_name, headers=headers, rows_to_write=data_rows
)

logger.info("Uploading daily restricted report file to S3")
self.s3_service.upload_file(
s3_bucket_name=self.reports_bucket,
file_key=f"{self.s3_key_prefix}/{file_name}",
file_name=f"/tmp/{file_name}",
)
logger.info("Uploading daily restricted report file to S3")
self.s3_service.upload_file(
s3_bucket_name=self.reports_bucket,
file_key=f"{self.s3_key_prefix}/{file_name}",
file_name=f"/tmp/{file_name}",
)
else:
logger.info("No data to report for daily deceased report file")

def generate_rejected_report(self, ods_reports: list[OdsReport]):
file_name = (
Expand All @@ -283,16 +295,19 @@ def generate_rejected_report(self, ods_reports: list[OdsReport]):
]
)

self.write_additional_report_items_to_csv(
file_name=file_name, headers=headers, rows_to_write=data_rows
)
if data_rows:
self.write_additional_report_items_to_csv(
file_name=file_name, headers=headers, rows_to_write=data_rows
)

logger.info("Uploading daily rejected report file to S3")
self.s3_service.upload_file(
s3_bucket_name=self.reports_bucket,
file_key=f"{self.s3_key_prefix}/{file_name}",
file_name=f"/tmp/{file_name}",
)
logger.info("Uploading daily rejected report file to S3")
self.s3_service.upload_file(
s3_bucket_name=self.reports_bucket,
file_key=f"{self.s3_key_prefix}/{file_name}",
file_name=f"/tmp/{file_name}",
)
else:
logger.info("No data to report for daily rejected report file")

@staticmethod
def write_items_to_csv(items: list[BulkUploadReport], csv_file_path: str):
Expand Down
90 changes: 90 additions & 0 deletions lambdas/tests/unit/services/test_bulk_upload_report_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,24 @@ def test_generate_success_report_writes_csv(
)


def test_generate_success_report_does_not_write_when_no_data(
bulk_upload_report_service, mock_get_times_for_scan
):
# just used to assert this isn't created
mock_file_name = (
f"daily_statistical_report_bulk_upload_success_{MOCK_TIMESTAMP}.csv"
)

blank_ods_reports = bulk_upload_report_service.generate_ods_reports([])

bulk_upload_report_service.generate_success_report(blank_ods_reports)

with pytest.raises(FileNotFoundError):
open(f"/tmp/{mock_file_name}")

bulk_upload_report_service.s3_service.upload_file.assert_not_called()


def test_generate_suspended_report_writes_csv(
bulk_upload_report_service, mock_get_times_for_scan
):
Expand All @@ -485,6 +503,24 @@ def test_generate_suspended_report_writes_csv(
)


def test_generate_suspended_report_does_not_write_when_no_data(
bulk_upload_report_service, mock_get_times_for_scan
):
# just used to assert this isn't created
mock_file_name = (
f"daily_statistical_report_bulk_upload_suspended_{MOCK_TIMESTAMP}.csv"
)

blank_ods_reports = bulk_upload_report_service.generate_ods_reports([])

bulk_upload_report_service.generate_suspended_report(blank_ods_reports)

with pytest.raises(FileNotFoundError):
open(f"/tmp/{mock_file_name}")

bulk_upload_report_service.s3_service.upload_file.assert_not_called()


def test_generate_deceased_report_writes_csv(
bulk_upload_report_service, mock_get_times_for_scan
):
Expand All @@ -511,6 +547,24 @@ def test_generate_deceased_report_writes_csv(
)


def test_generate_deceased_report_does_not_write_when_no_data(
bulk_upload_report_service, mock_get_times_for_scan
):
# just used to assert this isn't created
mock_file_name = (
f"daily_statistical_report_bulk_upload_deceased_{MOCK_TIMESTAMP}.csv"
)

blank_ods_reports = bulk_upload_report_service.generate_ods_reports([])

bulk_upload_report_service.generate_deceased_report(blank_ods_reports)

with pytest.raises(FileNotFoundError):
open(f"/tmp/{mock_file_name}")

bulk_upload_report_service.s3_service.upload_file.assert_not_called()


def test_generate_restricted_report_writes_csv(
bulk_upload_report_service, mock_get_times_for_scan
):
Expand All @@ -537,6 +591,24 @@ def test_generate_restricted_report_writes_csv(
)


def test_generate_restricted_report_does_not_write_when_no_data(
bulk_upload_report_service, mock_get_times_for_scan
):
# just used to assert this isn't created
mock_file_name = (
f"daily_statistical_report_bulk_upload_restricted_{MOCK_TIMESTAMP}.csv"
)

blank_ods_reports = bulk_upload_report_service.generate_ods_reports([])

bulk_upload_report_service.generate_restricted_report(blank_ods_reports)

with pytest.raises(FileNotFoundError):
open(f"/tmp/{mock_file_name}")

bulk_upload_report_service.s3_service.upload_file.assert_not_called()


def test_generate_rejected_report_writes_csv(
bulk_upload_report_service, mock_get_times_for_scan
):
Expand All @@ -561,3 +633,21 @@ def test_generate_rejected_report_writes_csv(
file_key=f"bulk-upload-reports/2012-01-13/{mock_file_name}",
file_name=f"/tmp/{mock_file_name}",
)


def test_generate_rejected_report_does_not_write_when_no_data(
bulk_upload_report_service, mock_get_times_for_scan
):
# just used to assert this isn't created
mock_file_name = (
f"daily_statistical_report_bulk_upload_rejected_{MOCK_TIMESTAMP}.csv"
)

blank_ods_reports = bulk_upload_report_service.generate_ods_reports([])

bulk_upload_report_service.generate_rejected_report(blank_ods_reports)

with pytest.raises(FileNotFoundError):
open(f"/tmp/{mock_file_name}")

bulk_upload_report_service.s3_service.upload_file.assert_not_called()

0 comments on commit aff278e

Please sign in to comment.