test_query.py 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from searx.query import RawTextQuery
  2. from searx.testing import SearxTestCase
  3. class TestQuery(SearxTestCase):
  4. def test_simple_query(self):
  5. query_text = 'the query'
  6. query = RawTextQuery(query_text, [])
  7. query.parse_query()
  8. self.assertEquals(query.getFullQuery(), query_text)
  9. self.assertEquals(len(query.query_parts), 1)
  10. self.assertEquals(len(query.languages), 0)
  11. self.assertFalse(query.specific)
  12. def test_language_code(self):
  13. language = 'es-ES'
  14. query_text = 'the query'
  15. full_query = ':' + language + ' ' + query_text
  16. query = RawTextQuery(full_query, [])
  17. query.parse_query()
  18. self.assertEquals(query.getFullQuery(), full_query)
  19. self.assertEquals(len(query.query_parts), 3)
  20. self.assertEquals(len(query.languages), 1)
  21. self.assertIn(language, query.languages)
  22. self.assertFalse(query.specific)
  23. def test_language_name(self):
  24. language = 'english'
  25. query_text = 'the query'
  26. full_query = ':' + language + ' ' + query_text
  27. query = RawTextQuery(full_query, [])
  28. query.parse_query()
  29. self.assertEquals(query.getFullQuery(), full_query)
  30. self.assertEquals(len(query.query_parts), 3)
  31. self.assertIn('en', query.languages)
  32. self.assertFalse(query.specific)
  33. def test_unlisted_language_code(self):
  34. language = 'all'
  35. query_text = 'the query'
  36. full_query = ':' + language + ' ' + query_text
  37. query = RawTextQuery(full_query, [])
  38. query.parse_query()
  39. self.assertEquals(query.getFullQuery(), full_query)
  40. self.assertEquals(len(query.query_parts), 3)
  41. self.assertIn('all', query.languages)
  42. self.assertFalse(query.specific)
  43. def test_invalid_language_code(self):
  44. language = 'not_a_language'
  45. query_text = 'the query'
  46. full_query = ':' + language + ' ' + query_text
  47. query = RawTextQuery(full_query, [])
  48. query.parse_query()
  49. self.assertEquals(query.getFullQuery(), full_query)
  50. self.assertEquals(len(query.query_parts), 1)
  51. self.assertEquals(len(query.languages), 0)
  52. self.assertFalse(query.specific)