test_mediawiki.py 4.2 KB

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