1
|
1 |
|
import os |
2
|
1 |
|
from glob import glob |
3
|
|
|
|
4
|
1 |
|
import pytest |
5
|
1 |
|
|
6
|
|
|
from music_album_creation.downloading import ( |
7
|
|
|
CertificateVerificationError, |
8
|
|
|
CMDYoutubeDownloader, |
9
|
1 |
|
) |
10
|
|
|
from music_album_creation.web_parsing import video_title |
11
|
|
|
|
12
|
1 |
|
|
13
|
|
|
@pytest.fixture(scope='module') |
14
|
|
|
def download(): |
15
|
|
|
youtue = CMDYoutubeDownloader() |
16
|
|
|
return lambda url, target_directory, times, suppress_certificate_validation: youtue.download_trials( |
17
|
|
|
url, |
18
|
|
|
target_directory, |
19
|
1 |
|
times=times, |
20
|
|
|
suppress_certificate_validation=suppress_certificate_validation, |
21
|
|
|
delay=0.8, |
22
|
|
|
) |
23
|
|
|
|
24
|
|
|
|
25
|
1 |
|
@pytest.fixture(scope='module') |
26
|
1 |
|
def nb_download_trials(): |
27
|
1 |
|
return 3 |
28
|
1 |
|
|
29
|
1 |
|
|
30
|
|
|
NON_EXISTANT_YOUTUBE_URL = 'https://www.youtube.com/watch?v=alpharegavgavasdfsh' |
31
|
1 |
|
INVALID_URL = 'gav' |
32
|
|
|
duration = '3:43' |
33
|
|
|
duration_in_seconds = 223 |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
@pytest.fixture |
37
|
1 |
|
def download_trial(): |
38
|
|
|
def _download_trial(url, directory, download_callback, nb_trials): |
39
|
|
|
try: |
40
|
|
|
return download_callback( |
41
|
|
|
url, directory, times=1, suppress_certificate_validation=False |
42
|
|
|
) |
43
|
|
|
except CertificateVerificationError: |
44
|
|
|
return download_callback( |
45
|
1 |
|
url, directory, times=nb_trials, suppress_certificate_validation=True |
46
|
|
|
) |
47
|
|
|
|
48
|
|
|
return _download_trial |
49
|
1 |
|
|
50
|
|
|
|
51
|
|
|
# @pytest.mark.parametrize("url, target_file", [ |
52
|
|
|
# ('https://www.youtube.com/watch?v=Q3dvbM6Pias', 'Rage Against The Machine - Testify (Official Video).mp3')]) |
53
|
|
|
@pytest.mark.network_bound |
54
|
1 |
|
def test_downloading_valid_youtube_url( |
55
|
|
|
download_trial, tmp_path_factory, download, nb_download_trials |
56
|
|
|
): |
57
|
|
|
from pathlib import Path |
58
|
|
|
|
59
|
|
|
target_directory = tmp_path_factory.mktemp("youtubedownloads") |
60
|
|
|
target_directory = str(target_directory) |
61
|
|
|
# for youtube_video in [('https://www.youtube.com/watch?v=UO2JIPOYhIk&list=OLAK5uy_k80e1ODmXyVy6K25BL6PS4wCFg1hwjkX0&index=3', 'The Witch')]: |
62
|
|
|
for youtube_video in [ |
63
|
|
|
('https://www.youtube.com/watch?v=bj1JRuyYeco', '20 Second Timer (Minimal)') |
64
|
|
|
]: |
65
|
|
|
url, title_name = youtube_video |
66
|
|
|
downloaded_file = download_trial(url, target_directory, download, nb_download_trials) |
67
|
|
|
|
68
|
|
|
expected_downloaded_path: Path = Path(str(downloaded_file)) |
69
|
|
|
|
70
|
|
|
# THEN File has been downloaded |
71
|
|
|
assert expected_downloaded_path.exists() |
72
|
|
|
assert expected_downloaded_path.is_file() |
73
|
|
|
|
74
|
|
|
# AND FILE has the expected name |
75
|
|
|
assert expected_downloaded_path.name == f'{title_name}.mp4' |
76
|
|
|
|
77
|
|
|
# AND file has extension mp4 |
78
|
|
|
assert expected_downloaded_path.suffix == '.mp4' |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
@pytest.mark.network_bound |
82
|
|
|
def test_downloading_false_youtube_url(download_trial, download, nb_download_trials, tmp_path_factory): |
83
|
|
|
from pytube.exceptions import VideoUnavailable |
84
|
|
|
|
85
|
|
|
with pytest.raises(VideoUnavailable): |
86
|
|
|
download_trial(NON_EXISTANT_YOUTUBE_URL, str(tmp_path_factory.mktemp("unit-test")), download, nb_download_trials) |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
@pytest.mark.network_bound |
90
|
|
|
def test_downloading_invalid_url(download, tmp_path_factory): |
91
|
|
|
from pytube.exceptions import RegexMatchError |
92
|
|
|
|
93
|
|
|
with pytest.raises(RegexMatchError): |
94
|
|
|
download(INVALID_URL, str(tmp_path_factory.mktemp("unit-test")), 1, False) |
95
|
|
|
|