test_tokyotoshokan.py 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import mock
  2. from collections import defaultdict
  3. from searx.engines import tokyotoshokan
  4. from searx.testing import SearxTestCase
  5. from datetime import datetime
  6. class TestTokyotoshokanEngine(SearxTestCase):
  7. def test_request(self):
  8. query = 'test_query'
  9. dic = defaultdict(dict)
  10. dic['pageno'] = 1
  11. params = tokyotoshokan.request(query, dic)
  12. self.assertTrue('url' in params)
  13. self.assertTrue(query in params['url'])
  14. self.assertTrue('tokyotosho.info' in params['url'])
  15. def test_response(self):
  16. resp = mock.Mock(text='<html></html>')
  17. self.assertEqual(tokyotoshokan.response(resp), [])
  18. html = """
  19. <table class="listing">
  20. <tbody>
  21. <tr class="shade category_0">
  22. <td rowspan="2">
  23. <a href="/?cat=7"><span class="sprite_cat-raw"></span></a>
  24. </td>
  25. <td class="desc-top">
  26. <a href="magnet:?xt=urn:btih:4c19eb46b5113685fbd2288ed2531b0b">
  27. <span class="sprite_magnet"></span>
  28. </a>
  29. <a rel="nofollow" type="application/x-bittorrent" href="http://www.nyaa.se/f">
  30. Koyomimonogatari
  31. </a>
  32. </td>
  33. <td class="web"><a rel="nofollow" href="details.php?id=975700">Details</a></td>
  34. </tr>
  35. <tr class="shade category_0">
  36. <td class="desc-bot">
  37. Authorized: <span class="auth_ok">Yes</span>
  38. Submitter: <a href="?username=Ohys">Ohys</a> |
  39. Size: 10.5MB |
  40. Date: 2016-03-26 16:41 UTC |
  41. Comment: sample comment
  42. </td>
  43. <td style="color: #BBB; font-family: monospace" class="stats" align="right">
  44. S: <span style="color: red">53</span>
  45. L: <span style="color: red">18</span>
  46. C: <span style="color: red">0</span>
  47. ID: 975700
  48. </td>
  49. </tr>
  50. <tr class="category_0">
  51. <td rowspan="2">
  52. <a href="/?cat=7"><span class="sprite_cat-raw"></span></a>
  53. </td>
  54. <td class="desc-top">
  55. <a rel="nofollow" type="application/x-bittorrent" href="http://google.com/q">
  56. Owarimonogatari
  57. </a>
  58. </td>
  59. <td class="web"><a rel="nofollow" href="details.php?id=975700">Details</a></td>
  60. </tr>
  61. <tr class="category_0">
  62. <td class="desc-bot">
  63. Submitter: <a href="?username=Ohys">Ohys</a> |
  64. Size: 932.84EB |
  65. Date: QWERTY-03-26 16:41 UTC
  66. </td>
  67. <td style="color: #BBB; font-family: monospace" class="stats" align="right">
  68. S: <span style="color: red">0</span>
  69. </td>
  70. </tr>
  71. </tbody>
  72. </table>
  73. """
  74. resp = mock.Mock(text=html)
  75. results = tokyotoshokan.response(resp)
  76. self.assertEqual(type(results), list)
  77. self.assertEqual(len(results), 2)
  78. # testing the first result, which has correct format
  79. # and should have all information fields filled
  80. r = results[0]
  81. self.assertEqual(r['url'], 'http://www.nyaa.se/f')
  82. self.assertEqual(r['title'], 'Koyomimonogatari')
  83. self.assertEqual(r['magnetlink'], 'magnet:?xt=urn:btih:4c19eb46b5113685fbd2288ed2531b0b')
  84. self.assertEqual(r['filesize'], int(1024 * 1024 * 10.5))
  85. self.assertEqual(r['publishedDate'], datetime(2016, 03, 26, 16, 41))
  86. self.assertEqual(r['content'], 'Comment: sample comment')
  87. self.assertEqual(r['seed'], 53)
  88. self.assertEqual(r['leech'], 18)
  89. # testing the second result, which does not include magnet link,
  90. # seed & leech info, and has incorrect size & creation date
  91. r = results[1]
  92. self.assertEqual(r['url'], 'http://google.com/q')
  93. self.assertEqual(r['title'], 'Owarimonogatari')
  94. self.assertFalse('magnetlink' in r)
  95. self.assertFalse('filesize' in r)
  96. self.assertFalse('content' in r)
  97. self.assertFalse('publishedDate' in r)
  98. self.assertFalse('seed' in r)
  99. self.assertFalse('leech' in r)