Skip to content

Commit 6391fc9

Browse files
committed
Add more tests
1 parent 64e36a9 commit 6391fc9

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

tests/core/test_rp_responses.py

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,59 @@
2020
from reportportal_client.core.rp_responses import RPResponse, AsyncRPResponse
2121

2222

23+
class JSONDecodeError(ValueError):
24+
pass
25+
26+
2327
def json_error():
2428
raise json.JSONDecodeError('Expecting value: line 1 column 1 (char 0)', '<html />', 0)
2529

2630

31+
def custom_error():
32+
raise JSONDecodeError('Expecting value: line 1 column 1 (char 0)')
33+
34+
2735
@mock.patch('reportportal_client.core.rp_responses.logging.Logger.error')
28-
def test_json_decode_error(error_log):
36+
@pytest.mark.parametrize('ok, response_code, error_function, expected_message', [
37+
(False, 404, json_error, 'Unable to decode JSON response, got failed response with code "404" please check your '
38+
'endpoint configuration or API key'),
39+
(True, 200, json_error, 'Unable to decode JSON response, got passed response with code "200" please check your '
40+
'endpoint configuration or API key'),
41+
(True, 200, custom_error, 'Unable to decode JSON response, got passed response with code "200" please check your '
42+
'endpoint configuration or API key'),
43+
])
44+
def test_custom_decode_error(error_log, ok, response_code, error_function, expected_message):
2945
response = mock.Mock()
30-
response.ok = False
46+
response.ok = ok
3147
del response.status
32-
response.status_code = 404
33-
response.json.side_effect = json_error
48+
response.status_code = response_code
49+
response.json.side_effect = error_function
3450

3551
rp_response = RPResponse(response)
3652
assert rp_response.json is None
3753
error_log.assert_called_once()
38-
assert error_log.call_args_list[0][0][0] == ('Unable to decode JSON response, got failed response with code "404" '
39-
'please check your endpoint configuration or API key')
54+
assert error_log.call_args_list[0][0][0] == expected_message
4055

4156

4257
@pytest.mark.skipif(sys.version_info < (3, 8),
4358
reason='the test requires AsyncMock which was introduced in Python 3.8')
4459
@mock.patch('reportportal_client.core.rp_responses.logging.Logger.error')
4560
@pytest.mark.asyncio
46-
async def test_json_decode_error_async(error_log):
61+
@pytest.mark.parametrize('ok, response_code, error_function, expected_message', [
62+
(False, 404, json_error, 'Unable to decode JSON response, got failed response with code "404" please check your '
63+
'endpoint configuration or API key'),
64+
(True, 200, json_error, 'Unable to decode JSON response, got passed response with code "200" please check your '
65+
'endpoint configuration or API key'),
66+
(True, 200, custom_error, 'Unable to decode JSON response, got passed response with code "200" please check your '
67+
'endpoint configuration or API key'),
68+
])
69+
async def test_json_decode_error_async(error_log, ok, response_code, error_function, expected_message):
4770
response = mock.AsyncMock()
48-
response.ok = False
49-
response.status = 403
50-
response.json.side_effect = json_error
71+
response.ok = ok
72+
response.status = response_code
73+
response.json.side_effect = error_function
5174

5275
rp_response = AsyncRPResponse(response)
5376
assert await rp_response.json is None
5477
error_log.assert_called_once()
55-
assert error_log.call_args_list[0][0][0] == ('Unable to decode JSON response, got failed response with code "403" '
56-
'please check your endpoint configuration or API key')
78+
assert error_log.call_args_list[0][0][0] == expected_message

0 commit comments

Comments
 (0)