logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

test_utils.py (5169B)


  1. # -*- coding: utf-8 -*-
  2. import mock
  3. import sys
  4. from searx.testing import SearxTestCase
  5. from searx import utils
  6. if sys.version_info[0] == 3:
  7. unicode = str
  8. class TestUtils(SearxTestCase):
  9. def test_gen_useragent(self):
  10. self.assertIsInstance(utils.gen_useragent(), str)
  11. self.assertIsNotNone(utils.gen_useragent())
  12. self.assertTrue(utils.gen_useragent().startswith('Mozilla'))
  13. def test_searx_useragent(self):
  14. self.assertIsInstance(utils.searx_useragent(), str)
  15. self.assertIsNotNone(utils.searx_useragent())
  16. self.assertTrue(utils.searx_useragent().startswith('searx'))
  17. def test_highlight_content(self):
  18. self.assertEqual(utils.highlight_content(0, None), None)
  19. self.assertEqual(utils.highlight_content(None, None), None)
  20. self.assertEqual(utils.highlight_content('', None), None)
  21. self.assertEqual(utils.highlight_content(False, None), None)
  22. contents = [
  23. '<html></html>'
  24. 'not<'
  25. ]
  26. for content in contents:
  27. self.assertEqual(utils.highlight_content(content, None), content)
  28. content = 'a'
  29. query = b'test'
  30. self.assertEqual(utils.highlight_content(content, query), content)
  31. query = b'a test'
  32. self.assertEqual(utils.highlight_content(content, query), content)
  33. def test_html_to_text(self):
  34. html = """
  35. <a href="/testlink" class="link_access_account">
  36. <span class="toto">
  37. <span>
  38. <img src="test.jpg" />
  39. </span>
  40. </span>
  41. <span class="titi">
  42. Test text
  43. </span>
  44. </a>
  45. """
  46. self.assertIsInstance(utils.html_to_text(html), unicode)
  47. self.assertIsNotNone(utils.html_to_text(html))
  48. self.assertEqual(utils.html_to_text(html), "Test text")
  49. def test_prettify_url(self):
  50. data = (('https://searx.me/', 'https://searx.me/'),
  51. (u'https://searx.me/ű', u'https://searx.me/ű'),
  52. ('https://searx.me/' + (100 * 'a'), 'https://searx.me/[...]aaaaaaaaaaaaaaaaa'),
  53. (u'https://searx.me/' + (100 * u'ű'), u'https://searx.me/[...]űűűűűűűűűűűűűűűűű'))
  54. for test_url, expected in data:
  55. self.assertEqual(utils.prettify_url(test_url, max_length=32), expected)
  56. def test_match_language(self):
  57. self.assertEqual(utils.match_language('es', ['es']), 'es')
  58. self.assertEqual(utils.match_language('es', [], fallback='fallback'), 'fallback')
  59. self.assertEqual(utils.match_language('ja', ['jp'], {'ja': 'jp'}), 'jp')
  60. aliases = {'en-GB': 'en-UK', 'he': 'iw'}
  61. # guess country
  62. self.assertEqual(utils.match_language('de-DE', ['de']), 'de')
  63. self.assertEqual(utils.match_language('de', ['de-DE']), 'de-DE')
  64. self.assertEqual(utils.match_language('es-CO', ['es-AR', 'es-ES', 'es-MX']), 'es-ES')
  65. self.assertEqual(utils.match_language('es-CO', ['es-MX']), 'es-MX')
  66. self.assertEqual(utils.match_language('en-UK', ['en-AU', 'en-GB', 'en-US']), 'en-GB')
  67. self.assertEqual(utils.match_language('en-GB', ['en-AU', 'en-UK', 'en-US'], aliases), 'en-UK')
  68. # language aliases
  69. self.assertEqual(utils.match_language('iw', ['he']), 'he')
  70. self.assertEqual(utils.match_language('he', ['iw'], aliases), 'iw')
  71. self.assertEqual(utils.match_language('iw-IL', ['he']), 'he')
  72. self.assertEqual(utils.match_language('he-IL', ['iw'], aliases), 'iw')
  73. self.assertEqual(utils.match_language('iw', ['he-IL']), 'he-IL')
  74. self.assertEqual(utils.match_language('he', ['iw-IL'], aliases), 'iw-IL')
  75. self.assertEqual(utils.match_language('iw-IL', ['he-IL']), 'he-IL')
  76. self.assertEqual(utils.match_language('he-IL', ['iw-IL'], aliases), 'iw-IL')
  77. class TestHTMLTextExtractor(SearxTestCase):
  78. def setUp(self):
  79. self.html_text_extractor = utils.HTMLTextExtractor()
  80. def test__init__(self):
  81. self.assertEqual(self.html_text_extractor.result, [])
  82. def test_handle_charref(self):
  83. self.html_text_extractor.handle_charref('xF')
  84. self.assertIn(u'\x0f', self.html_text_extractor.result)
  85. self.html_text_extractor.handle_charref('XF')
  86. self.assertIn(u'\x0f', self.html_text_extractor.result)
  87. self.html_text_extractor.handle_charref('97')
  88. self.assertIn(u'a', self.html_text_extractor.result)
  89. def test_handle_entityref(self):
  90. entity = 'test'
  91. self.html_text_extractor.handle_entityref(entity)
  92. self.assertIn(entity, self.html_text_extractor.result)
  93. class TestUnicodeWriter(SearxTestCase):
  94. def setUp(self):
  95. self.unicode_writer = utils.UnicodeWriter(mock.MagicMock())
  96. def test_write_row(self):
  97. row = [1, 2, 3]
  98. self.assertEqual(self.unicode_writer.writerow(row), None)
  99. def test_write_rows(self):
  100. self.unicode_writer.writerow = mock.MagicMock()
  101. rows = [1, 2, 3]
  102. self.unicode_writer.writerows(rows)
  103. self.assertEqual(self.unicode_writer.writerow.call_count, len(rows))