Skip to content

Commit

Permalink
Added tests for Track genre handling
Browse files Browse the repository at this point in the history
  • Loading branch information
phw committed Jan 9, 2025
1 parent bbb8901 commit d222b6e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 10 deletions.
12 changes: 7 additions & 5 deletions test/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down
81 changes: 76 additions & 5 deletions test/test_track.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,16 +24,87 @@

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):
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)
Expand Down Expand Up @@ -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'])

0 comments on commit d222b6e

Please sign in to comment.