From 8af37c736ba57ed3aff7b0959300ff7e664048d6 Mon Sep 17 00:00:00 2001 From: Adrien Barbaresi Date: Tue, 25 Jan 2022 15:48:30 +0100 Subject: [PATCH] tests: improve coverage --- .coveragerc | 1 + tests/downloads_tests.py | 17 +++++++++++++---- tests/unit_tests.py | 2 +- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/.coveragerc b/.coveragerc index f4163257..ac4a77f9 100644 --- a/.coveragerc +++ b/.coveragerc @@ -10,5 +10,6 @@ omit = exclude_lines = pragma: no cover if __name__ == .__main__.: + except .*ImportError.*: except .*UnicodeDecodeError.*: except .*urllib3.exceptions.*: diff --git a/tests/downloads_tests.py b/tests/downloads_tests.py index 28644316..ea6f1c15 100644 --- a/tests/downloads_tests.py +++ b/tests/downloads_tests.py @@ -51,16 +51,20 @@ def test_fetch(): else: assert fetch_url('1234') == '' assert fetch_url('https://httpbin.org/status/404') is None + # empty request? + #assert _send_request('') is None # test if the fonctions default to no_ssl assert _send_request('https://expired.badssl.com/', False, DEFAULT_CONFIG) is not None if pycurl is not None: assert _send_pycurl_request('https://expired.badssl.com/', False, DEFAULT_CONFIG) is not None # no SSL, no decoding + url = 'https://httpbin.org/status/200' response = _send_request('https://httpbin.org/status/200', True, DEFAULT_CONFIG) assert response.data == b'' if pycurl is not None: - response = _send_pycurl_request('https://httpbin.org/status/200', True, DEFAULT_CONFIG) - assert response.data == b'' + response1 = _send_pycurl_request('https://httpbin.org/status/200', True, DEFAULT_CONFIG) + assert _handle_response(url, response1, False, DEFAULT_CONFIG) == _handle_response(url, response, False, DEFAULT_CONFIG) + assert _handle_response(url, response1, True, DEFAULT_CONFIG) == _handle_response(url, response, True, DEFAULT_CONFIG) # response object url = 'https://httpbin.org/encoding/utf8' response = _send_request(url, False, DEFAULT_CONFIG) @@ -68,8 +72,13 @@ def test_fetch(): assert myobject.data.startswith(b'

Unicode Demo

') # too large response object mock = Mock() - mock.data = (b' '*10000000) - assert _handle_response(url, mock, False, DEFAULT_CONFIG) is None + mock.status = 200 + # too large + mock.data = (b'ABC'*10000000) + assert _handle_response(url, mock, False, DEFAULT_CONFIG) == '' + # too small + mock.data = (b'ABC') + assert _handle_response(url, mock, False, DEFAULT_CONFIG) == '' # straight handling of response object assert load_html(response) is not None # nothing to see here diff --git a/tests/unit_tests.py b/tests/unit_tests.py index 517f0e79..0ac21504 100644 --- a/tests/unit_tests.py +++ b/tests/unit_tests.py @@ -240,7 +240,7 @@ def test_lrucache(): def test_formatting(): '''Test HTML formatting conversion and extraction''' # trailing - my_document = html.fromstring('

This here is the text.


') + my_document = html.fromstring('

This here is the text.

') my_result = extract(my_document, output_format='xml', config=ZERO_CONFIG) assert 'lb' not in my_result