Skip to content

Commit 71e2190

Browse files
committed
Avoid mock.patch decorators
Using these as decorators causes a lot of problems when combined with Pytest fixtures, often resulting in the mock not applying.
1 parent 99ea07e commit 71e2190

File tree

2 files changed

+55
-56
lines changed

2 files changed

+55
-56
lines changed

tests/test_jhu.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@
1010

1111

1212
@pytest.mark.asyncio
13-
@mock.patch("app.services.location.jhu.datetime")
14-
async def test_get_locations(mock_datetime, mock_client_session):
15-
mock_datetime.utcnow.return_value.isoformat.return_value = DATETIME_STRING
16-
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
17-
18-
output = await jhu.get_locations()
13+
async def test_get_locations(mock_client_session):
14+
with mock.patch("app.services.location.jhu.datetime") as mock_datetime:
15+
mock_datetime.utcnow.return_value.isoformat.return_value = DATETIME_STRING
16+
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
17+
output = await jhu.get_locations()
1918

2019
assert isinstance(output, list)
2120
assert isinstance(output[0], location.Location)

tests/test_routes.py

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -31,82 +31,81 @@ def read_file_v1(self, state):
3131
expected_json_output = file.read()
3232
return expected_json_output
3333

34-
@mock.patch("app.services.location.jhu.datetime")
35-
async def test_root_api(self, mock_datetime):
34+
async def test_root_api(self):
3635
"""Validate that / returns a 200 and is not a redirect."""
3736
response = await self.asgi_client.get("/")
3837

3938
assert response.status_code == 200
4039
assert not response.is_redirect
4140

42-
@mock.patch("app.services.location.jhu.datetime")
43-
async def test_v1_confirmed(self, mock_datetime):
44-
mock_datetime.utcnow.return_value.isoformat.return_value = self.date
45-
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
46-
41+
async def test_v1_confirmed(self):
4742
state = "confirmed"
4843
expected_json_output = self.read_file_v1(state=state)
49-
response = await self.asgi_client.get("/{}".format(state))
50-
return_data = response.json()
5144

52-
assert return_data == json.loads(expected_json_output)
45+
with mock.patch("app.services.location.jhu.datetime") as mock_datetime:
46+
mock_datetime.utcnow.return_value.isoformat.return_value = self.date
47+
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
48+
response = await self.asgi_client.get("/{}".format(state))
5349

54-
@mock.patch("app.services.location.jhu.datetime")
55-
async def test_v1_deaths(self, mock_datetime):
56-
mock_datetime.utcnow.return_value.isoformat.return_value = self.date
57-
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
50+
return_data = response.json()
51+
assert return_data == json.loads(expected_json_output)
5852

53+
async def test_v1_deaths(self):
5954
state = "deaths"
6055
expected_json_output = self.read_file_v1(state=state)
61-
response = await self.asgi_client.get("/{}".format(state))
62-
return_data = response.json()
6356

64-
assert return_data == json.loads(expected_json_output)
57+
with mock.patch("app.services.location.jhu.datetime") as mock_datetime:
58+
mock_datetime.utcnow.return_value.isoformat.return_value = self.date
59+
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
60+
response = await self.asgi_client.get("/{}".format(state))
6561

66-
@mock.patch("app.services.location.jhu.datetime")
67-
async def test_v1_recovered(self, mock_datetime):
68-
mock_datetime.utcnow.return_value.isoformat.return_value = self.date
69-
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
62+
return_data = response.json()
63+
assert return_data == json.loads(expected_json_output)
7064

65+
async def test_v1_recovered(self):
7166
state = "recovered"
7267
expected_json_output = self.read_file_v1(state=state)
73-
response = await self.asgi_client.get("/{}".format(state))
74-
return_data = response.json()
7568

76-
assert return_data == json.loads(expected_json_output)
69+
with mock.patch("app.services.location.jhu.datetime") as mock_datetime:
70+
mock_datetime.utcnow.return_value.isoformat.return_value = self.date
71+
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
72+
response = await self.asgi_client.get("/{}".format(state))
7773

78-
@mock.patch("app.services.location.jhu.datetime")
79-
async def test_v1_all(self, mock_datetime):
80-
mock_datetime.utcnow.return_value.isoformat.return_value = self.date
81-
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
74+
return_data = response.json()
75+
assert return_data == json.loads(expected_json_output)
8276

77+
async def test_v1_all(self):
8378
state = "all"
8479
expected_json_output = self.read_file_v1(state=state)
85-
response = await self.asgi_client.get("/{}".format(state))
86-
return_data = response.json()
8780

88-
assert return_data == json.loads(expected_json_output)
81+
with mock.patch("app.services.location.jhu.datetime") as mock_datetime:
82+
mock_datetime.utcnow.return_value.isoformat.return_value = self.date
83+
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
84+
response = await self.asgi_client.get("/{}".format(state))
8985

90-
@mock.patch("app.services.location.jhu.datetime")
91-
async def test_v2_latest(self, mock_datetime):
92-
mock_datetime.utcnow.return_value.isoformat.return_value = DATETIME_STRING
93-
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
86+
return_data = response.json()
87+
assert return_data == json.loads(expected_json_output)
9488

89+
async def test_v2_latest(self):
9590
state = "latest"
96-
response = await self.asgi_client.get(f"/v2/{state}")
97-
return_data = response.json()
9891

99-
check_dict = {"latest": {"confirmed": 1940, "deaths": 1940, "recovered": 0}}
92+
with mock.patch("app.services.location.jhu.datetime") as mock_datetime:
93+
mock_datetime.utcnow.return_value.isoformat.return_value = DATETIME_STRING
94+
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
95+
response = await self.asgi_client.get(f"/v2/{state}")
10096

97+
return_data = response.json()
98+
check_dict = {"latest": {"confirmed": 1940, "deaths": 1940, "recovered": 0}}
10199
assert return_data == check_dict
102100

103-
@mock.patch("app.services.location.jhu.datetime")
104-
async def test_v2_locations(self, mock_datetime):
105-
mock_datetime.utcnow.return_value.isoformat.return_value = DATETIME_STRING
106-
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
107-
101+
async def test_v2_locations(self):
108102
state = "locations"
109-
response = await self.asgi_client.get("/v2/{}".format(state))
103+
104+
with mock.patch("app.services.location.jhu.datetime") as mock_datetime:
105+
mock_datetime.utcnow.return_value.isoformat.return_value = DATETIME_STRING
106+
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
107+
response = await self.asgi_client.get("/v2/{}".format(state))
108+
110109
return_data = response.json()
111110

112111
filepath = "tests/expected_output/v2_{state}.json".format(state=state)
@@ -116,14 +115,15 @@ async def test_v2_locations(self, mock_datetime):
116115
# TODO: Why is this failing?
117116
# assert return_data == json.loads(expected_json_output)
118117

119-
@mock.patch("app.services.location.jhu.datetime")
120-
async def test_v2_locations_id(self, mock_datetime):
121-
mock_datetime.utcnow.return_value.isoformat.return_value = DATETIME_STRING
122-
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
123-
118+
async def test_v2_locations_id(self):
124119
state = "locations"
125120
test_id = 1
126-
response = await self.asgi_client.get("/v2/{}/{}".format(state, test_id))
121+
122+
with mock.patch("app.services.location.jhu.datetime") as mock_datetime:
123+
mock_datetime.utcnow.return_value.isoformat.return_value = DATETIME_STRING
124+
mock_datetime.strptime.side_effect = mocked_strptime_isoformat
125+
response = await self.asgi_client.get("/v2/{}/{}".format(state, test_id))
126+
127127
return_data = response.json()
128128

129129
filepath = "tests/expected_output/v2_{state}_id_{test_id}.json".format(state=state, test_id=test_id)

0 commit comments

Comments
 (0)