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

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

Merged
merged 7 commits into from
Jul 10, 2023
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
44 changes: 31 additions & 13 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 All @@ -35,7 +36,8 @@ class CovidcastRow:
- converting from and to formats (dict, csv, df, kwargs)
- creating consistent views, with consistent data types (dict, csv, df)

The rows are specified in 'v4_schema.sql'. The datatypes are made to match database. When writing to Pandas, the dtypes match the JIT model.py schema.
The rows are specified in 'v4_schema.sql'. The datatypes are made to match
database. When writing to Pandas, the dtypes match the JIT model.py schema.
"""

# Arguments.
Expand All @@ -54,15 +56,20 @@ class CovidcastRow:
missing_sample_size: int
issue: int
lag: int
# The following three fields are only the database, but are not ingested at acquisition and not returned by the API.
# The following three fields are only in the database, but are not ingested at
# acquisition and not returned by the API.
epimetric_id: Optional[int] = None
direction: Optional[int] = None
value_updated_timestamp: Optional[int] = 0

# 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,17 +79,22 @@ 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."""
"""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 []))
"""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 [])
)

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."""
"""Returns a dict view into the row with the fields returned by the
database."""
return self.as_dict(ignore_fields=self._db_row_ignore_fields + (ignore_fields or []))

def as_dataframe(self, ignore_fields: Optional[List[str]] = None) -> pd.DataFrame:
Expand All @@ -92,15 +104,22 @@ def as_dataframe(self, ignore_fields: Optional[List[str]] = None) -> pd.DataFram
return df

def as_api_row_df(self, ignore_fields: Optional[List[str]] = None) -> pd.DataFrame:
"""Returns a dataframe view into the row with the fields returned by the API server."""
"""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 []))

# fmt: off
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 []))
"""Returns a dataframe view into the row with the fields returned by the
old API server (the PHP server)."""
# fmt: on
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."""
"""Returns a dataframe view into the row with the fields returned by an
all-field database query."""
return self.as_dataframe(ignore_fields=self._db_row_ignore_fields + (ignore_fields or []))

def signal_pair(self):
Expand All @@ -113,7 +132,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