From 0a4bfb66c09da75cc68135a85bf7cafe293f3d13 Mon Sep 17 00:00:00 2001 From: minhkhul Date: Thu, 8 Aug 2024 23:45:36 -0400 Subject: [PATCH] add test for multi-week patching --- nssp/tests/source_dir/2021-01-02.csv | 4 ++- nssp/tests/source_dir/2021-01-08.csv | 4 +++ nssp/tests/source_dir/2021-01-09.csv | 4 +++ nssp/tests/source_dir/2021-01-12.csv | 4 +++ nssp/tests/test_patch.py | 51 ++++++++++++++++++++++++++++ 5 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 nssp/tests/source_dir/2021-01-08.csv create mode 100644 nssp/tests/source_dir/2021-01-09.csv create mode 100644 nssp/tests/source_dir/2021-01-12.csv diff --git a/nssp/tests/source_dir/2021-01-02.csv b/nssp/tests/source_dir/2021-01-02.csv index b783f0162..7ae1b3340 100644 --- a/nssp/tests/source_dir/2021-01-02.csv +++ b/nssp/tests/source_dir/2021-01-02.csv @@ -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 \ No newline at end of file +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 diff --git a/nssp/tests/source_dir/2021-01-08.csv b/nssp/tests/source_dir/2021-01-08.csv new file mode 100644 index 000000000..7ae1b3340 --- /dev/null +++ b/nssp/tests/source_dir/2021-01-08.csv @@ -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 diff --git a/nssp/tests/source_dir/2021-01-09.csv b/nssp/tests/source_dir/2021-01-09.csv new file mode 100644 index 000000000..5c8012ac0 --- /dev/null +++ b/nssp/tests/source_dir/2021-01-09.csv @@ -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 diff --git a/nssp/tests/source_dir/2021-01-12.csv b/nssp/tests/source_dir/2021-01-12.csv new file mode 100644 index 000000000..5c8012ac0 --- /dev/null +++ b/nssp/tests/source_dir/2021-01-12.csv @@ -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 diff --git a/nssp/tests/test_patch.py b/nssp/tests/test_patch.py index a5a4a6bf8..9397255e6 100644 --- a/nssp/tests/test_patch.py +++ b/nssp/tests/test_patch.py @@ -4,6 +4,7 @@ import os import shutil from datetime import datetime, timedelta +import pandas as pd class TestPatchModule: @mock_patch('logging.Logger') @@ -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"]) \ No newline at end of file