test_download.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python2
  2. import hashlib
  3. import os
  4. import json
  5. import unittest
  6. import sys
  7. from youtube_dl.FileDownloader import FileDownloader
  8. #import all the info extractor
  9. import youtube_dl
  10. DEF_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tests.json')
  11. PARAM_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'parameters.json'
  12. )
  13. class TestDownload(unittest.TestCase):
  14. pass
  15. def md5_for_file(filename, block_size=2**20):
  16. with open(filename) as f:
  17. md5 = hashlib.md5()
  18. while True:
  19. data = f.read(block_size)
  20. if not data:
  21. break
  22. md5.update(data)
  23. return md5.hexdigest()
  24. def generator(name, url, md5, file, ie_param, optional_ie):
  25. def test_template(self):
  26. fd = FileDownloader(ie_param)
  27. fd.add_info_extractor(getattr(youtube_dl, name + "IE")())
  28. fd.download([url])
  29. self.assertTrue(os.path.exists(file))
  30. self.assertEqual(md5_for_file(file), md5)
  31. return test_template
  32. #only python 2.7
  33. def clean_generator(files):
  34. def clean_template(self):
  35. for file_name in files:
  36. if os.path.exists(file_name):
  37. os.remove(file_name)
  38. return clean_template
  39. with open(DEF_FILE, "r") as f:
  40. with open(PARAM_FILE) as fp:
  41. p = json.load(fp)
  42. test_param = json.load(f)
  43. files = set()
  44. for test_case in test_param:
  45. if test_case.get("broken", False):
  46. continue
  47. try:
  48. files.add(test_case["file"])
  49. test_method = generator(test_case['name'], test_case['url'], test_case['md5'], test_case['file'], p, test_case.get('add_ie', []))
  50. test_method.__name__ = "test_{0}".format(test_case["name"])
  51. setattr(TestDownload, test_method.__name__, test_method)
  52. del test_method
  53. except KeyError as e:
  54. sys.stderr.write("Issue with the parameters of test {0}.\n".format(test_case.get("name", "unknown test")))
  55. #clean the files
  56. ff = clean_generator(files)
  57. ff.__name__ = "tearDown"
  58. setattr(TestDownload, ff.__name__, ff)
  59. del ff
  60. if __name__ == '__main__':
  61. unittest.main()