1
|
|
|
import os |
2
|
|
|
from time import sleep |
3
|
|
|
import pytest |
4
|
|
|
|
5
|
|
|
from music_album_creation.downloading import YoutubeDownloader, InvalidUrlError, UnavailableVideoError, CertificateVerificationError |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
@pytest.fixture(scope='module') |
9
|
|
|
def download(): |
10
|
|
|
return lambda url, target_directory, times, suppress_certificate_validation: YoutubeDownloader.download_times(url, |
11
|
|
|
target_directory, |
12
|
|
|
times=10, |
13
|
|
|
spawn=False, |
14
|
|
|
verbose=False, |
15
|
|
|
supress_stdout=True, |
16
|
|
|
suppress_certificate_validation=suppress_certificate_validation, |
17
|
|
|
delay=0.8) |
18
|
|
|
|
19
|
|
|
@pytest.fixture(scope='module') |
20
|
|
|
def download_trials(): |
21
|
|
|
return 15 |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
|
25
|
|
|
class TestYoutubeDownloader: |
26
|
|
|
NON_EXISTANT_YOUTUBE_URL = 'https://www.youtube.com/watch?v=alpharegavgav' |
27
|
|
|
INVALID_URL = 'gav' |
28
|
|
|
duration = '3:43' |
29
|
|
|
duration_in_seconds = 223 |
30
|
|
|
|
31
|
|
|
def download_trial(self, url, directory, download_callback, nb_trials): |
32
|
|
|
suppress_certificate_validation = False |
33
|
|
|
try: |
34
|
|
|
download_callback(url, directory, 1, suppress_certificate_validation) |
35
|
|
|
except CertificateVerificationError as e: |
36
|
|
|
download_callback(url, directory, nb_trials, True) |
37
|
|
|
|
38
|
|
|
@pytest.mark.parametrize("url, target_file", [ |
39
|
|
|
('https://www.youtube.com/watch?v=Q3dvbM6Pias', 'Rage Against The Machine - Testify (Official Video).mp3')]) |
40
|
|
|
def test_downloading_valid_youtube_url(self, url, target_file, tmpdir, download, download_trials): |
41
|
|
|
target_directory = str(tmpdir.mkdir('youtubedownloads')) |
42
|
|
|
self.download_trial(url, target_directory, download, download_trials) |
43
|
|
|
assert os.path.isfile(os.path.join(target_directory, target_file)) |
44
|
|
|
|
45
|
|
|
def test_downloading_false_youtube_url(self, download, download_trials): |
46
|
|
|
with pytest.raises(UnavailableVideoError): |
47
|
|
|
self.download_trial(self.NON_EXISTANT_YOUTUBE_URL, '/tmp', download, download_trials) |
48
|
|
|
|
49
|
|
|
def test_downloading_invalid_url(self, download): |
50
|
|
|
with pytest.raises(InvalidUrlError): |
51
|
|
|
download(self.INVALID_URL, '/tmp/', 1, False) |
52
|
|
|
|