Test Failed
Push — dev ( 3a28f6...0451e7 )
by Konstantinos
05:08
created

TestSegmenting.test_valid_segmentation()   B

Complexity

Conditions 1

Size

Total Lines 50
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 41
nop 7
dl 0
loc 50
ccs 7
cts 7
cp 1
crap 1
rs 8.896
c 0
b 0
f 0
1 1
import os
2 1
import sys
3
4 1
import mutagen
5 1
import pytest
6 1
7 1
from music_album_creation import AudioSegmenter
8
from music_album_creation.audio_segmentation.data import (
9
    SegmentationInformation,
10
    TrackTimestampsSequenceError,
11 1
    WrongTimestampFormat,
12
)
13
14 1
this_dir = os.path.dirname(os.path.realpath(__file__))
15
16 1
17
@pytest.fixture(scope='module')
18
def test_audio_file_path():
19 1
    return os.path.join(this_dir, 'know_your_enemy.mp3')
20
21 1
22
@pytest.fixture(scope='module')
23
def segmenter():
24 1
    return AudioSegmenter()
25
26
27
class TestSegmenting:
28
    # def test_input_durations_to_segment(self, tmpdir, test_audio_file_path):
29
    #     durations_data = [['t1', '0:10'], ['t2', '0:40'], ['t3', '0:10']]
30
    #
31
    #     with pytest.raises(NotStartingFromZeroTimestampError, match=r"First track \({}\) is supposed to have a 0:00 timestamp. Instead {} found".format(durations_data[0][0], durations_data[0][1])):
32
    #         segmenter.target_directory = str(tmpdir.mkdir('album'))
33 1
    #         segmenter.segment_from_list(test_audio_file_path, durations_data)
34 1
35 1
    def test_illogical_timetamp_sequence(self, tmpdir, segmenter, test_audio_file_path):
36 1
        with pytest.raises(TrackTimestampsSequenceError):
37 1
            segmenter.target_directory = str(tmpdir.mkdir('album'))
38
            c = '\n'.join(
39
                "{} - {}".format(x[0], x[1])
40 1
                for x in [['t1', '0:00'], ['t2', '1:00'], ['t3', '0:35']]
41 1
            )
42 1
            _ = SegmentationInformation.from_multiline(c, 'timestamps')
43 1
            # segmenter.segment_from_list(test_audio_file_path, [['t1', '0:00'], ['t2', '1:00'], ['t3', '0:35']], supress_stdout=True, verbose=False, sleep_seconds=0)
44
45
    def test_wrong_timestamp_input(self, tmpdir, segmenter, test_audio_file_path):
46 1
        with pytest.raises(WrongTimestampFormat):
47
            segmenter.target_directory = str(tmpdir.mkdir('album'))
48
            segmenter.segment(
49
                test_audio_file_path,
50
                SegmentationInformation.from_tracks_information(
51
                    [['t1', '0:00'], ['t2', '1:a0'], ['t3', '1:35']], 'timestamps'
52
                ),
53
                supress_stderr=False,
54
            )
55
            # segmenter.segment(test_audio_file_path, [['t1', '0:00'], ['t2', '1:a0'], ['t3', '1:35']], supress_stdout=True, verbose=False, sleep_seconds=0)
56 1
57 1
    @pytest.mark.parametrize(
58 1
        "tracks_info, names, durations",
59 1
        [
60 1
            (
61 1
                "1. tr1 - 0:00\n2. tr2 - 1:12\n3. tr3 - 2:00\n",
62 1
                ['01 - tr1.mp4', '02 - tr2.mp4', '03 - tr3.mp4'],
63 1
                [72, 48, 236],
64
            ),
65
            pytest.param(
66
                "1. tr1 - 0:00\n2. tr2 - 1:12\n3. tr3 - 1:00\n",
67
                ['01 - tr1.mp4', '02 - tr2.mp4', '03 - tr3.mp4'],
68
                [72, 48, 236],
69
                marks=pytest.mark.xfail,
70
            ),
71
            pytest.param(
72
                "1. tr1 - 0:00\n2. tr2 - 1:72\n3. tr3 - 3:00\n",
73
                ['01 - tr1.mp4', '02 - tr2.mp4', '03 - tr3.mp4'],
74
                [72, 48, 236],
75
                marks=pytest.mark.xfail,
76
            ),
77
        ],
78
    )
79
    def test_valid_segmentation(
80
        self, tracks_info, names, durations, tmpdir, test_audio_file_path, segmenter
81
    ):
82
        segmenter.target_directory = str(tmpdir.mkdir('album'))
83
        tracks_file = tmpdir.join('tracks_info.txt')
84
        tracks_info = {2: u'{}'.format(tracks_info)}.get(sys.version_info[0], tracks_info)
85
        tracks_file.write_text(tracks_info, 'utf-8')
86
        segmenter.segment_from_file(
87
            test_audio_file_path,
88
            str(tracks_file),
89
            'timestamps',
90
            supress_stdout=False,
91
            supress_stderr=False,
92
            sleep_seconds=0,
93
        )
94
        file_names = sorted(os.listdir(segmenter.target_directory))
95
        assert file_names == names
96
        assert [
97
            abs(
98
                getattr(
99
                    mutagen.File(os.path.join(segmenter.target_directory, x[0])).info,
100
                    'length',
101
                    0,
102
                )
103
                - x[1]
104
            )
105
            < 1
106
            for x in zip(file_names, durations)
107
        ]
108