|
1 | 1 | """Integration tests for delphi_epidata.py."""
|
| 2 | + |
| 3 | +# standard library |
2 | 4 | import unittest
|
3 | 5 |
|
| 6 | +# third party |
| 7 | +import mysql.connector |
| 8 | + |
4 | 9 | # first party
|
5 | 10 | from delphi.epidata.client.delphi_epidata import Epidata
|
6 | 11 |
|
7 | 12 | # py3tester coverage target
|
8 | 13 | __test_target__ = 'delphi.epidata.client.delphi_epidata'
|
9 | 14 |
|
| 15 | + |
10 | 16 | class DelphiEpidataPythonClientTests(unittest.TestCase):
|
11 |
| - """Tests the Python client.""" |
12 |
| - |
13 |
| - def test_covidcast_happy_path(self): |
14 |
| - """Test that the covidcast endpoint returns expected data.""" |
15 |
| - |
16 |
| - # Fetch data |
17 |
| - res = Epidata.covidcast('fb-survey', 'cli', 'day', 'county', [20200401, Epidata.range(20200405, 20200414)], '06001') |
18 |
| - # Check result |
19 |
| - self.assertEqual(res['result'], 1) |
20 |
| - self.assertEqual(res['message'], 'success') |
21 |
| - self.assertGreater(len(res['epidata']), 0) |
22 |
| - item = res['epidata'][0] |
23 |
| - self.assertIn('geo_value', item) |
24 |
| - self.assertIn('time_value', item) |
25 |
| - self.assertIn('direction', item) |
26 |
| - self.assertIn('value', item) |
27 |
| - self.assertIn('stderr', item) |
28 |
| - self.assertIn('sample_size', item) |
29 |
| - |
30 |
| - |
31 |
| - def test_covidcast_meta(self): |
32 |
| - """Test that the covidcast_meta endpoint returns expected data.""" |
33 |
| - |
34 |
| - # Fetch data |
35 |
| - res = Epidata.covidcast_meta() |
36 |
| - # Check result |
37 |
| - self.assertEqual(res['result'], 1) |
38 |
| - self.assertEqual(res['message'], 'success') |
39 |
| - self.assertGreater(len(res['epidata']), 0) |
40 |
| - item = res['epidata'][0] |
41 |
| - self.assertIn('data_source', item) |
42 |
| - self.assertIn('time_type', item) |
43 |
| - self.assertIn('geo_type', item) |
44 |
| - self.assertIn('min_time', item) |
45 |
| - self.assertIn('max_time', item) |
46 |
| - self.assertIn('num_locations', item) |
47 |
| - self.assertIn('min_value', item) |
48 |
| - self.assertIn('max_value', item) |
49 |
| - self.assertIn('mean_value', item) |
50 |
| - self.assertIn('stdev_value', item) |
51 |
| - |
52 |
| -if __name__ == '__main__': |
53 |
| - unittest.main() |
| 17 | + """Tests the Python client.""" |
| 18 | + |
| 19 | + def setUp(self): |
| 20 | + """Perform per-test setup.""" |
| 21 | + |
| 22 | + # connect to the `epidata` database and clear relevant tables |
| 23 | + cnx = mysql.connector.connect( |
| 24 | + user='user', |
| 25 | + password='pass', |
| 26 | + host='delphi_database_epidata', |
| 27 | + database='epidata') |
| 28 | + cur = cnx.cursor() |
| 29 | + cur.execute('truncate table covidcast') |
| 30 | + cnx.commit() |
| 31 | + cur.close() |
| 32 | + |
| 33 | + # make connection and cursor available to test cases |
| 34 | + self.cnx = cnx |
| 35 | + self.cur = cnx.cursor() |
| 36 | + |
| 37 | + # use the local instance of the Epidata API |
| 38 | + Epidata.BASE_URL = 'http://delphi_web_epidata/epidata/api.php' |
| 39 | + |
| 40 | + def tearDown(self): |
| 41 | + """Perform per-test teardown.""" |
| 42 | + self.cur.close() |
| 43 | + self.cnx.close() |
| 44 | + |
| 45 | + def test_covidcast(self): |
| 46 | + """Test that the covidcast endpoint returns expected data.""" |
| 47 | + |
| 48 | + # insert dummy data |
| 49 | + self.cur.execute(''' |
| 50 | + insert into covidcast values |
| 51 | + (0, 'src', 'sig', 'day', 'county', 20200414, '01234', 1.5, 2.5, 3.5, 4) |
| 52 | + ''') |
| 53 | + self.cnx.commit() |
| 54 | + |
| 55 | + # fetch data |
| 56 | + response = Epidata.covidcast( |
| 57 | + 'src', 'sig', 'day', 'county', 20200414, '01234') |
| 58 | + |
| 59 | + # check result |
| 60 | + self.assertEqual(response, { |
| 61 | + 'result': 1, |
| 62 | + 'epidata': [{ |
| 63 | + 'time_value': 20200414, |
| 64 | + 'geo_value': '01234', |
| 65 | + 'value': 1.5, |
| 66 | + 'stderr': 2.5, |
| 67 | + 'sample_size': 3.5, |
| 68 | + 'direction': 4, |
| 69 | + }], |
| 70 | + 'message': 'success', |
| 71 | + }) |
| 72 | + |
| 73 | + def test_covidcast_meta(self): |
| 74 | + """Test that the covidcast_meta endpoint returns expected data.""" |
| 75 | + |
| 76 | + # insert dummy data |
| 77 | + self.cur.execute(''' |
| 78 | + insert into covidcast values |
| 79 | + (0, 'src', 'sig', 'day', 'county', 20200414, '01234', 1.5, 2.5, 3.5, 4) |
| 80 | + ''') |
| 81 | + self.cnx.commit() |
| 82 | + |
| 83 | + # fetch data |
| 84 | + response = Epidata.covidcast_meta() |
| 85 | + |
| 86 | + # check result |
| 87 | + self.assertEqual(response, { |
| 88 | + 'result': 1, |
| 89 | + 'epidata': [{ |
| 90 | + 'data_source': 'src', |
| 91 | + 'signal': 'sig', |
| 92 | + 'time_type': 'day', |
| 93 | + 'geo_type': 'county', |
| 94 | + 'min_time': 20200414, |
| 95 | + 'max_time': 20200414, |
| 96 | + 'num_locations': 1, |
| 97 | + 'min_value': 1.5, |
| 98 | + 'max_value': 1.5, |
| 99 | + 'mean_value': 1.5, |
| 100 | + 'stdev_value': 0, |
| 101 | + }], |
| 102 | + 'message': 'success', |
| 103 | + }) |
0 commit comments