# pylint:disable-msg=I1101,W1401 """ Unit tests for the trafilatura library. """ import logging import sys import time from copy import copy from os import path import pytest from lxml import etree, html try: from cchardet import detect except ImportError: from charset_normalizer import detect import trafilatura.htmlprocessing from trafilatura import bare_extraction, extract, extract_with_metadata, xml from trafilatura.core import Extractor from trafilatura.external import sanitize_tree, try_justext, try_readability from trafilatura.main_extractor import (handle_formatting, handle_image, handle_lists, handle_paragraphs, handle_quotes, handle_table, handle_textelem) from trafilatura.meta import reset_caches from trafilatura.metadata import Document from trafilatura.readability_lxml import is_probably_readerable from trafilatura.settings import DEFAULT_CONFIG, TAG_CATALOG, use_config from trafilatura.utils import (LANGID_FLAG, detect_encoding, is_dubious_html, is_image_file, language_classifier, load_html, normalize_unicode, repair_faulty_html, sanitize, textfilter, trim) logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) TEST_DIR = path.abspath(path.dirname(__file__)) RESOURCES_DIR = path.join(TEST_DIR, 'resources') SAMPLE_META = Document() ZERO_CONFIG = DEFAULT_CONFIG ZERO_CONFIG['DEFAULT']['MIN_OUTPUT_SIZE'] = '0' ZERO_CONFIG['DEFAULT']['MIN_EXTRACTED_SIZE'] = '0' NEW_CONFIG = use_config(filename=path.join(RESOURCES_DIR, 'newsettings.cfg')) MOCK_PAGES = { 'http://exotic_tags': 'exotic_tags.html', } DEFAULT_OPTIONS = Extractor() def load_mock_page(url, xml_flag=False, langcheck=None, tei_output=False): '''load mock page from samples''' try: with open(path.join(TEST_DIR, "resources", MOCK_PAGES[url]), "r", encoding="utf-8") as inputf: htmlstring = inputf.read() # encoding/windows fix for the tests except UnicodeDecodeError: # read as binary with open(path.join(TEST_DIR, "resources", MOCK_PAGES[url]), "rb") as inputf: htmlbinary = inputf.read() guessed_encoding = detect(htmlbinary)['encoding'] if guessed_encoding is not None: try: htmlstring = htmlbinary.decode(guessed_encoding) except UnicodeDecodeError: htmlstring = htmlbinary else: print('Encoding error') if xml_flag: output_format = 'xml' elif tei_output: output_format = 'xmltei' else: output_format = 'txt' return extract( htmlstring, url, record_id='0000', output_format=output_format, target_language=langcheck ) def test_trim(): '''test string trimming''' assert trim(' Test ') == 'Test' assert trim('\t\tTest Test\r\n') == 'Test Test' my_elem = etree.Element('body') my_elem.text = 'Test Text' assert textfilter(my_elem) is False # my_elem.text = 'Tags: Arbeit, Urlaub' my_elem.text = 'Instagram' assert textfilter(my_elem) is True my_elem.text = '\t\t' assert textfilter(my_elem) is True # sanitize logic assert sanitize(None) is None # non-breaking spaces print(sanitize('Test Text')) assert sanitize('Test Text') == 'Test Text' # clear cache # reset caches: examine_date_elements used above old_values = trim.cache_info() reset_caches() assert trim.cache_info() != old_values def test_input(): '''test if loaded strings/trees are handled properly''' teststring = "高山云雾出好茶".encode("utf-8") assert detect_encoding(teststring) == ["utf-8"] teststring = "高山云雾出好茶".encode("gb18030") assert "gb18030" in detect_encoding(teststring) assert "gb18030" in detect_encoding(teststring*1000) assert is_dubious_html("This is a string.") is True htmlstring = "\n" beginning = htmlstring[:50].lower() assert repair_faulty_html(htmlstring, beginning) == "\n" htmlstring = "\n" beginning = htmlstring[:50].lower() assert repair_faulty_html(htmlstring, beginning) == htmlstring htmlstring = "
\n" beginning = htmlstring[:50].lower() assert repair_faulty_html(htmlstring, beginning) == "\n" htmlstring = '\nFoo\n
\n
\n' beginning = htmlstring[:50].lower() assert ( repair_faulty_html(htmlstring, beginning) == '\n\n
\n
\n' ) htmlstring = '