1
0
mirror of https://github.com/searxng/searxng.git synced 2024-11-16 17:40:11 +01:00
searxng/tests/unit/test_utils.py

106 lines
3.6 KiB
Python
Raw Normal View History

2015-09-07 19:22:01 +02:00
# -*- coding: utf-8 -*-
2014-07-12 15:46:55 +02:00
import mock
2016-11-30 18:43:03 +01:00
import sys
2014-07-12 15:46:55 +02:00
from searx.testing import SearxTestCase
from searx import utils
2016-11-30 18:43:03 +01:00
if sys.version_info[0] == 3:
unicode = str
2014-07-12 15:46:55 +02:00
class TestUtils(SearxTestCase):
def test_gen_useragent(self):
self.assertIsInstance(utils.gen_useragent(), str)
self.assertIsNotNone(utils.gen_useragent())
self.assertTrue(utils.gen_useragent().startswith('Mozilla'))
2015-01-27 20:03:33 +01:00
def test_searx_useragent(self):
self.assertIsInstance(utils.searx_useragent(), str)
self.assertIsNotNone(utils.searx_useragent())
self.assertTrue(utils.searx_useragent().startswith('searx'))
2014-07-12 15:46:55 +02:00
def test_highlight_content(self):
self.assertEqual(utils.highlight_content(0, None), None)
self.assertEqual(utils.highlight_content(None, None), None)
self.assertEqual(utils.highlight_content('', None), None)
self.assertEqual(utils.highlight_content(False, None), None)
contents = [
'<html></html>'
'not<'
]
for content in contents:
self.assertEqual(utils.highlight_content(content, None), content)
content = 'a'
2016-11-30 18:43:03 +01:00
query = b'test'
2014-07-12 15:46:55 +02:00
self.assertEqual(utils.highlight_content(content, query), content)
2016-11-30 18:43:03 +01:00
query = b'a test'
2014-07-12 15:46:55 +02:00
self.assertEqual(utils.highlight_content(content, query), content)
2015-01-27 20:03:33 +01:00
def test_html_to_text(self):
html = """
<a href="/testlink" class="link_access_account">
<span class="toto">
<span>
<img src="test.jpg" />
</span>
</span>
<span class="titi">
Test text
</span>
</a>
"""
self.assertIsInstance(utils.html_to_text(html), unicode)
self.assertIsNotNone(utils.html_to_text(html))
self.assertEqual(utils.html_to_text(html), "Test text")
2015-09-07 19:22:01 +02:00
def test_prettify_url(self):
data = (('https://searx.me/', 'https://searx.me/'),
(u'https://searx.me/ű', u'https://searx.me/ű'),
('https://searx.me/' + (100 * 'a'), 'https://searx.me/[...]aaaaaaaaaaaaaaaaa'),
(u'https://searx.me/' + (100 * u'ű'), u'https://searx.me/[...]űűűűűűűűűűűűűűűűű'))
for test_url, expected in data:
self.assertEqual(utils.prettify_url(test_url, max_length=32), expected)
2014-07-12 15:46:55 +02:00
class TestHTMLTextExtractor(SearxTestCase):
def setUp(self):
self.html_text_extractor = utils.HTMLTextExtractor()
def test__init__(self):
self.assertEqual(self.html_text_extractor.result, [])
def test_handle_charref(self):
self.html_text_extractor.handle_charref('xF')
self.assertIn(u'\x0f', self.html_text_extractor.result)
self.html_text_extractor.handle_charref('XF')
self.assertIn(u'\x0f', self.html_text_extractor.result)
self.html_text_extractor.handle_charref('97')
self.assertIn(u'a', self.html_text_extractor.result)
def test_handle_entityref(self):
entity = 'test'
self.html_text_extractor.handle_entityref(entity)
self.assertIn(entity, self.html_text_extractor.result)
class TestUnicodeWriter(SearxTestCase):
def setUp(self):
self.unicode_writer = utils.UnicodeWriter(mock.MagicMock())
def test_write_row(self):
row = [1, 2, 3]
self.assertEqual(self.unicode_writer.writerow(row), None)
def test_write_rows(self):
self.unicode_writer.writerow = mock.MagicMock()
rows = [1, 2, 3]
self.unicode_writer.writerows(rows)
self.assertEqual(self.unicode_writer.writerow.call_count, len(rows))