1
0
mirror of https://github.com/searxng/searxng.git synced 2024-11-22 12:10:11 +01:00

Deviant Art's unit test

This commit is contained in:
Cqoicebordel 2015-01-29 01:13:33 +01:00
parent 1d255061c7
commit d495704551
4 changed files with 168 additions and 4 deletions

View File

@ -14,6 +14,7 @@ from urllib import urlencode
from urlparse import urljoin
from lxml import html
import re
from searx.engines.xpath import extract_text
# engine dependent config
categories = ['images']
@ -50,9 +51,9 @@ def response(resp):
for result in dom.xpath('//div[contains(@class, "tt-a tt-fh")]'):
link = result.xpath('.//a[contains(@class, "thumb")]')[0]
url = urljoin(base_url, link.attrib.get('href'))
title_links = result.xpath('.//span[@class="details"]//a[contains(@class, "t")]') # noqa
title = ''.join(title_links[0].xpath('.//text()'))
thumbnail_src = link.xpath('.//img')[0].attrib['src']
title_links = result.xpath('.//span[@class="details"]//a[contains(@class, "t")]')
title = extract_text(title_links[0])
thumbnail_src = link.xpath('.//img')[0].attrib.get('src')
img_src = regex.sub('/', thumbnail_src)
# append result

View File

@ -0,0 +1,118 @@
from collections import defaultdict
import mock
from searx.engines import deviantart
from searx.testing import SearxTestCase
class TestDeviantartEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 0
params = deviantart.request(query, dicto)
self.assertTrue('url' in params)
self.assertTrue(query in params['url'])
self.assertTrue('deviantart.com' in params['url'])
def test_response(self):
self.assertRaises(AttributeError, deviantart.response, None)
self.assertRaises(AttributeError, deviantart.response, [])
self.assertRaises(AttributeError, deviantart.response, '')
self.assertRaises(AttributeError, deviantart.response, '[]')
response = mock.Mock(text='<html></html>')
self.assertEqual(deviantart.response(response), [])
response = mock.Mock(status_code=302)
self.assertEqual(deviantart.response(response), [])
html = """
<div class="tt-a tt-fh tt-boxed" collect_rid="1:149167425"
usericon="http://a.deviantart.net/avatars/t/e/test-0.gif" userid="233301"
username="test-0" symbol="~" category="digitalart/animation">
<span class="tt-w" style="width: auto; max-width: 277px;">
<span class="tt-fh-tc" style="width: 202px;">
<span class="tt-bb" style="width: 202px;">
</span>
<span class="shadow">
<a class="thumb" href="http://url.of.result/2nd.part.of.url"
title="Behoimi BE Animation Test by test-0, Jan 4,
2010 in Digital Art &gt; Animation"> <i></i>
<img width="200" height="200" alt="Test"
src="http://url.of.thumbnail" data-src="http://th08.deviantart.net/test.jpg">
</a>
</span>
<!-- ^TTT -->
</span>
<span class="details">
<a href="http://test-0.deviantart.com/art/Test" class="t"
title="Behoimi BE Animation Test by test-0, Jan 4, 2010">
<span class="tt-fh-oe">Title of image</span> </a>
<small>
<span class="category">
<span class="age">
5 years ago
</span>
in <a title="Behoimi BE Animation Test by test-0, Jan 4, 2010"
href="http://www.deviantart.com/browse/all/digitalart/animation/">Animation</a>
</span>
<div class="commentcount">
<a href="http://test-0.deviantart.com/art/Test#comments">
<span class="iconcommentsstats"></span>9 Comments</a>
</div>
<a class="mlt-link" href="http://www.deviantart.com/morelikethis/149167425">
<span class="mlt-icon"></span> <span class="mlt-text">More Like This</span> </a>
</span>
</small> <!-- TTT$ -->
</span>
</div>
"""
response = mock.Mock(text=html)
results = deviantart.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], 'Title of image')
self.assertEqual(results[0]['url'], 'http://url.of.result/2nd.part.of.url')
self.assertNotIn('content', results[0])
self.assertEqual(results[0]['thumbnail_src'], 'http://url.of.thumbnail')
html = """
<span class="tt-fh-tc" style="width: 202px;">
<span class="tt-bb" style="width: 202px;">
</span>
<span class="shadow">
<a class="thumb" href="http://url.of.result/2nd.part.of.url"
title="Behoimi BE Animation Test by test-0, Jan 4,
2010 in Digital Art &gt; Animation"> <i></i>
<img width="200" height="200" alt="Test"
src="http://url.of.thumbnail" data-src="http://th08.deviantart.net/test.jpg">
</a>
</span>
<!-- ^TTT -->
</span>
<span class="details">
<a href="http://test-0.deviantart.com/art/Test" class="t"
title="Behoimi BE Animation Test by test-0, Jan 4, 2010">
<span class="tt-fh-oe">Title of image</span> </a>
<small>
<span class="category">
<span class="age">
5 years ago
</span>
in <a title="Behoimi BE Animation Test by test-0, Jan 4, 2010"
href="http://www.deviantart.com/browse/all/digitalart/animation/">Animation</a>
</span>
<div class="commentcount">
<a href="http://test-0.deviantart.com/art/Test#comments">
<span class="iconcommentsstats"></span>9 Comments</a>
</div>
<a class="mlt-link" href="http://www.deviantart.com/morelikethis/149167425">
<span class="mlt-icon"></span> <span class="mlt-text">More Like This</span> </a>
</span>
</small> <!-- TTT$ -->
"""
response = mock.Mock(text=html)
results = deviantart.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)

View File

@ -32,9 +32,53 @@ class TestDiggEngine(SearxTestCase):
"status": "ok",
"num": 10,
"next_position": 20,
"html": "<article itemscope itemtype=\\"http://schema.org/Article\\" class=\\"story-container digg-story-el hentry entry story-1sRANah col-1\\" data-content-id=\\"1sRANah\\" data-contenturl=\\"http://url.of.link\\" data-position=\\"0\\" data-diggs=\\"24\\" data-tweets=\\"69\\" data-digg-score=\\"1190\\"> <div class=\\"story-image story-image-thumb\\"> <a data-position=\\"0\\" data-content-id=\\"1sRANah\\" class=\\"story-link\\" href=\\"http://www.thedailybeast.com/\\" target=\\"_blank\\"><img class=\\"story-image-img\\" src=\\"http://url.of.image.jpeg\\" width=\\"312\\" height=\\"170\\" alt=\\"\\" /> </a> </div> <div class=\\"story-content\\"><header class=\\"story-header\\"> <div itemprop=\\"alternativeHeadline\\" class=\\"story-kicker\\" >Kicker</div> <h2 itemprop=\\"headline\\" class=\\"story-title entry-title\\"><a class=\\"story-title-link story-link\\" rel=\\"bookmark\\" itemprop=\\"url\\" href=\\"http://www.thedailybeast.com/\\" target=\\"_blank\\">Title of article</h2> <div class=\\"story-meta\\"><div class=\\"story-score \\"> <div class=\\"story-score-diggscore diggscore-1sRANah\\">1190</div> <div class=\\"story-score-details\\"> <div class=\\"arrow\\"></div><ul class=\\"story-score-details-list\\"> <li class=\\"story-score-detail story-score-diggs\\"><span class=\\"label\\">Diggs:</span> <span class=\\"count diggs-1sRANah\\">24</span></li> <li class=\\"story-score-detail story-score-twitter\\"><span class=\\"label\\">Tweets:</span> <span class=\\"count tweets-1sRANah\\">69</span></li> <li class=\\"story-score-detail story-score-facebook\\"><span class=\\"label\\">Facebook Shares:</span> <span class=\\"count fb_shares-1sRANah\\">1097</span></li> </ul> </div> </div> <span class=\\"story-meta-item story-source\\"> <a itemprop=\\"publisher copyrightHolder sourceOrganization provider\\" class=\\"story-meta-item-link story-source-link\\" href=\\"/source/thedailybeast.com\\">The Daily Beast </a> </span> <span class=\\"story-meta-item story-tag first-tag\\"> <a itemprop=\\"keywords\\" rel=\\"tag\\" class=\\"story-meta-item-link story-tag-link\\" href=\\"/tag/news\\">News</a> </span> <abbr class=\\"published story-meta-item story-timestamp\\" title=\\"2014-10-18 14:53:45\\"> <time datetime=\\"2014-10-18 14:53:45\\">18 Oct 2014</time> </abbr> </div> </header> </div> <ul class=\\"story-actions\\"> <li class=\\"story-action story-action-digg btn-story-action-container\\"><a class=\\"target digg-1sRANah\\" href=\\"#\\">Digg</a></li> <li class=\\"story-action story-action-save btn-story-action-container\\"><a class=\\"target save-1sRANah\\" href=\\"#\\">Save</a></li> <li class=\\"story-action story-action-share\\"><a class=\\"target share-facebook\\" href=\\"https://www.facebook.com/\\">Facebook</a></li> <li class=\\"story-action story-action-share\\"><a class=\\"target share-twitter\\" href=\\"https://twitter.com/\\">Twitter</a></li> </ul> </article>"
"html": "<article itemscope itemtype=\\"http://schema.org/Article\\"
class=\\"story-container digg-story-el hentry entry story-1sRANah col-1\\"
data-content-id=\\"1sRANah\\" data-contenturl=\\"http://url.of.link\\"
data-position=\\"0\\" data-diggs=\\"24\\" data-tweets=\\"69\\"
data-digg-score=\\"1190\\"> <div class=\\"story-image story-image-thumb\\">
<a data-position=\\"0\\" data-content-id=\\"1sRANah\\"
class=\\"story-link\\" href=\\"http://www.thedailybeast.com/\\"
target=\\"_blank\\"><img class=\\"story-image-img\\"
src=\\"http://url.of.image.jpeg\\" width=\\"312\\" height=\\"170\\"
alt=\\"\\" /> </a> </div> <div class=\\"story-content\\"><header
class=\\"story-header\\"> <div itemprop=\\"alternativeHeadline\\"
class=\\"story-kicker\\" >Kicker</div> <h2 itemprop=\\"headline\\"
class=\\"story-title entry-title\\"><a class=\\"story-title-link story-link\\"
rel=\\"bookmark\\" itemprop=\\"url\\" href=\\"http://www.thedailybeast.com/\\"
target=\\"_blank\\">Title of article</h2> <div class=\\"story-meta\\">
<div class=\\"story-score \\">
<div class=\\"story-score-diggscore diggscore-1sRANah\\">1190</div>
<div class=\\"story-score-details\\"> <div class=\\"arrow\\"></div>
<ul class=\\"story-score-details-list\\"> <li
class=\\"story-score-detail story-score-diggs\\"><span
class=\\"label\\">Diggs:</span> <span class=\\"count diggs-1sRANah\\">24</span>
</li> <li class=\\"story-score-detail story-score-twitter\\"><span
class=\\"label\\">Tweets:</span> <span class=\\"count tweets-1sRANah\\">69</span>
</li> <li class=\\"story-score-detail story-score-facebook\\"><span
class=\\"label\\">Facebook Shares:</span> <span
class=\\"count fb_shares-1sRANah\\">1097</span></li> </ul> </div> </div>
<span class=\\"story-meta-item story-source\\"> <a
itemprop=\\"publisher copyrightHolder sourceOrganization provider\\"
class=\\"story-meta-item-link story-source-link\\"
href=\\"/source/thedailybeast.com\\">The Daily Beast </a> </span>
<span class=\\"story-meta-item story-tag first-tag\\"> <a
itemprop=\\"keywords\\" rel=\\"tag\\"
class=\\"story-meta-item-link story-tag-link\\" href=\\"/tag/news\\">News</a>
</span> <abbr class=\\"published story-meta-item story-timestamp\\"
title=\\"2014-10-18 14:53:45\\"> <time datetime=\\"2014-10-18 14:53:45\\">18 Oct 2014</time>
</abbr> </div> </header> </div> <ul class=\\"story-actions\\"> <li
class=\\"story-action story-action-digg btn-story-action-container\\">
<a class=\\"target digg-1sRANah\\" href=\\"#\\">Digg</a></li> <li
class=\\"story-action story-action-save btn-story-action-container\\">
<a class=\\"target save-1sRANah\\" href=\\"#\\">Save</a></li> <li
class=\\"story-action story-action-share\\"><a
class=\\"target share-facebook\\" href=\\"https://www.facebook.com/\\">Facebook</a></li>
<li class=\\"story-action story-action-share\\"><a class=\\"target share-twitter\\"
href=\\"https://twitter.com/\\">Twitter</a></li> </ul> </article>"
}
"""
json = json.replace('\r\n', '').replace('\n', '').replace('\r', '')
response = mock.Mock(text=json)
results = digg.response(response)
self.assertEqual(type(results), list)

View File

@ -1,6 +1,7 @@
from searx.tests.engines.test_bing import * # noqa
from searx.tests.engines.test_dailymotion import * # noqa
from searx.tests.engines.test_deezer import * # noqa
from searx.tests.engines.test_deviantart import * # noqa
from searx.tests.engines.test_digg import * # noqa
from searx.tests.engines.test_dummy import * # noqa
from searx.tests.engines.test_flickr import * # noqa