Spaces:
Sleeping
Sleeping
Upload test_musicbrainz_client.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194"""Test the musicbrainz_client module."""
import unittestfrom unittest.mock import patchfrom src.modules.musicbrainz_client import get_music_infos
class TestGetMusicInfos(unittest.TestCase):
@patch('musicbrainzngs.search_artists') @patch('musicbrainzngs.search_release_groups') def test_get_music_infos(self, mock_search_release_groups, mock_search_artists): # Arrange artist = 'UltraSinger' title = 'That\'s Rocking!' search = f'{artist} - {title} (UltrStar 2023) FULL HD'
# Set up mock return values for the MusicBrainz API calls mock_search_artists.return_value = { 'artist-list': [ {'name': artist} ] }
mock_search_release_groups.return_value = { 'release-group-list': [ { 'title': title, 'artist-credit-phrase': artist, 'first-release-date': '2023-01-01', 'tag-list': [ {'name': 'Genre 1'}, {'name': 'Genre 2'} ] } ] }
# Call the function to test title, artist, year, genre = get_music_infos(search)
# Assert the returned values self.assertEqual(title, 'That\'s Rocking!') self.assertEqual(artist, 'UltraSinger') self.assertEqual(year, '2023-01-01') self.assertEqual(genre, 'Genre 1,Genre 2,')
@patch('musicbrainzngs.search_artists') @patch('musicbrainzngs.search_release_groups') def test_get_music_infos_when_title_and_artist_are_the_same(self, mock_search_release_groups, mock_search_artists): # Arrange artist = "ArtistIsTitle" title = "ArtistIsTitle" search_not_same = "ArtistIsTitle - ArtistNotTitle" search_is_same = f"{artist} - {title}"
# Set up mock return values for the MusicBrainz API calls mock_search_artists.return_value = { 'artist-list': [ {'name': artist} ] }
mock_search_release_groups.return_value = { 'release-group-list': [ { 'title': title, 'artist-credit-phrase': artist, } ] }
# Act search_not_same but musicbrainz returns the same artist and title title, artist, year, genre = get_music_infos(search_not_same)
# Assert self.assertEqual(title, None) self.assertEqual(artist, None) self.assertEqual(year, None) self.assertEqual(genre, None)
# Act search_is_same and musicbrainz returns the same artist and title title, artist, year, genre = get_music_infos(search_is_same)
# Assert self.assertEqual(title, 'ArtistIsTitle') self.assertEqual(artist, 'ArtistIsTitle') self.assertEqual(year, None) self.assertEqual(genre, None)
@patch('musicbrainzngs.search_artists') @patch('musicbrainzngs.search_release_groups') def test_get_music_infos(self, mock_search_release_groups, mock_search_artists): # Arrange artist = 'UltraSinger' title = 'That\'s Rocking!' search = f'{artist} - {title} (UltrStar 2023) FULL HD'
# Set up mock return values for the MusicBrainz API calls mock_search_artists.return_value = { 'artist-list': [ {'name': f' {artist} '} # Also test leading and trailing whitespaces ] }
mock_search_release_groups.return_value = { 'release-group-list': [ { 'title': f' {title} ', # Also test leading and trailing whitespaces 'artist-credit-phrase': f' {artist} ', # Also test leading and trailing whitespaces 'first-release-date': ' 2023-01-01 ', # Also test leading and trailing whitespaces 'tag-list': [ {'name': ' Genre 1 '}, # Also test leading and trailing whitespaces {'name': ' Genre 2 '} # Also test leading and trailing whitespaces ] } ] }
# Act title, artist, year, genre = get_music_infos(search)
# Assert self.assertEqual(title, 'That\'s Rocking!') self.assertEqual(artist, 'UltraSinger') self.assertEqual(year, '2023-01-01') self.assertEqual(genre, 'Genre 1,Genre 2,')
@patch('musicbrainzngs.search_artists') @patch('musicbrainzngs.search_release_groups') def test_get_empty_artist_music_infos(self, mock_search_release_groups, mock_search_artists): # Arrange artist = 'UltraSinger' title = 'That\'s Rocking!' search = f'{artist} - {title} (UltrStar 2023) FULL HD'
# Set up mock return values for the MusicBrainz API calls mock_search_artists.return_value = { 'artist-list': [] }
mock_search_release_groups.return_value = { 'release-group-list': [ { 'title': f' {title} ', # Also test leading and trailing whitespaces 'artist-credit-phrase': f' {artist} ', # Also test leading and trailing whitespaces 'first-release-date': ' 2023-01-01 ', # Also test leading and trailing whitespaces 'tag-list': [ {'name': ' Genre 1 '}, # Also test leading and trailing whitespaces {'name': ' Genre 2 '} # Also test leading and trailing whitespaces ] } ] }
# Act title, artist, year, genre = get_music_infos(search)
# Assert self.assertEqual(title, None) self.assertEqual(artist, None) self.assertEqual(year, None) self.assertEqual(genre, None)
@patch('musicbrainzngs.search_artists') @patch('musicbrainzngs.search_release_groups') def test_get_empty_release_music_infos(self, mock_search_release_groups, mock_search_artists): # Arrange artist = 'UltraSinger' title = 'That\'s Rocking!' search = f'{artist} - {title} (UltrStar 2023) FULL HD'
# Set up mock return values for the MusicBrainz API calls mock_search_artists.return_value = { 'artist-list': [ {'name': f' {artist} '} # Also test leading and trailing whitespaces ] }
mock_search_release_groups.return_value = { 'release-group-list': [] }
# Act title, artist, year, genre = get_music_infos(search)
# Assert self.assertEqual(title, None) self.assertEqual(artist, None) self.assertEqual(year, None) self.assertEqual(genre, None)
if __name__ == '__main__': unittest.main()pytest/test_musicbrainz_client.pyAdded+194