logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://anongit.hacktivis.me/git/searx.git/

test_mediawiki.py (4224B)


  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. import mock
  4. from searx.engines import mediawiki
  5. from searx.testing import SearxTestCase
  6. class TestMediawikiEngine(SearxTestCase):
  7. def test_request(self):
  8. query = 'test_query'
  9. dicto = defaultdict(dict)
  10. dicto['pageno'] = 1
  11. dicto['language'] = 'fr_FR'
  12. params = mediawiki.request(query, dicto)
  13. self.assertIn('url', params)
  14. self.assertIn(query, params['url'])
  15. self.assertIn('wikipedia.org', params['url'])
  16. self.assertIn('fr', params['url'])
  17. mediawiki.base_url = "http://test.url/"
  18. mediawiki.search_url = mediawiki.base_url +\
  19. 'w/api.php?action=query'\
  20. '&list=search'\
  21. '&{query}'\
  22. '&srprop=timestamp'\
  23. '&format=json'\
  24. '&sroffset={offset}'\
  25. '&srlimit={limit}' # noqa
  26. params = mediawiki.request(query, dicto)
  27. self.assertIn('test.url', params['url'])
  28. def test_response(self):
  29. dicto = defaultdict(dict)
  30. dicto['language'] = 'fr'
  31. mediawiki.base_url = "https://{language}.wikipedia.org/"
  32. self.assertRaises(AttributeError, mediawiki.response, None)
  33. self.assertRaises(AttributeError, mediawiki.response, [])
  34. self.assertRaises(AttributeError, mediawiki.response, '')
  35. self.assertRaises(AttributeError, mediawiki.response, '[]')
  36. response = mock.Mock(text='{}', search_params=dicto)
  37. self.assertEqual(mediawiki.response(response), [])
  38. response = mock.Mock(text='{"data": []}', search_params=dicto)
  39. self.assertEqual(mediawiki.response(response), [])
  40. json = """
  41. {
  42. "query-continue": {
  43. "search": {
  44. "sroffset": 1
  45. }
  46. },
  47. "query": {
  48. "searchinfo": {
  49. "totalhits": 29721
  50. },
  51. "search": [
  52. {
  53. "ns": 0,
  54. "title": "This is the title étude",
  55. "timestamp": "2014-12-19T17:42:52Z"
  56. }
  57. ]
  58. }
  59. }
  60. """
  61. response = mock.Mock(text=json, search_params=dicto)
  62. results = mediawiki.response(response)
  63. self.assertEqual(type(results), list)
  64. self.assertEqual(len(results), 1)
  65. self.assertEqual(results[0]['title'], u'This is the title étude')
  66. self.assertIn('fr.wikipedia.org', results[0]['url'])
  67. self.assertIn('This_is_the_title', results[0]['url'])
  68. self.assertIn('%C3%A9tude', results[0]['url'])
  69. self.assertEqual(results[0]['content'], '')
  70. json = """
  71. {
  72. "query-continue": {
  73. "search": {
  74. "sroffset": 1
  75. }
  76. },
  77. "query": {
  78. "searchinfo": {
  79. "totalhits": 29721
  80. },
  81. "search": [
  82. ]
  83. }
  84. }
  85. """
  86. response = mock.Mock(text=json, search_params=dicto)
  87. results = mediawiki.response(response)
  88. self.assertEqual(type(results), list)
  89. self.assertEqual(len(results), 0)
  90. json = """
  91. {
  92. "query-continue": {
  93. "search": {
  94. "sroffset": 1
  95. }
  96. },
  97. "query": {
  98. }
  99. }
  100. """
  101. response = mock.Mock(text=json, search_params=dicto)
  102. results = mediawiki.response(response)
  103. self.assertEqual(type(results), list)
  104. self.assertEqual(len(results), 0)
  105. json = r"""
  106. {"toto":[
  107. {"id":200,"name":"Artist Name",
  108. "link":"http:\/\/www.mediawiki.com\/artist\/1217","type":"artist"}
  109. ]}
  110. """
  111. response = mock.Mock(text=json, search_params=dicto)
  112. results = mediawiki.response(response)
  113. self.assertEqual(type(results), list)
  114. self.assertEqual(len(results), 0)