test_qwant_social.py 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import qwant_social
  4. from searx.testing import SearxTestCase
  5. class TestQwantSocialEngine(SearxTestCase):
  6. def test_request(self):
  7. query = 'test_query'
  8. dicto = defaultdict(dict)
  9. dicto['pageno'] = 0
  10. dicto['language'] = 'fr_FR'
  11. params = qwant_social.request(query, dicto)
  12. self.assertIn('url', params)
  13. self.assertIn(query, params['url'])
  14. self.assertIn('qwant.com', params['url'])
  15. self.assertIn('fr_fr', params['url'])
  16. dicto['language'] = 'all'
  17. params = qwant_social.request(query, dicto)
  18. self.assertFalse('fr' in params['url'])
  19. def test_response(self):
  20. self.assertRaises(AttributeError, qwant_social.response, None)
  21. self.assertRaises(AttributeError, qwant_social.response, [])
  22. self.assertRaises(AttributeError, qwant_social.response, '')
  23. self.assertRaises(AttributeError, qwant_social.response, '[]')
  24. response = mock.Mock(text='{}')
  25. self.assertEqual(qwant_social.response(response), [])
  26. response = mock.Mock(text='{"data": {}}')
  27. self.assertEqual(qwant_social.response(response), [])
  28. json = """
  29. {
  30. "status": "success",
  31. "data": {
  32. "query": {
  33. "locale": "en_us",
  34. "query": "Test",
  35. "offset": 10
  36. },
  37. "result": {
  38. "items": [
  39. {
  40. "_id": "dc0b3f24c93684c7d7f1b0a4c2d9f1b0",
  41. "__index": 32,
  42. "title": "Title",
  43. "img": "img",
  44. "desc": "Description",
  45. "date": 1432643480,
  46. "type": "twitter",
  47. "card": "XXX",
  48. "post": "603176590856556545",
  49. "url": "http://www.url.xyz",
  50. "userUrl": "https://twitter.com/XXX"
  51. }
  52. ],
  53. "filters": []
  54. },
  55. "cache": {
  56. "key": "e66aa864c00147a0e3a16ff7a5efafde",
  57. "created": 1433092754,
  58. "expiration": 259200,
  59. "status": "miss",
  60. "age": 0
  61. }
  62. }
  63. }
  64. """
  65. response = mock.Mock(text=json)
  66. results = qwant_social.response(response)
  67. self.assertEqual(type(results), list)
  68. self.assertEqual(len(results), 1)
  69. self.assertEqual(results[0]['title'], 'Title')
  70. self.assertEqual(results[0]['url'], 'http://www.url.xyz')
  71. self.assertEqual(results[0]['content'], 'Description')
  72. json = """
  73. {
  74. "status": "success",
  75. "data": {
  76. "query": {
  77. "locale": "en_us",
  78. "query": "Test",
  79. "offset": 10
  80. },
  81. "result": {
  82. "filters": []
  83. },
  84. "cache": {
  85. "key": "e66aa864c00147a0e3a16ff7a5efafde",
  86. "created": 1433092754,
  87. "expiration": 259200,
  88. "status": "miss",
  89. "age": 0
  90. }
  91. }
  92. }
  93. """
  94. response = mock.Mock(text=json)
  95. results = qwant_social.response(response)
  96. self.assertEqual(type(results), list)
  97. self.assertEqual(len(results), 0)
  98. json = """
  99. {
  100. "status": "success",
  101. "data": {
  102. "query": {
  103. "locale": "en_us",
  104. "query": "Test",
  105. "offset": 10
  106. },
  107. "cache": {
  108. "key": "e66aa864c00147a0e3a16ff7a5efafde",
  109. "created": 1433092754,
  110. "expiration": 259200,
  111. "status": "miss",
  112. "age": 0
  113. }
  114. }
  115. }
  116. """
  117. response = mock.Mock(text=json)
  118. results = qwant_social.response(response)
  119. self.assertEqual(type(results), list)
  120. self.assertEqual(len(results), 0)
  121. json = """
  122. {
  123. "status": "success"
  124. }
  125. """
  126. response = mock.Mock(text=json)
  127. results = qwant_social.response(response)
  128. self.assertEqual(type(results), list)
  129. self.assertEqual(len(results), 0)