1
|
|
|
"""This module tests writting metadata to audio files . It tests both valid and invalid values for the 'year' (TDRC) field""" |
2
|
1 |
|
import os |
3
|
1 |
|
from glob import glob |
4
|
|
|
|
5
|
1 |
|
import pytest |
6
|
1 |
|
from mutagen.id3 import ID3 |
7
|
1 |
|
|
8
|
1 |
|
from music_album_creation.metadata import MetadataDealer as MD |
9
|
|
|
from music_album_creation.tracks_parsing import StringParser |
10
|
|
|
|
11
|
1 |
|
|
12
|
|
|
@pytest.fixture(scope='module') |
13
|
1 |
|
def metadata(): |
14
|
|
|
return MD() |
15
|
1 |
|
|
16
|
|
|
|
17
|
1 |
|
@pytest.fixture(scope='module') |
18
|
|
|
def test_album_dir(): |
19
|
|
|
return os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data/album_0/') |
20
|
1 |
|
|
21
|
|
|
|
22
|
1 |
|
@pytest.fixture(scope='module') |
23
|
|
|
def album_info(): |
24
|
|
|
return { |
25
|
|
|
'artist': ['ratm', 'TPE1'], |
26
|
|
|
'album-artist': ['ratm', 'TPE2'], |
27
|
1 |
|
'album': ['renegades', 'TALB'], |
28
|
|
|
'year': ['2000', 'TDRC'], |
29
|
1 |
|
} |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
@pytest.fixture(scope='module') |
33
|
1 |
|
def tags_from_file_name(): |
34
|
|
|
return { |
35
|
|
|
'track_number': { |
36
|
|
|
'tag': 'TRCK', |
37
|
|
|
'parser': lambda x: str(int(x)), |
38
|
|
|
}, # 4.2.1 TRCK [#TRCK Track number/Position in set] |
39
|
1 |
|
'track_name': {'tag': 'TIT2'}, |
40
|
|
|
} # 4.2.1 TIT2 [#TIT2 Title/songname/content description] |
41
|
1 |
|
|
42
|
1 |
|
|
43
|
1 |
|
@pytest.mark.parametrize( |
44
|
|
|
"year", |
45
|
|
|
[ |
46
|
|
|
('2000'), |
47
|
|
|
(''), |
48
|
1 |
|
pytest.param('a_year', marks=pytest.mark.xfail), |
49
|
|
|
], |
50
|
|
|
) |
51
|
1 |
|
def test_writting_album_metadata(year, test_album_dir, album_info, tags_from_file_name): |
52
|
1 |
|
MD.set_album_metadata( |
53
|
|
|
test_album_dir, |
54
|
|
|
track_number=True, |
55
|
|
|
track_name=True, |
56
|
|
|
artist=album_info['artist'][0], |
57
|
|
|
album_artist=album_info['album-artist'][0], |
58
|
|
|
album=album_info['album'][0], |
59
|
|
|
year=year, |
60
|
|
|
) |
61
|
|
|
for file_path in glob(test_album_dir + '/*'): |
62
|
|
|
c = StringParser().parse_track_number_n_name(os.path.basename(file_path)) |
63
|
|
|
audio = ID3(file_path) |
64
|
|
|
# this is true because when trying to set the year metadata with '' the tag s do not change and so remain to the '2000' value set in the first test in line |
65
|
|
|
# assert all([str(audio.get(album_info[k][1]) == album_info[k][0] for k in ['artist', 'album-artist', 'album'])]) and str(audio.get(album_info['year'][1])) == album_info |
66
|
|
|
# for k, v in tags_from_file_name.items(): |
67
|
|
|
# print("TAG SET: {}={}, requested: {}={}".format(v, str(audio.get(v)), k, c[k])) |
68
|
|
|
assert all( |
69
|
|
|
[str(audio.get(album_info[k][1]) == album_info[k][0] for k in album_info.keys())] |
70
|
|
|
) and all( |
71
|
|
|
[ |
72
|
|
|
str(audio.get(data['tag'])) == data.get('parser', lambda x: x)(c[k]) |
73
|
|
|
for k, data in tags_from_file_name.items() |
74
|
|
|
] |
75
|
|
|
) |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
def test_metadata_dealer_object(metadata): |
79
|
|
|
assert hasattr(metadata, '_filters') |
80
|
|
|
|