Skip to content

Commit f7a279b

Browse files
authored
Merge pull request #1219 from cmu-delphi/ds/format-tests
style(tests): format many tests with black
2 parents 27bf48d + 36123e7 commit f7a279b

File tree

8 files changed

+257
-231
lines changed

8 files changed

+257
-231
lines changed

Diff for: src/common/covidcast_row.py

+31-13
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"value_updated_timestamp": "Int64",
2626
}
2727

28+
2829
@dataclass
2930
class CovidcastRow:
3031
"""A container for the values of a single covidcast database row.
@@ -35,7 +36,8 @@ class CovidcastRow:
3536
- converting from and to formats (dict, csv, df, kwargs)
3637
- creating consistent views, with consistent data types (dict, csv, df)
3738
38-
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.
39+
The rows are specified in 'v4_schema.sql'. The datatypes are made to match
40+
database. When writing to Pandas, the dtypes match the JIT model.py schema.
3941
"""
4042

4143
# Arguments.
@@ -54,15 +56,20 @@ class CovidcastRow:
5456
missing_sample_size: int
5557
issue: int
5658
lag: int
57-
# The following three fields are only the database, but are not ingested at acquisition and not returned by the API.
59+
# The following three fields are only in the database, but are not ingested at
60+
# acquisition and not returned by the API.
5861
epimetric_id: Optional[int] = None
5962
direction: Optional[int] = None
6063
value_updated_timestamp: Optional[int] = 0
6164

6265
# Classvars.
6366
_db_row_ignore_fields: ClassVar = []
6467
_api_row_ignore_fields: ClassVar = ["epimetric_id", "value_updated_timestamp"]
65-
_api_row_compatibility_ignore_fields: ClassVar = _api_row_ignore_fields + ["source", "time_type", "geo_type"]
68+
_api_row_compatibility_ignore_fields: ClassVar = _api_row_ignore_fields + [
69+
"source",
70+
"time_type",
71+
"geo_type",
72+
]
6673

6774
_pandas_dtypes: ClassVar = PANDAS_DTYPES
6875

@@ -72,17 +79,22 @@ def as_dict(self, ignore_fields: Optional[List[str]] = None) -> dict:
7279
for key in ignore_fields:
7380
del d[key]
7481
return d
75-
82+
7683
def as_api_row_dict(self, ignore_fields: Optional[List[str]] = None) -> dict:
77-
"""Returns a dict view into the row with the fields returned by the API server."""
84+
"""Returns a dict view into the row with the fields returned by the API
85+
server."""
7886
return self.as_dict(ignore_fields=self._api_row_ignore_fields + (ignore_fields or []))
7987

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

8495
def as_db_row_dict(self, ignore_fields: Optional[List[str]] = None) -> dict:
85-
"""Returns a dict view into the row with the fields returned by the database."""
96+
"""Returns a dict view into the row with the fields returned by the
97+
database."""
8698
return self.as_dict(ignore_fields=self._db_row_ignore_fields + (ignore_fields or []))
8799

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

94106
def as_api_row_df(self, ignore_fields: Optional[List[str]] = None) -> pd.DataFrame:
95-
"""Returns a dataframe view into the row with the fields returned by the API server."""
107+
"""Returns a dataframe view into the row with the fields returned by the
108+
API server."""
96109
return self.as_dataframe(ignore_fields=self._api_row_ignore_fields + (ignore_fields or []))
97110

111+
# fmt: off
98112
def as_api_compatibility_row_df(self, ignore_fields: Optional[List[str]] = None) -> pd.DataFrame:
99-
"""Returns a dataframe view into the row with the fields returned by the old API server (the PHP server)."""
100-
return self.as_dataframe(ignore_fields=self._api_row_compatibility_ignore_fields + (ignore_fields or []))
113+
"""Returns a dataframe view into the row with the fields returned by the
114+
old API server (the PHP server)."""
115+
# fmt: on
116+
return self.as_dataframe(
117+
ignore_fields=self._api_row_compatibility_ignore_fields + (ignore_fields or [])
118+
)
101119

102120
def as_db_row_df(self, ignore_fields: Optional[List[str]] = None) -> pd.DataFrame:
103-
"""Returns a dataframe view into the row with the fields returned by an all-field database query."""
121+
"""Returns a dataframe view into the row with the fields returned by an
122+
all-field database query."""
104123
return self.as_dataframe(ignore_fields=self._db_row_ignore_fields + (ignore_fields or []))
105124

106125
def signal_pair(self):
@@ -113,7 +132,6 @@ def time_pair(self):
113132
return f"{self.time_type}:{self.time_value}"
114133

115134

116-
117135
def check_valid_dtype(dtype):
118136
try:
119137
pd.api.types.pandas_dtype(dtype)

Diff for: src/common/logger.py

+6-11
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
import threading
55
import structlog
66

7+
78
def handle_exceptions(logger):
89
"""Handle exceptions using the provided logger."""
10+
911
def exception_handler(etype, value, traceback):
10-
logger.exception("Top-level exception occurred",
11-
exc_info=(etype, value, traceback))
12+
logger.exception("Top-level exception occurred", exc_info=(etype, value, traceback))
1213

1314
def multithread_exception_handler(args):
1415
exception_handler(args.exc_type, args.exc_value, args.exc_traceback)
@@ -17,9 +18,7 @@ def multithread_exception_handler(args):
1718
threading.excepthook = multithread_exception_handler
1819

1920

20-
def get_structured_logger(name=__name__,
21-
filename=None,
22-
log_exceptions=True):
21+
def get_structured_logger(name=__name__, filename=None, log_exceptions=True):
2322
"""Create a new structlog logger.
2423
2524
Use the logger returned from this in indicator code using the standard
@@ -49,11 +48,7 @@ def get_structured_logger(name=__name__,
4948
else:
5049
log_level = logging.INFO
5150

52-
logging.basicConfig(
53-
format="%(message)s",
54-
level=log_level,
55-
handlers=handlers
56-
)
51+
logging.basicConfig(format="%(message)s", level=log_level, handlers=handlers)
5752

5853
def add_pid(logger, method_name, event_dict):
5954
"""
@@ -85,7 +80,7 @@ def add_pid(logger, method_name, event_dict):
8580
# Decode unicode characters
8681
structlog.processors.UnicodeDecoder(),
8782
# Render as JSON
88-
structlog.processors.JSONRenderer()
83+
structlog.processors.JSONRenderer(),
8984
],
9085
# Use a dict class for keeping track of data.
9186
context_class=dict,

Diff for: tests/acquisition/covidcast_nowcast/test_load_sensors.py

+35-29
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,41 @@
1313
from delphi.epidata.acquisition.covidcast_nowcast.load_sensors import main, load_and_prepare_file
1414

1515
# py3tester coverage target
16-
__test_target__ = 'delphi.epidata.acquisition.covidcast_nowcast.load_sensors'
16+
__test_target__ = "delphi.epidata.acquisition.covidcast_nowcast.load_sensors"
1717

1818

1919
class UpdateTests(unittest.TestCase):
20-
21-
@mock.patch('time.time', mock.MagicMock(return_value=12345))
22-
def test_load_and_prepare_file(self):
23-
24-
test_attributes = PathDetails(
25-
20210102,
26-
3,
27-
"test_source",
28-
"test_signal",
29-
"test_time_type",
30-
20201231,
31-
"test_geo_type",
32-
)
33-
34-
test_df = load_and_prepare_file(StringIO("sensor_name,geo_value,value\ntestname,01001,1.5"), test_attributes)
35-
pd.testing.assert_frame_equal(test_df,
36-
pd.DataFrame({"sensor_name": ["testname"],
37-
"geo_value": ["01001"],
38-
"value": [1.5],
39-
"source": ["test_source"],
40-
"signal": ["test_signal"],
41-
"time_type": ["test_time_type"],
42-
"geo_type": ["test_geo_type"],
43-
"time_value": [20201231],
44-
"issue": [20210102],
45-
"lag": [3],
46-
"value_updated_timestamp": [12345]})
47-
)
20+
@mock.patch("time.time", mock.MagicMock(return_value=12345))
21+
def test_load_and_prepare_file(self):
22+
23+
test_attributes = PathDetails(
24+
20210102,
25+
3,
26+
"test_source",
27+
"test_signal",
28+
"test_time_type",
29+
20201231,
30+
"test_geo_type",
31+
)
32+
33+
test_df = load_and_prepare_file(
34+
StringIO("sensor_name,geo_value,value\ntestname,01001,1.5"), test_attributes
35+
)
36+
pd.testing.assert_frame_equal(
37+
test_df,
38+
pd.DataFrame(
39+
{
40+
"sensor_name": ["testname"],
41+
"geo_value": ["01001"],
42+
"value": [1.5],
43+
"source": ["test_source"],
44+
"signal": ["test_signal"],
45+
"time_type": ["test_time_type"],
46+
"geo_type": ["test_geo_type"],
47+
"time_value": [20201231],
48+
"issue": [20210102],
49+
"lag": [3],
50+
"value_updated_timestamp": [12345],
51+
}
52+
),
53+
)

Diff for: tests/acquisition/flusurv/test_flusurv.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,26 @@
88
from delphi.epidata.acquisition.flusurv.flusurv import fetch_json
99

1010
# py3tester coverage target
11-
__test_target__ = 'delphi.epidata.acquisition.flusurv.flusurv'
11+
__test_target__ = "delphi.epidata.acquisition.flusurv.flusurv"
1212

1313

1414
class FunctionTests(unittest.TestCase):
15-
"""Tests each function individually."""
15+
"""Tests each function individually."""
1616

17-
def test_fetch_json(self):
18-
"""Run through a successful flow."""
17+
def test_fetch_json(self):
18+
"""Run through a successful flow."""
1919

20-
path = 'path'
21-
payload = None
20+
path = "path"
21+
payload = None
2222

23-
response_object = MagicMock()
24-
response_object.status_code = 200
25-
response_object.headers = {'Content-Type': 'application/json'}
26-
response_object.json.return_value = sentinel.expected
23+
response_object = MagicMock()
24+
response_object.status_code = 200
25+
response_object.headers = {"Content-Type": "application/json"}
26+
response_object.json.return_value = sentinel.expected
2727

28-
requests_impl = MagicMock()
29-
requests_impl.get.return_value = response_object
28+
requests_impl = MagicMock()
29+
requests_impl.get.return_value = response_object
3030

31-
actual = fetch_json(path, payload, requests_impl=requests_impl)
31+
actual = fetch_json(path, payload, requests_impl=requests_impl)
3232

33-
self.assertEqual(actual, sentinel.expected)
33+
self.assertEqual(actual, sentinel.expected)

Diff for: tests/acquisition/flusurv/test_flusurv_update.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
import unittest
55

66
# py3tester coverage target
7-
__test_target__ = 'delphi.epidata.acquisition.flusurv.flusurv_update'
7+
__test_target__ = "delphi.epidata.acquisition.flusurv.flusurv_update"
88

99

1010
class FunctionTests(unittest.TestCase):
11-
"""Tests each function individually."""
11+
"""Tests each function individually."""
1212

13-
def test_syntax(self):
14-
"""This no-op test ensures that syntax is valid."""
15-
pass
13+
def test_syntax(self):
14+
"""This no-op test ensures that syntax is valid."""
15+
pass

0 commit comments

Comments
 (0)