Browse Source

Kickass' unit test

Cqoicebordel 10 years ago
parent
commit
8ea749d6ec
3 changed files with 403 additions and 4 deletions
  1. 4 4
      searx/engines/kickass.py
  2. 398 0
      searx/tests/engines/test_kickass.py
  3. 1 0
      searx/tests/test_engines.py

+ 4 - 4
searx/engines/kickass.py

@@ -13,6 +13,7 @@ from cgi import escape
 from urllib import quote
 from lxml import html
 from operator import itemgetter
+from searx.engines.xpath import extract_text
 
 # engine dependent config
 categories = ['videos', 'music', 'files']
@@ -56,9 +57,8 @@ def response(resp):
     for result in search_res[1:]:
         link = result.xpath('.//a[@class="cellMainLink"]')[0]
         href = urljoin(url, link.attrib['href'])
-        title = ' '.join(link.xpath('.//text()'))
-        content = escape(html.tostring(result.xpath(content_xpath)[0],
-                                       method="text"))
+        title = extract_text(link)
+        content = escape(extract_text(result.xpath(content_xpath)))
         seed = result.xpath('.//td[contains(@class, "green")]/text()')[0]
         leech = result.xpath('.//td[contains(@class, "red")]/text()')[0]
         filesize = result.xpath('.//td[contains(@class, "nobr")]/text()')[0]
@@ -88,7 +88,7 @@ def response(resp):
                 filesize = int(filesize * 1024 * 1024 * 1024)
             elif filesize_multiplier == 'MB':
                 filesize = int(filesize * 1024 * 1024)
-            elif filesize_multiplier == 'kb':
+            elif filesize_multiplier == 'KB':
                 filesize = int(filesize * 1024)
         except:
             filesize = None

+ 398 - 0
searx/tests/engines/test_kickass.py

@@ -0,0 +1,398 @@
+# -*- coding: utf-8 -*-
+from collections import defaultdict
+import mock
+from searx.engines import kickass
+from searx.testing import SearxTestCase
+
+
+class TestKickassEngine(SearxTestCase):
+
+    def test_request(self):
+        query = 'test_query'
+        dicto = defaultdict(dict)
+        dicto['pageno'] = 1
+        params = kickass.request(query, dicto)
+        self.assertIn('url', params)
+        self.assertIn(query, params['url'])
+        self.assertIn('kickass.so', params['url'])
+        self.assertIn('verify', params)
+        self.assertFalse(params['verify'])
+
+    def test_response(self):
+        self.assertRaises(AttributeError, kickass.response, None)
+        self.assertRaises(AttributeError, kickass.response, [])
+        self.assertRaises(AttributeError, kickass.response, '')
+        self.assertRaises(AttributeError, kickass.response, '[]')
+
+        response = mock.Mock(text='<html></html>')
+        self.assertEqual(kickass.response(response), [])
+
+        html = """
+        <table cellpadding="0" cellspacing="0" class="data" style="width: 100%">
+            <tr class="firstr">
+                <th class="width100perc nopad">torrent name</th>
+                <th class="center">
+                    <a href="/search/test/?field=size&sorder=desc" rel="nofollow">size</a>
+                </th>
+                <th class="center"><span class="files">
+                    <a href="/search/test/?field=files_count&sorder=desc" rel="nofollow">files</a></span>
+                </th>
+                <th class="center"><span>
+                    <a href="/search/test/?field=time_add&sorder=desc" rel="nofollow">age</a></span>
+                </th>
+                <th class="center"><span class="seed">
+                    <a href="/search/test/?field=seeders&sorder=desc" rel="nofollow">seed</a></span>
+                </th>
+                <th class="lasttd nobr center">
+                    <a href="/search/test/?field=leechers&sorder=desc" rel="nofollow">leech</a>
+                </th>
+            </tr>
+            <tr class="even" id="torrent_test6478745">
+                <td>
+                    <div class="iaconbox center floatright">
+                        <a rel="6478745,0" class="icommentjs icon16" href="/test-t6478745.html#comment">
+                            <em style="font-size: 12px; margin: 0 4px 0 4px;" class="iconvalue">3</em>
+                            <i class="ka ka-comment"></i>
+                        </a>
+                        <a class="iverify icon16" href="/test-t6478745.html" title="Verified Torrent">
+                            <i class="ka ka16 ka-verify ka-green"></i>
+                        </a>
+                        <a href="#" onclick="_scq.push([]); return false;" class="partner1Button idownload icon16">
+                            <i class="ka ka16 ka-arrow-down partner1Button"></i>
+                        </a>
+                        <a title="Torrent magnet link"
+                            href="magnet:?xt=urn:btih:MAGNETURL&dn=test" class="imagnet icon16">
+                            <i class="ka ka16 ka-magnet"></i>
+                        </a>
+                        <a title="Download torrent file"
+                            href="http://torcache.net/torrent/53917.torrent?title=test" class="idownload icon16">
+                            <i class="ka ka16 ka-arrow-down"></i>
+                        </a>
+                    </div>
+                    <div class="torrentname">
+                    <a href="/test-t6478745.html" class="torType txtType"></a>
+                    <a href="/test-t6478745.html" class="normalgrey font12px plain bold"></a>
+                    <div class="markeredBlock torType txtType">
+                        <a href="/url.html" class="cellMainLink">
+                            <strong class="red">This should be the title</strong>
+                        </a>
+                        <span class="font11px lightgrey block">
+                            Posted by <i class="ka ka-verify" style="font-size: 16px;color:orange;"></i>
+                            <a class="plain" href="/user/riri/">riri</a> in
+                            <span id="cat_6478745">
+                                <strong><a href="/other/">Other</a> > <a href="/unsorted/">Unsorted</a></strong>
+                            </span>
+                        </span>
+                    </div>
+                </td>
+                <td class="nobr center">449 <span>bytes</span></td>
+                <td class="center">4</td>
+                <td class="center">2&nbsp;years</td>
+                <td class="green center">10</td>
+                <td class="red lasttd center">1</td>
+            </tr>
+        </table>
+        """
+        response = mock.Mock(text=html)
+        results = kickass.response(response)
+        self.assertEqual(type(results), list)
+        self.assertEqual(len(results), 1)
+        self.assertEqual(results[0]['title'], 'This should be the title')
+        self.assertEqual(results[0]['url'], 'https://kickass.so/url.html')
+        self.assertEqual(results[0]['content'], 'Posted by riri in Other &gt; Unsorted')
+        self.assertEqual(results[0]['seed'], 10)
+        self.assertEqual(results[0]['leech'], 1)
+        self.assertEqual(results[0]['filesize'], 449)
+        self.assertEqual(results[0]['files'], 4)
+        self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:MAGNETURL&dn=test')
+        self.assertEqual(results[0]['torrentfile'], 'http://torcache.net/torrent/53917.torrent?title=test')
+
+        html = """
+        <table cellpadding="0" cellspacing="0" class="data" style="width: 100%">
+            <tr class="firstr">
+                <th class="width100perc nopad">torrent name</th>
+                <th class="center">
+                    <a href="/search/test/?field=size&sorder=desc" rel="nofollow">size</a>
+                </th>
+                <th class="center"><span class="files">
+                    <a href="/search/test/?field=files_count&sorder=desc" rel="nofollow">files</a></span>
+                </th>
+                <th class="center"><span>
+                    <a href="/search/test/?field=time_add&sorder=desc" rel="nofollow">age</a></span>
+                </th>
+                <th class="center"><span class="seed">
+                    <a href="/search/test/?field=seeders&sorder=desc" rel="nofollow">seed</a></span>
+                </th>
+                <th class="lasttd nobr center">
+                    <a href="/search/test/?field=leechers&sorder=desc" rel="nofollow">leech</a>
+                </th>
+            </tr>
+        </table>
+        """
+        response = mock.Mock(text=html)
+        results = kickass.response(response)
+        self.assertEqual(type(results), list)
+        self.assertEqual(len(results), 0)
+
+        html = """
+        <table cellpadding="0" cellspacing="0" class="data" style="width: 100%">
+            <tr class="firstr">
+                <th class="width100perc nopad">torrent name</th>
+                <th class="center">
+                    <a href="/search/test/?field=size&sorder=desc" rel="nofollow">size</a>
+                </th>
+                <th class="center"><span class="files">
+                    <a href="/search/test/?field=files_count&sorder=desc" rel="nofollow">files</a></span>
+                </th>
+                <th class="center"><span>
+                    <a href="/search/test/?field=time_add&sorder=desc" rel="nofollow">age</a></span>
+                </th>
+                <th class="center"><span class="seed">
+                    <a href="/search/test/?field=seeders&sorder=desc" rel="nofollow">seed</a></span>
+                </th>
+                <th class="lasttd nobr center">
+                    <a href="/search/test/?field=leechers&sorder=desc" rel="nofollow">leech</a>
+                </th>
+            </tr>
+            <tr class="even" id="torrent_test6478745">
+                <td>
+                    <div class="iaconbox center floatright">
+                        <a rel="6478745,0" class="icommentjs icon16" href="/test-t6478745.html#comment">
+                            <em style="font-size: 12px; margin: 0 4px 0 4px;" class="iconvalue">3</em>
+                            <i class="ka ka-comment"></i>
+                        </a>
+                        <a class="iverify icon16" href="/test-t6478745.html" title="Verified Torrent">
+                            <i class="ka ka16 ka-verify ka-green"></i>
+                        </a>
+                        <a href="#" onclick="_scq.push([]); return false;" class="partner1Button idownload icon16">
+                            <i class="ka ka16 ka-arrow-down partner1Button"></i>
+                        </a>
+                        <a title="Torrent magnet link"
+                            href="magnet:?xt=urn:btih:MAGNETURL&dn=test" class="imagnet icon16">
+                            <i class="ka ka16 ka-magnet"></i>
+                        </a>
+                        <a title="Download torrent file"
+                            href="http://torcache.net/torrent/53917.torrent?title=test" class="idownload icon16">
+                            <i class="ka ka16 ka-arrow-down"></i>
+                        </a>
+                    </div>
+                    <div class="torrentname">
+                    <a href="/test-t6478745.html" class="torType txtType"></a>
+                    <a href="/test-t6478745.html" class="normalgrey font12px plain bold"></a>
+                    <div class="markeredBlock torType txtType">
+                        <a href="/url.html" class="cellMainLink">
+                            <strong class="red">This should be the title</strong>
+                        </a>
+                        <span class="font11px lightgrey block">
+                            Posted by <i class="ka ka-verify" style="font-size: 16px;color:orange;"></i>
+                            <a class="plain" href="/user/riri/">riri</a> in
+                            <span id="cat_6478745">
+                                <strong><a href="/other/">Other</a> > <a href="/unsorted/">Unsorted</a></strong>
+                            </span>
+                        </span>
+                    </div>
+                </td>
+                <td class="nobr center">1 <span>KB</span></td>
+                <td class="center">4</td>
+                <td class="center">2&nbsp;years</td>
+                <td class="green center">10</td>
+                <td class="red lasttd center">1</td>
+            </tr>
+            <tr class="even" id="torrent_test6478745">
+                <td>
+                    <div class="iaconbox center floatright">
+                        <a rel="6478745,0" class="icommentjs icon16" href="/test-t6478745.html#comment">
+                            <em style="font-size: 12px; margin: 0 4px 0 4px;" class="iconvalue">3</em>
+                            <i class="ka ka-comment"></i>
+                        </a>
+                        <a class="iverify icon16" href="/test-t6478745.html" title="Verified Torrent">
+                            <i class="ka ka16 ka-verify ka-green"></i>
+                        </a>
+                        <a href="#" onclick="_scq.push([]); return false;" class="partner1Button idownload icon16">
+                            <i class="ka ka16 ka-arrow-down partner1Button"></i>
+                        </a>
+                        <a title="Torrent magnet link"
+                            href="magnet:?xt=urn:btih:MAGNETURL&dn=test" class="imagnet icon16">
+                            <i class="ka ka16 ka-magnet"></i>
+                        </a>
+                        <a title="Download torrent file"
+                            href="http://torcache.net/torrent/53917.torrent?title=test" class="idownload icon16">
+                            <i class="ka ka16 ka-arrow-down"></i>
+                        </a>
+                    </div>
+                    <div class="torrentname">
+                    <a href="/test-t6478745.html" class="torType txtType"></a>
+                    <a href="/test-t6478745.html" class="normalgrey font12px plain bold"></a>
+                    <div class="markeredBlock torType txtType">
+                        <a href="/url.html" class="cellMainLink">
+                            <strong class="red">This should be the title</strong>
+                        </a>
+                        <span class="font11px lightgrey block">
+                            Posted by <i class="ka ka-verify" style="font-size: 16px;color:orange;"></i>
+                            <a class="plain" href="/user/riri/">riri</a> in
+                            <span id="cat_6478745">
+                                <strong><a href="/other/">Other</a> > <a href="/unsorted/">Unsorted</a></strong>
+                            </span>
+                        </span>
+                    </div>
+                </td>
+                <td class="nobr center">1 <span>MB</span></td>
+                <td class="center">4</td>
+                <td class="center">2&nbsp;years</td>
+                <td class="green center">9</td>
+                <td class="red lasttd center">1</td>
+            </tr>
+            <tr class="even" id="torrent_test6478745">
+                <td>
+                    <div class="iaconbox center floatright">
+                        <a rel="6478745,0" class="icommentjs icon16" href="/test-t6478745.html#comment">
+                            <em style="font-size: 12px; margin: 0 4px 0 4px;" class="iconvalue">3</em>
+                            <i class="ka ka-comment"></i>
+                        </a>
+                        <a class="iverify icon16" href="/test-t6478745.html" title="Verified Torrent">
+                            <i class="ka ka16 ka-verify ka-green"></i>
+                        </a>
+                        <a href="#" onclick="_scq.push([]); return false;" class="partner1Button idownload icon16">
+                            <i class="ka ka16 ka-arrow-down partner1Button"></i>
+                        </a>
+                        <a title="Torrent magnet link"
+                            href="magnet:?xt=urn:btih:MAGNETURL&dn=test" class="imagnet icon16">
+                            <i class="ka ka16 ka-magnet"></i>
+                        </a>
+                        <a title="Download torrent file"
+                            href="http://torcache.net/torrent/53917.torrent?title=test" class="idownload icon16">
+                            <i class="ka ka16 ka-arrow-down"></i>
+                        </a>
+                    </div>
+                    <div class="torrentname">
+                    <a href="/test-t6478745.html" class="torType txtType"></a>
+                    <a href="/test-t6478745.html" class="normalgrey font12px plain bold"></a>
+                    <div class="markeredBlock torType txtType">
+                        <a href="/url.html" class="cellMainLink">
+                            <strong class="red">This should be the title</strong>
+                        </a>
+                        <span class="font11px lightgrey block">
+                            Posted by <i class="ka ka-verify" style="font-size: 16px;color:orange;"></i>
+                            <a class="plain" href="/user/riri/">riri</a> in
+                            <span id="cat_6478745">
+                                <strong><a href="/other/">Other</a> > <a href="/unsorted/">Unsorted</a></strong>
+                            </span>
+                        </span>
+                    </div>
+                </td>
+                <td class="nobr center">1 <span>GB</span></td>
+                <td class="center">4</td>
+                <td class="center">2&nbsp;years</td>
+                <td class="green center">8</td>
+                <td class="red lasttd center">1</td>
+            </tr>
+            <tr class="even" id="torrent_test6478745">
+                <td>
+                    <div class="iaconbox center floatright">
+                        <a rel="6478745,0" class="icommentjs icon16" href="/test-t6478745.html#comment">
+                            <em style="font-size: 12px; margin: 0 4px 0 4px;" class="iconvalue">3</em>
+                            <i class="ka ka-comment"></i>
+                        </a>
+                        <a class="iverify icon16" href="/test-t6478745.html" title="Verified Torrent">
+                            <i class="ka ka16 ka-verify ka-green"></i>
+                        </a>
+                        <a href="#" onclick="_scq.push([]); return false;" class="partner1Button idownload icon16">
+                            <i class="ka ka16 ka-arrow-down partner1Button"></i>
+                        </a>
+                        <a title="Torrent magnet link"
+                            href="magnet:?xt=urn:btih:MAGNETURL&dn=test" class="imagnet icon16">
+                            <i class="ka ka16 ka-magnet"></i>
+                        </a>
+                        <a title="Download torrent file"
+                            href="http://torcache.net/torrent/53917.torrent?title=test" class="idownload icon16">
+                            <i class="ka ka16 ka-arrow-down"></i>
+                        </a>
+                    </div>
+                    <div class="torrentname">
+                    <a href="/test-t6478745.html" class="torType txtType"></a>
+                    <a href="/test-t6478745.html" class="normalgrey font12px plain bold"></a>
+                    <div class="markeredBlock torType txtType">
+                        <a href="/url.html" class="cellMainLink">
+                            <strong class="red">This should be the title</strong>
+                        </a>
+                        <span class="font11px lightgrey block">
+                            Posted by <i class="ka ka-verify" style="font-size: 16px;color:orange;"></i>
+                            <a class="plain" href="/user/riri/">riri</a> in
+                            <span id="cat_6478745">
+                                <strong><a href="/other/">Other</a> > <a href="/unsorted/">Unsorted</a></strong>
+                            </span>
+                        </span>
+                    </div>
+                </td>
+                <td class="nobr center">1 <span>TB</span></td>
+                <td class="center">4</td>
+                <td class="center">2&nbsp;years</td>
+                <td class="green center">7</td>
+                <td class="red lasttd center">1</td>
+            </tr>
+            <tr class="even" id="torrent_test6478745">
+                <td>
+                    <div class="iaconbox center floatright">
+                        <a rel="6478745,0" class="icommentjs icon16" href="/test-t6478745.html#comment">
+                            <em style="font-size: 12px; margin: 0 4px 0 4px;" class="iconvalue">3</em>
+                            <i class="ka ka-comment"></i>
+                        </a>
+                        <a class="iverify icon16" href="/test-t6478745.html" title="Verified Torrent">
+                            <i class="ka ka16 ka-verify ka-green"></i>
+                        </a>
+                        <a href="#" onclick="_scq.push([]); return false;" class="partner1Button idownload icon16">
+                            <i class="ka ka16 ka-arrow-down partner1Button"></i>
+                        </a>
+                        <a title="Torrent magnet link"
+                            href="magnet:?xt=urn:btih:MAGNETURL&dn=test" class="imagnet icon16">
+                            <i class="ka ka16 ka-magnet"></i>
+                        </a>
+                        <a title="Download torrent file"
+                            href="http://torcache.net/torrent/53917.torrent?title=test" class="idownload icon16">
+                            <i class="ka ka16 ka-arrow-down"></i>
+                        </a>
+                    </div>
+                    <div class="torrentname">
+                    <a href="/test-t6478745.html" class="torType txtType"></a>
+                    <a href="/test-t6478745.html" class="normalgrey font12px plain bold"></a>
+                    <div class="markeredBlock torType txtType">
+                        <a href="/url.html" class="cellMainLink">
+                            <strong class="red">This should be the title</strong>
+                        </a>
+                        <span class="font11px lightgrey block">
+                            Posted by <i class="ka ka-verify" style="font-size: 16px;color:orange;"></i>
+                            <a class="plain" href="/user/riri/">riri</a> in
+                            <span id="cat_6478745">
+                                <strong><a href="/other/">Other</a> > <a href="/unsorted/">Unsorted</a></strong>
+                            </span>
+                        </span>
+                    </div>
+                </td>
+                <td class="nobr center">z <span>bytes</span></td>
+                <td class="center">r</td>
+                <td class="center">2&nbsp;years</td>
+                <td class="green center">a</td>
+                <td class="red lasttd center">t</td>
+            </tr>
+        </table>
+        """
+        response = mock.Mock(text=html)
+        results = kickass.response(response)
+        self.assertEqual(type(results), list)
+        self.assertEqual(len(results), 5)
+        self.assertEqual(results[0]['title'], 'This should be the title')
+        self.assertEqual(results[0]['url'], 'https://kickass.so/url.html')
+        self.assertEqual(results[0]['content'], 'Posted by riri in Other &gt; Unsorted')
+        self.assertEqual(results[0]['seed'], 10)
+        self.assertEqual(results[0]['leech'], 1)
+        self.assertEqual(results[0]['files'], 4)
+        self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:MAGNETURL&dn=test')
+        self.assertEqual(results[0]['torrentfile'], 'http://torcache.net/torrent/53917.torrent?title=test')
+        self.assertEqual(results[0]['filesize'], 1024)
+        self.assertEqual(results[1]['filesize'], 1048576)
+        self.assertEqual(results[2]['filesize'], 1073741824)
+        self.assertEqual(results[3]['filesize'], 1099511627776)
+        self.assertEqual(results[4]['seed'], 0)
+        self.assertEqual(results[4]['leech'], 0)
+        self.assertEqual(results[4]['files'], None)
+        self.assertEqual(results[4]['filesize'], None)

+ 1 - 0
searx/tests/test_engines.py

@@ -9,6 +9,7 @@ from searx.tests.engines.test_digg import *  # noqa
 from searx.tests.engines.test_dummy import *  # noqa
 from searx.tests.engines.test_flickr import *  # noqa
 from searx.tests.engines.test_github import *  # noqa
+from searx.tests.engines.test_kickass import *  # noqa
 from searx.tests.engines.test_mixcloud import *  # noqa
 from searx.tests.engines.test_searchcode_code import *  # noqa
 from searx.tests.engines.test_searchcode_doc import *  # noqa