From d222b6e425c890e2ba9a8aa57afd351f09cedaf6 Mon Sep 17 00:00:00 2001 From: Philipp Wolfer Date: Thu, 9 Jan 2025 08:50:22 +0100 Subject: [PATCH] Added tests for Track genre handling --- test/test_item.py | 12 ++++--- test/test_track.py | 81 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 83 insertions(+), 10 deletions(-) diff --git a/test/test_item.py b/test/test_item.py index 7784adaa76..15061bd434 100644 --- a/test/test_item.py +++ b/test/test_item.py @@ -2,7 +2,7 @@ # # Picard, the next-generation MusicBrainz tagger # -# Copyright (C) 2018, 2021, 2024 Philipp Wolfer +# Copyright (C) 2018, 2021, 2024-2025 Philipp Wolfer # Copyright (C) 2020-2021 Laurent Monin # # This program is free software; you can redistribute it and/or @@ -20,6 +20,7 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +from collections import Counter from unittest.mock import Mock from test.picardtestcase import PicardTestCase @@ -78,10 +79,11 @@ def test_set_genre_inc_params_with_user_tags(self): self.assertTrue(require_auth) def test_add_genres(self): - self.obj.add_genre('genre1', 2) - self.assertEqual(self.obj.genres['genre1'], 2) - self.obj.add_genre('genre1', 5) - self.assertEqual(self.obj.genres['genre1'], 7) + self.obj.add_genre('pop', 1) + self.obj.add_genre('rock', 1) + self.obj.add_genre('blues', 2) + self.obj.add_genre('pop', 2) + self.assertEqual(self.obj._genres, Counter(pop=3, rock=1, blues=2)) def test_set_genre_inc_custom_config(self): inc = set() diff --git a/test/test_track.py b/test/test_track.py index 71731708e0..9692f2436f 100644 --- a/test/test_track.py +++ b/test/test_track.py @@ -3,7 +3,7 @@ # Picard, the next-generation MusicBrainz tagger # # Copyright (C) 2021-2022 Laurent Monin -# Copyright (C) 2021-2022, 2024 Philipp Wolfer +# Copyright (C) 2021-2022, 2024-2025 Philipp Wolfer # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -24,7 +24,16 @@ from test.picardtestcase import PicardTestCase -from picard.track import Track +from picard.album import ( + Album, + AlbumArtist, +) +from picard.const import VARIOUS_ARTISTS_ID +from picard.releasegroup import ReleaseGroup +from picard.track import ( + Track, + TrackArtist, +) class TrackTest(PicardTestCase): @@ -32,8 +41,70 @@ def test_can_link_fingerprint(self): track = Track('123') self.assertTrue(track.can_link_fingerprint) + def test_merge_folksonomy_tags_no_album(self): + track = Track('123') + track._genres = Counter(pop=6, rock=7, blues=2) + self.assertEqual(track._merge_folksonomy_tags('genres'), track._genres) + + def test_merge_folksonomy_tags_with_album(self): + track = Track('123') + track._genres = Counter(pop=6, rock=7) + album = Album('456') + album._genres = Counter(rock=3, blues=2) + release_group = ReleaseGroup('789') + release_group._genres = Counter(blues=1) + album.release_group = release_group + track.album = album + expected = Counter(pop=6, rock=10, blues=3) + self.assertEqual(track._merge_folksonomy_tags('genres'), expected) + + def test_merge_folksonomy_tags_artist_genre_fallback(self): + self.set_config_values({'artists_genres': True}) + track = Track('123') + album = Album('456') + artist1 = AlbumArtist('1') + artist1._genres = Counter(rock=1, blues=2) + album._album_artists.append(artist1) + artist2 = AlbumArtist('2') + artist2._genres = Counter(pop=2, rock=1) + album._album_artists.append(artist2) + track.album = album + expected = artist1._genres + artist2._genres + self.assertEqual(track._merge_folksonomy_tags('genres'), expected) + + def test_merge_folksonomy_tags_various_artist_genre_fallback(self): + self.set_config_values({'artists_genres': True}) + track = Track('123') + track.metadata['musicbrainz_albumartistid'] = VARIOUS_ARTISTS_ID + album = Album('456') + album_artist = AlbumArtist('1') + album_artist._genres = Counter(country=1) + album._album_artists.append(album_artist) + track.album = album + track_artist1 = TrackArtist('2') + track_artist1._genres = Counter(rock=1, blues=2) + track._track_artists.append(track_artist1) + track_artist2 = TrackArtist('3') + track_artist2._genres = Counter(pop=2, rock=1) + track._track_artists.append(track_artist2) + expected = track_artist1._genres + track_artist2._genres + self.assertEqual(track._merge_folksonomy_tags('genres'), expected) + + def test_add_genres_variable(self): + track = Track('123') + track._genres = Counter(pop=6, rock=7, blues=2) + track._add_genres_variable() + self.assertEqual(track.metadata.getall('~genres'), ['blues', 'pop', 'rock']) + + def test_add_folksonomy_tags_variable(self): + track = Track('123') + track._genres = Counter(pop=6, rock=7, blues=2) + track._folksonomy_tags = Counter(live=2, pop=6, rock=7, blues=2, favorite=1) + track._add_folksonomy_tags_variable() + self.assertEqual(track.metadata.getall('~folksonomy_tags'), ['favorite', 'live']) + -class TrackGenres2MetadataTest(PicardTestCase): +class TrackGenresToMetadataTest(PicardTestCase): def test_empty(self): genres = Counter() ret = Track._genres_to_metadata(genres) @@ -79,10 +150,10 @@ def test_minusage(self): def test_filters(self): genres = Counter(pop=6, rock=7, blues=2) - ret = Track._genres_to_metadata(genres, filters="-blues") + ret = Track._genres_to_metadata(genres, filters='-blues') self.assertEqual(ret, ['Pop', 'Rock']) def test_join_with(self): genres = Counter(pop=6, rock=7, blues=2) - ret = Track._genres_to_metadata(genres, join_with=",") + ret = Track._genres_to_metadata(genres, join_with=',') self.assertEqual(ret, ['Blues,Pop,Rock'])