1
1
import glob
2
- from datetime import datetime , date
3
- import json
2
+ import logging
3
+ import os
4
4
from pathlib import Path
5
+ import json
5
6
from unittest .mock import patch
6
- import tempfile
7
- import os
8
- import time
9
- from datetime import datetime
10
-
11
7
import numpy as np
12
8
import pandas as pd
9
+ from delphi_nssp .constants import GEOS , SIGNALS_MAP , DATASET_ID
10
+ from delphi_nssp .run import add_needed_columns , run_module
13
11
from epiweeks import Week
14
- from pandas .testing import assert_frame_equal
15
- from delphi_nssp .constants import GEOS , SIGNALS , SIGNALS_MAP , DATASET_ID
16
- from delphi_nssp .run import (
17
- add_needed_columns
18
- )
19
12
13
+ TEST_DIR = Path (__file__ ).parent
14
+
15
+ def remove_backup_and_receiving (params ):
16
+ export_dir = params ["common" ]["export_dir" ]
17
+ for file in Path (export_dir ).glob ("*.csv" ):
18
+ os .remove (file )
19
+
20
+ today = pd .Timestamp .today ().strftime ("%Y%m%d" )
21
+ backup_dir = glob .glob (f"{ Path (params ['common' ]['backup_dir' ])} /{ today } *" )
22
+ for file in backup_dir :
23
+ os .remove (file )
20
24
21
25
class TestRun :
22
26
def test_add_needed_columns (self ):
@@ -42,7 +46,7 @@ def generate_week_file_prefix(self, dates):
42
46
]
43
47
return date_prefix
44
48
45
- def test_output_files_exist (self , params , run_as_module ):
49
+ def test_output_files (self , params , run_as_module ):
46
50
export_dir = params ["common" ]["export_dir" ]
47
51
csv_files = [f .name for f in Path (export_dir ).glob ("*.csv" )]
48
52
@@ -68,13 +72,10 @@ def test_output_files_exist(self, params, run_as_module):
68
72
]
69
73
assert set (expected_columns ).issubset (set (df .columns .values ))
70
74
71
- for file in Path ( export_dir ). glob ( "*.csv" ):
72
- os . remove ( file )
75
+ # Verify that there's no NA/empty values in the val columns
76
+ assert not df [ "val" ]. isnull (). any ( )
73
77
74
- today = pd .Timestamp .today ().strftime ("%Y%m%d" )
75
- backup_dir = glob .glob (f"{ Path (params ['common' ]['backup_dir' ])} /{ today } *" )
76
- for file in backup_dir :
77
- os .remove (file )
78
+ remove_backup_and_receiving (params )
78
79
79
80
def test_valid_hrr (self , run_as_module_hrr , params ):
80
81
export_dir = params ["common" ]["export_dir" ]
@@ -85,10 +86,28 @@ def test_valid_hrr(self, run_as_module_hrr, params):
85
86
df = pd .read_csv (f )
86
87
assert (df .val == 100 ).all ()
87
88
88
- for file in Path (export_dir ).glob ("*.csv" ):
89
- os .remove (file )
89
+ remove_backup_and_receiving (params )
90
+
91
+ @patch ("sodapy.Socrata.get" )
92
+ def test_empty_data (self , mock_get , params , caplog ):
93
+ """
94
+ Tests correct handling when there is a geo and signal combination that has no data.
95
+ """
96
+
97
+ with open (f"{ TEST_DIR } /test_data/page_no_data.json" , "r" ) as f :
98
+ EMPTY_TEST_DATA = json .load (f )
99
+ mock_get .side_effect = [EMPTY_TEST_DATA , []]
100
+ run_module (params )
101
+
102
+ assert "No data for signal and geo combination" in caplog .text
103
+
104
+ export_dir = params ["common" ]["export_dir" ]
105
+ csv_files = [f for f in Path (export_dir ).glob ("*.csv" )]
106
+
107
+ # Since only one national entry in page_no_data.json with numeric data,
108
+ # while the two counties have no numeric fields,
109
+ # there should be no county, hrr, hhs, or msa files.
110
+ assert not any (geo in f .name for geo in ["county" , "hrr" , "hhs" , "msa" ] for f in csv_files )
111
+ assert all ("nation" in f .name for f in csv_files )
90
112
91
- today = pd .Timestamp .today ().strftime ("%Y%m%d" )
92
- backup_dir = glob .glob (f"{ Path (params ['common' ]['backup_dir' ])} /{ today } *" )
93
- for file in backup_dir :
94
- os .remove (file )
113
+ remove_backup_and_receiving (params )
0 commit comments