12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import ina
  4. from searx.testing import SearxTestCase
  5. class TestInaEngine(SearxTestCase):
  6. def test_request(self):
  7. query = 'test_query'
  8. dicto = defaultdict(dict)
  9. dicto['pageno'] = 0
  10. params = ina.request(query, dicto)
  11. self.assertTrue('url' in params)
  12. self.assertTrue(query in params['url'])
  13. self.assertTrue('ina.fr' in params['url'])
  14. def test_response(self):
  15. self.assertRaises(AttributeError, ina.response, None)
  16. self.assertRaises(AttributeError, ina.response, [])
  17. self.assertRaises(AttributeError, ina.response, '')
  18. self.assertRaises(AttributeError, ina.response, '[]')
  19. response = mock.Mock(text='{}')
  20. self.assertEqual(ina.response(response), [])
  21. response = mock.Mock(text='{"data": []}')
  22. self.assertEqual(ina.response(response), [])
  23. json = """
  24. {"content":"\\t<div class=\\"container\\">\\n\\t\\n\
  25. <!-- DEBUT CONTENU PRINCIPAL -->\\n<div class=\\"row\\">\\n\
  26. <div class=\\"search-results--list\\"><div class=\\"media\\">\\n\
  27. \\t\\t\\t\\t<a class=\\"media-left media-video premium xiti_click_action\\" \
  28. data-xiti-params=\\"recherche_v4::resultats_conference_de_presse_du_general_de_gaulle::N\\" \
  29. href=\\"\\/video\\/CAF89035682\\/conference-de-presse-du-general-de-gaulle-video.html\\">\\n\
  30. <img src=\\"https:\\/\\/www.ina.fr\\/images_v2\\/140x105\\/CAF89035682.jpeg\\" \
  31. alt=\\"Conf\\u00e9rence de presse du G\\u00e9n\\u00e9ral de Gaulle \\">\\n\
  32. \\t\\t\\t\\t\\t<\\/a>\\n\
  33. \\t\\t\\t\\t\\t<div class=\\"media-body\\">\\n\\t\\t\\t\\t\\t\\t<h3 class=\\"h3--title media-heading\\">\\n\
  34. \\t\\t\\t\\t\\t\\t\\t<a class=\\"xiti_click_action\\" \
  35. data-xiti-params=\\"recherche_v4::resultats_conference_de_presse_du_general_de_gaulle::N\\" \
  36. href=\\"\\/video\\/CAF89035682\\/conference-de-presse-du-general-de-gaulle-video.html\\">\
  37. Conf\\u00e9rence de presse du G\\u00e9n\\u00e9ral de Gaulle <\\/a>\\n\
  38. <\\/h3>\\n\
  39. <div class=\\"media-body__info\\">\\n<span class=\\"broadcast\\">27\\/11\\/1967<\\/span>\\n\
  40. <span class=\\"views\\">29321 vues<\\/span>\\n\
  41. <span class=\\"duration\\">01h 33m 07s<\\/span>\\n\
  42. <\\/div>\\n\
  43. <p class=\\"media-body__summary\\">VERSION INTEGRALE DE LA CONFERENCE DE PRESSE DU GENERAL DE GAULLE . \
  44. - PA le Pr\\u00e9sident DE GAULLE : il ouvre les bras et s'assied. DP journalis...<\\/p>\\n\
  45. <\\/div>\\n<\\/div><!-- \\/.media -->\\n"
  46. }
  47. """
  48. response = mock.Mock(text=json)
  49. results = ina.response(response)
  50. self.assertEqual(type(results), list)
  51. self.assertEqual(len(results), 1)
  52. self.assertEqual(results[0]['title'], u'Conf\xe9rence de presse du G\xe9n\xe9ral de Gaulle')
  53. self.assertEqual(results[0]['url'],
  54. 'https://www.ina.fr/video/CAF89035682/conference-de-presse-du-general-de-gaulle-video.html')
  55. self.assertEqual(results[0]['content'],
  56. u"VERSION INTEGRALE DE LA CONFERENCE DE PRESSE DU GENERAL DE GAULLE ."
  57. u" - PA le Pr\u00e9sident DE GAULLE : il ouvre les bras et s'assied. DP journalis...")