test_dailymotion.py 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import dailymotion
  4. from searx.testing import SearxTestCase
  5. class TestDailymotionEngine(SearxTestCase):
  6. def test_request(self):
  7. query = 'test_query'
  8. dicto = defaultdict(dict)
  9. dicto['pageno'] = 0
  10. dicto['language'] = 'fr_FR'
  11. params = dailymotion.request(query, dicto)
  12. self.assertTrue('url' in params)
  13. self.assertTrue(query in params['url'])
  14. self.assertTrue('dailymotion.com' in params['url'])
  15. self.assertTrue('fr' in params['url'])
  16. dicto['language'] = 'all'
  17. params = dailymotion.request(query, dicto)
  18. self.assertTrue('en' in params['url'])
  19. def test_response(self):
  20. self.assertRaises(AttributeError, dailymotion.response, None)
  21. self.assertRaises(AttributeError, dailymotion.response, [])
  22. self.assertRaises(AttributeError, dailymotion.response, '')
  23. self.assertRaises(AttributeError, dailymotion.response, '[]')
  24. response = mock.Mock(text='{}')
  25. self.assertEqual(dailymotion.response(response), [])
  26. response = mock.Mock(text='{"data": []}')
  27. self.assertEqual(dailymotion.response(response), [])
  28. json = """
  29. {
  30. "page": 1,
  31. "limit": 5,
  32. "explicit": false,
  33. "total": 289487,
  34. "has_more": true,
  35. "list": [
  36. {
  37. "created_time": 1422173451,
  38. "title": "Title",
  39. "description": "Description",
  40. "duration": 81,
  41. "url": "http://www.url",
  42. "thumbnail_360_url": "http://thumbnail",
  43. "id": "x2fit7q"
  44. }
  45. ]
  46. }
  47. """
  48. response = mock.Mock(text=json)
  49. results = dailymotion.response(response)
  50. self.assertEqual(type(results), list)
  51. self.assertEqual(len(results), 1)
  52. self.assertEqual(results[0]['title'], 'Title')
  53. self.assertEqual(results[0]['url'], 'http://www.url')
  54. self.assertEqual(results[0]['content'], 'Description')
  55. self.assertIn('x2fit7q', results[0]['embedded'])
  56. json = r"""
  57. {"toto":[
  58. {"id":200,"name":"Artist Name",
  59. "link":"http:\/\/www.dailymotion.com\/artist\/1217","type":"artist"}
  60. ]}
  61. """
  62. response = mock.Mock(text=json)
  63. results = dailymotion.response(response)
  64. self.assertEqual(type(results), list)
  65. self.assertEqual(len(results), 0)