test_digbt.py 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import digbt
  4. from searx.testing import SearxTestCase
  5. class TestDigBTEngine(SearxTestCase):
  6. def test_request(self):
  7. query = 'test_query'
  8. dicto = defaultdict(dict)
  9. dicto['pageno'] = 0
  10. params = digbt.request(query, dicto)
  11. self.assertIn('url', params)
  12. self.assertIn(query, params['url'])
  13. self.assertIn('digbt.org', params['url'])
  14. def test_response(self):
  15. self.assertRaises(AttributeError, digbt.response, None)
  16. self.assertRaises(AttributeError, digbt.response, [])
  17. self.assertRaises(AttributeError, digbt.response, '')
  18. self.assertRaises(AttributeError, digbt.response, '[]')
  19. response = mock.Mock(text='<html></html>')
  20. self.assertEqual(digbt.response(response), [])
  21. html = """
  22. <table class="table">
  23. <tr><td class="x-item">
  24. <div>
  25. <a title="The Big Bang Theory" class="title" href="/The-Big-Bang-Theory-d2.html">
  26. The Big <span class="highlight">Bang</span> Theory
  27. </a>
  28. <span class="ctime"><span style="color:red;">4 hours ago</span></span>
  29. </div>
  30. <div class="files">
  31. <ul>
  32. <li>The Big Bang Theory 2.9 GB</li>
  33. <li>....</li>
  34. </ul>
  35. </div>
  36. <div class="tail">
  37. Files: 1 Size: 2.9 GB Downloads: 1 Updated: <span style="color:red;">4 hours ago</span>
  38. &nbsp; &nbsp;
  39. <a class="title" href="magnet:?xt=urn:btih:a&amp;dn=The+Big+Bang+Theory">
  40. <span class="glyphicon glyphicon-magnet"></span> magnet-link
  41. </a>
  42. &nbsp; &nbsp;
  43. </div>
  44. </td></tr>
  45. </table>
  46. """
  47. response = mock.Mock(text=html.encode('utf-8'))
  48. results = digbt.response(response)
  49. self.assertEqual(type(results), list)
  50. self.assertEqual(len(results), 1)
  51. self.assertEqual(results[0]['title'], 'The Big Bang Theory')
  52. self.assertEqual(results[0]['url'], 'https://digbt.org/The-Big-Bang-Theory-d2.html')
  53. self.assertEqual(results[0]['content'], 'The Big Bang Theory 2.9 GB ....')
  54. self.assertEqual(results[0]['filesize'], 3113851289)
  55. self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:a&dn=The+Big+Bang+Theory')