@@ -45,150 +45,160 @@ def setUp(self):
45
45
cur .execute ("truncate table covid_hosp_meta" )
46
46
cur .execute ("truncate table api_user" )
47
47
cur .execute ('insert into api_user(api_key, tracking, registered) values ("key", 1, 1)' )
48
-
49
-
50
- # make sure the data does not yet exist
51
- with self .subTest (name = 'no data yet' ):
52
- response = Epidata .covid_hosp_facility (
53
- '450822' , Epidata .range (20200101 , 20210101 ))
54
- self .assertEqual (response ['result' ], - 2 , response )
55
-
56
- # acquire sample data into local database
57
- with self .subTest (name = 'first acquisition' ):
58
- acquired = Update .run (network = mock_network )
59
- self .assertTrue (acquired )
60
-
61
- # make sure the data now exists
62
- with self .subTest (name = 'initial data checks' ):
63
- expected_spotchecks = {
64
- "hospital_pk" : "450822" ,
65
- "collection_week" : 20201030 ,
66
- "publication_date" : 20210315 ,
67
- "previous_day_total_ed_visits_7_day_sum" : 536 ,
68
- "total_personnel_covid_vaccinated_doses_all_7_day_sum" : 18 ,
69
- "total_beds_7_day_avg" : 69.3 ,
70
- "previous_day_admission_influenza_confirmed_7_day_sum" : - 999999
71
- }
72
- response = Epidata .covid_hosp_facility (
73
- '450822' , Epidata .range (20200101 , 20210101 ))
74
- self .assertEqual (response ['result' ], 1 )
75
- self .assertEqual (len (response ['epidata' ]), 2 )
76
- row = response ['epidata' ][0 ]
77
- for k ,v in expected_spotchecks .items ():
78
- self .assertTrue (
79
- k in row ,
80
- f"no '{ k } ' in row:\n { NEWLINE .join (sorted (row .keys ()))} "
81
- )
82
- if isinstance (v , float ):
83
- self .assertAlmostEqual (row [k ], v , f"row[{ k } ] is { row [k ]} not { v } " )
84
- else :
85
- self .assertEqual (row [k ], v , f"row[{ k } ] is { row [k ]} not { v } " )
86
-
87
- # expect 113 fields per row (114 database columns, except `id`)
88
- self .assertEqual (len (row ), 113 )
89
-
90
- # re-acquisition of the same dataset should be a no-op
91
- with self .subTest (name = 'second acquisition' ):
92
- acquired = Update .run (network = mock_network )
93
- self .assertFalse (acquired )
94
-
95
- # make sure the data still exists
96
- with self .subTest (name = 'final data checks' ):
97
- response = Epidata .covid_hosp_facility (
98
- '450822' , Epidata .range (20200101 , 20210101 ))
99
- self .assertEqual (response ['result' ], 1 )
100
- self .assertEqual (len (response ['epidata' ]), 2 )
101
-
102
- @freeze_time ("2021-03-17" )
103
- def test_facility_lookup (self ):
104
- """Lookup facilities using various filters."""
105
-
106
- # only mock out network calls to external hosts
107
- mock_network = MagicMock ()
108
- mock_network .fetch_metadata .return_value = \
109
- self .test_utils .load_sample_metadata ()
110
- mock_network .fetch_dataset .return_value = \
111
- self .test_utils .load_sample_dataset ()
112
-
113
- # acquire sample data into local database
114
- with self .subTest (name = 'first acquisition' ):
115
- acquired = Update .run (network = mock_network )
116
- self .assertTrue (acquired )
117
-
118
- # texas ground truth, sorted by `hospital_pk`
119
- # see sample data at testdata/acquisition/covid_hosp/facility/dataset.csv
120
- texas_hospitals = [{
121
- 'hospital_pk' : '450771' ,
122
- 'state' : 'TX' ,
123
- 'ccn' : '450771' ,
124
- 'hospital_name' : 'TEXAS HEALTH PRESBYTERIAN HOSPITAL PLANO' ,
125
- 'address' : '6200 W PARKER RD' ,
126
- 'city' : 'PLANO' ,
127
- 'zip' : '75093' ,
128
- 'hospital_subtype' : 'Short Term' ,
129
- 'fips_code' : '48085' ,
130
- 'is_metro_micro' : 1 ,
131
- }, {
132
- 'hospital_pk' : '450822' ,
133
- 'state' : 'TX' ,
134
- 'ccn' : '450822' ,
135
- 'hospital_name' : 'MEDICAL CITY LAS COLINAS' ,
136
- 'address' : '6800 N MACARTHUR BLVD' ,
137
- 'city' : 'IRVING' ,
138
- 'zip' : '77777' , # most-recent collection week should take precedence
139
- 'hospital_subtype' : 'Short Term' ,
140
- 'fips_code' : '48113' ,
141
- 'is_metro_micro' : 1 ,
142
- }, {
143
- 'hospital_pk' : '451329' ,
144
- 'state' : 'TX' ,
145
- 'ccn' : '451329' ,
146
- 'hospital_name' : 'RANKIN HOSPITAL MEDICAL CLINIC' ,
147
- 'address' : '1611 SPUR 576' ,
148
- 'city' : 'RANKIN' ,
149
- 'zip' : '99999' , # most-recent collection week should take precedence
150
- 'hospital_subtype' : 'Critical Access Hospitals' ,
151
- 'fips_code' : '48461' ,
152
- 'is_metro_micro' : 0 ,
153
- }]
154
-
155
- with self .subTest (name = 'by state' ):
156
- response = Epidata .covid_hosp_facility_lookup (state = 'tx' )
157
- self .assertEqual (response ['epidata' ], texas_hospitals )
158
-
159
- with self .subTest (name = 'by zip' ):
160
- response = Epidata .covid_hosp_facility_lookup (zip = '75093' )
161
- self .assertEqual (response ['epidata' ], texas_hospitals [0 :1 ])
162
-
163
- with self .subTest (name = 'by city' ):
164
- response = Epidata .covid_hosp_facility_lookup (city = 'irving' )
165
- self .assertEqual (response ['epidata' ], texas_hospitals [1 :2 ])
166
-
167
- with self .subTest (name = 'by ccn' ):
168
- response = Epidata .covid_hosp_facility_lookup (ccn = '451329' )
169
- self .assertEqual (response ['epidata' ], texas_hospitals [2 :3 ])
170
-
171
- with self .subTest (name = 'by fips_code' ):
172
- response = Epidata .covid_hosp_facility_lookup (fips_code = '48085' )
173
- self .assertEqual (response ['epidata' ], texas_hospitals [0 :1 ])
174
-
175
- with self .subTest (name = 'no results' ):
176
- response = Epidata .covid_hosp_facility_lookup (state = 'not a state' )
177
- self .assertEqual (response ['result' ], - 2 )
178
-
179
- # update facility info
180
- mock_network = MagicMock ()
181
- mock_network .fetch_metadata .return_value = \
182
- self .test_utils .load_sample_metadata ('metadata_update_facility.csv' )
183
- mock_network .fetch_dataset .return_value = \
184
- self .test_utils .load_sample_dataset ('dataset_update_facility.csv' )
185
-
186
- # acquire sample data into local database
187
- with self .subTest (name = 'second acquisition' ):
188
- acquired = Update .run (network = mock_network )
189
- self .assertTrue (acquired )
190
-
191
- texas_hospitals [1 ]['zip' ] = '88888'
192
- with self .subTest (name = 'by city after update' ):
193
- response = Epidata .covid_hosp_facility_lookup (city = 'irving' )
194
- self .assertEqual (response ['epidata' ], texas_hospitals [1 :2 ])
48
+
49
+ @freeze_time ("2021-03-16" )
50
+ def test_acquire_dataset (self ):
51
+ """Acquire a new dataset."""
52
+
53
+ # only mock out network calls to external hosts
54
+ mock_network = MagicMock ()
55
+ mock_network .fetch_metadata .return_value = \
56
+ self .test_utils .load_sample_metadata ()
57
+ mock_network .fetch_dataset .return_value = \
58
+ self .test_utils .load_sample_dataset ()
59
+
60
+ # make sure the data does not yet exist
61
+ with self .subTest (name = 'no data yet' ):
62
+ response = Epidata .covid_hosp_facility (
63
+ '450822' , Epidata .range (20200101 , 20210101 ))
64
+ self .assertEqual (response ['result' ], - 2 , response )
65
+
66
+ # acquire sample data into local database
67
+ with self .subTest (name = 'first acquisition' ):
68
+ acquired = Update .run (network = mock_network )
69
+ self .assertTrue (acquired )
70
+
71
+ # make sure the data now exists
72
+ with self .subTest (name = 'initial data checks' ):
73
+ expected_spotchecks = {
74
+ "hospital_pk" : "450822" ,
75
+ "collection_week" : 20201030 ,
76
+ "publication_date" : 20210315 ,
77
+ "previous_day_total_ed_visits_7_day_sum" : 536 ,
78
+ "total_personnel_covid_vaccinated_doses_all_7_day_sum" : 18 ,
79
+ "total_beds_7_day_avg" : 69.3 ,
80
+ "previous_day_admission_influenza_confirmed_7_day_sum" : - 999999
81
+ }
82
+ response = Epidata .covid_hosp_facility (
83
+ '450822' , Epidata .range (20200101 , 20210101 ))
84
+ self .assertEqual (response ['result' ], 1 )
85
+ self .assertEqual (len (response ['epidata' ]), 2 )
86
+ row = response ['epidata' ][0 ]
87
+ for k , v in expected_spotchecks .items ():
88
+ self .assertTrue (
89
+ k in row ,
90
+ f"no '{ k } ' in row:\n { NEWLINE .join (sorted (row .keys ()))} "
91
+ )
92
+ if isinstance (v , float ):
93
+ self .assertAlmostEqual (row [k ], v , f"row[{ k } ] is { row [k ]} not { v } " )
94
+ else :
95
+ self .assertEqual (row [k ], v , f"row[{ k } ] is { row [k ]} not { v } " )
96
+
97
+ # expect 113 fields per row (114 database columns, except `id`)
98
+ self .assertEqual (len (row ), 113 )
99
+
100
+ # re-acquisition of the same dataset should be a no-op
101
+ with self .subTest (name = 'second acquisition' ):
102
+ acquired = Update .run (network = mock_network )
103
+ self .assertFalse (acquired )
104
+
105
+ # make sure the data still exists
106
+ with self .subTest (name = 'final data checks' ):
107
+ response = Epidata .covid_hosp_facility (
108
+ '450822' , Epidata .range (20200101 , 20210101 ))
109
+ self .assertEqual (response ['result' ], 1 )
110
+ self .assertEqual (len (response ['epidata' ]), 2 )
111
+
112
+ @freeze_time ("2021-03-17" )
113
+ def test_facility_lookup (self ):
114
+ """Lookup facilities using various filters."""
115
+
116
+ # only mock out network calls to external hosts
117
+ mock_network = MagicMock ()
118
+ mock_network .fetch_metadata .return_value = \
119
+ self .test_utils .load_sample_metadata ()
120
+ mock_network .fetch_dataset .return_value = \
121
+ self .test_utils .load_sample_dataset ()
122
+
123
+ # acquire sample data into local database
124
+ with self .subTest (name = 'first acquisition' ):
125
+ acquired = Update .run (network = mock_network )
126
+ self .assertTrue (acquired )
127
+
128
+ # texas ground truth, sorted by `hospital_pk`
129
+ # see sample data at testdata/acquisition/covid_hosp/facility/dataset.csv
130
+ texas_hospitals = [{
131
+ 'hospital_pk' : '450771' ,
132
+ 'state' : 'TX' ,
133
+ 'ccn' : '450771' ,
134
+ 'hospital_name' : 'TEXAS HEALTH PRESBYTERIAN HOSPITAL PLANO' ,
135
+ 'address' : '6200 W PARKER RD' ,
136
+ 'city' : 'PLANO' ,
137
+ 'zip' : '75093' ,
138
+ 'hospital_subtype' : 'Short Term' ,
139
+ 'fips_code' : '48085' ,
140
+ 'is_metro_micro' : 1 ,
141
+ }, {
142
+ 'hospital_pk' : '450822' ,
143
+ 'state' : 'TX' ,
144
+ 'ccn' : '450822' ,
145
+ 'hospital_name' : 'MEDICAL CITY LAS COLINAS' ,
146
+ 'address' : '6800 N MACARTHUR BLVD' ,
147
+ 'city' : 'IRVING' ,
148
+ 'zip' : '77777' , # most-recent collection week should take precedence
149
+ 'hospital_subtype' : 'Short Term' ,
150
+ 'fips_code' : '48113' ,
151
+ 'is_metro_micro' : 1 ,
152
+ }, {
153
+ 'hospital_pk' : '451329' ,
154
+ 'state' : 'TX' ,
155
+ 'ccn' : '451329' ,
156
+ 'hospital_name' : 'RANKIN HOSPITAL MEDICAL CLINIC' ,
157
+ 'address' : '1611 SPUR 576' ,
158
+ 'city' : 'RANKIN' ,
159
+ 'zip' : '99999' , # most-recent collection week should take precedence
160
+ 'hospital_subtype' : 'Critical Access Hospitals' ,
161
+ 'fips_code' : '48461' ,
162
+ 'is_metro_micro' : 0 ,
163
+ }]
164
+
165
+ with self .subTest (name = 'by state' ):
166
+ response = Epidata .covid_hosp_facility_lookup (state = 'tx' )
167
+ self .assertEqual (response ['epidata' ], texas_hospitals )
168
+
169
+ with self .subTest (name = 'by zip' ):
170
+ response = Epidata .covid_hosp_facility_lookup (zip = '75093' )
171
+ self .assertEqual (response ['epidata' ], texas_hospitals [0 :1 ])
172
+
173
+ with self .subTest (name = 'by city' ):
174
+ response = Epidata .covid_hosp_facility_lookup (city = 'irving' )
175
+ self .assertEqual (response ['epidata' ], texas_hospitals [1 :2 ])
176
+
177
+ with self .subTest (name = 'by ccn' ):
178
+ response = Epidata .covid_hosp_facility_lookup (ccn = '451329' )
179
+ self .assertEqual (response ['epidata' ], texas_hospitals [2 :3 ])
180
+
181
+ with self .subTest (name = 'by fips_code' ):
182
+ response = Epidata .covid_hosp_facility_lookup (fips_code = '48085' )
183
+ self .assertEqual (response ['epidata' ], texas_hospitals [0 :1 ])
184
+
185
+ with self .subTest (name = 'no results' ):
186
+ response = Epidata .covid_hosp_facility_lookup (state = 'not a state' )
187
+ self .assertEqual (response ['result' ], - 2 )
188
+
189
+ # update facility info
190
+ mock_network = MagicMock ()
191
+ mock_network .fetch_metadata .return_value = \
192
+ self .test_utils .load_sample_metadata ('metadata_update_facility.csv' )
193
+ mock_network .fetch_dataset .return_value = \
194
+ self .test_utils .load_sample_dataset ('dataset_update_facility.csv' )
195
+
196
+ # acquire sample data into local database
197
+ with self .subTest (name = 'second acquisition' ):
198
+ acquired = Update .run (network = mock_network )
199
+ self .assertTrue (acquired )
200
+
201
+ texas_hospitals [1 ]['zip' ] = '88888'
202
+ with self .subTest (name = 'by city after update' ):
203
+ response = Epidata .covid_hosp_facility_lookup (city = 'irving' )
204
+ self .assertEqual (response ['epidata' ], texas_hospitals [1 :2 ])
0 commit comments