66import pandas as pd
77from delphi_utils import create_export_csv
88
9+ def _clean_directory (directory ):
10+ """Clean files out of a directory."""
11+ for fname in listdir (directory ):
12+ if fname .startswith ("." ):
13+ continue
14+ remove (join (directory , fname ))
15+
16+
17+ def _non_ignored_files_set (directory ):
18+ """List all files in a directory not preceded by a '.' and store them in a set."""
19+ out = set ()
20+ for fname in listdir (directory ):
21+ if fname .startswith ("." ):
22+ continue
23+ out .add (fname )
24+ return out
25+
926
1027class TestExport :
28+ """Tests for exporting CSVs."""
1129 # List of times for data points.
1230 TIMES = [
1331 datetime .strptime (x , "%Y-%m-%d" )
1432 for x in ["2020-02-15" , "2020-02-15" , "2020-03-01" , "2020-03-15" ]
1533 ]
1634
17- # A sample data frame
35+ # A sample data frame.
1836 DF = pd .DataFrame (
1937 {
2038 "geo_id" : ["51093" , "51175" , "51175" , "51620" ],
@@ -25,23 +43,25 @@ class TestExport:
2543 }
2644 )
2745
46+ # Directory in which to store tests.
47+ TEST_DIR = "test_dir"
48+
2849 def test_export_with_metric (self ):
2950 """Test that exporting CSVs with the `metrics` argument yields the correct files."""
3051
3152 # Clean receiving directory
32- for fname in listdir ("test_dir" ):
33- remove (join ("test_dir" , fname ))
53+ _clean_directory (self .TEST_DIR )
3454
3555 create_export_csv (
3656 df = self .DF ,
3757 start_date = datetime .strptime ("2020-02-15" , "%Y-%m-%d" ),
38- export_dir = "test_dir" ,
58+ export_dir = self . TEST_DIR ,
3959 metric = "deaths" ,
4060 geo_res = "county" ,
4161 sensor = "test" ,
4262 )
4363
44- assert set ( listdir ( "test_dir" ) ) == set (
64+ assert _non_ignored_files_set ( self . TEST_DIR ) == set (
4565 [
4666 "20200215_county_deaths_test.csv" ,
4767 "20200301_county_deaths_test.csv" ,
@@ -53,18 +73,17 @@ def test_export_without_metric(self):
5373 """Test that exporting CSVs without the `metrics` argument yields the correct files."""
5474
5575 # Clean receiving directory
56- for fname in listdir ("test_dir" ):
57- remove (join ("test_dir" , fname ))
76+ _clean_directory (self .TEST_DIR )
5877
5978 create_export_csv (
6079 df = self .DF ,
6180 start_date = datetime .strptime ("2020-02-15" , "%Y-%m-%d" ),
62- export_dir = "test_dir" ,
81+ export_dir = self . TEST_DIR ,
6382 geo_res = "county" ,
6483 sensor = "test" ,
6584 )
6685
67- assert set ( listdir ( "test_dir" ) ) == set (
86+ assert _non_ignored_files_set ( self . TEST_DIR ) == set (
6887 [
6988 "20200215_county_test.csv" ,
7089 "20200301_county_test.csv" ,
@@ -76,18 +95,17 @@ def test_export_with_limiting_start_date(self):
7695 """Test that the `start_date` prevents earlier dates from being exported."""
7796
7897 # Clean receiving directory
79- for fname in listdir ("test_dir" ):
80- remove (join ("test_dir" , fname ))
98+ _clean_directory (self .TEST_DIR )
8199
82100 create_export_csv (
83101 df = self .DF ,
84102 start_date = datetime .strptime ("2020-02-20" , "%Y-%m-%d" ),
85- export_dir = "test_dir" ,
103+ export_dir = self . TEST_DIR ,
86104 geo_res = "county" ,
87105 sensor = "test" ,
88106 )
89107
90- assert set ( listdir ( "test_dir" ) ) == set (
108+ assert _non_ignored_files_set ( self . TEST_DIR ) == set (
91109 [
92110 "20200301_county_test.csv" ,
93111 "20200315_county_test.csv" ,
@@ -98,17 +116,16 @@ def test_export_with_no_start_date(self):
98116 """Test that omitting the `start_date` exports all dates."""
99117
100118 # Clean receiving directory
101- for fname in listdir ("test_dir" ):
102- remove (join ("test_dir" , fname ))
119+ _clean_directory (self .TEST_DIR )
103120
104121 create_export_csv (
105122 df = self .DF ,
106- export_dir = "test_dir" ,
123+ export_dir = self . TEST_DIR ,
107124 geo_res = "state" ,
108125 sensor = "test" ,
109126 )
110127
111- assert set ( listdir ( "test_dir" ) ) == set (
128+ assert _non_ignored_files_set ( self . TEST_DIR ) == set (
112129 [
113130 "20200215_state_test.csv" ,
114131 "20200301_state_test.csv" ,
0 commit comments