Skip to content

Commit 5625307

Browse files
committed
tests
1 parent 58a573b commit 5625307

File tree

3 files changed

+144
-166
lines changed

3 files changed

+144
-166
lines changed

runpod/serverless/modules/rp_job.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
Job related helpers.
33
"""
44

5-
import asyncio
65
import inspect
76
import json
87
import os

tests/test_serverless/test_modules/test_job.py

Lines changed: 56 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -13,165 +13,109 @@
1313

1414

1515
class TestJob(IsolatedAsyncioTestCase):
16-
"""Tests the Job class."""
16+
"""Tests for the get_job function."""
1717

1818
async def test_get_job_200(self):
19-
"""
20-
Tests the get_job function
21-
"""
19+
"""Tests the get_job function with a valid 200 response."""
2220
# Mock the 200 response
23-
response4 = Mock()
24-
response4.status = 200
25-
response4.json = make_mocked_coro(
21+
response = Mock(ClientResponse)
22+
response.status = 200
23+
response.content_type = "application/json"
24+
response.content_length = 50
25+
response.json = make_mocked_coro(
2626
return_value={"id": "123", "input": {"number": 1}}
2727
)
2828

2929
with patch("aiohttp.ClientSession") as mock_session, patch(
3030
"runpod.serverless.modules.rp_job.JOB_GET_URL", "http://mock.url"
3131
):
32-
33-
# Set side_effect to a list of mock responses
34-
mock_session.get.return_value.__aenter__.side_effect = [response4]
35-
32+
mock_session.get.return_value.__aenter__.return_value = response
3633
job = await rp_job.get_job(mock_session)
37-
3834
# Assertions for the success case
39-
assert job == [{"id": "123", "input": {"number": 1}}]
35+
self.assertEqual(job, [{"id": "123", "input": {"number": 1}}])
4036

4137
async def test_get_job_204(self):
42-
"""
43-
Tests the get_job function with a 204 response
44-
"""
45-
# 204 Mock
46-
response_204 = Mock()
47-
response_204.status = 204
48-
response_204.json = make_mocked_coro(return_value=None)
38+
"""Tests the get_job function with a 204 response."""
39+
# Mock 204 No Content response
40+
response = Mock(ClientResponse)
41+
response.status = 204
42+
response.content_type = "application/json"
43+
response.content_length = 0
4944

50-
with patch("aiohttp.ClientSession") as mock_session_204, patch(
45+
with patch("aiohttp.ClientSession") as mock_session, patch(
5146
"runpod.serverless.modules.rp_job.JOB_GET_URL", "http://mock.url"
5247
):
53-
54-
mock_session_204.get.return_value.__aenter__.return_value = response_204
55-
job = await rp_job.get_job(mock_session_204)
56-
57-
assert job is None
58-
assert mock_session_204.get.call_count == 1
48+
mock_session.get.return_value.__aenter__.return_value = response
49+
job = await rp_job.get_job(mock_session)
50+
self.assertIsNone(job)
51+
self.assertEqual(mock_session.get.call_count, 1)
5952

6053
async def test_get_job_400(self):
61-
"""
62-
Test the get_job function with a 400 response
63-
"""
64-
# 400 Mock
65-
response_400 = Mock(ClientResponse)
66-
response_400.status = 400
67-
68-
with patch("aiohttp.ClientSession") as mock_session_400, patch(
69-
"runpod.serverless.modules.rp_job.JOB_GET_URL", "http://mock.url"
70-
):
71-
72-
mock_session_400.get.return_value.__aenter__.return_value = response_400
73-
job = await rp_job.get_job(mock_session_400)
74-
75-
assert job is None
76-
77-
async def test_get_job_500(self):
78-
"""
79-
Tests the get_job function with a 500 response
80-
"""
81-
# 500 Mock
82-
response_500 = Mock(ClientResponse)
83-
response_500.status = 500
54+
"""Tests the get_job function with a 400 response."""
55+
# Mock 400 response
56+
response = Mock(ClientResponse)
57+
response.status = 400
8458

85-
with patch("aiohttp.ClientSession") as mock_session_500, patch(
59+
with patch("aiohttp.ClientSession") as mock_session, patch(
8660
"runpod.serverless.modules.rp_job.JOB_GET_URL", "http://mock.url"
8761
):
88-
89-
mock_session_500.get.return_value.__aenter__.return_value = response_500
90-
job = await rp_job.get_job(mock_session_500)
91-
92-
assert job is None
62+
mock_session.get.return_value.__aenter__.return_value = response
63+
job = await rp_job.get_job(mock_session)
64+
self.assertIsNone(job)
9365

9466
async def test_get_job_no_id(self):
95-
"""
96-
Tests the get_job function with a 200 response but no id
97-
"""
67+
"""Tests the get_job function with a 200 response but no 'id' field."""
9868
response = Mock(ClientResponse)
9969
response.status = 200
100-
response.json = make_mocked_coro(return_value={})
70+
response.content_type = "application/json"
71+
response.content_length = 50
72+
response.json = make_mocked_coro(return_value={"input": "foobar"})
10173

10274
with patch("aiohttp.ClientSession") as mock_session, patch(
103-
"runpod.serverless.modules.rp_job.log", new_callable=Mock
104-
) as mock_log, patch(
10575
"runpod.serverless.modules.rp_job.JOB_GET_URL", "http://mock.url"
10676
):
107-
10877
mock_session.get.return_value.__aenter__.return_value = response
78+
with self.assertRaises(Exception) as context:
79+
await rp_job.get_job(mock_session)
80+
self.assertEqual(str(context.exception), "Job has missing field(s): id or input.")
10981

110-
job = await rp_job.get_job(mock_session)
111-
112-
assert job == []
113-
assert mock_log.error.call_count == 1
114-
115-
async def test_get_job_no_input(self):
116-
"""
117-
Tests the get_job function with a 200 response but no input
118-
"""
82+
async def test_get_job_invalid_content_type(self):
83+
"""Tests the get_job function with an invalid content type."""
11984
response = Mock(ClientResponse)
12085
response.status = 200
121-
response.json = make_mocked_coro(return_value={"id": "123"})
86+
response.content_type = "text/html" # Invalid content type
87+
response.content_length = 50
12288

12389
with patch("aiohttp.ClientSession") as mock_session, patch(
124-
"runpod.serverless.modules.rp_job.log", new_callable=Mock
125-
) as mock_log, patch(
12690
"runpod.serverless.modules.rp_job.JOB_GET_URL", "http://mock.url"
12791
):
128-
12992
mock_session.get.return_value.__aenter__.return_value = response
130-
13193
job = await rp_job.get_job(mock_session)
94+
self.assertIsNone(job)
13295

133-
assert job == []
134-
assert mock_log.error.call_count == 1
135-
136-
async def test_get_job_no_timeout(self):
137-
"""Tests the get_job function with a timeout"""
138-
# Timeout Mock
139-
response_timeout = Mock(ClientResponse)
140-
response_timeout.status = 200
96+
async def test_get_job_empty_content(self):
97+
"""Tests the get_job function with an empty content response."""
98+
response = Mock(ClientResponse)
99+
response.status = 200
100+
response.content_type = "application/json"
101+
response.content_length = 0 # No content to parse
141102

142-
with patch("aiohttp.ClientSession") as mock_session_timeout, patch(
143-
"runpod.serverless.modules.rp_job.log", new_callable=Mock
144-
) as mock_log, patch(
103+
with patch("aiohttp.ClientSession") as mock_session, patch(
145104
"runpod.serverless.modules.rp_job.JOB_GET_URL", "http://mock.url"
146105
):
147-
148-
mock_session_timeout.get.return_value.__aenter__.side_effect = (
149-
asyncio.TimeoutError
150-
)
151-
job = await rp_job.get_job(mock_session_timeout)
152-
153-
assert job == []
154-
assert mock_log.error.call_count == 0
106+
mock_session.get.return_value.__aenter__.return_value = response
107+
job = await rp_job.get_job(mock_session)
108+
self.assertIsNone(job)
155109

156110
async def test_get_job_exception(self):
157-
"""
158-
Tests the get_job function with an exception
159-
"""
160-
# Exception Mock
161-
response_exception = Mock(ClientResponse)
162-
response_exception.status = 200
163-
164-
with patch("aiohttp.ClientSession") as mock_session_exception, patch(
165-
"runpod.serverless.modules.rp_job.log", new_callable=Mock
166-
) as mock_log, patch(
111+
"""Tests the get_job function with a raised exception."""
112+
with patch("aiohttp.ClientSession") as mock_session, patch(
167113
"runpod.serverless.modules.rp_job.JOB_GET_URL", "http://mock.url"
168114
):
169-
170-
mock_session_exception.get.return_value.__aenter__.side_effect = Exception
171-
job = await rp_job.get_job(mock_session_exception)
172-
173-
assert job == []
174-
assert mock_log.error.call_count == 1
115+
mock_session.get.return_value.__aenter__.side_effect = Exception("Unexpected error")
116+
with self.assertRaises(Exception) as context:
117+
await rp_job.get_job(mock_session)
118+
self.assertEqual(str(context.exception), "Unexpected error")
175119

176120

177121
class TestRunJob(IsolatedAsyncioTestCase):

0 commit comments

Comments
 (0)