test_dailymotion.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. import mock
  4. from searx.engines import dailymotion
  5. from searx.testing import SearxTestCase
  6. class TestDailymotionEngine(SearxTestCase):
  7. def test_request(self):
  8. query = 'test_query'
  9. dicto = defaultdict(dict)
  10. dicto['pageno'] = 0
  11. dicto['language'] = 'fr_FR'
  12. params = dailymotion.request(query, dicto)
  13. self.assertTrue('url' in params)
  14. self.assertTrue(query in params['url'])
  15. self.assertTrue('dailymotion.com' in params['url'])
  16. self.assertTrue('fr' in params['url'])
  17. dicto['language'] = 'all'
  18. params = dailymotion.request(query, dicto)
  19. self.assertTrue('en' in params['url'])
  20. def test_response(self):
  21. self.assertRaises(AttributeError, dailymotion.response, None)
  22. self.assertRaises(AttributeError, dailymotion.response, [])
  23. self.assertRaises(AttributeError, dailymotion.response, '')
  24. self.assertRaises(AttributeError, dailymotion.response, '[]')
  25. response = mock.Mock(text='{}')
  26. self.assertEqual(dailymotion.response(response), [])
  27. response = mock.Mock(text='{"data": []}')
  28. self.assertEqual(dailymotion.response(response), [])
  29. json = """
  30. {
  31. "page": 1,
  32. "limit": 5,
  33. "explicit": false,
  34. "total": 289487,
  35. "has_more": true,
  36. "list": [
  37. {
  38. "created_time": 1422173451,
  39. "title": "Title",
  40. "description": "Description",
  41. "duration": 81,
  42. "url": "http://www.url",
  43. "thumbnail_360_url": "http://thumbnail",
  44. "id": "x2fit7q"
  45. }
  46. ]
  47. }
  48. """
  49. response = mock.Mock(text=json)
  50. results = dailymotion.response(response)
  51. self.assertEqual(type(results), list)
  52. self.assertEqual(len(results), 1)
  53. self.assertEqual(results[0]['title'], 'Title')
  54. self.assertEqual(results[0]['url'], 'http://www.url')
  55. self.assertEqual(results[0]['content'], 'Description')
  56. self.assertIn('x2fit7q', results[0]['embedded'])
  57. json = r"""
  58. {"toto":[
  59. {"id":200,"name":"Artist Name",
  60. "link":"http:\/\/www.dailymotion.com\/artist\/1217","type":"artist"}
  61. ]}
  62. """
  63. response = mock.Mock(text=json)
  64. results = dailymotion.response(response)
  65. self.assertEqual(type(results), list)
  66. self.assertEqual(len(results), 0)
  67. def test_fetch_supported_languages(self):
  68. json = r"""
  69. {"list":[{"code":"af","name":"Afrikaans","native_name":"Afrikaans",
  70. "localized_name":"Afrikaans","display_name":"Afrikaans"},
  71. {"code":"ar","name":"Arabic","native_name":"\u0627\u0644\u0639\u0631\u0628\u064a\u0629",
  72. "localized_name":"Arabic","display_name":"Arabic"},
  73. {"code":"la","name":"Latin","native_name":null,
  74. "localized_name":"Latin","display_name":"Latin"}
  75. ]}
  76. """
  77. response = mock.Mock(text=json)
  78. languages = dailymotion._fetch_supported_languages(response)
  79. self.assertEqual(type(languages), dict)
  80. self.assertEqual(len(languages), 3)
  81. self.assertIn('af', languages)
  82. self.assertIn('ar', languages)
  83. self.assertIn('la', languages)
  84. self.assertEqual(type(languages['af']), dict)
  85. self.assertEqual(type(languages['ar']), dict)
  86. self.assertEqual(type(languages['la']), dict)
  87. self.assertIn('name', languages['af'])
  88. self.assertIn('name', languages['ar'])
  89. self.assertNotIn('name', languages['la'])
  90. self.assertIn('english_name', languages['af'])
  91. self.assertIn('english_name', languages['ar'])
  92. self.assertIn('english_name', languages['la'])
  93. self.assertEqual(languages['af']['name'], 'Afrikaans')
  94. self.assertEqual(languages['af']['english_name'], 'Afrikaans')
  95. self.assertEqual(languages['ar']['name'], u'العربية')
  96. self.assertEqual(languages['ar']['english_name'], 'Arabic')
  97. self.assertEqual(languages['la']['english_name'], 'Latin')