test_archlinux.py 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import archlinux
  4. from searx.testing import SearxTestCase
  5. domains = {
  6. 'en': 'https://wiki.archlinux.org',
  7. 'de': 'https://wiki.archlinux.de',
  8. 'fr': 'https://wiki.archlinux.fr',
  9. 'ja': 'https://wiki.archlinuxjp.org',
  10. 'ro': 'http://wiki.archlinux.ro',
  11. 'tr': 'http://archtr.org/wiki'
  12. }
  13. class TestArchLinuxEngine(SearxTestCase):
  14. def test_request(self):
  15. query = 'test_query'
  16. dic = defaultdict(dict)
  17. dic['pageno'] = 1
  18. dic['language'] = 'en_US'
  19. params = archlinux.request(query, dic)
  20. self.assertTrue('url' in params)
  21. self.assertTrue(query in params['url'])
  22. self.assertTrue('wiki.archlinux.org' in params['url'])
  23. for lang, domain in domains.items():
  24. dic['language'] = lang
  25. params = archlinux.request(query, dic)
  26. self.assertTrue(domain in params['url'])
  27. def test_response(self):
  28. response = mock.Mock(text='<html></html>',
  29. search_params={'language': 'en_US'})
  30. self.assertEqual(archlinux.response(response), [])
  31. html = """
  32. <ul class="mw-search-results">
  33. <li>
  34. <div class="mw-search-result-heading">
  35. <a href="/index.php/ATI" title="ATI">ATI</a>
  36. </div>
  37. <div class="searchresult">
  38. Lorem ipsum dolor sit amet
  39. </div>
  40. <div class="mw-search-result-data">
  41. 30 KB (4,630 words) - 19:04, 17 March 2016</div>
  42. </li>
  43. <li>
  44. <div class="mw-search-result-heading">
  45. <a href="/index.php/Frequently_asked_questions" title="Frequently asked questions">
  46. Frequently asked questions
  47. </a>
  48. </div>
  49. <div class="searchresult">
  50. CPUs with AMDs instruction set "AMD64"
  51. </div>
  52. <div class="mw-search-result-data">
  53. 17 KB (2,722 words) - 20:13, 21 March 2016
  54. </div>
  55. </li>
  56. <li>
  57. <div class="mw-search-result-heading">
  58. <a href="/index.php/CPU_frequency_scaling" title="CPU frequency scaling">CPU frequency scaling</a>
  59. </div>
  60. <div class="searchresult">
  61. ondemand for AMD and older Intel CPU
  62. </div>
  63. <div class="mw-search-result-data">
  64. 15 KB (2,319 words) - 23:46, 16 March 2016
  65. </div>
  66. </li>
  67. </ul>
  68. """
  69. expected = [
  70. {
  71. 'title': 'ATI',
  72. 'url': 'https://wiki.archlinux.org/index.php/ATI'
  73. },
  74. {
  75. 'title': 'Frequently asked questions',
  76. 'url': 'https://wiki.archlinux.org/index.php/Frequently_asked_questions'
  77. },
  78. {
  79. 'title': 'CPU frequency scaling',
  80. 'url': 'https://wiki.archlinux.org/index.php/CPU_frequency_scaling'
  81. }
  82. ]
  83. response = mock.Mock(text=html)
  84. response.search_params = {
  85. 'language': 'en_US'
  86. }
  87. results = archlinux.response(response)
  88. self.assertEqual(type(results), list)
  89. self.assertEqual(len(results), len(expected))
  90. i = 0
  91. for exp in expected:
  92. res = results[i]
  93. i += 1
  94. for key, value in exp.items():
  95. self.assertEqual(res[key], value)