Skip to content

style(tests): format many tests with black #1219

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 7 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
22 changes: 16 additions & 6 deletions src/common/covidcast_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"value_updated_timestamp": "Int64",
}


@dataclass
class CovidcastRow:
"""A container for the values of a single covidcast database row.
Expand Down Expand Up @@ -62,7 +63,11 @@ class CovidcastRow:
# Classvars.
_db_row_ignore_fields: ClassVar = []
_api_row_ignore_fields: ClassVar = ["epimetric_id", "value_updated_timestamp"]
_api_row_compatibility_ignore_fields: ClassVar = _api_row_ignore_fields + ["source", "time_type", "geo_type"]
_api_row_compatibility_ignore_fields: ClassVar = _api_row_ignore_fields + [
"source",
"time_type",
"geo_type",
]

_pandas_dtypes: ClassVar = PANDAS_DTYPES

Expand All @@ -72,14 +77,16 @@ def as_dict(self, ignore_fields: Optional[List[str]] = None) -> dict:
for key in ignore_fields:
del d[key]
return d

def as_api_row_dict(self, ignore_fields: Optional[List[str]] = None) -> dict:
"""Returns a dict view into the row with the fields returned by the API server."""
return self.as_dict(ignore_fields=self._api_row_ignore_fields + (ignore_fields or []))

def as_api_compatibility_row_dict(self, ignore_fields: Optional[List[str]] = None) -> dict:
"""Returns a dict view into the row with the fields returned by the old API server (the PHP server)."""
return self.as_dict(ignore_fields=self._api_row_compatibility_ignore_fields + (ignore_fields or []))
return self.as_dict(
ignore_fields=self._api_row_compatibility_ignore_fields + (ignore_fields or [])
)

def as_db_row_dict(self, ignore_fields: Optional[List[str]] = None) -> dict:
"""Returns a dict view into the row with the fields returned by the database."""
Expand All @@ -95,9 +102,13 @@ def as_api_row_df(self, ignore_fields: Optional[List[str]] = None) -> pd.DataFra
"""Returns a dataframe view into the row with the fields returned by the API server."""
return self.as_dataframe(ignore_fields=self._api_row_ignore_fields + (ignore_fields or []))

def as_api_compatibility_row_df(self, ignore_fields: Optional[List[str]] = None) -> pd.DataFrame:
def as_api_compatibility_row_df(
self, ignore_fields: Optional[List[str]] = None
) -> pd.DataFrame:
"""Returns a dataframe view into the row with the fields returned by the old API server (the PHP server)."""
return self.as_dataframe(ignore_fields=self._api_row_compatibility_ignore_fields + (ignore_fields or []))
return self.as_dataframe(
ignore_fields=self._api_row_compatibility_ignore_fields + (ignore_fields or [])
)

def as_db_row_df(self, ignore_fields: Optional[List[str]] = None) -> pd.DataFrame:
"""Returns a dataframe view into the row with the fields returned by an all-field database query."""
Expand All @@ -113,7 +124,6 @@ def time_pair(self):
return f"{self.time_type}:{self.time_value}"



def check_valid_dtype(dtype):
try:
pd.api.types.pandas_dtype(dtype)
Expand Down
17 changes: 6 additions & 11 deletions src/common/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
import threading
import structlog


def handle_exceptions(logger):
"""Handle exceptions using the provided logger."""

def exception_handler(etype, value, traceback):
logger.exception("Top-level exception occurred",
exc_info=(etype, value, traceback))
logger.exception("Top-level exception occurred", exc_info=(etype, value, traceback))

def multithread_exception_handler(args):
exception_handler(args.exc_type, args.exc_value, args.exc_traceback)
Expand All @@ -17,9 +18,7 @@ def multithread_exception_handler(args):
threading.excepthook = multithread_exception_handler


def get_structured_logger(name=__name__,
filename=None,
log_exceptions=True):
def get_structured_logger(name=__name__, filename=None, log_exceptions=True):
"""Create a new structlog logger.

Use the logger returned from this in indicator code using the standard
Expand Down Expand Up @@ -49,11 +48,7 @@ def get_structured_logger(name=__name__,
else:
log_level = logging.INFO

logging.basicConfig(
format="%(message)s",
level=log_level,
handlers=handlers
)
logging.basicConfig(format="%(message)s", level=log_level, handlers=handlers)

def add_pid(logger, method_name, event_dict):
"""
Expand Down Expand Up @@ -85,7 +80,7 @@ def add_pid(logger, method_name, event_dict):
# Decode unicode characters
structlog.processors.UnicodeDecoder(),
# Render as JSON
structlog.processors.JSONRenderer()
structlog.processors.JSONRenderer(),
],
# Use a dict class for keeping track of data.
context_class=dict,
Expand Down
64 changes: 35 additions & 29 deletions tests/acquisition/covidcast_nowcast/test_load_sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,41 @@
from delphi.epidata.acquisition.covidcast_nowcast.load_sensors import main, load_and_prepare_file

# py3tester coverage target
__test_target__ = 'delphi.epidata.acquisition.covidcast_nowcast.load_sensors'
__test_target__ = "delphi.epidata.acquisition.covidcast_nowcast.load_sensors"


class UpdateTests(unittest.TestCase):

@mock.patch('time.time', mock.MagicMock(return_value=12345))
def test_load_and_prepare_file(self):

test_attributes = PathDetails(
20210102,
3,
"test_source",
"test_signal",
"test_time_type",
20201231,
"test_geo_type",
)

test_df = load_and_prepare_file(StringIO("sensor_name,geo_value,value\ntestname,01001,1.5"), test_attributes)
pd.testing.assert_frame_equal(test_df,
pd.DataFrame({"sensor_name": ["testname"],
"geo_value": ["01001"],
"value": [1.5],
"source": ["test_source"],
"signal": ["test_signal"],
"time_type": ["test_time_type"],
"geo_type": ["test_geo_type"],
"time_value": [20201231],
"issue": [20210102],
"lag": [3],
"value_updated_timestamp": [12345]})
)
@mock.patch("time.time", mock.MagicMock(return_value=12345))
def test_load_and_prepare_file(self):

test_attributes = PathDetails(
20210102,
3,
"test_source",
"test_signal",
"test_time_type",
20201231,
"test_geo_type",
)

test_df = load_and_prepare_file(
StringIO("sensor_name,geo_value,value\ntestname,01001,1.5"), test_attributes
)
pd.testing.assert_frame_equal(
test_df,
pd.DataFrame(
{
"sensor_name": ["testname"],
"geo_value": ["01001"],
"value": [1.5],
"source": ["test_source"],
"signal": ["test_signal"],
"time_type": ["test_time_type"],
"geo_type": ["test_geo_type"],
"time_value": [20201231],
"issue": [20210102],
"lag": [3],
"value_updated_timestamp": [12345],
}
),
)
28 changes: 14 additions & 14 deletions tests/acquisition/flusurv/test_flusurv.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@
from delphi.epidata.acquisition.flusurv.flusurv import fetch_json

# py3tester coverage target
__test_target__ = 'delphi.epidata.acquisition.flusurv.flusurv'
__test_target__ = "delphi.epidata.acquisition.flusurv.flusurv"


class FunctionTests(unittest.TestCase):
"""Tests each function individually."""
"""Tests each function individually."""

def test_fetch_json(self):
"""Run through a successful flow."""
def test_fetch_json(self):
"""Run through a successful flow."""

path = 'path'
payload = None
path = "path"
payload = None

response_object = MagicMock()
response_object.status_code = 200
response_object.headers = {'Content-Type': 'application/json'}
response_object.json.return_value = sentinel.expected
response_object = MagicMock()
response_object.status_code = 200
response_object.headers = {"Content-Type": "application/json"}
response_object.json.return_value = sentinel.expected

requests_impl = MagicMock()
requests_impl.get.return_value = response_object
requests_impl = MagicMock()
requests_impl.get.return_value = response_object

actual = fetch_json(path, payload, requests_impl=requests_impl)
actual = fetch_json(path, payload, requests_impl=requests_impl)

self.assertEqual(actual, sentinel.expected)
self.assertEqual(actual, sentinel.expected)
10 changes: 5 additions & 5 deletions tests/acquisition/flusurv/test_flusurv_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import unittest

# py3tester coverage target
__test_target__ = 'delphi.epidata.acquisition.flusurv.flusurv_update'
__test_target__ = "delphi.epidata.acquisition.flusurv.flusurv_update"


class FunctionTests(unittest.TestCase):
"""Tests each function individually."""
"""Tests each function individually."""

def test_syntax(self):
"""This no-op test ensures that syntax is valid."""
pass
def test_syntax(self):
"""This no-op test ensures that syntax is valid."""
pass
Loading