Skip to content

modularize, implement covid_survey parts #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions integrations/test_covid_survey_county_weekly.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
"""Integration tests for the `covid_survey_county_weekly` endpoint."""

# standard library
import unittest

# third party
import mysql.connector
import requests


# use the local instance of the Epidata API
BASE_URL = 'http://delphi_web_epidata/epidata/api.php'


class CovidSurveyCountyWeeklyTests(unittest.TestCase):
"""Tests the `covid_survey_county_weekly` endpoint."""

def setUp(self):
"""Perform per-test setup."""

# connect to the `epidata` database and clear the
# `covid_survey_county_weekly` table
cnx = mysql.connector.connect(
user='user',
password='pass',
host='delphi_database_epidata',
database='epidata')
cur = cnx.cursor()
cur.execute('truncate table covid_survey_county_weekly')
cnx.commit()
cur.close()

# make connection and cursor available to test cases
self.cnx = cnx
self.cur = cnx.cursor()

def tearDown(self):
"""Perform per-test teardown."""
self.cur.close()
self.cnx.close()

def test_round_trip(self):
"""Make a simple round-trip with some sample data."""

# insert dummy data
self.cur.execute('''
insert into covid_survey_county_weekly values
(0, 202014, '42003', 1.5, 2.5, 3.5, 4.5, 5678.5)
''')
self.cnx.commit()

# make the request
response = requests.get(BASE_URL, params={
'source': 'covid_survey_county_weekly',
'counties': '42003',
'epiweeks': 202014,
})
response.raise_for_status()
response = response.json()

# assert that the right data came back
self.assertEqual(response, {
"result": 1,
"epidata": [{
'epiweek': 202014,
'county': '42003',
'ili': 1.5,
'ili_stdev': 2.5,
'cli': 3.5,
'cli_stdev': 4.5,
'denominator': 5678.5,
}],
"message": "success",
})

def test_privacy_filtering(self):
"""Don't return rows with too small of a denominator."""

# shared constants
request_params = {
'source': 'covid_survey_county_weekly',
'counties': '42003',
'epiweeks': 202014,
}

with self.subTest(name='filtered'):

# insert dummy data
self.cur.execute('''
insert into covid_survey_county_weekly values
(0, 202014, '42003', 1.5, 2.5, 3.5, 4.5, 99.5)
''')
self.cnx.commit()

# make the request
response = requests.get(BASE_URL, params=request_params)
response.raise_for_status()
response = response.json()

# assert that no data came back
self.assertEqual(response, {
"result": -2,
"message": "no results",
})

with self.subTest(name='unfiltered'):

# amend the denominator
self.cur.execute('''
update covid_survey_county_weekly set denominator = 100.5
''')
self.cnx.commit()

# make the request
response = requests.get(BASE_URL, params=request_params)
response.raise_for_status()
response = response.json()

# assert that the right data came back
self.assertEqual(response, {
"result": 1,
"epidata": [{
'epiweek': 202014,
'county': '42003',
'ili': 1.5,
'ili_stdev': 2.5,
'cli': 3.5,
'cli_stdev': 4.5,
'denominator': 100.5,
}],
"message": "success",
})
132 changes: 132 additions & 0 deletions integrations/test_covid_survey_hrr_daily.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
"""Integration tests for the `covid_survey_hrr_daily` endpoint."""

# standard library
import unittest

# third party
import mysql.connector
import requests


# use the local instance of the Epidata API
BASE_URL = 'http://delphi_web_epidata/epidata/api.php'


class CovidSurveyHrrDailyTests(unittest.TestCase):
"""Tests the `covid_survey_hrr_daily` endpoint."""

def setUp(self):
"""Perform per-test setup."""

# connect to the `epidata` database and clear the `covid_survey_hrr_daily`
# table
cnx = mysql.connector.connect(
user='user',
password='pass',
host='delphi_database_epidata',
database='epidata')
cur = cnx.cursor()
cur.execute('truncate table covid_survey_hrr_daily')
cnx.commit()
cur.close()

# make connection and cursor available to test cases
self.cnx = cnx
self.cur = cnx.cursor()

def tearDown(self):
"""Perform per-test teardown."""
self.cur.close()
self.cnx.close()

def test_round_trip(self):
"""Make a simple round-trip with some sample data."""

# insert dummy data
self.cur.execute('''
insert into covid_survey_hrr_daily values
(0, '2020-04-08', 123, 1.5, 2.5, 3.5, 4.5, 5678.5)
''')
self.cnx.commit()

# make the request
response = requests.get(BASE_URL, params={
'source': 'covid_survey_hrr_daily',
'hrrs': 123,
'dates': 20200408,
})
response.raise_for_status()
response = response.json()

# assert that the right data came back
self.assertEqual(response, {
"result": 1,
"epidata": [{
'date': '2020-04-08',
'hrr': 123,
'ili': 1.5,
'ili_stdev': 2.5,
'cli': 3.5,
'cli_stdev': 4.5,
'denominator': 5678.5,
}],
"message": "success",
})

def test_privacy_filtering(self):
"""Don't return rows with too small of a denominator."""

# shared constants
request_params = {
'source': 'covid_survey_hrr_daily',
'hrrs': 123,
'dates': 20200408,
}

with self.subTest(name='filtered'):

# insert dummy data
self.cur.execute('''
insert into covid_survey_hrr_daily values
(0, '2020-04-08', 123, 1.5, 2.5, 3.5, 4.5, 99.5)
''')
self.cnx.commit()

# make the request
response = requests.get(BASE_URL, params=request_params)
response.raise_for_status()
response = response.json()

# assert that no data came back
self.assertEqual(response, {
"result": -2,
"message": "no results",
})

with self.subTest(name='unfiltered'):

# amend the denominator
self.cur.execute('''
update covid_survey_hrr_daily set denominator = 100.5
''')
self.cnx.commit()

# make the request
response = requests.get(BASE_URL, params=request_params)
response.raise_for_status()
response = response.json()

# assert that the right data came back
self.assertEqual(response, {
"result": 1,
"epidata": [{
'date': '2020-04-08',
'hrr': 123,
'ili': 1.5,
'ili_stdev': 2.5,
'cli': 3.5,
'cli_stdev': 4.5,
'denominator': 100.5,
}],
"message": "success",
})
82 changes: 82 additions & 0 deletions integrations/test_fluview.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
"""Integration tests for the `fluview` endpoint."""

# standard library
import unittest

# third party
import mysql.connector

# first party
from delphi.epidata.client.delphi_epidata import Epidata


class FluviewTests(unittest.TestCase):
"""Tests the `fluview` endpoint."""

@classmethod
def setUpClass(cls):
"""Perform one-time setup."""

# use the local instance of the Epidata API
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata/api.php'

def setUp(self):
"""Perform per-test setup."""

# connect to the `epidata` database and clear the `fluview` table
cnx = mysql.connector.connect(
user='user',
password='pass',
host='delphi_database_epidata',
database='epidata')
cur = cnx.cursor()
cur.execute('truncate table fluview')
cnx.commit()
cur.close()

# make connection and cursor available to test cases
self.cnx = cnx
self.cur = cnx.cursor()

def tearDown(self):
"""Perform per-test teardown."""
self.cur.close()
self.cnx.close()

def test_round_trip(self):
"""Make a simple round-trip with some sample data."""

# insert dummy data
self.cur.execute('''
insert into fluview values
(0, "2020-04-07", 202021, 202020, "nat", 1, 2, 3, 4, 3.14159, 1.41421,
10, 11, 12, 13, 14, 15)
''')
self.cnx.commit()

# make the request
response = Epidata.fluview('nat', 202020)

# assert that the right data came back
self.assertEqual(response, {
"result": 1,
"epidata": [{
"release_date": "2020-04-07",
"region": "nat",
"issue": 202021,
"epiweek": 202020,
"lag": 1,
"num_ili": 2,
"num_patients": 3,
"num_providers": 4,
"num_age_0": 10,
"num_age_1": 11,
"num_age_2": 12,
"num_age_3": 13,
"num_age_4": 14,
"num_age_5": 15,
"wili": 3.14159,
"ili": 1.41421,
}],
"message": "success",
})
48 changes: 48 additions & 0 deletions src/ddl/covid_survey_county_weekly.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Stores a subset of data from the COVID-19 survey, aggregated by weeks and
counties.

+-------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| epiweek | int(11) | NO | MUL | NULL | |
| county | varchar(5) | NO | | NULL | |
| ili | double | NO | | NULL | |
| ili_stdev | double | NO | | NULL | |
| cli | double | NO | | NULL | |
| cli_stdev | double | NO | | NULL | |
| denominator | double | NO | | NULL | |
+-------------+------------+------+-----+---------+----------------+

id:
unique identifier for each record
epiweek:
the epidemiological week during which the survey was submitted
county:
assumed fips 6-4 county code
ili:
estimated percent of sample experiencing influenza-like illness (ILI)
ili_stdev:
standard deviation for the ILI estimate
cli:
estimated percent of sample experiencing codid-19-like illness (CLI)
cli_stdev:
standard deviation for the CLI estimate
denominator:
estimated sample size
*/

CREATE TABLE `covid_survey_county_weekly` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`epiweek` int(11) NOT NULL,
`county` varchar(5) NOT NULL,
`ili` double NOT NULL,
`ili_stdev` double NOT NULL,
`cli` double NOT NULL,
`cli_stdev` double NOT NULL,
`denominator` double NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY (`epiweek`, `county`),
KEY (`county`, `epiweek`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Loading