123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import nyaa
  4. from searx.testing import SearxTestCase
  5. class TestNyaaEngine(SearxTestCase):
  6. def test_request(self):
  7. query = 'test_query'
  8. dic = defaultdict(dict)
  9. dic['pageno'] = 1
  10. params = nyaa.request(query, dic)
  11. self.assertTrue('url' in params)
  12. self.assertTrue(query in params['url'])
  13. self.assertTrue('nyaa.se' in params['url'])
  14. def test_response(self):
  15. resp = mock.Mock(text='<html></html>')
  16. self.assertEqual(nyaa.response(resp), [])
  17. html = """
  18. <table class="tlist">
  19. <tbody>
  20. <tr class="trusted tlistrow">
  21. <td class="tlisticon">
  22. <a href="//www.nyaa.se" title="English-translated Anime">
  23. <img src="//files.nyaa.se" alt="English-translated Anime">
  24. </a>
  25. </td>
  26. <td class="tlistname">
  27. <a href="//www.nyaa.se/?page3">
  28. Sample torrent title
  29. </a>
  30. </td>
  31. <td class="tlistdownload">
  32. <a href="//www.nyaa.se/?page_dl" title="Download">
  33. <img src="//files.nyaa.se/www-dl.png" alt="DL">
  34. </a>
  35. </td>
  36. <td class="tlistsize">10 MiB</td>
  37. <td class="tlistsn">1</td>
  38. <td class="tlistln">3</td>
  39. <td class="tlistdn">666</td>
  40. <td class="tlistmn">0</td>
  41. </tr>
  42. </tbody>
  43. </table>
  44. """
  45. resp = mock.Mock(text=html)
  46. results = nyaa.response(resp)
  47. self.assertEqual(type(results), list)
  48. self.assertEqual(len(results), 1)
  49. r = results[0]
  50. self.assertTrue(r['url'].find('www.nyaa.se/?page3') >= 0)
  51. self.assertTrue(r['torrentfile'].find('www.nyaa.se/?page_dl') >= 0)
  52. self.assertTrue(r['content'].find('English-translated Anime') >= 0)
  53. self.assertTrue(r['content'].find('Downloaded 666 times.') >= 0)
  54. self.assertEqual(r['title'], 'Sample torrent title')
  55. self.assertEqual(r['seed'], 1)
  56. self.assertEqual(r['leech'], 3)
  57. self.assertEqual(r['filesize'], 10 * 1024 * 1024)