Test Failed
Push — dev ( 0451e7...525e4d )
by Konstantinos
02:22
created

tests.test_downloading.download_trial()   A

Complexity

Conditions 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3.6875

Importance

Changes 0
Metric Value
cc 2
eloc 10
nop 0
dl 0
loc 13
ccs 2
cts 8
cp 0.25
crap 3.6875
rs 9.9
c 0
b 0
f 0
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
14
# def download_trial():
15
#     def _download_trial(url, directory, download_callback):
16
#             return download_callback(
17
#                 url, directory, times=1, suppress_certificate_validation=False
18
#             )
19 1
#     return _download_trial
20
21
22
NON_EXISTANT_YOUTUBE_URL = 'https://www.youtube.com/watch?v=alpharegavgavasdfsh'
23
INVALID_URL = 'gav'
24
duration = '3:43'
25 1
duration_in_seconds = 223
26 1
27 1
28 1
@pytest.fixture(scope='module')
29 1
def download():
30
    youtue = CMDYoutubeDownloader()
31 1
    return lambda url, target_directory, times, suppress_certificate_validation: youtue.download_trials(
32
        url,
33
        target_directory,
34
        times=times,
35
        suppress_certificate_validation=suppress_certificate_validation,
36
        delay=0.8,
37 1
    )
38
39
40
@pytest.mark.network_bound
41
@pytest.mark.parametrize('url, target_file', [
42
    (
43
        'https://www.youtube.com/watch?v=Q3dvbM6Pias',
44
        'Rage Against The Machine - Testify (Official HD Video).mp4'
45 1
    ),
46
    (
47
        'https://www.youtube.com/watch?v=bj1JRuyYeco',
48
        '20 Second Timer (Minimal).webm'
49 1
    ),
50
])
51
def test_downloading_valid_youtube_url(
52
    url, target_file,
53
    tmp_path_factory
54 1
):
55
    from pathlib import Path
56
    from music_album_creation.downloading import CMDYoutubeDownloader
57
58
    target_directory = tmp_path_factory.mktemp("youtubedownloads")
59
    target_directory = str(target_directory)
60
    # for youtube_video in [('https://www.youtube.com/watch?v=UO2JIPOYhIk&list=OLAK5uy_k80e1ODmXyVy6K25BL6PS4wCFg1hwjkX0&index=3', 'The Witch')]:
61
    # for youtube_video in [
62
    #     ('https://www.youtube.com/watch?v=bj1JRuyYeco', '20 Second Timer (Minimal)')
63
    # ]:
64
    
65
    expected_file_name = target_file
66
67
    youtube = CMDYoutubeDownloader()
68
    downloaded_file = youtube.download_trials(
69
        url,
70
        target_directory,
71
        times=1,
72
        delay=0.8,
73
    )
74
75
    downloaded_path: Path = Path(str(downloaded_file))
76
77
    # THEN File has been downloaded
78
    assert downloaded_path.exists()
79
    assert downloaded_path.is_file()
80
81
    # AND FILE has the expected name
82
    assert downloaded_path.name == expected_file_name
83
84
    # AND file has extension mp4
85
    assert downloaded_path.suffix == '.' + expected_file_name.split('.')[-1]
86
87
88
# Test that expected Exceptions are fired up
89
90
@pytest.mark.network_bound
91
def test_downloading_false_youtube_url(tmp_path_factory):
92
    from pytube.exceptions import VideoUnavailable
93
    from music_album_creation.downloading import CMDYoutubeDownloader
94
    youtube = CMDYoutubeDownloader()
95
96
    with pytest.raises(VideoUnavailable):
97
        youtube.download_trials(
98
            NON_EXISTANT_YOUTUBE_URL,
99
            str(tmp_path_factory.mktemp("unit-test")),
100
            times=3,
101
            delay=0.8,
102
        )
103
104
105
@pytest.mark.network_bound
106
def test_downloading_invalid_url(download, tmp_path_factory):
107
    from pytube.exceptions import RegexMatchError
108
    from music_album_creation.downloading import CMDYoutubeDownloader
109
    youtube = CMDYoutubeDownloader()
110
111
    with pytest.raises(RegexMatchError):
112
        youtube.download_trials(
113
            INVALID_URL,
114
            str(tmp_path_factory.mktemp("unit-test")),
115
            times=1,
116
            delay=0.8,
117
        )
118