1
|
|
|
import os |
|
|
|
|
2
|
|
|
from time import sleep |
3
|
|
|
import pytest |
4
|
|
|
|
5
|
|
|
from music_album_creation.downloading import YoutubeDownloader, InvalidUrlError, UnavailableVideoError, TooManyRequestsError, CertificateVerificationError |
|
|
|
|
6
|
|
|
|
7
|
|
|
|
8
|
|
|
@pytest.fixture(scope='module') |
9
|
|
|
def youtube(): |
10
|
|
|
return YoutubeDownloader |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
@pytest.fixture(scope='module') |
14
|
|
|
def download(): |
15
|
|
|
return lambda url, target_directory, times, suppress_certificate_validation: YoutubeDownloader.download_times(url, |
|
|
|
|
16
|
|
|
target_directory, |
|
|
|
|
17
|
|
|
times=10, |
|
|
|
|
18
|
|
|
spawn=False, |
|
|
|
|
19
|
|
|
verbose=False, |
|
|
|
|
20
|
|
|
supress_stdout=True, |
|
|
|
|
21
|
|
|
suppress_certificate_validation=suppress_certificate_validation, |
|
|
|
|
22
|
|
|
delay=0.8) |
|
|
|
|
23
|
|
|
|
24
|
|
|
@pytest.fixture(scope='module') |
25
|
|
|
def download_trials(): |
26
|
|
|
return 15 |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
|
30
|
|
|
class TestYoutubeDownloader: |
31
|
|
|
NON_EXISTANT_YOUTUBE_URL = 'https://www.youtube.com/watch?v=alpharegavgav' |
32
|
|
|
INVALID_URL = 'gav' |
33
|
|
|
duration = '3:43' |
34
|
|
|
duration_in_seconds = 223 |
35
|
|
|
|
36
|
|
|
def download_trial(self, download_callback, nb_trials): |
37
|
|
|
suppress_certificate_validation = False |
38
|
|
|
try: |
39
|
|
|
download_callback(url, target_directory, 1, suppress_certificate_validation) |
|
|
|
|
40
|
|
|
except CertificateVerificationError as e: |
41
|
|
|
print('{}\nWill try to download without an SSL certificate (plain text as http request)'.format(e)) |
|
|
|
|
42
|
|
|
suppress_certificate_validation = True |
43
|
|
|
download_callback(url, target_directory, nb_trials, suppress_certificate_validation) |
44
|
|
|
|
45
|
|
|
@pytest.mark.parametrize("url, target_file", [ |
46
|
|
|
('https://www.youtube.com/watch?v=Q3dvbM6Pias', 'Rage Against The Machine - Testify (Official Video).mp3')]) |
|
|
|
|
47
|
|
|
def test_downloading_valid_youtube_url(self, url, target_file, tmpdir, download, download_trials): |
|
|
|
|
48
|
|
|
target_directory = str(tmpdir.mkdir('youtubedownloads')) |
49
|
|
|
self.download_trial(download, download_trials) |
50
|
|
|
assert os.path.isfile(os.path.join(target_directory, target_file)) |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
def test_downloading_false_youtube_url(self, download, download_trials): |
54
|
|
|
with pytest.raises(UnavailableVideoError): |
55
|
|
|
self.download_trial(download, download_trials) |
56
|
|
|
|
57
|
|
|
def test_downloading_invalid_url(self, download): |
58
|
|
|
with pytest.raises(InvalidUrlError): |
59
|
|
|
download(self.INVALID_URL, '/tmp/', 1, False) |
60
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.