test_query.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. from mock import patch
  2. from searx.search import initialize
  3. from searx.query import RawTextQuery
  4. from searx.testing import SearxTestCase
  5. import searx.engines
  6. TEST_ENGINES = [
  7. {
  8. 'name': 'dummy engine',
  9. 'engine': 'dummy',
  10. 'categories': 'general',
  11. 'shortcut': 'du',
  12. 'timeout': 3.0,
  13. 'tokens': [],
  14. },
  15. ]
  16. class TestQuery(SearxTestCase):
  17. def test_simple_query(self):
  18. query_text = 'the query'
  19. query = RawTextQuery(query_text, [])
  20. self.assertEqual(query.getFullQuery(), query_text)
  21. self.assertEqual(len(query.query_parts), 0)
  22. self.assertEqual(len(query.user_query_parts), 2)
  23. self.assertEqual(len(query.languages), 0)
  24. self.assertFalse(query.specific)
  25. def test_multiple_spaces_query(self):
  26. query_text = '\tthe query'
  27. query = RawTextQuery(query_text, [])
  28. self.assertEqual(query.getFullQuery(), 'the query')
  29. self.assertEqual(len(query.query_parts), 0)
  30. self.assertEqual(len(query.user_query_parts), 2)
  31. self.assertEqual(len(query.languages), 0)
  32. self.assertFalse(query.specific)
  33. def test_str_method(self):
  34. query_text = '<7 the query'
  35. query = RawTextQuery(query_text, [])
  36. self.assertEqual(str(query), '<7 the query')
  37. def test_repr_method(self):
  38. query_text = '<8 the query'
  39. query = RawTextQuery(query_text, [])
  40. r = repr(query)
  41. self.assertTrue(r.startswith(f"<RawTextQuery query='{query_text}' "))
  42. def test_change_query(self):
  43. query_text = '<8 the query'
  44. query = RawTextQuery(query_text, [])
  45. another_query = query.changeQuery('another text')
  46. self.assertEqual(query, another_query)
  47. self.assertEqual(query.getFullQuery(), '<8 another text')
  48. class TestLanguageParser(SearxTestCase):
  49. def test_language_code(self):
  50. language = 'es-ES'
  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.assertEqual(len(query.languages), 1)
  57. self.assertIn(language, query.languages)
  58. self.assertFalse(query.specific)
  59. def test_language_name(self):
  60. language = 'english'
  61. query_text = 'the query'
  62. full_query = ':' + language + ' ' + query_text
  63. query = RawTextQuery(full_query, [])
  64. self.assertEqual(query.getFullQuery(), full_query)
  65. self.assertEqual(len(query.query_parts), 1)
  66. self.assertIn('en', query.languages)
  67. self.assertFalse(query.specific)
  68. def test_unlisted_language_code(self):
  69. language = 'all'
  70. query_text = 'the query'
  71. full_query = ':' + language + ' ' + query_text
  72. query = RawTextQuery(full_query, [])
  73. self.assertEqual(query.getFullQuery(), full_query)
  74. self.assertEqual(len(query.query_parts), 1)
  75. self.assertIn('all', query.languages)
  76. self.assertFalse(query.specific)
  77. def test_invalid_language_code(self):
  78. language = 'not_a_language'
  79. query_text = 'the query'
  80. full_query = ':' + language + ' ' + query_text
  81. query = RawTextQuery(full_query, [])
  82. self.assertEqual(query.getFullQuery(), full_query)
  83. self.assertEqual(len(query.query_parts), 0)
  84. self.assertEqual(len(query.languages), 0)
  85. self.assertFalse(query.specific)
  86. def test_empty_colon_in_query(self):
  87. query_text = 'the : query'
  88. query = RawTextQuery(query_text, [])
  89. self.assertEqual(query.getFullQuery(), query_text)
  90. self.assertEqual(len(query.query_parts), 0)
  91. self.assertEqual(len(query.languages), 0)
  92. self.assertFalse(query.specific)
  93. def test_autocomplete_empty(self):
  94. query_text = 'the query :'
  95. query = RawTextQuery(query_text, [])
  96. self.assertEqual(query.autocomplete_list, [":en", ":en_us", ":english", ":united_kingdom"])
  97. def test_autocomplete(self):
  98. query = RawTextQuery(':englis', [])
  99. self.assertEqual(query.autocomplete_list, [":english"])
  100. query = RawTextQuery(':deutschla', [])
  101. self.assertEqual(query.autocomplete_list, [":deutschland"])
  102. query = RawTextQuery(':new_zea', [])
  103. self.assertEqual(query.autocomplete_list, [":new_zealand"])
  104. query = RawTextQuery(':hu-H', [])
  105. self.assertEqual(query.autocomplete_list, [":hu-hu"])
  106. query = RawTextQuery(':v', [])
  107. self.assertEqual(query.autocomplete_list, [":vi", ":tiếng việt"])
  108. class TestTimeoutParser(SearxTestCase):
  109. def test_timeout_below100(self):
  110. query_text = '<3 the query'
  111. query = RawTextQuery(query_text, [])
  112. self.assertEqual(query.getFullQuery(), query_text)
  113. self.assertEqual(len(query.query_parts), 1)
  114. self.assertEqual(query.timeout_limit, 3)
  115. self.assertFalse(query.specific)
  116. def test_timeout_above100(self):
  117. query_text = '<350 the query'
  118. query = RawTextQuery(query_text, [])
  119. self.assertEqual(query.getFullQuery(), query_text)
  120. self.assertEqual(len(query.query_parts), 1)
  121. self.assertEqual(query.timeout_limit, 0.35)
  122. self.assertFalse(query.specific)
  123. def test_timeout_above1000(self):
  124. query_text = '<3500 the query'
  125. query = RawTextQuery(query_text, [])
  126. self.assertEqual(query.getFullQuery(), query_text)
  127. self.assertEqual(len(query.query_parts), 1)
  128. self.assertEqual(query.timeout_limit, 3.5)
  129. self.assertFalse(query.specific)
  130. def test_timeout_invalid(self):
  131. # invalid number: it is not bang but it is part of the query
  132. query_text = '<xxx 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.assertEqual(query.timeout_limit, None)
  138. self.assertFalse(query.specific)
  139. def test_timeout_autocomplete(self):
  140. # invalid number: it is not bang but it is part of the query
  141. query_text = 'the query <'
  142. query = RawTextQuery(query_text, [])
  143. self.assertEqual(query.getFullQuery(), query_text)
  144. self.assertEqual(len(query.query_parts), 0)
  145. self.assertEqual(query.getQuery(), query_text)
  146. self.assertEqual(query.timeout_limit, None)
  147. self.assertFalse(query.specific)
  148. self.assertEqual(query.autocomplete_list, ['<3', '<850'])
  149. class TestExternalBangParser(SearxTestCase):
  150. def test_external_bang(self):
  151. query_text = '!!ddg the query'
  152. query = RawTextQuery(query_text, [])
  153. self.assertEqual(query.getFullQuery(), query_text)
  154. self.assertEqual(len(query.query_parts), 1)
  155. self.assertFalse(query.specific)
  156. def test_external_bang_not_found(self):
  157. query_text = '!!notfoundbang the query'
  158. query = RawTextQuery(query_text, [])
  159. self.assertEqual(query.getFullQuery(), query_text)
  160. self.assertEqual(query.external_bang, None)
  161. self.assertFalse(query.specific)
  162. def test_external_bang_autocomplete(self):
  163. query_text = 'the query !!dd'
  164. query = RawTextQuery(query_text, [])
  165. self.assertEqual(query.getFullQuery(), '!!dd the query')
  166. self.assertEqual(len(query.query_parts), 1)
  167. self.assertFalse(query.specific)
  168. self.assertGreater(len(query.autocomplete_list), 0)
  169. a = query.autocomplete_list[0]
  170. self.assertEqual(query.get_autocomplete_full_query(a), a + ' the query')
  171. def test_external_bang_autocomplete_empty(self):
  172. query_text = 'the query !!'
  173. query = RawTextQuery(query_text, [])
  174. self.assertEqual(query.getFullQuery(), 'the query !!')
  175. self.assertEqual(len(query.query_parts), 0)
  176. self.assertFalse(query.specific)
  177. self.assertGreater(len(query.autocomplete_list), 2)
  178. a = query.autocomplete_list[0]
  179. self.assertEqual(query.get_autocomplete_full_query(a), 'the query ' + a)
  180. class TestBang(SearxTestCase):
  181. SPECIFIC_BANGS = ['!dummy_engine', '!du', '!general']
  182. NOT_SPECIFIC_BANGS = ['?dummy_engine', '?du', '?general']
  183. THE_QUERY = 'the query'
  184. def test_bang(self):
  185. initialize(TEST_ENGINES)
  186. for bang in TestBang.SPECIFIC_BANGS + TestBang.NOT_SPECIFIC_BANGS:
  187. with self.subTest(msg="Check bang", bang=bang):
  188. query_text = TestBang.THE_QUERY + ' ' + bang
  189. query = RawTextQuery(query_text, [])
  190. self.assertEqual(query.getFullQuery(), bang + ' ' + TestBang.THE_QUERY)
  191. self.assertEqual(query.query_parts, [bang])
  192. self.assertEqual(query.user_query_parts, TestBang.THE_QUERY.split(' '))
  193. def test_specific(self):
  194. for bang in TestBang.SPECIFIC_BANGS:
  195. with self.subTest(msg="Check bang is specific", bang=bang):
  196. query_text = TestBang.THE_QUERY + ' ' + bang
  197. query = RawTextQuery(query_text, [])
  198. self.assertTrue(query.specific)
  199. def test_not_specific(self):
  200. for bang in TestBang.NOT_SPECIFIC_BANGS:
  201. with self.subTest(msg="Check bang is not specific", bang=bang):
  202. query_text = TestBang.THE_QUERY + ' ' + bang
  203. query = RawTextQuery(query_text, [])
  204. self.assertFalse(query.specific)
  205. def test_bang_not_found(self):
  206. initialize(TEST_ENGINES)
  207. query = RawTextQuery('the query !bang_not_found', [])
  208. self.assertEqual(query.getFullQuery(), 'the query !bang_not_found')
  209. def test_bang_autocomplete(self):
  210. initialize(TEST_ENGINES)
  211. query = RawTextQuery('the query !dum', [])
  212. self.assertEqual(query.autocomplete_list, ['!dummy_engine'])
  213. query = RawTextQuery('!dum the query', [])
  214. self.assertEqual(query.autocomplete_list, [])
  215. self.assertEqual(query.getQuery(), '!dum the query')
  216. def test_bang_autocomplete_empty(self):
  217. with patch.object(searx.engines, 'initialize_engines', searx.engines.load_engines):
  218. initialize()
  219. query = RawTextQuery('the query !', [])
  220. self.assertEqual(query.autocomplete_list, ['!images', '!wikipedia', '!osm'])
  221. query = RawTextQuery('the query ?', ['osm'])
  222. self.assertEqual(query.autocomplete_list, ['?images', '?wikipedia'])