Skip to content
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

Integration tests refactoring #1307

Open
wants to merge 63 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
e59aeac
Remove usage of PHP alias in the Python client
rzats Sep 19, 2023
b5128f0
Post review tweaks
rzats Sep 22, 2023
c65095e
removed 'api.php' from comments where not appropriate
melange396 Sep 23, 2023
3d8b827
Merge branch 'dev' into rzatserkovnyi/api-php-refactor
rzats Sep 26, 2023
05e2b84
Refactor?
rzats Sep 27, 2023
6e7190f
Add pypi changelog
rzats Sep 27, 2023
4513ced
Extra test for compat endpoint
rzats Sep 27, 2023
832b25c
Use del
rzats Sep 27, 2023
d5363e3
Added basic integration tests for all endpoints
dmytrotsko Jul 20, 2023
9d99885
Added base class for tests. Added API keys tests
dmytrotsko Jul 21, 2023
544ba94
Added TESTING_MODE to the ci.yaml, removed from Makefile
dmytrotsko Jul 21, 2023
9eed497
Updated acquisition tests with missing api_key creation
dmytrotsko Jul 26, 2023
da2a6a6
Update src/server/_config.py
dmytrotsko Sep 19, 2023
3b6b652
Update integrations/server/test_delphi.py
dmytrotsko Sep 19, 2023
b134408
Update integrations/server/test_meta.py
dmytrotsko Sep 20, 2023
15177ac
Requested changes from PR's comments
dmytrotsko Sep 21, 2023
9e4a332
Moved test_meta_norostat to test_norostat.py
dmytrotsko Sep 21, 2023
80c355c
Remove test_meta_norostat.py as this test was moved to test_norostat.py
dmytrotsko Sep 22, 2023
5e3210f
Update integrations/server/test_api_keys.py
dmytrotsko Sep 22, 2023
dca4c0b
Update integrations/server/test_norostat.py
dmytrotsko Sep 22, 2023
27f66a4
Update src/common/integration_test_base_class.py
dmytrotsko Sep 22, 2023
d9d83f3
Update integrations/server/test_norostat.py
dmytrotsko Sep 22, 2023
386c348
Remove usage of PHP alias in the Python client
rzats Sep 19, 2023
591059c
Post review tweaks
rzats Sep 22, 2023
66257b2
Adjusted tests to use new API url
dmytrotsko Sep 25, 2023
4259294
Changed base test class, renamed class name
dmytrotsko Sep 26, 2023
f1f9744
Changed base class name. Replaced Epidata usage with local class object.
dmytrotsko Sep 26, 2023
8538532
Renamed test_utils.py to covidcast_test_base.py and moved it to src/c…
dmytrotsko Sep 26, 2023
e0c370d
Replaced localSetUp with setUp in order to uselocalSetUp in covidcast…
dmytrotsko Sep 26, 2023
6fa0072
Replaced base class to CovidcastTestBase, removed unnecessary code fr…
dmytrotsko Sep 26, 2023
dcc31b1
Moved super().setUp() call to the end of the method.
dmytrotsko Sep 26, 2023
0466420
Replaced CovidcastBase with new CovidcastTestBase. Removed duplicated…
dmytrotsko Sep 26, 2023
9e601c4
Changed base class for CovidcastTestBase.
dmytrotsko Sep 26, 2023
c5f74d6
Removed unused import
dmytrotsko Sep 26, 2023
7ecae27
Replaced base class, removed unused imports, replaced Epidata client …
dmytrotsko Sep 26, 2023
ae07c0f
Fixed import
dmytrotsko Sep 26, 2023
d3fa119
Fixed import
dmytrotsko Sep 26, 2023
d01df1f
Fixed wrong import
dmytrotsko Sep 26, 2023
eba110b
Changed base class. Removed unncessary code. Replaced Epidata calls w…
dmytrotsko Sep 26, 2023
236e87d
Removed epidata client auth set
dmytrotsko Sep 26, 2023
92fcefd
Changed base class. Removed unnecessary code.
dmytrotsko Sep 26, 2023
105be04
Fix wrong import
dmytrotsko Sep 26, 2023
7bdf683
Replace unittest.TestCase with custom base class. Remove unnecessary …
dmytrotsko Sep 27, 2023
987d990
Final adjustments
dmytrotsko Sep 28, 2023
a2377f4
Fixed ght test
dmytrotsko Oct 2, 2023
9eb559c
revert to previous behavior, and mark deprecated ( not fully tested)
melange396 Oct 2, 2023
3f5e1a6
also revert tests
melange396 Oct 2, 2023
15e4987
Merge latest dev changes
rzats Oct 3, 2023
5f0b521
test_api_keys tweak
rzats Oct 3, 2023
cd665bd
Fix failing test
rzats Oct 3, 2023
65d1587
More test tweaks
rzats Oct 4, 2023
1f9bf19
Merge pull request #1295 from cmu-delphi/revert_deprecate_async_epidata
melange396 Oct 4, 2023
e701cb6
changelog update
melange396 Oct 5, 2023
fefc088
changelog update (formatting)
melange396 Oct 5, 2023
b527347
Remove public_route
rzats Oct 5, 2023
7e59806
Sync integration_tests with rzatserkovnyi/api-php-refactor
dmytrotsko Oct 11, 2023
c97adad
Removed unnecessary tests debugging options
dmytrotsko Oct 11, 2023
3dfdea6
Removed comment
dmytrotsko Oct 12, 2023
1677629
Moved params to the top of the test case
dmytrotsko Oct 12, 2023
6df302d
Merge branch 'dev' into integration_tests_refactoring
dmytrotsko Oct 12, 2023
f2fe087
Resolved conflict
dmytrotsko Oct 30, 2023
474e702
Merge branch 'dev' into integration_tests_refactoring
dmytrotsko Jan 11, 2024
b333c65
Merge branch 'dev' into integration_tests_refactoring
dmytrotsko Jan 11, 2024
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
108 changes: 18 additions & 90 deletions integrations/acquisition/covidcast/test_covidcast_meta_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@

# standard library
import json
import unittest

# third party
import mysql.connector
import requests

# first party
from delphi_utils import Nans
from delphi.epidata.client.delphi_epidata import Epidata
import delphi.operations.secrets as secrets
import delphi.epidata.acquisition.covidcast.database as live
from delphi.epidata.common.covidcast_test_base import CovidcastTestBase
from delphi.epidata.maintenance.covidcast_meta_cache_updater import main

# py3tester coverage target (equivalent to `import *`)
Expand All @@ -21,88 +14,26 @@
'covidcast_meta_cache_updater'
)

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


class CovidcastMetaCacheTests(unittest.TestCase):
class CovidcastMetaCacheTests(CovidcastTestBase):
"""Tests covidcast metadata caching."""

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

# connect to the `epidata` database
cnx = mysql.connector.connect(
user='user',
password='pass',
host='delphi_database_epidata',
database='covid')
cur = cnx.cursor()

# clear all tables
cur.execute("truncate table epimetric_load")
cur.execute("truncate table epimetric_full")
cur.execute("truncate table epimetric_latest")
cur.execute("truncate table geo_dim")
cur.execute("truncate table signal_dim")
# reset the `covidcast_meta_cache` table (it should always have one row)
cur.execute('update covidcast_meta_cache set timestamp = 0, epidata = "[]"')
cnx.commit()
cur.close()

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

# use the local instance of the epidata database
secrets.db.host = 'delphi_database_epidata'
secrets.db.epi = ('user', 'pass')

epidata_cnx = mysql.connector.connect(
user='user',
password='pass',
host='delphi_database_epidata',
database='epidata')
epidata_cur = epidata_cnx.cursor()

epidata_cur.execute("DELETE FROM `api_user`")
epidata_cur.execute('INSERT INTO `api_user`(`api_key`, `email`) VALUES("key", "email")')
epidata_cnx.commit()
epidata_cur.close()
epidata_cnx.close()

# use the local instance of the Epidata API
Epidata.BASE_URL = BASE_URL
Epidata.auth = ('epidata', 'key')

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

@staticmethod
def _make_request():
params = {'cached': 'true'}
response = requests.get(f"{Epidata.BASE_URL}/covidcast_meta", params=params, auth=Epidata.auth)
response.raise_for_status()
return response.json()

def test_caching(self):
"""Populate, query, cache, query, and verify the cache."""

# insert dummy data
self.cur.execute(f'''
self._db._cursor.execute('''
INSERT INTO `signal_dim` (`signal_key_id`, `source`, `signal`)
VALUES
(42, 'src', 'sig');
''')
self.cur.execute(f'''
self._db._cursor.execute('''
INSERT INTO `geo_dim` (`geo_key_id`, `geo_type`, `geo_value`)
VALUES
(96, 'state', 'pa'),
(96, 'state', 'pa'),
(97, 'state', 'wa');
''')
self.cur.execute(f'''
self._db._cursor.execute(f'''
INSERT INTO
`epimetric_latest` (`epimetric_id`, `signal_key_id`, `geo_key_id`, `time_type`,
`time_value`, `value_updated_timestamp`,
Expand All @@ -115,14 +46,11 @@ def test_caching(self):
(16, 42, 97, 'day', 20200422,
789, 1, 2, 3, 20200423, 1, {Nans.NOT_MISSING}, {Nans.NOT_MISSING}, {Nans.NOT_MISSING})
''')
self.cnx.commit()
self._db._connection.commit()

# make sure the live utility is serving something sensible
cvc_database = live.Database()
cvc_database.connect()
epidata1 = cvc_database.compute_covidcast_meta()
cvc_database.disconnect(False)
self.assertEqual(len(epidata1),1)
epidata1 = self._db.compute_covidcast_meta()
self.assertEqual(len(epidata1), 1)
self.assertEqual(epidata1, [
{
'data_source': 'src',
Expand All @@ -142,33 +70,33 @@ def test_caching(self):
'max_lag': 1,
}
])
epidata1={'result':1, 'message':'success', 'epidata':epidata1}
epidata1 = {'result': 1, 'message': 'success', 'epidata': epidata1}

# make sure the API covidcast_meta is still blank, since it only serves
# the cached version and we haven't cached anything yet
epidata2 = Epidata.covidcast_meta()
epidata2 = self.epidata_client.covidcast_meta()
self.assertEqual(epidata2['result'], -2, json.dumps(epidata2))

# update the cache
args = None
main(args)

# fetch the cached version
epidata3 = Epidata.covidcast_meta()
epidata3 = self.epidata_client.covidcast_meta()

# cached version should now equal live version
self.assertEqual(epidata1, epidata3)

# insert dummy data timestamped as of now
self.cur.execute('''
self._db._cursor.execute('''
update covidcast_meta_cache set
timestamp = UNIX_TIMESTAMP(NOW()),
epidata = '[{"hello": "world"}]'
''')
self.cnx.commit()
self._db._connection.commit()

# fetch the cached version (manually)
epidata4 = self._make_request()
epidata4 = self._make_request(endpoint="covidcast_meta", json=True, params={'cached': 'true'}, auth=self.epidata_client.auth, raise_for_status=True)

# make sure the cache was actually served
self.assertEqual(epidata4, {
Expand All @@ -180,15 +108,15 @@ def test_caching(self):
})

# insert dummy data timestamped as 2 hours old
self.cur.execute('''
self._db._cursor.execute('''
update covidcast_meta_cache set
timestamp = UNIX_TIMESTAMP(NOW()) - 3600 * 2,
epidata = '[{"hello": "world"}]'
''')
self.cnx.commit()
self._db._connection.commit()

# fetch the cached version (manually)
epidata5 = self._make_request()
epidata5 = self._make_request(endpoint="covidcast_meta", json=True, params={'cached': 'true'}, auth=self.epidata_client.auth, raise_for_status=True)

# make sure the cache was returned anyhow
self.assertEqual(epidata4, epidata5)
81 changes: 12 additions & 69 deletions integrations/acquisition/covidcast/test_csv_uploading.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,80 +3,23 @@
# standard library
from datetime import date
import os
import unittest
import argparse

# third party
import mysql.connector
import pandas as pd
import numpy as np

# first party
from delphi_utils import Nans
from delphi.epidata.client.delphi_epidata import Epidata
from delphi.epidata.acquisition.covidcast.csv_to_database import main, get_argument_parser
import delphi.operations.secrets as secrets
from delphi.epidata.common.covidcast_test_base import CovidcastTestBase

# py3tester coverage target (equivalent to `import *`)
__test_target__ = 'delphi.epidata.acquisition.covidcast.csv_to_database'


class CsvUploadingTests(unittest.TestCase):
class CsvUploadingTests(CovidcastTestBase):
"""Tests covidcast CSV uploading."""

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

# connect to the `epidata` database and clear the `covidcast` table
cnx = mysql.connector.connect(
user='user',
password='pass',
host='delphi_database_epidata',
database='covid')
cur = cnx.cursor()

# clear all tables
cur.execute("truncate table epimetric_load")
cur.execute("truncate table epimetric_full")
cur.execute("truncate table epimetric_latest")
cur.execute("truncate table geo_dim")
cur.execute("truncate table signal_dim")
# reset the `covidcast_meta_cache` table (it should always have one row)
cur.execute('update covidcast_meta_cache set timestamp = 0, epidata = "[]"')

cnx.commit()
cur.close()

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

# use the local instance of the epidata database
secrets.db.host = 'delphi_database_epidata'
secrets.db.epi = ('user', 'pass')

epidata_cnx = mysql.connector.connect(
user='user',
password='pass',
host='delphi_database_epidata',
database='epidata')
epidata_cur = epidata_cnx.cursor()

epidata_cur.execute("DELETE FROM `api_user`")
epidata_cur.execute('INSERT INTO `api_user`(`api_key`, `email`) VALUES("key", "email")')
epidata_cnx.commit()
epidata_cur.close()
epidata_cnx.close()

# use the local instance of the Epidata API
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata'
Epidata.auth = ('epidata', 'key')

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

@staticmethod
def apply_lag(expected_epidata):
expected_issue_day=date.today()
Expand All @@ -91,11 +34,11 @@ def apply_lag(expected_epidata):
return expected_epidata

def verify_timestamps_and_defaults(self):
self.cur.execute('''
self._db._cursor.execute('''
select value_updated_timestamp from epimetric_full
UNION ALL
select value_updated_timestamp from epimetric_latest''')
for (value_updated_timestamp,) in self.cur:
for (value_updated_timestamp,) in self._db._cursor:
self.assertGreater(value_updated_timestamp, 0)

def test_uploading(self):
Expand Down Expand Up @@ -130,16 +73,16 @@ def test_uploading(self):

# upload CSVs
main(args)
response = Epidata.covidcast('src-name', signal_name, 'day', 'state', 20200419, '*')
response = self.epidata_client.covidcast('src-name', signal_name, 'day', 'state', 20200419, '*')

expected_values = pd.concat([values, pd.DataFrame({ "geo_type": "state", "source": "src-name", "time_type": "day", "time_value": [20200419] * 3, "signal": [signal_name] * 3, "direction": [None] * 3})], axis=1).rename(columns=uploader_column_rename).to_dict(orient="records")
expected_values = pd.concat([values, pd.DataFrame({"geo_type": "state", "source": "src-name", "time_type": "day", "time_value": [20200419] * 3, "signal": [signal_name] * 3, "direction": [None] * 3})], axis=1).rename(columns=uploader_column_rename).to_dict(orient="records")
expected_response = {'result': 1, 'epidata': self.apply_lag(expected_values), 'message': 'success'}

self.assertEqual(response, expected_response)
self.verify_timestamps_and_defaults()

# Verify that files were archived
path = data_dir + f'/archive/successful/src-name/20200419_state_test.csv.gz'
path = data_dir + '/archive/successful/src-name/20200419_state_test.csv.gz'
self.assertIsNotNone(os.stat(path))

self.tearDown()
Expand All @@ -158,7 +101,7 @@ def test_uploading(self):

# upload CSVs
main(args)
response = Epidata.covidcast('src-name', signal_name, 'day', 'state', 20200419, '*')
response = self.epidata_client.covidcast('src-name', signal_name, 'day', 'state', 20200419, '*')

expected_values = pd.concat([values, pd.DataFrame({
"geo_type": "state",
Expand Down Expand Up @@ -195,7 +138,7 @@ def test_uploading(self):

# upload CSVs
main(args)
response = Epidata.covidcast('src-name', signal_name, 'day', 'state', 20200419, '*')
response = self.epidata_client.covidcast('src-name', signal_name, 'day', 'state', 20200419, '*')

expected_response = {'epidata': [], 'result': -2, 'message': 'no results'}

Expand All @@ -220,7 +163,7 @@ def test_uploading(self):

# upload CSVs
main(args)
response = Epidata.covidcast('src-name', signal_name, 'day', 'state', 20200419, '*')
response = self.epidata_client.covidcast('src-name', signal_name, 'day', 'state', 20200419, '*')

expected_values_df = pd.concat([values, pd.DataFrame({
"geo_type": "state",
Expand Down Expand Up @@ -256,7 +199,7 @@ def test_uploading(self):

# upload CSVs
main(args)
response = Epidata.covidcast('src-name', signal_name, 'day', 'state', 20200419, '*')
response = self.epidata_client.covidcast('src-name', signal_name, 'day', 'state', 20200419, '*')

expected_values = pd.concat([values, pd.DataFrame({
"geo_type": "state",
Expand Down Expand Up @@ -290,7 +233,7 @@ def test_uploading(self):

# upload CSVs
main(args)
response = Epidata.covidcast('src-name', signal_name, 'day', 'state', 20200419, '*')
response = self.epidata_client.covidcast('src-name', signal_name, 'day', 'state', 20200419, '*')

expected_response = {'epidata': [], 'result': -2, 'message': 'no results'}

Expand Down
4 changes: 2 additions & 2 deletions integrations/acquisition/covidcast/test_db.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from delphi_utils import Nans

from delphi.epidata.acquisition.covidcast.database import DBLoadStateException
from delphi.epidata.acquisition.covidcast.test_utils import CovidcastBase, CovidcastTestRow
from delphi.epidata.common.covidcast_test_base import CovidcastTestBase, CovidcastTestRow


# all the Nans we use here are just one value, so this is a shortcut to it:
nmv = Nans.NOT_MISSING.value

class TestTest(CovidcastBase):
class TestDatabase(CovidcastTestBase):

def _find_matches_for_row(self, row):
# finds (if existing) row from both history and latest views that matches long-key of provided CovidcastTestRow
Expand Down
Loading
Loading