test_results.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # pylint: disable=missing-module-docstring,disable=missing-class-docstring,invalid-name
  3. from searx.result_types import LegacyResult
  4. from searx.results import ResultContainer
  5. from tests import SearxTestCase
  6. class ResultContainerTestCase(SearxTestCase):
  7. # pylint: disable=use-dict-literal
  8. TEST_SETTINGS = "test_result_container.yml"
  9. def test_empty(self):
  10. container = ResultContainer()
  11. self.assertEqual(container.get_ordered_results(), [])
  12. def test_one_result(self):
  13. result = dict(url="https://example.org", title="title ..", content="Lorem ..")
  14. container = ResultContainer()
  15. container.extend("google", [result])
  16. container.close()
  17. self.assertEqual(container.results_length(), 1)
  18. self.assertIn(LegacyResult(result), container.get_ordered_results())
  19. def test_one_suggestion(self):
  20. result = dict(suggestion="lorem ipsum ..")
  21. container = ResultContainer()
  22. container.extend("duckduckgo", [result])
  23. container.close()
  24. self.assertEqual(container.results_length(), 0)
  25. self.assertEqual(len(container.suggestions), 1)
  26. self.assertIn(result["suggestion"], container.suggestions)
  27. def test_merge_url_result(self):
  28. # from the merge of eng1 and eng2 we expect this result
  29. result = LegacyResult(
  30. url="https://example.org", title="very long title, lorem ipsum", content="Lorem ipsum dolor sit amet .."
  31. )
  32. eng1 = dict(url=result.url, title="short title", content=result.content, engine="google")
  33. eng2 = dict(url="http://example.org", title=result.title, content="lorem ipsum", engine="duckduckgo")
  34. container = ResultContainer()
  35. container.extend(None, [eng1, eng2])
  36. container.close()
  37. result_list = container.get_ordered_results()
  38. self.assertEqual(container.results_length(), 1)
  39. self.assertIn(result, result_list)
  40. self.assertEqual(result_list[0].title, result.title)
  41. self.assertEqual(result_list[0].content, result.content)