1
|
1 |
|
import os |
2
|
1 |
|
from glob import glob |
3
|
|
|
|
4
|
1 |
|
import pytest |
5
|
1 |
|
from music_album_creation.downloading import (CertificateVerificationError, |
6
|
|
|
CMDYoutubeDownloader, |
7
|
|
|
InvalidUrlError, |
8
|
|
|
UnavailableVideoError) |
9
|
1 |
|
from music_album_creation.web_parsing import video_title |
10
|
|
|
|
11
|
|
|
|
12
|
1 |
|
@pytest.fixture(scope='module') |
13
|
|
|
def download(): |
14
|
|
|
youtue = CMDYoutubeDownloader() |
15
|
|
|
return lambda url, target_directory, times, suppress_certificate_validation: youtue.download_trials(url, target_directory, |
16
|
|
|
times=10, |
17
|
|
|
suppress_certificate_validation=suppress_certificate_validation, |
18
|
|
|
delay=0.8) |
19
|
1 |
|
@pytest.fixture(scope='module') |
20
|
|
|
def download_trials(): |
21
|
|
|
return 3 |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
NON_EXISTANT_YOUTUBE_URL = 'https://www.youtube.com/watch?v=alpharegavgav' |
25
|
1 |
|
INVALID_URL = 'gav' |
26
|
1 |
|
duration = '3:43' |
27
|
1 |
|
duration_in_seconds = 223 |
28
|
1 |
|
|
29
|
1 |
|
@pytest.fixture |
30
|
|
|
def download_trial(): |
31
|
1 |
|
def _download_trial(url, directory, download_callback, nb_trials): |
32
|
|
|
try: |
33
|
|
|
download_callback(url, directory, times=1, suppress_certificate_validation=False) |
34
|
|
|
except CertificateVerificationError: |
35
|
|
|
download_callback(url, directory, times=nb_trials, suppress_certificate_validation=True) |
36
|
|
|
return _download_trial |
37
|
1 |
|
|
38
|
|
|
# @pytest.mark.parametrize("url, target_file", [ |
39
|
|
|
# ('https://www.youtube.com/watch?v=Q3dvbM6Pias', 'Rage Against The Machine - Testify (Official Video).mp3')]) |
40
|
|
|
@pytest.mark.network_bound |
41
|
|
|
def test_downloading_valid_youtube_url(download_trial, tmpdir, download, download_trials, valid_youtube_videos): |
42
|
|
|
target_directory = str(tmpdir.mkdir('youtubedownloads')) |
43
|
|
|
# for youtube_video in [('https://www.youtube.com/watch?v=UO2JIPOYhIk&list=OLAK5uy_k80e1ODmXyVy6K25BL6PS4wCFg1hwjkX0&index=3', 'The Witch')]: |
44
|
|
|
for youtube_video in [ |
45
|
1 |
|
('https://www.youtube.com/watch?v=bj1JRuyYeco', |
46
|
|
|
'20 Second Timer (Minimal)')]: |
47
|
|
|
|
48
|
|
|
url, title_name = youtube_video |
49
|
1 |
|
download_trial(url, target_directory, download, download_trials) |
50
|
|
|
assert len(glob('{}/*'.format(target_directory))) == 1 |
51
|
|
|
assert os.path.isfile(os.path.join(target_directory, f'{title_name}.mp3')) or os.path.isfile(os.path.join(target_directory, '_.mp3')) |
52
|
|
|
|
53
|
|
|
@pytest.mark.network_bound |
54
|
1 |
|
def test_downloading_false_youtube_url(download_trial, download, download_trials): |
55
|
|
|
with pytest.raises(UnavailableVideoError): |
56
|
|
|
download_trial(NON_EXISTANT_YOUTUBE_URL, '/tmp', download, download_trials) |
57
|
|
|
|
58
|
|
|
@pytest.mark.network_bound |
59
|
|
|
def test_downloading_invalid_url(download): |
60
|
|
|
with pytest.raises(InvalidUrlError): |
61
|
|
|
download(INVALID_URL, '/tmp/', 1, False) |
62
|
|
|
|