Skip to content

Commit 41b6fc7

Browse files
committed
change data to dict and update integration tests
1 parent 61a199a commit 41b6fc7

File tree

2 files changed

+77
-22
lines changed

2 files changed

+77
-22
lines changed

integrations/acquisition/rvdss/test_scenarios.py

Lines changed: 72 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
"""Integration tests for acquisition of rvdss data."""
22
# standard library
33
import unittest
4-
from unittest.mock import MagicMock
4+
from unittest.mock import MagicMock, patch
5+
import mock
6+
from copy import copy
57

68
# first party
79
from delphi.epidata.client.delphi_epidata import Epidata
8-
from delphi.epidata.acquisition.rvdss.database import update
10+
from delphi.epidata.acquisition.rvdss.database import update, rvdss_cols, get_num_rows
911
import delphi.operations.secrets as secrets
12+
from delphi_utils import get_structured_logger
13+
1014

1115
# third party
1216
import mysql.connector
1317
import pandas as pd
1418
from pathlib import Path
19+
import pdb
1520

1621
# py3tester coverage target (equivalent to `import *`)
1722
# __test_target__ = 'delphi.epidata.acquisition.covid_hosp.facility.update'
1823

1924
NEWLINE="\n"
2025

2126
class AcquisitionTests(unittest.TestCase):
22-
27+
logger = get_structured_logger()
28+
2329
def setUp(self):
2430
"""Perform per-test setup."""
2531

@@ -35,7 +41,6 @@ def setUp(self):
3541
secrets.db.epi = ('user', 'pass')
3642

3743
# clear relevant tables
38-
3944
epidata_cnx = mysql.connector.connect(
4045
user='user',
4146
password='pass',
@@ -48,26 +53,76 @@ def setUp(self):
4853
epidata_cur.execute('INSERT INTO api_user(api_key, email) VALUES ("key", "email")')
4954
epidata_cnx.commit()
5055
epidata_cur.close()
51-
epidata_cnx.close()
52-
53-
def test_rvdss_repiratory_detections(self):
54-
#TEST_DIR = Path(__file__).parent.parent.parent
55-
#detection_data = pd.read_csv(str(TEST_DIR) + "/testdata/acquisition/rvdss/RVD_CurrentWeekTable_Formatted.csv")
56-
#detection_subset = detection_data[(detection_data['geo_value'].isin(['nl', 'nb'])) & (detection_data['time_value'].isin(['2024-08-31', '2024-09-07'])) ]
57-
56+
#epidata_cnx.close()
57+
58+
# make connection and cursor available to test cases
59+
self.cnx = epidata_cnx
60+
#self.cur = epidata_cnx.cursor()
61+
62+
def tearDown(self):
63+
"""Perform per-test teardown."""
64+
#self.cur.close()
65+
self.cnx.close()
66+
67+
@mock.patch("mysql.connector.connect")
68+
def test_rvdss_repiratory_detections(self,mock_sql):
69+
TEST_DIR = Path(__file__).parent.parent.parent.parent
70+
detection_data = pd.read_csv(str(TEST_DIR) + "/testdata/acquisition/rvdss/RVD_CurrentWeekTable_Formatted.csv")
71+
detection_data['time_type'] = "week"
72+
detection_subset = detection_data[(detection_data['geo_value'].isin(['nl', 'nb'])) & (detection_data['time_value'].isin([202408-31, 20240907])) ]
73+
74+
connection_mock = MagicMock()
5875
# make sure the data does not yet exist
5976
with self.subTest(name='no data yet'):
6077
response = Epidata.rvdss(geo_type='province',
61-
time_values= ['2024-08-31', '2024-09-07'],
78+
time_values= [202435, 202436],
6279
geo_value = ['nl','nb'])
6380
self.assertEqual(response['result'], -2, response)
6481

6582

66-
# # acquire sample data into local database
67-
# # TODO: Define example data
68-
# with self.subTest(name='first acquisition'):
69-
# acquired = Update.run(network=mock_network)
70-
# #self.assertTrue(acquired)
83+
# acquire sample data into local database
84+
with self.subTest(name='first acquisition'):
85+
#mock_sql.cursor.return_value = self.cnx.cursor()
86+
connection_mock.cursor.return_value = self.cnx.cursor()
87+
mock_sql.return_value = connection_mock
88+
89+
rvdss_cols_subset = [col for col in detection_subset.columns if col in rvdss_cols]
90+
pdb.set_trace()
91+
update(detection_subset,self.logger)
92+
93+
response = Epidata.rvdss(geo_type='province',
94+
time_values= [202435, 202436],
95+
geo_value = ['nl','nb'])
96+
97+
self.assertEqual(response['result'], 1)
98+
99+
with self.subTest(name='first acquisition2'):
100+
#mock_sql.cursor.return_value = self.cnx.cursor()
101+
connection_mock.cursor.return_value = self.cnx.cursor()
102+
mock_sql.return_value = connection_mock
103+
104+
rvdss_cols_subset = [col for col in detection_subset.columns if col in rvdss_cols]
105+
update(detection_subset,self.logger)
106+
107+
response = Epidata.rvdss(geo_type='province',
108+
time_values= [202435, 202436],
109+
geo_value = ['nl','nb'])
110+
111+
self.assertEqual(response['result'], 1)
112+
113+
with self.subTest(name='first acquisition3'):
114+
#mock_sql.cursor.return_value = self.cnx.cursor()
115+
connection_mock.cursor.return_value = self.cnx.cursor()
116+
mock_sql.return_value = connection_mock
117+
118+
rvdss_cols_subset = [col for col in detection_subset.columns if col in rvdss_cols]
119+
update(detection_subset,self.logger)
120+
121+
response = Epidata.rvdss(geo_type='province',
122+
time_values= [202435, 202436],
123+
geo_value = ['nl','nb'])
124+
125+
self.assertEqual(response['result'], 1)
71126

72127
# # make sure the data now exists
73128
# with self.subTest(name='initial data checks'):

src/acquisition/rvdss/database.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ def update(data,logger):
8181
u, p = secrets.db.epi
8282
cnx = mysql.connector.connect(user=u, password=p, database="epidata")
8383
cur = cnx.cursor()
84+
85+
rvdss_cols_subset = [col for col in data.columns if col in rvdss_cols]
86+
data = data.to_dict(orient = "records")
8487

85-
data_tuples = list(data.itertuples(index=False,name=None))
86-
87-
rvdss_cols_subset = [col for col in rvdss_cols if col in data.columns]
8888
field_names = ", ".join(f"`{name}`" for name in rvdss_cols_subset)
8989
field_values = ", ".join(f"%({name})s" for name in rvdss_cols_subset)
9090

@@ -99,13 +99,13 @@ def update(data,logger):
9999
total_rows = 0
100100

101101
#insert data
102-
cur.executemany(sql, data_tuples)
102+
cur.executemany(sql, data)
103+
cnx.commit()
103104

104105
# keep track of how many rows were added
105106
rows_after = get_num_rows(cur)
106107
logger.info(f"Inserted {int(rows_after - rows_before)}/{int(total_rows)} row(s) into table rvdss")
107108

108109
# cleanup
109110
cur.close()
110-
cnx.commit()
111111
cnx.close()

0 commit comments

Comments
 (0)