Skip to content

Commit ed2b4c4

Browse files
Raise on unsuccessful scheduled task listings.
1 parent 10420b7 commit ed2b4c4

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

‎pythonanywhere_core/schedule.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,15 @@ def delete(self, task_id: int) -> Literal[True]:
6969
def get_list(self) -> List[dict]:
7070
"""Gets list of existing scheduled tasks.
7171
72-
:returns: list of existing scheduled tasks specs"""
72+
:returns: list of existing scheduled tasks specs on HTTP 200
73+
:raises PythonAnywhereApiException: if the response is not HTTP 200"""
7374

74-
return call_api(self.base_url, "GET").json()
75+
result = call_api(self.base_url, "GET")
76+
if result.status_code != 200:
77+
raise PythonAnywhereApiException(
78+
f"GET scheduled tasks via API failed, got {result}: {result.text}"
79+
)
80+
return result.json()
7581

7682
def get_specs(self, task_id: int) -> dict:
7783
"""Get task specs by id.

‎tests/test_schedule.py‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,36 @@ def test_raises_because_attempt_to_get_nonexisting_task(api_token, api_responses
174174
assert str(e.value) == expected_error_msg
175175

176176

177-
def test_returns_tasks_list(api_token, api_responses, task_base_url):
178-
fake_specs = [{"fake": "specs"}, {"and": "more"}]
177+
@pytest.mark.parametrize("fake_specs", [[], [{"fake": "specs"}, {"and": "more"}]])
178+
def test_returns_tasks_list(api_token, api_responses, task_base_url, fake_specs):
179179
api_responses.add(
180180
responses.GET, url=task_base_url, status=200, body=json.dumps(fake_specs),
181181
)
182182

183183
assert Schedule().get_list() == fake_specs
184184

185185

186+
@pytest.mark.parametrize("status_code, body", [
187+
(201, "[]"),
188+
(202, '{"status": "accepted"}'),
189+
(204, ""),
190+
(403, '{"detail": "Forbidden"}'),
191+
(429, '{"detail": "Throttled"}'),
192+
(500, '{"detail": "Server error"}'),
193+
(500, "Non-JSON server error"),
194+
])
195+
def test_list_raises_unless_response_is_200(
196+
api_token, api_responses, task_base_url, status_code, body
197+
):
198+
api_responses.add(responses.GET, task_base_url, status=status_code, body=body)
199+
200+
with pytest.raises(PythonAnywhereApiException) as exc:
201+
Schedule().get_list()
202+
203+
assert str(status_code) in str(exc.value)
204+
assert body in str(exc.value)
205+
206+
186207
def test_updates_daily_task(
187208
api_token, api_responses, task_specs, daily_task_params, task_base_url
188209
):

0 commit comments

Comments
 (0)