coub-dl/test.py

48 lines
1.6 KiB
Python

import unittest
from random import choice
from os import chdir
from os.path import join
from tempfile import TemporaryDirectory
from copy import deepcopy
from hashlib import sha256
from coub_dl import coub_dl, File
class TestCoubDl(unittest.TestCase):
def setUp(self):
self.tmpdir = TemporaryDirectory()
self.directory = self.tmpdir.name
print(self.directory)
chdir(self.directory)
self.files = deepcopy(coub_dl.default_files)
for key in {key: self.files[key] for key in self.files if key not in coub_dl.output_files}:
self.files[key] = join(self.directory, self.files[key])
self.files[File.OUTPUT] = 'coub.mp4'
def tearDown(self):
self.tmpdir.cleanup()
def test_checksums(self):
urls = {'https://coub.com/view/dshfp': 'b5e1a4b3376a08d5e0bd833c0f5a0e82fbc09100b829b43c176396d4f26e08e4',
'https://coub.com/view/suj0d': '82895d364290f88a1ccca1ca03d1a96344501a5556d23c7193ce6107823ddbe9',
'https://coub.com/view/ucshn': 'f70b98cf28837d58e083d523933bcca20c2de344b7f13a322c771f0a284113d7'}
test_url = choice(list(urls.keys()))
coub_dl(test_url, self.files)()
self.assertEqual(TestCoubDl.hash_file(self.files[File.OUTPUT], sha256), urls[test_url])
@staticmethod
def hash_file(file, hasher, read_buffer=65536):
with open(file, 'rb') as ifile:
hasher = hasher()
while True:
chunk = ifile.read(read_buffer)
if not chunk: break
hasher.update(chunk)
return hasher.hexdigest()
if __name__ == '__main__':
unittest.main()