test_download.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env python
  2. import hashlib
  3. import io
  4. import os
  5. import json
  6. import unittest
  7. import sys
  8. import hashlib
  9. import socket
  10. # Allow direct execution
  11. sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  12. import youtube_dl.FileDownloader
  13. import youtube_dl.InfoExtractors
  14. from youtube_dl.utils import *
  15. DEF_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tests.json')
  16. PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
  17. # General configuration (from __init__, not very elegant...)
  18. jar = compat_cookiejar.CookieJar()
  19. cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar)
  20. proxy_handler = compat_urllib_request.ProxyHandler()
  21. opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler())
  22. compat_urllib_request.install_opener(opener)
  23. class FileDownloader(youtube_dl.FileDownloader):
  24. def __init__(self, *args, **kwargs):
  25. self.to_stderr = self.to_screen
  26. self.processed_info_dicts = []
  27. return youtube_dl.FileDownloader.__init__(self, *args, **kwargs)
  28. def process_info(self, info_dict):
  29. self.processed_info_dicts.append(info_dict)
  30. return youtube_dl.FileDownloader.process_info(self, info_dict)
  31. def _file_md5(fn):
  32. with open(fn, 'rb') as f:
  33. return hashlib.md5(f.read()).hexdigest()
  34. with io.open(DEF_FILE, encoding='utf-8') as deff:
  35. defs = json.load(deff)
  36. with io.open(PARAMETERS_FILE, encoding='utf-8') as pf:
  37. parameters = json.load(pf)
  38. class TestDownload(unittest.TestCase):
  39. def setUp(self):
  40. self.parameters = parameters
  41. self.defs = defs
  42. # Clear old files
  43. self.tearDown()
  44. def tearDown(self):
  45. for fn in [ test.get('file', False) for test in self.defs ]:
  46. if fn and os.path.exists(fn):
  47. os.remove(fn)
  48. ### Dynamically generate tests
  49. def generator(test_case):
  50. def test_template(self):
  51. ie = getattr(youtube_dl.InfoExtractors, test_case['name'] + 'IE')
  52. if not ie._WORKING:
  53. print('Skipping: IE marked as not _WORKING')
  54. return
  55. if not test_case['file']:
  56. print('Skipping: No output file specified')
  57. return
  58. if 'skip' in test_case:
  59. print('Skipping: {0}'.format(test_case['skip']))
  60. return
  61. params = dict(self.parameters) # Duplicate it locally
  62. for p in test_case.get('params', {}):
  63. params[p] = test_case['params'][p]
  64. fd = FileDownloader(params)
  65. fd.add_info_extractor(ie())
  66. for ien in test_case.get('add_ie', []):
  67. fd.add_info_extractor(getattr(youtube_dl.InfoExtractors, ien + 'IE')())
  68. fd.download([test_case['url']])
  69. self.assertTrue(os.path.exists(test_case['file']))
  70. if 'md5' in test_case:
  71. md5_for_file = _file_md5(test_case['file'])
  72. self.assertEqual(md5_for_file, test_case['md5'])
  73. info_dict = fd.processed_info_dicts[0]
  74. for (info_field, value) in test_case.get('info_dict', {}).items():
  75. if value.startswith('md5:'):
  76. md5_info_value = hashlib.md5(info_dict.get(info_field, '')).hexdigest()
  77. self.assertEqual(value[3:], md5_info_value)
  78. else:
  79. self.assertEqual(value, info_dict.get(info_field))
  80. return test_template
  81. ### And add them to TestDownload
  82. for test_case in defs:
  83. test_method = generator(test_case)
  84. test_method.__name__ = "test_{0}".format(test_case["name"])
  85. setattr(TestDownload, test_method.__name__, test_method)
  86. del test_method
  87. if __name__ == '__main__':
  88. unittest.main()