Skip to content

Commit

Permalink
add test for multi-week patching
Browse files Browse the repository at this point in the history
  • Loading branch information
minhkhul committed Aug 9, 2024
1 parent e29e07e commit 0a4bfb6
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
4 changes: 3 additions & 1 deletion nssp/tests/source_dir/2021-01-02.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
week_end,geography,county,percent_visits_combined,percent_visits_covid,percent_visits_influenza,percent_visits_rsv,percent_visits_smoothed,percent_visits_smoothed_covid,percent_visits_smoothed_1,percent_visits_smoothed_rsv,ed_trends_covid,ed_trends_influenza,ed_trends_rsv,hsa,hsa_counties,hsa_nci_id,fips,trend_source
2022-10-01T00:00:00.000,United States,All,2.84,1.84,0.48,0.55,2.83,2.07,0.34,0.44,Decreasing,Increasing,Increasing,All,All,All,0,United States
2020-10-01T00:00:00.000,United States,All,2.84,1.84,0.48,0.55,2.83,2.07,0.34,0.44,Decreasing,Increasing,Increasing,All,All,All,0,United States
2020-06-29T00:00:00.000,Alabama,All,1.01,0.85,0.17,0.0,0.89,0.66,0.22,0.03,Increasing,Decreasing,No Change,All,All,All,1000,State
2020-02-25T00:00:00.000,Alabama,Blount,,,,,,,,,Data Unavailable,Data Unavailable,Data Unavailable,"Jefferson (Birmingham), AL - Shelby, AL","Bibb, Blount, Chilton, Cullman, Jefferson, Shelby, St. Clair, Walker",150,1009,HSA
4 changes: 4 additions & 0 deletions nssp/tests/source_dir/2021-01-08.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
week_end,geography,county,percent_visits_combined,percent_visits_covid,percent_visits_influenza,percent_visits_rsv,percent_visits_smoothed,percent_visits_smoothed_covid,percent_visits_smoothed_1,percent_visits_smoothed_rsv,ed_trends_covid,ed_trends_influenza,ed_trends_rsv,hsa,hsa_counties,hsa_nci_id,fips,trend_source
2020-10-01T00:00:00.000,United States,All,2.84,1.84,0.48,0.55,2.83,2.07,0.34,0.44,Decreasing,Increasing,Increasing,All,All,All,0,United States
2020-06-29T00:00:00.000,Alabama,All,1.01,0.85,0.17,0.0,0.89,0.66,0.22,0.03,Increasing,Decreasing,No Change,All,All,All,1000,State
2020-02-25T00:00:00.000,Alabama,Blount,,,,,,,,,Data Unavailable,Data Unavailable,Data Unavailable,"Jefferson (Birmingham), AL - Shelby, AL","Bibb, Blount, Chilton, Cullman, Jefferson, Shelby, St. Clair, Walker",150,1009,HSA
4 changes: 4 additions & 0 deletions nssp/tests/source_dir/2021-01-09.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
week_end,geography,county,percent_visits_combined,percent_visits_covid,percent_visits_influenza,percent_visits_rsv,percent_visits_smoothed,percent_visits_smoothed_covid,percent_visits_smoothed_1,percent_visits_smoothed_rsv,ed_trends_covid,ed_trends_influenza,ed_trends_rsv,hsa,hsa_counties,hsa_nci_id,fips,trend_source
2020-10-01T00:00:00.000,United States,All,1,1,1,1,1,1,1,1,Decreasing,Decreasing,Decreasing,All,All,All,0,United States
2020-06-29T00:00:00.000,Oklahoma,All,1.01,0.85,0.17,0.0,0.89,0.66,0.22,0.03,Increasing,Decreasing,No Change,All,All,All,1000,State
2020-02-25T00:00:00.000,Alabama,Blount,,,,,,,,,Data Unavailable,Data Unavailable,Data Unavailable,"Jefferson (Birmingham), AL - Shelby, AL","Bibb, Blount, Chilton, Cullman, Jefferson, Shelby, St. Clair, Walker",150,1009,HSA
4 changes: 4 additions & 0 deletions nssp/tests/source_dir/2021-01-12.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
week_end,geography,county,percent_visits_combined,percent_visits_covid,percent_visits_influenza,percent_visits_rsv,percent_visits_smoothed,percent_visits_smoothed_covid,percent_visits_smoothed_1,percent_visits_smoothed_rsv,ed_trends_covid,ed_trends_influenza,ed_trends_rsv,hsa,hsa_counties,hsa_nci_id,fips,trend_source
2020-10-01T00:00:00.000,United States,All,1,1,1,1,1,1,1,1,Decreasing,Decreasing,Decreasing,All,All,All,0,United States
2020-06-29T00:00:00.000,Oklahoma,All,1.01,0.85,0.17,0.0,0.89,0.66,0.22,0.03,Increasing,Decreasing,No Change,All,All,All,1000,State
2020-02-25T00:00:00.000,Alabama,Blount,,,,,,,,,Data Unavailable,Data Unavailable,Data Unavailable,"Jefferson (Birmingham), AL - Shelby, AL","Bibb, Blount, Chilton, Cullman, Jefferson, Shelby, St. Clair, Walker",150,1009,HSA
51 changes: 51 additions & 0 deletions nssp/tests/test_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import shutil
from datetime import datetime, timedelta
import pandas as pd

class TestPatchModule:
@mock_patch('logging.Logger')
Expand Down Expand Up @@ -124,5 +125,55 @@ def test_patch(self, mock_read_params, mock_get_structured_logger, mock_run_modu
assert not os.path.isdir(f'./patch_dir/issue_{date_str}/nssp')
date += timedelta(days=1)

# Clean up the created directories after the test
shutil.rmtree(mock_read_params.return_value["patch"]["patch_dir"])

# @mock_patch('delphi_nssp.patch.run_module')
@mock_patch('delphi_nssp.patch.get_structured_logger')
@mock_patch('delphi_nssp.patch.read_params')
def test_patch(self, mock_read_params, mock_get_structured_logger):
# def test_patch(self, mock_read_params, mock_get_structured_logger, mock_run_module):
mock_read_params.return_value = {
"common": {
"log_filename": "test.log",
"custom_run": True
},
"indicator": {
"socrata_token": "test_token"
},
"patch": {
"start_issue": "2021-01-01",
"end_issue": "2021-01-16",
"patch_dir": "./patch_dir",
"source_dir": "./source_dir"
}
}

patch()

start_date = datetime(2021, 1, 1)
end_date = datetime(2021, 1, 16)
date = start_date

# Only Sundays should be issue dirs.
while date <= end_date:
date_str = date.strftime("%Y%m%d")
if date.weekday() == 6:
assert os.path.isdir(f'./patch_dir/issue_{date_str}/nssp')
else:
assert not os.path.isdir(f'./patch_dir/issue_{date_str}/nssp')
date += timedelta(days=1)

# Make sure issue_20210103 has latest weekly data (data from 20210109 instead of 20210108)
df_20210108 = pd.read_csv('source_dir/2021-01-08.csv')
df_20210108_nation_combined = df_20210108['percent_visits_combined'].iloc[0]
df_20210109 = pd.read_csv('source_dir/2021-01-09.csv')
df_20210109_nation_combined = df_20210109['percent_visits_combined'].iloc[0]
assert df_20210108_nation_combined != df_20210109_nation_combined

df_issue_20210103 = pd.read_csv('patch_dir/issue_20210103/nssp/weekly_202040_nation_pct_ed_visits_combined.csv')
df_issue_20210103_nation_combined = df_issue_20210103['val'].iloc[0]
assert df_20210109_nation_combined == df_issue_20210103_nation_combined

# Clean up the created directories after the test
shutil.rmtree(mock_read_params.return_value["patch"]["patch_dir"])

0 comments on commit 0a4bfb6

Please sign in to comment.