summaryrefslogtreecommitdiff
path: root/tests/unit/engines/test_tokyotoshokan.py
blob: b5c6fad17b25b358cf8592e0856a90b737db5977 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import mock
from collections import defaultdict
from searx.engines import tokyotoshokan
from searx.testing import SearxTestCase
from datetime import datetime


class TestTokyotoshokanEngine(SearxTestCase):

    def test_request(self):
        query = 'test_query'
        dic = defaultdict(dict)
        dic['pageno'] = 1
        params = tokyotoshokan.request(query, dic)
        self.assertTrue('url' in params)
        self.assertTrue(query in params['url'])
        self.assertTrue('tokyotosho.info' in params['url'])

    def test_response(self):
        resp = mock.Mock(text='<html></html>')
        self.assertEqual(tokyotoshokan.response(resp), [])

        html = """
        <table class="listing">
          <tbody>
            <tr class="shade category_0">
              <td rowspan="2">
                <a href="/?cat=7"><span class="sprite_cat-raw"></span></a>
              </td>
              <td class="desc-top">
                <a href="magnet:?xt=urn:btih:4c19eb46b5113685fbd2288ed2531b0b">
                  <span class="sprite_magnet"></span>
                </a>
                <a rel="nofollow" type="application/x-bittorrent" href="http://www.nyaa.se/f">
                  Koyomimonogatari
                </a>
              </td>
              <td class="web"><a rel="nofollow" href="details.php?id=975700">Details</a></td>
            </tr>
            <tr class="shade category_0">
              <td class="desc-bot">
                Authorized: <span class="auth_ok">Yes</span>
                Submitter: <a href="?username=Ohys">Ohys</a> |
                Size: 10.5MB |
                Date: 2016-03-26 16:41 UTC |
                Comment: sample comment
              </td>
              <td style="color: #BBB; font-family: monospace" class="stats" align="right">
                S: <span style="color: red">53</span>
                L: <span style="color: red">18</span>
                C: <span style="color: red">0</span>
                ID: 975700
              </td>
            </tr>

            <tr class="category_0">
              <td rowspan="2">
                <a href="/?cat=7"><span class="sprite_cat-raw"></span></a>
              </td>
              <td class="desc-top">
                <a rel="nofollow" type="application/x-bittorrent" href="http://google.com/q">
                  Owarimonogatari
                </a>
              </td>
              <td class="web"><a rel="nofollow" href="details.php?id=975700">Details</a></td>
            </tr>
            <tr class="category_0">
              <td class="desc-bot">
                Submitter: <a href="?username=Ohys">Ohys</a> |
                Size: 932.84EB |
                Date: QWERTY-03-26 16:41 UTC
              </td>
              <td style="color: #BBB; font-family: monospace" class="stats" align="right">
                S: <span style="color: red">0</span>
              </td>
            </tr>
          </tbody>
        </table>
        """

        resp = mock.Mock(text=html)
        results = tokyotoshokan.response(resp)

        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 2)

        # testing the first result, which has correct format
        # and should have all information fields filled
        r = results[0]
        self.assertEqual(r['url'], 'http://www.nyaa.se/f')
        self.assertEqual(r['title'], 'Koyomimonogatari')
        self.assertEqual(r['magnetlink'], 'magnet:?xt=urn:btih:4c19eb46b5113685fbd2288ed2531b0b')
        self.assertEqual(r['filesize'], int(1024 * 1024 * 10.5))
        self.assertEqual(r['publishedDate'], datetime(2016, 3, 26, 16, 41))
        self.assertEqual(r['content'], 'Comment: sample comment')
        self.assertEqual(r['seed'], 53)
        self.assertEqual(r['leech'], 18)

        # testing the second result, which does not include magnet link,
        # seed & leech info, and has incorrect size & creation date
        r = results[1]
        self.assertEqual(r['url'], 'http://google.com/q')
        self.assertEqual(r['title'], 'Owarimonogatari')

        self.assertFalse('magnetlink' in r)
        self.assertFalse('filesize' in r)
        self.assertFalse('content' in r)
        self.assertFalse('publishedDate' in r)
        self.assertFalse('seed' in r)
        self.assertFalse('leech' in r)