Skip to content

Commit dce2b54

Browse files
committed
Add integration tests to state_daily
1 parent 5345736 commit dce2b54

File tree

5 files changed

+94
-3
lines changed

5 files changed

+94
-3
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import sys
2+
import os
3+
4+
sys.path.append(os.getcwd())
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
"""Integration tests for acquisition of COVID hospitalization."""
2+
3+
# standard library
4+
import unittest
5+
from unittest.mock import MagicMock
6+
7+
# first party
8+
from delphi.epidata.acquisition.covid_hosp.common.database import Database
9+
from delphi.epidata.acquisition.covid_hosp.common.test_utils import TestUtils
10+
from delphi.epidata.client.delphi_epidata import Epidata
11+
from delphi.epidata.acquisition.covid_hosp.state_daily.update import Update
12+
import delphi.operations.secrets as secrets
13+
14+
# py3tester coverage target (equivalent to `import *`)
15+
__test_target__ = \
16+
'delphi.epidata.acquisition.covid_hosp.state_daily.update'
17+
18+
19+
class AcquisitionTests(unittest.TestCase):
20+
21+
def setUp(self):
22+
"""Perform per-test setup."""
23+
24+
# configure test data
25+
self.test_utils = TestUtils(__file__)
26+
27+
# use the local instance of the Epidata API
28+
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata/api.php'
29+
30+
# use the local instance of the epidata database
31+
secrets.db.host = 'delphi_database_epidata'
32+
secrets.db.epi = ('user', 'pass')
33+
34+
# clear relevant tables
35+
with Database.connect() as db:
36+
with db.new_cursor() as cur:
37+
cur.execute('truncate table covid_hosp_state_timeseries')
38+
cur.execute('truncate table covid_hosp_meta')
39+
40+
def test_acquire_dataset(self):
41+
"""Acquire a new dataset."""
42+
43+
# only mock out network calls to external hosts
44+
mock_network = MagicMock()
45+
mock_network.fetch_metadata.return_value = \
46+
self.test_utils.load_sample_metadata()
47+
mock_network.fetch_dataset.return_value = \
48+
self.test_utils.load_sample_dataset()
49+
50+
# make sure the data does not yet exist
51+
with self.subTest(name='no data yet'):
52+
response = Epidata.covid_hosp('MA', Epidata.range(20200101, 20210101))
53+
self.assertEqual(response['result'], -2)
54+
55+
# acquire sample data into local database
56+
with self.subTest(name='first acquisition'):
57+
acquired = Update.run(network=mock_network)
58+
self.assertTrue(acquired)
59+
60+
# make sure the data now exists
61+
with self.subTest(name='initial data checks'):
62+
response = Epidata.covid_hosp('WY', Epidata.range(20200101, 20210101))
63+
self.assertEqual(response['result'], 1)
64+
self.assertEqual(len(response['epidata']), 1)
65+
row = response['epidata'][0]
66+
self.assertEqual(row['state'], 'WY')
67+
self.assertEqual(row['date'], 20201209)
68+
self.assertEqual(row['issue'], 20201213)
69+
self.assertEqual(row['critical_staffing_shortage_today_yes'], 8)
70+
actual = row['inpatient_bed_covid_utilization']
71+
expected = 0.11729857819905214
72+
self.assertAlmostEqual(actual, expected)
73+
self.assertIsNone(row['critical_staffing_shortage_today_no'])
74+
75+
# expect 61 fields per row (63 database columns, except `id` and `record_type`)
76+
self.assertEqual(len(row), 61)
77+
78+
# re-acquisition of the same dataset should be a no-op
79+
with self.subTest(name='second acquisition'):
80+
acquired = Update.run(network=mock_network)
81+
self.assertFalse(acquired)
82+
83+
# make sure the data still exists
84+
with self.subTest(name='final data checks'):
85+
response = Epidata.covid_hosp('WY', Epidata.range(20200101, 20210101))
86+
self.assertEqual(response['result'], 1)
87+
self.assertEqual(len(response['epidata']), 1)

src/acquisition/covid_hosp/state_daily/database.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Database(BaseDatabase):
1818
# to update a column name, do it in both places.
1919
ORDERED_CSV_COLUMNS = [
2020
('state', 'state', str),
21-
('reporting_cutoff_start', 'reporting_cutoff_start', Utils.int_from_date),
21+
('reporting_cutoff_start', 'date', Utils.int_from_date),
2222
('critical_staffing_shortage_today_yes', 'critical_staffing_shortage_today_yes', int),
2323
('critical_staffing_shortage_today_no', 'critical_staffing_shortage_today_no', int),
2424
('critical_staffing_shortage_today_not_reported',

testdata/acquisition/covid_hosp/state_daily/dataset.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@ MI,30,129,4,32,127,4,41,159,23598,163,18003,163,3812,159,376,163,162,159,9,159,9
5151
MN,21,116,2,26,111,2,63,138,10358,139,7558,139,1516,138,182,139,70,138,3,138,2,138,806,139,346,138,355,139,1490,138,1358,139,26,138,21,138,1019,139,0.7296775439273991,139,7558,10358,0.20824175824175825,138,1516,7280,0.15163032606521304,138,1516,9998,0.36536430834213307,138,346,947,0.7909715407262021,139,806,1019,2020-12-09
5252
MO,47,78,16,62,63,16,22,137,17433,141,13521,141,2611,137,315,141,239,137,5,137,18,137,1615,141,645,137,604,141,2546,137,2307,141,65,137,26,137,1931,141,0.7755980037859233,141,13521,17433,0.19644872470092545,137,2611,13291,0.1523959610109146,137,2611,17133,0.34565916398713825,137,645,1866,0.8363542206110823,141,1615,1931,2020-12-09
5353
MS,21,85,2,30,76,2,12,106,8799,108,5637,108,1254,106,142,108,30,106,3,106,5,106,718,108,338,106,263,108,1234,106,1066,108,20,106,5,106,881,108,0.6406409819297647,108,5637,8799,0.22501345774268797,106,1254,5573,0.14392287386663605,106,1254,8713,0.3953216374269006,106,338,855,0.8149829738933031,108,718,881,2020-12-09
54-
WY,8,21,2,7,22,2,5,29,1729,31,856,31,198,29,26,31,15,29,0,29,0,29,58,31,32,29,32,31,196,29,189,31,2,29,2,29,137,31,0.4950838635049161,31,856,1729,0.23627684964200477,29,198,838,0.11729857819905214,29,198,1688,0.25196850393700787,29,32,127,0.4233576642335766,31,58,137,2020-12-09
54+
WY,8,,2,7,22,2,5,29,1729,31,856,31,198,29,26,31,15,29,0,29,0,29,58,31,32,29,32,31,196,29,189,31,2,29,2,29,137,31,0.4950838635049161,31,856,1729,0.23627684964200477,29,198,838,0.11729857819905214,29,198,1688,0.25196850393700787,29,32,127,0.4233576642335766,31,58,137,2020-12-09

tests/acquisition/covid_hosp/state_daily/test_database.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def test_insert_dataset(self):
4040

4141
last_query_values = mock_cursor.execute.call_args[0][-1]
4242
expected_query_values = (
43-
0, sentinel.issue, 'WY', 20201209, 8, 21, 2, 7, 22, 2, 5, 29, 1729,
43+
0, sentinel.issue, 'WY', 20201209, 8, None, 2, 7, 22, 2, 5, 29, 1729,
4444
31, 856, 31, 198, 29, 26, 31, 15, 29, 0, 29, 0, 29, 58, 31, 32, 29,
4545
32, 31, 196, 29, 189, 31, 2, 29, 2, 29, 137, 31, 0.4950838635049161, 31,
4646
856, 1729, 0.23627684964200477, 29, 198, 838, 0.11729857819905214,

0 commit comments

Comments
 (0)