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