test_external_bangs.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. from searx.external_bang import get_node, resolve_bang_definition, get_bang_url, get_bang_definition_and_autocomplete
  2. from searx.search import SearchQuery, EngineRef
  3. from searx.testing import SearxTestCase
  4. TEST_DB = {
  5. 'trie': {
  6. 'exam': {
  7. 'ple': '//example.com/' + chr(2) + chr(1) + '0',
  8. '*': '//wikipedia.org/wiki/' + chr(2) + chr(1) + '0',
  9. },
  10. 'sea': {
  11. '*': 'sea' + chr(2) + chr(1) + '0',
  12. 'rch': {
  13. '*': 'search' + chr(2) + chr(1) + '0',
  14. 'ing': 'searching' + chr(2) + chr(1) + '0',
  15. },
  16. 's': {
  17. 'on': 'season' + chr(2) + chr(1) + '0',
  18. 'capes': 'seascape' + chr(2) + chr(1) + '0',
  19. }
  20. },
  21. 'error': ['error in external_bangs.json']
  22. }
  23. }
  24. class TestGetNode(SearxTestCase):
  25. DB = {
  26. 'trie': {
  27. 'exam': {
  28. 'ple': 'test',
  29. '*': 'not used',
  30. }
  31. }
  32. }
  33. def test_found(self):
  34. node, before, after = get_node(TestGetNode.DB, 'example')
  35. self.assertEqual(node, 'test')
  36. self.assertEqual(before, 'example')
  37. self.assertEqual(after, '')
  38. def test_get_partial(self):
  39. node, before, after = get_node(TestGetNode.DB, 'examp')
  40. self.assertEqual(node, TestGetNode.DB['trie']['exam'])
  41. self.assertEqual(before, 'exam')
  42. self.assertEqual(after, 'p')
  43. def test_not_found(self):
  44. node, before, after = get_node(TestGetNode.DB, 'examples')
  45. self.assertEqual(node, 'test')
  46. self.assertEqual(before, 'example')
  47. self.assertEqual(after, 's')
  48. class TestResolveBangDefinition(SearxTestCase):
  49. def test_https(self):
  50. url, rank = resolve_bang_definition('//example.com/' + chr(2) + chr(1) + '42', 'query')
  51. self.assertEqual(url, 'https://example.com/query')
  52. self.assertEqual(rank, 42)
  53. def test_http(self):
  54. url, rank = resolve_bang_definition('http://example.com/' + chr(2) + chr(1) + '0', 'text')
  55. self.assertEqual(url, 'http://example.com/text')
  56. self.assertEqual(rank, 0)
  57. class TestGetBangDefinitionAndAutocomplete(SearxTestCase):
  58. def test_found(self):
  59. bang_definition, new_autocomplete = get_bang_definition_and_autocomplete('exam', external_bangs_db=TEST_DB)
  60. self.assertEqual(bang_definition, TEST_DB['trie']['exam']['*'])
  61. self.assertEqual(new_autocomplete, ['example'])
  62. def test_found_optimized(self):
  63. bang_definition, new_autocomplete = get_bang_definition_and_autocomplete('example', external_bangs_db=TEST_DB)
  64. self.assertEqual(bang_definition, TEST_DB['trie']['exam']['ple'])
  65. self.assertEqual(new_autocomplete, [])
  66. def test_partial(self):
  67. bang_definition, new_autocomplete = get_bang_definition_and_autocomplete('examp', external_bangs_db=TEST_DB)
  68. self.assertEqual(bang_definition, None)
  69. self.assertEqual(new_autocomplete, ['example'])
  70. def test_partial2(self):
  71. bang_definition, new_autocomplete = get_bang_definition_and_autocomplete('sea', external_bangs_db=TEST_DB)
  72. self.assertEqual(bang_definition, TEST_DB['trie']['sea']['*'])
  73. self.assertEqual(new_autocomplete, ['search', 'searching', 'seascapes', 'season'])
  74. def test_error(self):
  75. bang_definition, new_autocomplete = get_bang_definition_and_autocomplete('error', external_bangs_db=TEST_DB)
  76. self.assertEqual(bang_definition, None)
  77. self.assertEqual(new_autocomplete, [])
  78. def test_actual_data(self):
  79. bang_definition, new_autocomplete = get_bang_definition_and_autocomplete('duckduckgo')
  80. self.assertTrue(bang_definition.startswith('//duckduckgo.com/?q='))
  81. self.assertEqual(new_autocomplete, [])
  82. class TestExternalBangJson(SearxTestCase):
  83. def test_no_external_bang_query(self):
  84. result = get_bang_url(SearchQuery('test', engineref_list=[EngineRef('wikipedia', 'general')]))
  85. self.assertEqual(result, None)
  86. def test_get_bang_url(self):
  87. url = get_bang_url(SearchQuery('test', engineref_list=[], external_bang='example'), external_bangs_db=TEST_DB)
  88. self.assertEqual(url, 'https://example.com/test')
  89. def test_actual_data(self):
  90. google_url = get_bang_url(SearchQuery('test', engineref_list=[], external_bang='g'))
  91. self.assertEqual(google_url, 'https://www.google.com/search?q=test')