test_seedpeer.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. import mock
  4. from searx.engines import seedpeer
  5. from searx.testing import SearxTestCase
  6. class TestBtdiggEngine(SearxTestCase):
  7. def test_request(self):
  8. query = 'test_query'
  9. dicto = defaultdict(dict)
  10. dicto['pageno'] = 1
  11. params = seedpeer.request(query, dicto)
  12. self.assertIn('url', params)
  13. self.assertIn(query, params['url'])
  14. self.assertIn('seedpeer', params['url'])
  15. def test_response(self):
  16. self.assertRaises(AttributeError, seedpeer.response, None)
  17. self.assertRaises(AttributeError, seedpeer.response, [])
  18. self.assertRaises(AttributeError, seedpeer.response, '')
  19. self.assertRaises(AttributeError, seedpeer.response, '[]')
  20. response = mock.Mock(text='<html></html>')
  21. self.assertEqual(seedpeer.response(response), [])
  22. html = u"""
  23. <html>
  24. <head>
  25. <script></script>
  26. <script type="text/javascript" src="not_here.js"></script>
  27. <script type="text/javascript">
  28. window.initialData=
  29. {"data": {"list": [{"name": "Title", "seeds": "10", "peers": "20", "size": "1024", "hash": "abc123"}]}}
  30. </script>
  31. </head>
  32. <body>
  33. <table></table>
  34. <table>
  35. <thead><tr></tr></thead>
  36. <tbody>
  37. <tr>
  38. <td><a href="link">Title</a></td>
  39. <td>1 year</td>
  40. <td>1 KB</td>
  41. <td>10</td>
  42. <td>20</td>
  43. <td></td>
  44. </tr>
  45. </tbody>
  46. </table>
  47. </body>
  48. </html>
  49. """
  50. response = mock.Mock(text=html)
  51. results = seedpeer.response(response)
  52. self.assertEqual(type(results), list)
  53. self.assertEqual(len(results), 1)
  54. self.assertEqual(results[0]['title'], 'Title')
  55. self.assertEqual(results[0]['url'], 'https://seedpeer.me/link')
  56. self.assertEqual(results[0]['seed'], 10)
  57. self.assertEqual(results[0]['leech'], 20)
  58. self.assertEqual(results[0]['filesize'], 1024)
  59. self.assertEqual(results[0]['torrentfile'], 'https://seedpeer.me/torrent/abc123')
  60. self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:abc123')