From 0c16687ad18445b4c0e0bd6565159bbd8dd1c6f6 Mon Sep 17 00:00:00 2001 From: Venu Vardhan Reddy Tekula Date: Fri, 13 Sep 2024 18:20:49 -0400 Subject: [PATCH] test(api): add more tests to improve coverage --- tests/api/encoding_test.py | 29 +++++++++++++++++++++++++++++ tests/api/reporter_test.py | 14 ++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/tests/api/encoding_test.py b/tests/api/encoding_test.py index fd70d511..2e99f0bb 100644 --- a/tests/api/encoding_test.py +++ b/tests/api/encoding_test.py @@ -2,6 +2,7 @@ import os import subprocess import unittest +from unittest import mock from coveralls import Coveralls @@ -54,3 +55,31 @@ def test_malformed_encoding_declaration_py3_or_coverage4(): ' return 1' ) assert 'branches' not in result[0] + + def test_debug_bad_encoding(self): + data = { + 'source_files': [ + { + 'name': 'bad_file.py', + 'source': 'def foo():\n return "foo"\n', + 'coverage': [1, 1, 1], + }, + ], + } + + # Save the original json.dumps function + original_json_dumps = json.dumps + + def mock_json_dumps(value): + if value == 'def foo():\n return "foo"\n': + raise UnicodeDecodeError('utf8', b'', 0, 1, 'bad data') + + return original_json_dumps(value) + + with mock.patch( + 'coveralls.api.json.dumps', + side_effect=mock_json_dumps, + ): + with mock.patch('coveralls.api.log') as mock_log: + Coveralls.debug_bad_encoding(data) + mock_log.error.assert_called() diff --git a/tests/api/reporter_test.py b/tests/api/reporter_test.py index 60e0016a..7c861916 100644 --- a/tests/api/reporter_test.py +++ b/tests/api/reporter_test.py @@ -1,6 +1,7 @@ import os import subprocess import unittest +from unittest import mock import pytest @@ -267,3 +268,16 @@ def test_not_python(self): match=r"Couldn't parse .* as Python", ): Coveralls(repo_token='xxx').get_coverage() + + @mock.patch('requests.post') + def test_submit_report_422_github(self, mock_post): + response_mock = mock.Mock() + response_mock.status_code = 422 + mock_post.return_value = response_mock + + cov = Coveralls(repo_token='test_token', service_name='github') + cov.config['service_name'] = 'github' + + with mock.patch('builtins.print') as mock_print: + cov.submit_report('{}') + mock_print.assert_called()