2016-03-25 13:30:32 +00:00
|
|
|
from collections import defaultdict
|
|
|
|
import mock
|
|
|
|
from searx.engines import reddit
|
|
|
|
from searx.testing import SearxTestCase
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
class TestRedditEngine(SearxTestCase):
|
|
|
|
|
|
|
|
def test_request(self):
|
|
|
|
query = 'test_query'
|
|
|
|
dic = defaultdict(dict)
|
|
|
|
params = reddit.request(query, dic)
|
|
|
|
self.assertTrue('url' in params)
|
|
|
|
self.assertTrue(query in params['url'])
|
|
|
|
self.assertTrue('reddit.com' in params['url'])
|
|
|
|
|
|
|
|
def test_response(self):
|
|
|
|
resp = mock.Mock(text='{}')
|
|
|
|
self.assertEqual(reddit.response(resp), [])
|
|
|
|
|
|
|
|
json = """
|
|
|
|
{
|
|
|
|
"kind": "Listing",
|
|
|
|
"data": {
|
|
|
|
"children": [{
|
|
|
|
"data": {
|
2016-04-16 10:45:49 +00:00
|
|
|
"url": "http://google2.com/",
|
|
|
|
"permalink": "http://google.com/",
|
2016-03-25 13:30:32 +00:00
|
|
|
"title": "Title number one",
|
|
|
|
"selftext": "Sample",
|
|
|
|
"created_utc": 1401219957.0,
|
|
|
|
"thumbnail": "http://image.com/picture.jpg"
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
"data": {
|
2016-04-16 10:45:49 +00:00
|
|
|
"url": "https://reddit2.com/",
|
|
|
|
"permalink": "https://reddit.com/",
|
2016-03-25 13:30:32 +00:00
|
|
|
"title": "Title number two",
|
|
|
|
"selftext": "Dominus vobiscum",
|
|
|
|
"created_utc": 1438792533.0,
|
|
|
|
"thumbnail": "self"
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
resp = mock.Mock(text=json)
|
|
|
|
results = reddit.response(resp)
|
|
|
|
|
|
|
|
self.assertEqual(len(results), 2)
|
|
|
|
self.assertEqual(type(results), list)
|
|
|
|
|
|
|
|
# testing first result (picture)
|
|
|
|
r = results[0]
|
|
|
|
self.assertEqual(r['url'], 'http://google.com/')
|
|
|
|
self.assertEqual(r['title'], 'Title number one')
|
|
|
|
self.assertEqual(r['template'], 'images.html')
|
2016-04-16 10:45:49 +00:00
|
|
|
self.assertEqual(r['img_src'], 'http://google2.com/')
|
2016-03-25 13:30:32 +00:00
|
|
|
self.assertEqual(r['thumbnail_src'], 'http://image.com/picture.jpg')
|
|
|
|
|
|
|
|
# testing second result (self-post)
|
|
|
|
r = results[1]
|
|
|
|
self.assertEqual(r['url'], 'https://reddit.com/')
|
|
|
|
self.assertEqual(r['title'], 'Title number two')
|
|
|
|
self.assertEqual(r['content'], 'Dominus vobiscum')
|
|
|
|
created = datetime.fromtimestamp(1438792533.0)
|
|
|
|
self.assertEqual(r['publishedDate'], created)
|
|
|
|
self.assertTrue('thumbnail_src' not in r)
|
2016-04-16 10:45:49 +00:00
|
|
|
self.assertTrue('img_src' not in r)
|