Spaces:
Sleeping
Sleeping
File size: 6,991 Bytes
66772d7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
"""Test the musicbrainz_client module."""
import unittest
from unittest.mock import patch
from 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()
|