1
|
1 |
|
import os |
2
|
1 |
|
from unittest import mock |
3
|
|
|
|
4
|
1 |
|
import pytest |
5
|
1 |
|
|
6
|
1 |
|
from music_album_creation.create_album import main |
7
|
1 |
|
|
8
|
|
|
|
9
|
|
|
def test_invoking_cli_with_help_flag(run_subprocess): |
10
|
1 |
|
"""Smoke test to ensure that the CLI can be invoked with the help flag""" |
11
|
1 |
|
import sys |
12
|
1 |
|
result = run_subprocess( |
13
|
|
|
sys.executable, |
14
|
|
|
'-m', |
15
|
1 |
|
'music_album_creation', |
16
|
|
|
'--help', |
17
|
|
|
check=False, # we disable check, because we do it in unit test below |
18
|
|
|
) |
19
|
1 |
|
assert result.stderr == '' |
20
|
1 |
|
assert result.exit_code == 0 |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
@pytest.fixture |
24
|
|
|
def valid_youtube_videos(): |
25
|
|
|
"""Youtube video urls and their expected mp3 name to be 'downloaded'. |
26
|
|
|
|
27
|
|
|
Note: |
28
|
|
|
Maintain this fixture in cases such as a youtube video title changing |
29
|
|
|
over time, or a youtube url ceasing to exist. |
30
|
|
|
|
31
|
|
|
Returns: |
32
|
|
|
[type]: [description] |
33
|
|
|
""" |
34
|
|
|
from collections import namedtuple |
35
|
|
|
|
36
|
|
|
YoutubeVideo = namedtuple('YoutubeVideo', ['url', 'video_title']) |
37
|
|
|
return { |
38
|
|
|
YoutubeVideo(url, video_title) |
39
|
|
|
for url, video_title in { |
40
|
|
|
( |
41
|
|
|
'https://www.youtube.com/watch?v=jJkF3I5cBAc', |
42
|
|
|
'10 seconds countdown (video test)', |
43
|
|
|
), |
44
|
|
|
( |
45
|
|
|
'https://www.youtube.com/watch?v=Q3dvbM6Pias', |
46
|
|
|
'Rage Against The Machine - Testify (Official HD Video)' |
47
|
|
|
), |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
@pytest.mark.network_bound("Makes a request to youtube.com, thus using network") |
53
|
|
|
@pytest.mark.runner_setup(mix_stderr=False) |
54
|
|
|
@mock.patch('music_album_creation.create_album.inout') |
55
|
|
|
@mock.patch('music_album_creation.create_album.music_lib_directory') |
56
|
|
|
def test_integration( |
57
|
|
|
mock_music_lib_directory, mock_inout, tmpdir, isolated_cli_runner, valid_youtube_videos |
58
|
|
|
): |
59
|
|
|
target_directory = str(tmpdir.mkdir('temp-music-library')) |
60
|
|
|
|
61
|
|
|
mock_music_lib_directory.return_value = target_directory |
62
|
|
|
mock_inout.input_youtube_url_dialog.return_value = list(valid_youtube_videos)[1].url |
63
|
|
|
mock_inout.interactive_track_info_input_dialog.return_value = ( |
64
|
|
|
'1. Gasoline - 0:00\n' '2. Man vs. God - 0:07\n' |
65
|
|
|
) |
66
|
|
|
expected_album_dir: str = os.path.join( |
67
|
|
|
target_directory, 'del/Faith_in_Science' |
68
|
|
|
) |
69
|
|
|
mock_inout.track_information_type_dialog.return_value = 'Timestamps' |
70
|
|
|
mock_inout.album_directory_path_dialog.return_value = expected_album_dir |
71
|
|
|
mock_inout.interactive_metadata_dialogs.return_value = { |
72
|
|
|
'artist': 'del', |
73
|
|
|
'album-artist': 'del', |
74
|
|
|
'album': 'Faith in Science', |
75
|
|
|
'year': '2019', |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
result = isolated_cli_runner.invoke( |
79
|
|
|
main, |
80
|
|
|
args=None, |
81
|
|
|
input=None, |
82
|
|
|
env=None, |
83
|
|
|
catch_exceptions=False, |
84
|
|
|
color=False, |
85
|
|
|
**{}, |
86
|
|
|
) |
87
|
|
|
print(result.stdout) |
88
|
|
|
print(result.stderr) |
89
|
|
|
assert result.stderr == '' |
90
|
|
|
assert result.exit_code == 0 |
91
|
|
|
print("CAP\n{}\nCAP".format(result.output)) |
92
|
|
|
# captured = capsys.readouterr() |
93
|
|
|
|
94
|
|
|
# AND the album directory should be created |
95
|
|
|
assert os.path.isdir(expected_album_dir) |
96
|
|
|
|
97
|
|
|
# AND the album directory should contain the expected tracks |
98
|
|
|
expected_tracks = { |
99
|
|
|
'01 - Gasoline.mp4', |
100
|
|
|
'02 - Man vs. God.mp4', |
101
|
|
|
} |
102
|
|
|
assert set(os.listdir(expected_album_dir)) == expected_tracks |
103
|
|
|
|