Skip to content

Commit 870024e

Browse files
committed
adding tests for create_backup_csv
1 parent 1071cb3 commit 870024e

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

_delphi_utils_python/delphi_utils/export.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,16 @@ def create_backup_csv(
191191
backup_filename = [issue, geo_res, metric, sensor]
192192
backup_filename = "_".join(filter(None, backup_filename)) + ".csv.gz"
193193
backup_file = join(backup_dir, backup_filename)
194-
195-
with gzip.open(backup_file, "wt", newline="") as f:
196-
df.to_csv(f, index=False, na_rep="NA")
197-
198-
if logger:
199-
logger.info(
200-
"Backup file created",
201-
backup_file=backup_file,
202-
backup_size=getsize(backup_file),
203-
)
194+
try:
195+
with gzip.open(backup_file, "wt", newline="") as f:
196+
df.to_csv(f, index=False, na_rep="NA")
197+
198+
if logger:
199+
logger.info(
200+
"Backup file created",
201+
backup_file=backup_file,
202+
backup_size=getsize(backup_file),
203+
)
204+
#pylint: disable=W0703
205+
except Exception as e:
206+
logger.info("Backup file creation failed", msg=e)

_delphi_utils_python/tests/test_export.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
"""Tests for exporting CSV files."""
22
from datetime import datetime
3+
import logging
34
from os import listdir
45
from os.path import join
5-
from typing import Any, Dict, List
6+
from typing import Any, Dict
67

78
import mock
89
import numpy as np
910
import pandas as pd
1011
from pandas.testing import assert_frame_equal
1112

12-
from delphi_utils import create_export_csv, Nans
13+
from delphi_utils import create_export_csv, Nans, create_backup_csv, get_structured_logger
1314

1415

1516
def _set_df_dtypes(df: pd.DataFrame, dtypes: Dict[str, Any]) -> pd.DataFrame:
@@ -386,3 +387,18 @@ def test_export_sort(self, tmp_path):
386387
})
387388
sorted_csv = _set_df_dtypes(pd.read_csv(join(tmp_path, "20200215_county_test.csv")), dtypes={"geo_id": str})
388389
assert_frame_equal(sorted_csv,expected_df)
390+
391+
def test_create_backup_regular(self, caplog, tmp_path):
392+
caplog.set_level(logging.INFO)
393+
logger = get_structured_logger()
394+
today = datetime.strftime(datetime.today(), "%Y%m%d")
395+
dtypes = self.DF.dtypes.to_dict()
396+
del dtypes["timestamp"]
397+
geo_res = "county"
398+
metric = "test"
399+
sensor = "deaths"
400+
create_backup_csv(df=self.DF, backup_dir=tmp_path, custom_run=False, issue=None, geo_res=geo_res, metric=metric, sensor=sensor, logger=logger)
401+
assert "Backup file created" in caplog.text
402+
403+
actual = pd.read_csv(join(tmp_path, f"{today}_{geo_res}_{metric}_{sensor}.csv.gz"), dtype=dtypes, parse_dates=["timestamp"])
404+
assert self.DF.equals(actual)

0 commit comments

Comments
 (0)