test_framalibre.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. import mock
  4. from searx.engines import framalibre
  5. from searx.testing import SearxTestCase
  6. class TestFramalibreEngine(SearxTestCase):
  7. def test_request(self):
  8. query = 'test_query'
  9. dicto = defaultdict(dict)
  10. dicto['pageno'] = 0
  11. params = framalibre.request(query, dicto)
  12. self.assertTrue('url' in params)
  13. self.assertTrue(query in params['url'])
  14. self.assertTrue('framalibre.org' in params['url'])
  15. def test_response(self):
  16. self.assertRaises(AttributeError, framalibre.response, None)
  17. self.assertRaises(AttributeError, framalibre.response, [])
  18. self.assertRaises(AttributeError, framalibre.response, '')
  19. self.assertRaises(AttributeError, framalibre.response, '[]')
  20. response = mock.Mock(text='{}')
  21. self.assertEqual(framalibre.response(response), [])
  22. response = mock.Mock(text='{"data": []}')
  23. self.assertEqual(framalibre.response(response), [])
  24. html = u"""
  25. <div class="nodes-list-row">
  26. <div id="node-431"
  27. class="node node-logiciel-annuaires node-promoted node-teaser node-teaser node-sheet clearfix nodes-list"
  28. about="/content/gogs" typeof="sioc:Item foaf:Document">
  29. <header class="media">
  30. <div class="media-left">
  31. <div class="field field-name-field-logo field-type-image field-label-hidden">
  32. <div class="field-items">
  33. <div class="field-item even">
  34. <a href="/content/gogs">
  35. <img class="media-object img-responsive" typeof="foaf:Image"
  36. src="https://framalibre.org/sites/default/files/styles/teaser_logo/public/leslogos/gogs-lg.png?itok=rrCxKKBy"
  37. width="70" height="70" alt="" />
  38. </a>
  39. </div>
  40. </div>
  41. </div>
  42. </div>
  43. <div class="media-body">
  44. <h3 class="node-title"><a href="/content/gogs">Gogs</a></h3>
  45. <span property="dc:title" content="Gogs" class="rdf-meta element-hidden"></span>
  46. <div class="field field-name-field-annuaires field-type-taxonomy-term-reference field-label-hidden">
  47. <div class="field-items">
  48. <div class="field-item even">
  49. <a href="/annuaires/cloudwebapps"
  50. typeof="skos:Concept" property="rdfs:label skos:prefLabel"
  51. datatype="" class="label label-primary">Cloud/webApps</a>
  52. </div>
  53. </div>
  54. </div>
  55. </div>
  56. </header>
  57. <div class="content">
  58. <div class="field field-name-field-votre-appr-ciation field-type-fivestar field-label-hidden">
  59. <div class="field-items">
  60. <div class="field-item even">
  61. </div>
  62. </div>
  63. </div>
  64. <div class="field field-name-body field-type-text-with-summary field-label-hidden">
  65. <div class="field-items">
  66. <div class="field-item even" property="content:encoded">
  67. <p>Gogs est une interface web basée sur git et une bonne alternative à GitHub.</p>
  68. </div>
  69. </div>
  70. </div>
  71. </div>
  72. <footer>
  73. <a href="/content/gogs" class="read-more btn btn-default btn-sm">Voir la notice</a>
  74. <div class="field field-name-field-lien-officiel field-type-link-field field-label-hidden">
  75. <div class="field-items">
  76. <div class="field-item even">
  77. <a href="https://gogs.io/" target="_blank" title="Voir le site officiel">
  78. <span class="glyphicon glyphicon-globe"></span>
  79. <span class="sr-only">Lien officiel</span>
  80. </a>
  81. </div>
  82. </div>
  83. </div>
  84. </footer>
  85. </div>
  86. </div>
  87. """
  88. response = mock.Mock(text=html)
  89. results = framalibre.response(response)
  90. self.assertEqual(type(results), list)
  91. self.assertEqual(len(results), 1)
  92. self.assertEqual(results[0]['title'], 'Gogs')
  93. self.assertEqual(results[0]['url'],
  94. 'https://framalibre.org/content/gogs')
  95. self.assertEqual(results[0]['content'],
  96. u"Gogs est une interface web basée sur git et une bonne alternative à GitHub.")