1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import reddit
  4. from searx.testing import SearxTestCase
  5. from datetime import datetime
  6. class TestRedditEngine(SearxTestCase):
  7. def test_request(self):
  8. query = 'test_query'
  9. dic = defaultdict(dict)
  10. params = reddit.request(query, dic)
  11. self.assertTrue('url' in params)
  12. self.assertTrue(query in params['url'])
  13. self.assertTrue('reddit.com' in params['url'])
  14. def test_response(self):
  15. resp = mock.Mock(text='{}')
  16. self.assertEqual(reddit.response(resp), [])
  17. json = """
  18. {
  19. "kind": "Listing",
  20. "data": {
  21. "children": [{
  22. "data": {
  23. "url": "http://google2.com/",
  24. "permalink": "http://google.com/",
  25. "title": "Title number one",
  26. "selftext": "Sample",
  27. "created_utc": 1401219957.0,
  28. "thumbnail": "http://image.com/picture.jpg"
  29. }
  30. }, {
  31. "data": {
  32. "url": "https://reddit2.com/",
  33. "permalink": "https://reddit.com/",
  34. "title": "Title number two",
  35. "selftext": "Dominus vobiscum",
  36. "created_utc": 1438792533.0,
  37. "thumbnail": "self"
  38. }
  39. }]
  40. }
  41. }
  42. """
  43. resp = mock.Mock(text=json)
  44. results = reddit.response(resp)
  45. self.assertEqual(len(results), 2)
  46. self.assertEqual(type(results), list)
  47. # testing first result (picture)
  48. r = results[0]
  49. self.assertEqual(r['url'], 'http://google.com/')
  50. self.assertEqual(r['title'], 'Title number one')
  51. self.assertEqual(r['template'], 'images.html')
  52. self.assertEqual(r['img_src'], 'http://google2.com/')
  53. self.assertEqual(r['thumbnail_src'], 'http://image.com/picture.jpg')
  54. # testing second result (self-post)
  55. r = results[1]
  56. self.assertEqual(r['url'], 'https://reddit.com/')
  57. self.assertEqual(r['title'], 'Title number two')
  58. self.assertEqual(r['content'], 'Dominus vobiscum')
  59. created = datetime.fromtimestamp(1438792533.0)
  60. self.assertEqual(r['publishedDate'], created)
  61. self.assertTrue('thumbnail_src' not in r)
  62. self.assertTrue('img_src' not in r)