test_external_bangs.py 4.7 KB

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