test_query.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # pylint: disable=missing-module-docstring,disable=missing-class-docstring,invalid-name
  3. from parameterized.parameterized import parameterized
  4. from searx.query import RawTextQuery
  5. from tests import SearxTestCase
  6. class TestQuery(SearxTestCase):
  7. def test_simple_query(self):
  8. query_text = 'the query'
  9. query = RawTextQuery(query_text, [])
  10. self.assertEqual(query.getFullQuery(), query_text)
  11. self.assertEqual(len(query.query_parts), 0)
  12. self.assertEqual(len(query.user_query_parts), 2)
  13. self.assertEqual(len(query.languages), 0)
  14. self.assertFalse(query.specific)
  15. def test_multiple_spaces_query(self):
  16. query_text = '\tthe query'
  17. query = RawTextQuery(query_text, [])
  18. self.assertEqual(query.getFullQuery(), 'the query')
  19. self.assertEqual(len(query.query_parts), 0)
  20. self.assertEqual(len(query.user_query_parts), 2)
  21. self.assertEqual(len(query.languages), 0)
  22. self.assertFalse(query.specific)
  23. def test_str_method(self):
  24. query_text = '<7 the query'
  25. query = RawTextQuery(query_text, [])
  26. self.assertEqual(str(query), '<7 the query')
  27. def test_repr_method(self):
  28. query_text = '<8 the query'
  29. query = RawTextQuery(query_text, [])
  30. r = repr(query)
  31. self.assertTrue(r.startswith(f"<RawTextQuery query='{query_text}' "))
  32. def test_change_query(self):
  33. query_text = '<8 the query'
  34. query = RawTextQuery(query_text, [])
  35. another_query = query.changeQuery('another text')
  36. self.assertEqual(query, another_query)
  37. self.assertEqual(query.getFullQuery(), '<8 another text')
  38. class TestLanguageParser(SearxTestCase):
  39. def test_language_code(self):
  40. language = 'es-ES'
  41. query_text = 'the query'
  42. full_query = ':' + language + ' ' + query_text
  43. query = RawTextQuery(full_query, [])
  44. self.assertEqual(query.getFullQuery(), full_query)
  45. self.assertEqual(len(query.query_parts), 1)
  46. self.assertEqual(len(query.languages), 1)
  47. self.assertIn(language, query.languages)
  48. self.assertFalse(query.specific)
  49. def test_language_name(self):
  50. language = 'english'
  51. query_text = 'the query'
  52. full_query = ':' + language + ' ' + query_text
  53. query = RawTextQuery(full_query, [])
  54. self.assertEqual(query.getFullQuery(), full_query)
  55. self.assertEqual(len(query.query_parts), 1)
  56. self.assertIn('en', query.languages)
  57. self.assertFalse(query.specific)
  58. def test_unlisted_language_code(self):
  59. language = 'all'
  60. query_text = 'the query'
  61. full_query = ':' + language + ' ' + query_text
  62. query = RawTextQuery(full_query, [])
  63. self.assertEqual(query.getFullQuery(), full_query)
  64. self.assertEqual(len(query.query_parts), 1)
  65. self.assertIn('all', query.languages)
  66. self.assertFalse(query.specific)
  67. def test_auto_language_code(self):
  68. language = 'auto'
  69. query_text = 'una consulta'
  70. full_query = ':' + language + ' ' + query_text
  71. query = RawTextQuery(full_query, [])
  72. self.assertEqual(query.getFullQuery(), full_query)
  73. self.assertEqual(len(query.query_parts), 1)
  74. self.assertIn('auto', query.languages)
  75. self.assertFalse(query.specific)
  76. def test_invalid_language_code(self):
  77. language = 'not_a_language'
  78. query_text = 'the query'
  79. full_query = ':' + language + ' ' + query_text
  80. query = RawTextQuery(full_query, [])
  81. self.assertEqual(query.getFullQuery(), full_query)
  82. self.assertEqual(len(query.query_parts), 0)
  83. self.assertEqual(len(query.languages), 0)
  84. self.assertFalse(query.specific)
  85. def test_empty_colon_in_query(self):
  86. query_text = 'the : query'
  87. query = RawTextQuery(query_text, [])
  88. self.assertEqual(query.getFullQuery(), query_text)
  89. self.assertEqual(len(query.query_parts), 0)
  90. self.assertEqual(len(query.languages), 0)
  91. self.assertFalse(query.specific)
  92. def test_autocomplete_empty(self):
  93. query_text = 'the query :'
  94. query = RawTextQuery(query_text, [])
  95. self.assertEqual(query.autocomplete_list, [":en", ":en_us", ":english", ":united_kingdom"])
  96. @parameterized.expand(
  97. [
  98. (':englis', [":english"]),
  99. (':deutschla', [":deutschland"]),
  100. (':new_zea', [":new_zealand"]),
  101. (':zh-', [':zh-cn', ':zh-hk', ':zh-tw']),
  102. ]
  103. )
  104. def test_autocomplete(self, query: str, autocomplete_list: list):
  105. query = RawTextQuery(query, [])
  106. self.assertEqual(query.autocomplete_list, autocomplete_list)
  107. class TestTimeoutParser(SearxTestCase):
  108. @parameterized.expand(
  109. [
  110. ('<3 the query', 3),
  111. ('<350 the query', 0.35),
  112. ('<3500 the query', 3.5),
  113. ]
  114. )
  115. def test_timeout_limit(self, query_text: str, timeout_limit: float):
  116. query = RawTextQuery(query_text, [])
  117. self.assertEqual(query.getFullQuery(), query_text)
  118. self.assertEqual(len(query.query_parts), 1)
  119. self.assertEqual(query.timeout_limit, timeout_limit)
  120. self.assertFalse(query.specific)
  121. def test_timeout_invalid(self):
  122. # invalid number: it is not bang but it is part of the query
  123. query_text = '<xxx the query'
  124. query = RawTextQuery(query_text, [])
  125. self.assertEqual(query.getFullQuery(), query_text)
  126. self.assertEqual(len(query.query_parts), 0)
  127. self.assertEqual(query.getQuery(), query_text)
  128. self.assertIsNone(query.timeout_limit)
  129. self.assertFalse(query.specific)
  130. def test_timeout_autocomplete(self):
  131. # invalid number: it is not bang but it is part of the query
  132. query_text = 'the query <'
  133. query = RawTextQuery(query_text, [])
  134. self.assertEqual(query.getFullQuery(), query_text)
  135. self.assertEqual(len(query.query_parts), 0)
  136. self.assertEqual(query.getQuery(), query_text)
  137. self.assertIsNone(query.timeout_limit)
  138. self.assertFalse(query.specific)
  139. self.assertEqual(query.autocomplete_list, ['<3', '<850'])
  140. class TestExternalBangParser(SearxTestCase):
  141. def test_external_bang(self):
  142. query_text = '!!ddg the query'
  143. query = RawTextQuery(query_text, [])
  144. self.assertEqual(query.getFullQuery(), query_text)
  145. self.assertEqual(len(query.query_parts), 1)
  146. self.assertFalse(query.specific)
  147. def test_external_bang_not_found(self):
  148. query_text = '!!notfoundbang the query'
  149. query = RawTextQuery(query_text, [])
  150. self.assertEqual(query.getFullQuery(), query_text)
  151. self.assertIsNone(query.external_bang)
  152. self.assertFalse(query.specific)
  153. def test_external_bang_autocomplete(self):
  154. query_text = 'the query !!dd'
  155. query = RawTextQuery(query_text, [])
  156. self.assertEqual(query.getFullQuery(), '!!dd the query')
  157. self.assertEqual(len(query.query_parts), 1)
  158. self.assertFalse(query.specific)
  159. self.assertGreater(len(query.autocomplete_list), 0)
  160. a = query.autocomplete_list[0]
  161. self.assertEqual(query.get_autocomplete_full_query(a), a + ' the query')
  162. class TestBang(SearxTestCase):
  163. SPECIFIC_BANGS = ['!dummy_engine', '!gd', '!general']
  164. THE_QUERY = 'the query'
  165. @parameterized.expand(SPECIFIC_BANGS)
  166. def test_bang(self, bang: str):
  167. with self.subTest(msg="Check bang", bang=bang):
  168. query_text = TestBang.THE_QUERY + ' ' + bang
  169. query = RawTextQuery(query_text, [])
  170. self.assertEqual(query.getFullQuery(), bang + ' ' + TestBang.THE_QUERY)
  171. self.assertEqual(query.query_parts, [bang])
  172. self.assertEqual(query.user_query_parts, TestBang.THE_QUERY.split(' '))
  173. @parameterized.expand(SPECIFIC_BANGS)
  174. def test_specific(self, bang: str):
  175. with self.subTest(msg="Check bang is specific", bang=bang):
  176. query_text = TestBang.THE_QUERY + ' ' + bang
  177. query = RawTextQuery(query_text, [])
  178. self.assertTrue(query.specific)
  179. def test_bang_not_found(self):
  180. query = RawTextQuery('the query !bang_not_found', [])
  181. self.assertEqual(query.getFullQuery(), 'the query !bang_not_found')
  182. def test_bang_autocomplete(self):
  183. query = RawTextQuery('the query !dum', [])
  184. self.assertEqual(query.autocomplete_list, ['!dummy_engine', '!dummy_private_engine'])
  185. query = RawTextQuery('!dum the query', [])
  186. self.assertEqual(query.autocomplete_list, [])
  187. self.assertEqual(query.getQuery(), '!dum the query')
  188. def test_bang_autocomplete_empty(self):
  189. query = RawTextQuery('the query !', [])
  190. self.assertEqual(query.autocomplete_list, ['!images', '!wikipedia', '!osm'])
  191. query = RawTextQuery('the query !', ['osm'])
  192. self.assertEqual(query.autocomplete_list, ['!images', '!wikipedia'])