Skip to content

Commit 5d60ebf

Browse files
test(api): add more tests to improve coverage
-- Co-Authored-By: Kevin James <KevinJames@thekev.in>
1 parent cbf4e18 commit 5d60ebf

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

tests/api/encoding_test.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
import pathlib
44
import subprocess
5-
import unittest
5+
import unittest.mock
66

77
from coveralls import Coveralls
88

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

tests/api/reporter_test.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pathlib
44
import subprocess
55
import textwrap
6-
import unittest
6+
import unittest.mock
77

88
import pytest
99

@@ -253,3 +253,17 @@ def test_not_python(self):
253253
match=r"Couldn't parse .* as Python",
254254
):
255255
Coveralls(repo_token='xxx').get_coverage()
256+
257+
@unittest.mock.patch('requests.post')
258+
def test_submit_report_422_github(self, mock_post):
259+
response_mock = unittest.mock.Mock()
260+
response_mock.status_code = 422
261+
mock_post.return_value = response_mock
262+
263+
cov = Coveralls(repo_token='test_token', service_name='github')
264+
cov.config['service_name'] = 'github'
265+
266+
with unittest.mock.patch('builtins.print') as mock_print:
267+
cov.submit_report('{}')
268+
mock_print.assert_called()
269+
assert '422' in mock_print.call_args_list[0][0][0]

0 commit comments

Comments
 (0)