Test Failed
Push — test-coverage ( 7b8ae8...30721d )
by Konstantinos
02:24
created

tests.test_downloading.download()   A

Complexity

Conditions 2

Size

Total Lines 10
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nop 0
dl 0
loc 10
rs 9.9
c 0
b 0
f 0
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