Skip to content

Commit 3c44e26

Browse files
committed
tests(api): add more tests to improve coverage
1 parent ff75b50 commit 3c44e26

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

tests/api/encoding_test.py

+27
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import subprocess
44
import unittest
5+
from unittest import mock
56

67
from coveralls import Coveralls
78

@@ -54,3 +55,29 @@ def test_malformed_encoding_declaration_py3_or_coverage4():
5455
' return 1'
5556
)
5657
assert 'branches' not in result[0]
58+
59+
def test_debug_bad_encoding(self):
60+
"""Test debug_bad_encoding handles UnicodeDecodeError."""
61+
data = {
62+
'source_files': [
63+
{
64+
'name': 'bad_file.py',
65+
'source': 'def foo():\n return "foo"\n',
66+
'coverage': [1, 1, 1],
67+
}
68+
]
69+
}
70+
71+
# Save the original json.dumps function
72+
original_json_dumps = json.dumps
73+
74+
def mock_json_dumps(value):
75+
if value == 'def foo():\n return "foo"\n':
76+
raise UnicodeDecodeError('utf8', b'', 0, 1, 'bad data')
77+
else:
78+
return original_json_dumps(value)
79+
80+
with mock.patch('coveralls.api.json.dumps', side_effect=mock_json_dumps):
81+
with mock.patch('coveralls.api.log') as mock_log:
82+
Coveralls.debug_bad_encoding(data)
83+
mock_log.error.assert_called()

tests/api/reporter_test.py

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import subprocess
33
import unittest
4+
from unittest import mock
45

56
import pytest
67

@@ -267,3 +268,17 @@ def test_not_python(self):
267268
match=r"Couldn't parse .* as Python",
268269
):
269270
Coveralls(repo_token='xxx').get_coverage()
271+
272+
@mock.patch('requests.post')
273+
def test_submit_report_422_github(self, mock_post):
274+
"""Test submit_report handles 422 status code with github service_name."""
275+
response_mock = mock.Mock()
276+
response_mock.status_code = 422
277+
mock_post.return_value = response_mock
278+
279+
cov = Coveralls(repo_token='test_token', service_name='github')
280+
cov.config['service_name'] = 'github'
281+
282+
with mock.patch('builtins.print') as mock_print:
283+
cov.submit_report('{}')
284+
mock_print.assert_called()

0 commit comments

Comments
 (0)