test_metadata.py 27.1 KB
Newer Older
1
2
import datetime
import os
Eliot Berriot's avatar
Eliot Berriot committed
3
import uuid
Eliot Berriot's avatar
Eliot Berriot committed
4
5
import pytest

6
7
8
import mutagen.oggtheora
import mutagen.oggvorbis

9
10
11
12
13
from funkwhale_api.music import metadata

DATA_DIR = os.path.dirname(os.path.abspath(__file__))


14
15
16
17
18
@pytest.mark.parametrize(
    "field,value",
    [
        ("title", "Peer Gynt Suite no. 1, op. 46: I. Morning"),
        ("artist", "Edvard Grieg"),
19
        ("album_artist", "Edvard Grieg; Musopen Symphony Orchestra"),
20
        ("artists", "Edvard Grieg; Musopen Symphony Orchestra"),
21
        ("album", "Peer Gynt Suite no. 1, op. 46"),
22
23
24
25
26
27
        ("date", "2012-08-15"),
        ("position", "1"),
        ("disc_number", "1"),
        ("musicbrainz_albumid", "a766da8b-8336-47aa-a3ee-371cc41ccc75"),
        ("mbid", "bd21ac48-46d8-4e78-925f-d9cc2a294656"),
        ("musicbrainz_artistid", "013c8e5b-d72a-4cd3-8dee-6c64d6125823"),
Eliot Berriot's avatar
Eliot Berriot committed
28
29
        (
            "musicbrainz_albumartistid",
30
            "013c8e5b-d72a-4cd3-8dee-6c64d6125823;5b4d7d2d-36df-4b38-95e3-a964234f520f",
Eliot Berriot's avatar
Eliot Berriot committed
31
        ),
Eliot Berriot's avatar
Eliot Berriot committed
32
33
        ("license", "Dummy license: http://creativecommons.org/licenses/by-sa/4.0/"),
        ("copyright", "Someone"),
34
        ("comment", "Hello there"),
35
36
    ],
)
Eliot Berriot's avatar
Eliot Berriot committed
37
38
def test_can_get_metadata_from_ogg_file(field, value):
    path = os.path.join(DATA_DIR, "test.ogg")
39
40
41
42
43
    data = metadata.Metadata(path)

    assert data.get(field) == value


44
45
46
47
48
49
50
51
def test_can_get_metadata_all():
    path = os.path.join(DATA_DIR, "test.ogg")
    data = metadata.Metadata(path)

    expected = {
        "title": "Peer Gynt Suite no. 1, op. 46: I. Morning",
        "artist": "Edvard Grieg",
        "album_artist": "Edvard Grieg; Musopen Symphony Orchestra",
52
        "artists": "Edvard Grieg; Musopen Symphony Orchestra",
53
54
55
56
57
58
59
60
61
62
        "album": "Peer Gynt Suite no. 1, op. 46",
        "date": "2012-08-15",
        "position": "1",
        "disc_number": "1",
        "musicbrainz_albumid": "a766da8b-8336-47aa-a3ee-371cc41ccc75",
        "mbid": "bd21ac48-46d8-4e78-925f-d9cc2a294656",
        "musicbrainz_artistid": "013c8e5b-d72a-4cd3-8dee-6c64d6125823",
        "musicbrainz_albumartistid": "013c8e5b-d72a-4cd3-8dee-6c64d6125823;5b4d7d2d-36df-4b38-95e3-a964234f520f",
        "license": "Dummy license: http://creativecommons.org/licenses/by-sa/4.0/",
        "copyright": "Someone",
63
        "genre": "Classical",
64
        "comment": "Hello there",
65
66
67
68
    }
    assert data.all() == expected


Eliot Berriot's avatar
Eliot Berriot committed
69
70
71
72
73
@pytest.mark.parametrize(
    "field,value",
    [
        ("title", "Peer Gynt Suite no. 1, op. 46: I. Morning"),
        ("artist", "Edvard Grieg"),
74
        ("album_artist", "Edvard Grieg; Musopen Symphony Orchestra"),
75
        ("artists", "Edvard Grieg; Musopen Symphony Orchestra"),
Eliot Berriot's avatar
Eliot Berriot committed
76
        ("album", "Peer Gynt Suite no. 1, op. 46"),
77
78
79
80
81
82
        ("date", "2012-08-15"),
        ("position", "1"),
        ("disc_number", "1"),
        ("musicbrainz_albumid", "a766da8b-8336-47aa-a3ee-371cc41ccc75"),
        ("mbid", "bd21ac48-46d8-4e78-925f-d9cc2a294656"),
        ("musicbrainz_artistid", "013c8e5b-d72a-4cd3-8dee-6c64d6125823"),
Eliot Berriot's avatar
Eliot Berriot committed
83
84
        (
            "musicbrainz_albumartistid",
85
            "013c8e5b-d72a-4cd3-8dee-6c64d6125823;5b4d7d2d-36df-4b38-95e3-a964234f520f",
Eliot Berriot's avatar
Eliot Berriot committed
86
        ),
Eliot Berriot's avatar
Eliot Berriot committed
87
88
        ("license", "Dummy license: http://creativecommons.org/licenses/by-sa/4.0/"),
        ("copyright", "Someone"),
89
        ("comment", "Hello there"),
Eliot Berriot's avatar
Eliot Berriot committed
90
91
    ],
)
Eliot Berriot's avatar
Eliot Berriot committed
92
93
def test_can_get_metadata_from_opus_file(field, value):
    path = os.path.join(DATA_DIR, "test.opus")
94
95
96
97
    data = metadata.Metadata(path)

    assert data.get(field) == value

Eliot Berriot's avatar
Eliot Berriot committed
98
99
100
101
102
103

@pytest.mark.parametrize(
    "field,value",
    [
        ("title", "Drei Kreuze (dass wir hier sind)"),
        ("artist", "Die Toten Hosen"),
Eliot Berriot's avatar
Eliot Berriot committed
104
        ("album_artist", "Die Toten Hosen"),
Eliot Berriot's avatar
Eliot Berriot committed
105
        ("album", "Ballast der Republik"),
106
107
108
109
110
111
112
        ("date", "2012-05-04"),
        ("position", "1/16"),
        ("disc_number", "1/2"),
        ("musicbrainz_albumid", "1f0441ad-e609-446d-b355-809c445773cf"),
        ("mbid", "124d0150-8627-46bc-bc14-789a3bc960c8"),
        ("musicbrainz_artistid", "c3bc80a6-1f4a-4e17-8cf0-6b1efe8302f1"),
        ("musicbrainz_albumartistid", "c3bc80a6-1f4a-4e17-8cf0-6b1efe8302f1"),
113
        ("comment", "Hello there"),
Eliot Berriot's avatar
Eliot Berriot committed
114
115
116
        # somehow, I cannot successfully create an ogg theora file
        # with the proper license field
        # ("license", "Dummy license: http://creativecommons.org/licenses/by-sa/4.0/"),
Eliot Berriot's avatar
Eliot Berriot committed
117
118
    ],
)
EorlBruder's avatar
EorlBruder committed
119
def test_can_get_metadata_from_ogg_theora_file(field, value):
Eliot Berriot's avatar
Eliot Berriot committed
120
    path = os.path.join(DATA_DIR, "test_theora.ogg")
EorlBruder's avatar
EorlBruder committed
121
122
123
124
    data = metadata.Metadata(path)

    assert data.get(field) == value

125

Eliot Berriot's avatar
Eliot Berriot committed
126
127
128
129
@pytest.mark.parametrize(
    "field,value",
    [
        ("title", "Bend"),
Eliot Berriot's avatar
Eliot Berriot committed
130
131
        ("artist", "Binärpilot"),
        ("album_artist", "Binärpilot"),
132
        ("artists", "Binärpilot; Another artist"),
Eliot Berriot's avatar
Eliot Berriot committed
133
        ("album", "You Can't Stop Da Funk"),
134
135
136
137
138
139
140
        ("date", "2006-02-07"),
        ("position", "2/4"),
        ("disc_number", "1/1"),
        ("musicbrainz_albumid", "ce40cdb1-a562-4fd8-a269-9269f98d4124"),
        ("mbid", "f269d497-1cc0-4ae4-a0c4-157ec7d73fcb"),
        ("musicbrainz_artistid", "9c6bddde-6228-4d9f-ad0d-03f6fcb19e13"),
        ("musicbrainz_albumartistid", "9c6bddde-6228-4d9f-ad0d-03f6fcb19e13"),
Eliot Berriot's avatar
Eliot Berriot committed
141
142
        ("license", "https://creativecommons.org/licenses/by-nc-nd/2.5/"),
        ("copyright", "Someone"),
143
        ("comment", "Hello there"),
Eliot Berriot's avatar
Eliot Berriot committed
144
145
    ],
)
146
def test_can_get_metadata_from_id3_mp3_file(field, value):
Eliot Berriot's avatar
Eliot Berriot committed
147
    path = os.path.join(DATA_DIR, "test.mp3")
148
149
    data = metadata.Metadata(path)

150
    assert str(data.get(field)) == value
151
152


Philipp Wolfer's avatar
Philipp Wolfer committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
@pytest.mark.parametrize(
    "field,value",
    [
        ("title", "Bend"),
        ("artist", "Binärpilot"),
        ("album_artist", "Binärpilot"),
        # ("artists", "Binärpilot; Another artist"),  # FW does not properly extract multi-value artists from ID3
        ("album", "You Can't Stop Da Funk"),
        ("date", "2006-02-07"),
        ("position", "2/4"),
        ("disc_number", "1/1"),
        ("musicbrainz_albumid", "ce40cdb1-a562-4fd8-a269-9269f98d4124"),
        ("mbid", "f269d497-1cc0-4ae4-a0c4-157ec7d73fcb"),
        ("musicbrainz_artistid", "9c6bddde-6228-4d9f-ad0d-03f6fcb19e13"),
        ("musicbrainz_albumartistid", "9c6bddde-6228-4d9f-ad0d-03f6fcb19e13"),
        ("license", "https://creativecommons.org/licenses/by-nc-nd/2.5/"),
        ("copyright", "Someone"),
        ("comment", "Hello there"),
    ],
)
def test_can_get_metadata_from_id3_aiff_file(field, value):
    path = os.path.join(DATA_DIR, "test.aiff")
    data = metadata.Metadata(path)

    assert str(data.get(field)) == value


180
@pytest.mark.parametrize(
181
182
183
    "name",
    [
        "test.mp3",
Philipp Wolfer's avatar
Philipp Wolfer committed
184
        "test.aiff",
185
186
187
188
        "with_other_picture.mp3",
        "sample.flac",
        "with_cover.ogg",
        "with_cover.opus",
189
        "test.m4a",
190
    ],
191
)
192
193
194
195
def test_can_get_pictures(name):
    path = os.path.join(DATA_DIR, name)
    data = metadata.Metadata(path)

Eliot Berriot's avatar
Eliot Berriot committed
196
    pictures = data.get("pictures")
197
    assert len(pictures) == 1
198
    cover_data = data.get_picture("cover_front", "other")
Eliot Berriot's avatar
Eliot Berriot committed
199
200
201
202
203
204
205
206
207
208
209
    assert cover_data["mimetype"].startswith("image/")
    assert len(cover_data["content"]) > 0
    assert type(cover_data["content"]) == bytes
    assert type(cover_data["description"]) == str


@pytest.mark.parametrize(
    "field,value",
    [
        ("title", "999,999"),
        ("artist", "Nine Inch Nails"),
Eliot Berriot's avatar
Eliot Berriot committed
210
        ("album_artist", "Nine Inch Nails"),
Eliot Berriot's avatar
Eliot Berriot committed
211
        ("album", "The Slip"),
212
213
214
215
216
217
218
        ("date", "2008-05-05"),
        ("position", "1"),
        ("disc_number", "1"),
        ("musicbrainz_albumid", "12b57d46-a192-499e-a91f-7da66790a1c1"),
        ("mbid", "30f3f33e-8d0c-4e69-8539-cbd701d18f28"),
        ("musicbrainz_artistid", "b7ffd2af-418f-4be2-bdd1-22f8b48613da"),
        ("musicbrainz_albumartistid", "b7ffd2af-418f-4be2-bdd1-22f8b48613da"),
Eliot Berriot's avatar
Eliot Berriot committed
219
220
        ("license", "http://creativecommons.org/licenses/by-nc-sa/3.0/us/"),
        ("copyright", "2008 nin"),
221
        ("comment", "Hello there"),
Eliot Berriot's avatar
Eliot Berriot committed
222
223
    ],
)
224
def test_can_get_metadata_from_flac_file(field, value):
Eliot Berriot's avatar
Eliot Berriot committed
225
    path = os.path.join(DATA_DIR, "sample.flac")
226
227
228
    data = metadata.Metadata(path)

    assert data.get(field) == value
229
230


231
232
233
234
235
236
@pytest.mark.parametrize(
    "field,value",
    [
        ("title", "Peer Gynt Suite no. 1, op. 46: I. Morning"),
        ("artist", "Edvard Grieg"),
        ("album_artist", "Edvard Grieg; Musopen Symphony Orchestra"),
237
        ("artists", "Edvard Grieg; Musopen Symphony Orchestra"),
238
239
240
241
242
243
244
245
246
247
248
249
250
251
        ("album", "Peer Gynt Suite no. 1, op. 46"),
        ("date", "2012-08-15"),
        ("position", 1),
        ("disc_number", 2),
        ("musicbrainz_albumid", "a766da8b-8336-47aa-a3ee-371cc41ccc75"),
        ("mbid", "bd21ac48-46d8-4e78-925f-d9cc2a294656"),
        ("musicbrainz_artistid", "013c8e5b-d72a-4cd3-8dee-6c64d6125823"),
        (
            "musicbrainz_albumartistid",
            "013c8e5b-d72a-4cd3-8dee-6c64d6125823;5b4d7d2d-36df-4b38-95e3-a964234f520f",
        ),
        ("license", "Dummy license: http://creativecommons.org/licenses/by-sa/4.0/"),
        ("copyright", "Someone"),
        ("genre", "Dubstep"),
252
        ("comment", "Hello there"),
253
254
255
256
257
258
259
260
261
    ],
)
def test_can_get_metadata_from_m4a_file(field, value):
    path = os.path.join(DATA_DIR, "test.m4a")
    data = metadata.Metadata(path)

    assert data.get(field) == value


262
263
264
265
266
def test_get_pictures_m4a_empty():
    pictures = metadata.get_mp4_tag({}, "pictures")
    assert metadata.clean_mp4_pictures(pictures) == []


267
def test_can_get_metadata_from_flac_file_not_crash_if_empty():
Eliot Berriot's avatar
Eliot Berriot committed
268
    path = os.path.join(DATA_DIR, "sample.flac")
269
270
271
    data = metadata.Metadata(path)

    with pytest.raises(metadata.TagNotFound):
Eliot Berriot's avatar
Eliot Berriot committed
272
        data.get("test")
273
274


275
276
@pytest.mark.parametrize(
    "raw,expected",
277
278
279
280
    [
        ("2017", datetime.date(2017, 1, 1)),
        ("2017-12-31", datetime.date(2017, 12, 31)),
        ("2017-14-01 01:32", datetime.date(2017, 1, 14)),  # deezer format
281
        ("2017-02", datetime.date(2017, 1, 1)),  # weird format that exists
282
        ("0000", None),
283
        ("", None),
284
        ("nonsense", None),
285
    ],
286
287
)
def test_date_parsing(raw, expected):
288
    assert metadata.PermissiveDateField().to_internal_value(raw) == expected
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305


def test_metadata_fallback_ogg_theora(mocker):
    path = os.path.join(DATA_DIR, "with_cover.ogg")
    data = metadata.Metadata(path)

    assert isinstance(data._file, mutagen.oggtheora.OggTheora)
    assert isinstance(data.fallback, metadata.Metadata)
    assert isinstance(data.fallback._file, mutagen.oggvorbis.OggVorbis)

    expected_result = data.fallback.get("pictures")
    fallback_get = mocker.spy(data.fallback, "get")

    assert expected_result is not None
    assert data.get("pictures", "default") == expected_result

    fallback_get.assert_called_once_with("pictures", "default")
306
307
308
309
310
311
312
313
314
315
316
317
318


@pytest.mark.parametrize(
    "path, expected",
    [
        (
            "test.mp3",
            {
                "title": "Bend",
                "artists": [
                    {
                        "name": "Binärpilot",
                        "mbid": uuid.UUID("9c6bddde-6228-4d9f-ad0d-03f6fcb19e13"),
319
320
                    },
                    {"name": "Another artist", "mbid": None},
321
322
323
324
325
326
327
328
329
                ],
                "album": {
                    "title": "You Can't Stop Da Funk",
                    "mbid": uuid.UUID("ce40cdb1-a562-4fd8-a269-9269f98d4124"),
                    "release_date": datetime.date(2006, 2, 7),
                    "artists": [
                        {
                            "name": "Binärpilot",
                            "mbid": uuid.UUID("9c6bddde-6228-4d9f-ad0d-03f6fcb19e13"),
330
331
                        },
                        {"name": "Another artist", "mbid": None},
332
333
334
335
336
337
338
                    ],
                },
                "position": 2,
                "disc_number": 1,
                "mbid": uuid.UUID("f269d497-1cc0-4ae4-a0c4-157ec7d73fcb"),
                "license": "https://creativecommons.org/licenses/by-nc-nd/2.5/",
                "copyright": "Someone",
339
                "tags": ["Funk"],
340
                "description": {"text": "Hello there", "content_type": "text/plain"},
341
342
343
344
345
346
347
348
349
350
            },
        ),
        (
            "test.ogg",
            {
                "title": "Peer Gynt Suite no. 1, op. 46: I. Morning",
                "artists": [
                    {
                        "name": "Edvard Grieg",
                        "mbid": uuid.UUID("013c8e5b-d72a-4cd3-8dee-6c64d6125823"),
351
352
                    },
                    {"name": "Musopen Symphony Orchestra", "mbid": None},
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
                ],
                "album": {
                    "title": "Peer Gynt Suite no. 1, op. 46",
                    "mbid": uuid.UUID("a766da8b-8336-47aa-a3ee-371cc41ccc75"),
                    "release_date": datetime.date(2012, 8, 15),
                    "artists": [
                        {
                            "name": "Edvard Grieg",
                            "mbid": uuid.UUID("013c8e5b-d72a-4cd3-8dee-6c64d6125823"),
                        },
                        {
                            "name": "Musopen Symphony Orchestra",
                            "mbid": uuid.UUID("5b4d7d2d-36df-4b38-95e3-a964234f520f"),
                        },
                    ],
                },
                "position": 1,
                "disc_number": 1,
                "mbid": uuid.UUID("bd21ac48-46d8-4e78-925f-d9cc2a294656"),
                "license": "Dummy license: http://creativecommons.org/licenses/by-sa/4.0/",
                "copyright": "Someone",
374
                "tags": ["Classical"],
375
                "description": {"text": "Hello there", "content_type": "text/plain"},
376
377
378
379
380
381
382
383
384
385
            },
        ),
        (
            "test.opus",
            {
                "title": "Peer Gynt Suite no. 1, op. 46: I. Morning",
                "artists": [
                    {
                        "name": "Edvard Grieg",
                        "mbid": uuid.UUID("013c8e5b-d72a-4cd3-8dee-6c64d6125823"),
386
387
                    },
                    {"name": "Musopen Symphony Orchestra", "mbid": None},
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
                ],
                "album": {
                    "title": "Peer Gynt Suite no. 1, op. 46",
                    "mbid": uuid.UUID("a766da8b-8336-47aa-a3ee-371cc41ccc75"),
                    "release_date": datetime.date(2012, 8, 15),
                    "artists": [
                        {
                            "name": "Edvard Grieg",
                            "mbid": uuid.UUID("013c8e5b-d72a-4cd3-8dee-6c64d6125823"),
                        },
                        {
                            "name": "Musopen Symphony Orchestra",
                            "mbid": uuid.UUID("5b4d7d2d-36df-4b38-95e3-a964234f520f"),
                        },
                    ],
                },
                "position": 1,
                "disc_number": 1,
                "mbid": uuid.UUID("bd21ac48-46d8-4e78-925f-d9cc2a294656"),
                "license": "Dummy license: http://creativecommons.org/licenses/by-sa/4.0/",
                "copyright": "Someone",
409
                "tags": ["Classical"],
410
                "description": {"text": "Hello there", "content_type": "text/plain"},
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
            },
        ),
        (
            "test_theora.ogg",
            {
                "title": "Drei Kreuze (dass wir hier sind)",
                "artists": [
                    {
                        "name": "Die Toten Hosen",
                        "mbid": uuid.UUID("c3bc80a6-1f4a-4e17-8cf0-6b1efe8302f1"),
                    }
                ],
                "album": {
                    "title": "Ballast der Republik",
                    "mbid": uuid.UUID("1f0441ad-e609-446d-b355-809c445773cf"),
                    "release_date": datetime.date(2012, 5, 4),
                    "artists": [
                        {
                            "name": "Die Toten Hosen",
                            "mbid": uuid.UUID("c3bc80a6-1f4a-4e17-8cf0-6b1efe8302f1"),
                        }
                    ],
                },
434
                "tags": ["Rock"],
435
436
437
438
439
440
441
                "position": 1,
                "disc_number": 1,
                "mbid": uuid.UUID("124d0150-8627-46bc-bc14-789a3bc960c8"),
                # somehow, I cannot successfully create an ogg theora file
                # with the proper license field
                # ("license", "Dummy license: http://creativecommons.org/licenses/by-sa/4.0/"),
                "copyright": "℗ 2012 JKP GmbH & Co. KG",
442
                "description": {"text": "Hello there", "content_type": "text/plain"},
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
            },
        ),
        (
            "sample.flac",
            {
                "title": "999,999",
                "artists": [
                    {
                        "name": "Nine Inch Nails",
                        "mbid": uuid.UUID("b7ffd2af-418f-4be2-bdd1-22f8b48613da"),
                    }
                ],
                "album": {
                    "title": "The Slip",
                    "mbid": uuid.UUID("12b57d46-a192-499e-a91f-7da66790a1c1"),
                    "release_date": datetime.date(2008, 5, 5),
                    "artists": [
                        {
                            "name": "Nine Inch Nails",
                            "mbid": uuid.UUID("b7ffd2af-418f-4be2-bdd1-22f8b48613da"),
                        }
                    ],
                },
                "position": 1,
                "disc_number": 1,
                "mbid": uuid.UUID("30f3f33e-8d0c-4e69-8539-cbd701d18f28"),
                "license": "http://creativecommons.org/licenses/by-nc-sa/3.0/us/",
                "copyright": "2008 nin",
471
                "tags": ["Industrial"],
472
                "description": {"text": "Hello there", "content_type": "text/plain"},
473
474
475
476
477
478
479
480
            },
        ),
    ],
)
def test_track_metadata_serializer(path, expected, mocker):
    path = os.path.join(DATA_DIR, path)
    data = metadata.Metadata(path)
    get_picture = mocker.patch.object(data, "get_picture")
481
    expected["album"]["cover_data"] = get_picture.return_value
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563

    serializer = metadata.TrackMetadataSerializer(data=data)
    assert serializer.is_valid(raise_exception=True) is True
    assert serializer.validated_data == expected

    get_picture.assert_called_once_with("cover_front", "other")


@pytest.mark.parametrize(
    "raw, expected",
    [
        (
            {
                "names": "Hello; World",
                "mbids": "f269d497-1cc0-4ae4-a0c4-157ec7d73fcb; f269d497-1cc0-4ae4-a0c4-157ec7d73fcd ",
            },
            [
                {
                    "name": "Hello",
                    "mbid": uuid.UUID("f269d497-1cc0-4ae4-a0c4-157ec7d73fcb"),
                },
                {
                    "name": "World",
                    "mbid": uuid.UUID("f269d497-1cc0-4ae4-a0c4-157ec7d73fcd"),
                },
            ],
        ),
        (
            {
                "names": "Hello; World; Foo",
                "mbids": "f269d497-1cc0-4ae4-a0c4-157ec7d73fcb; f269d497-1cc0-4ae4-a0c4-157ec7d73fcd ",
            },
            [
                {
                    "name": "Hello",
                    "mbid": uuid.UUID("f269d497-1cc0-4ae4-a0c4-157ec7d73fcb"),
                },
                {
                    "name": "World",
                    "mbid": uuid.UUID("f269d497-1cc0-4ae4-a0c4-157ec7d73fcd"),
                },
                {"name": "Foo", "mbid": None},
            ],
        ),
    ],
)
def test_artists_cleaning(raw, expected):
    field = metadata.ArtistField()
    assert field.to_internal_value(raw) == expected


@pytest.mark.parametrize(
    "data, errored_field",
    [
        ({"name": "Hello", "mbid": "wrong-uuid"}, "mbid"),  # wrong uuid
        ({"name": "", "mbid": "f269d497-1cc0-4ae4-a0c4-157ec7d73fcd"}, "name"),
    ],
)
def test_artist_serializer_validation(data, errored_field):
    serializer = metadata.ArtistSerializer(data=data)
    assert serializer.is_valid() is False

    assert len(serializer.errors) == 1
    assert errored_field in serializer.errors


@pytest.mark.parametrize(
    "data, errored_field",
    [
        ({"title": "Hello", "mbid": "wrong"}, "mbid"),  # wrong uuid
        (
            {"title": "", "mbid": "f269d497-1cc0-4ae4-a0c4-157ec7d73fcd"},
            "title",
        ),  # empty title
    ],
)
def test_album_serializer_validation(data, errored_field):
    serializer = metadata.AlbumSerializer(data=data)
    assert serializer.is_valid() is False

    assert len(serializer.errors) == 1
    assert errored_field in serializer.errors
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580


def test_fake_metadata_with_serializer():
    data = {
        "title": "Peer Gynt Suite no. 1, op. 46: I. Morning",
        "artist": "Edvard Grieg",
        "album_artist": "Edvard Grieg; Musopen Symphony Orchestra",
        "album": "Peer Gynt Suite no. 1, op. 46",
        "date": "2012-08-15",
        "position": "1",
        "disc_number": "1",
        "musicbrainz_albumid": "a766da8b-8336-47aa-a3ee-371cc41ccc75",
        "mbid": "bd21ac48-46d8-4e78-925f-d9cc2a294656",
        "musicbrainz_artistid": "013c8e5b-d72a-4cd3-8dee-6c64d6125823",
        "musicbrainz_albumartistid": "013c8e5b-d72a-4cd3-8dee-6c64d6125823;5b4d7d2d-36df-4b38-95e3-a964234f520f",
        "license": "Dummy license: http://creativecommons.org/licenses/by-sa/4.0/",
        "copyright": "Someone",
581
        "comment": "hello there",
582
583
584
585
    }

    expected = {
        "title": "Peer Gynt Suite no. 1, op. 46: I. Morning",
586
        "description": {"text": "hello there", "content_type": "text/plain"},
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
        "artists": [
            {
                "name": "Edvard Grieg",
                "mbid": uuid.UUID("013c8e5b-d72a-4cd3-8dee-6c64d6125823"),
            }
        ],
        "album": {
            "title": "Peer Gynt Suite no. 1, op. 46",
            "mbid": uuid.UUID("a766da8b-8336-47aa-a3ee-371cc41ccc75"),
            "release_date": datetime.date(2012, 8, 15),
            "artists": [
                {
                    "name": "Edvard Grieg",
                    "mbid": uuid.UUID("013c8e5b-d72a-4cd3-8dee-6c64d6125823"),
                },
                {
                    "name": "Musopen Symphony Orchestra",
                    "mbid": uuid.UUID("5b4d7d2d-36df-4b38-95e3-a964234f520f"),
                },
            ],
607
            "cover_data": None,
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
        },
        "position": 1,
        "disc_number": 1,
        "mbid": uuid.UUID("bd21ac48-46d8-4e78-925f-d9cc2a294656"),
        "license": "Dummy license: http://creativecommons.org/licenses/by-sa/4.0/",
        "copyright": "Someone",
    }
    serializer = metadata.TrackMetadataSerializer(data=metadata.FakeMetadata(data))
    assert serializer.is_valid(raise_exception=True) is True
    assert serializer.validated_data == expected


def test_serializer_album_artist_missing():
    data = {
        "title": "Peer Gynt Suite no. 1, op. 46: I. Morning",
        "artist": "Edvard Grieg",
        "album": "Peer Gynt Suite no. 1, op. 46",
    }

    expected = {
        "title": "Peer Gynt Suite no. 1, op. 46: I. Morning",
        "artists": [{"name": "Edvard Grieg", "mbid": None}],
        "album": {
            "title": "Peer Gynt Suite no. 1, op. 46",
            "mbid": None,
            "release_date": None,
            "artists": [],
635
            "cover_data": None,
636
637
638
639
640
        },
    }
    serializer = metadata.TrackMetadataSerializer(data=metadata.FakeMetadata(data))
    assert serializer.is_valid(raise_exception=True) is True
    assert serializer.validated_data == expected
641
642


643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
@pytest.mark.parametrize(
    "data",
    [
        # no album tag
        {"title": "Track", "artist": "Artist"},
        # empty/null values
        {"title": "Track", "artist": "Artist", "album": ""},
        {"title": "Track", "artist": "Artist", "album": " "},
        {"title": "Track", "artist": "Artist", "album": None},
    ],
)
def test_serializer_album_default_title_when_missing_or_empty(data):
    expected = {
        "title": "Track",
        "artists": [{"name": "Artist", "mbid": None}],
        "album": {
Eliot Berriot's avatar
Eliot Berriot committed
659
            "title": metadata.UNKNOWN_ALBUM,
660
661
662
            "mbid": None,
            "release_date": None,
            "artists": [],
663
            "cover_data": None,
664
665
666
667
668
669
670
        },
    }
    serializer = metadata.TrackMetadataSerializer(data=metadata.FakeMetadata(data))
    assert serializer.is_valid(raise_exception=True) is True
    assert serializer.validated_data == expected


671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
@pytest.mark.parametrize(
    "field_name", ["copyright", "license", "mbid", "position", "disc_number"]
)
def test_serializer_empty_fields(field_name):
    data = {
        "title": "Track Title",
        "artist": "Track Artist",
        "album": "Track Album",
        # empty copyright/license field shouldn't fail, cf #850
        field_name: "",
    }
    expected = {
        "title": "Track Title",
        "artists": [{"name": "Track Artist", "mbid": None}],
        "album": {
            "title": "Track Album",
            "mbid": None,
            "release_date": None,
            "artists": [],
690
            "cover_data": None,
691
692
693
694
695
696
697
        },
    }
    serializer = metadata.TrackMetadataSerializer(data=metadata.FakeMetadata(data))
    assert serializer.is_valid(raise_exception=True) is True
    assert serializer.validated_data == expected


698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
def test_serializer_strict_mode_false():
    data = {}
    expected = {
        "artists": [{"name": None, "mbid": None}],
        "album": {
            "title": "[Unknown Album]",
            "mbid": None,
            "release_date": None,
            "artists": [],
            "cover_data": None,
        },
    }
    serializer = metadata.TrackMetadataSerializer(
        data=metadata.FakeMetadata(data), context={"strict": False}
    )
    assert serializer.is_valid(raise_exception=True) is True
    assert serializer.validated_data == expected


def test_serializer_strict_mode_true():
    data = {}
    serializer = metadata.TrackMetadataSerializer(
        data=metadata.FakeMetadata(data), context={"strict": True}
    )
    with pytest.raises(metadata.serializers.ValidationError):
        assert serializer.is_valid(raise_exception=True)


726
727
728
729
730
731
732
733
734
735
736
737
738
def test_artist_field_featuring():
    data = {
        "artist": "Santana feat. Chris Cornell",
        # here is the tricky bit, note the slash
        "musicbrainz_artistid": "9a3bf45c-347d-4630-894d-7cf3e8e0b632/cbf9738d-8f81-4a92-bc64-ede09341652d",
    }

    expected = [{"name": "Santana feat. Chris Cornell", "mbid": None}]

    field = metadata.ArtistField()
    value = field.get_value(data)

    assert field.to_internal_value(value) == expected
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769


@pytest.mark.parametrize(
    "genre, expected_tags",
    [
        ("Pop", ["Pop"]),
        ("pop", ["pop"]),
        ("Pop-Rock", ["PopRock"]),
        ("Pop - Rock", ["Pop", "Rock"]),
        ("Soundtrack - Cute Anime", ["Soundtrack", "CuteAnime"]),
        ("Pop, Rock", ["Pop", "Rock"]),
        ("Chanson française", ["ChansonFrançaise"]),
        ("Unhandled❤️", []),
        ("tag with non-breaking spaces", []),
    ],
)
def test_acquire_tags_from_genre(genre, expected_tags):
    data = {
        "title": "Track Title",
        "artist": "Track Artist",
        "album": "Track Album",
        "genre": genre,
    }
    expected = {
        "title": "Track Title",
        "artists": [{"name": "Track Artist", "mbid": None}],
        "album": {
            "title": "Track Album",
            "mbid": None,
            "release_date": None,
            "artists": [],
770
            "cover_data": None,
771
772
773
774
775
776
777
778
        },
    }
    if expected_tags:
        expected["tags"] = expected_tags

    serializer = metadata.TrackMetadataSerializer(data=metadata.FakeMetadata(data))
    assert serializer.is_valid(raise_exception=True) is True
    assert serializer.validated_data == expected