Spaces:
Sleeping
Sleeping
Upload test_musicbrainz_client.py
Browse files
pytest/test_musicbrainz_client.py
ADDED
@@ -0,0 +1,194 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Test the musicbrainz_client module."""
|
2 |
+
|
3 |
+
import unittest
|
4 |
+
from unittest.mock import patch
|
5 |
+
from src.modules.musicbrainz_client import get_music_infos
|
6 |
+
|
7 |
+
|
8 |
+
class TestGetMusicInfos(unittest.TestCase):
|
9 |
+
|
10 |
+
@patch('musicbrainzngs.search_artists')
|
11 |
+
@patch('musicbrainzngs.search_release_groups')
|
12 |
+
def test_get_music_infos(self, mock_search_release_groups, mock_search_artists):
|
13 |
+
# Arrange
|
14 |
+
artist = 'UltraSinger'
|
15 |
+
title = 'That\'s Rocking!'
|
16 |
+
search = f'{artist} - {title} (UltrStar 2023) FULL HD'
|
17 |
+
|
18 |
+
# Set up mock return values for the MusicBrainz API calls
|
19 |
+
mock_search_artists.return_value = {
|
20 |
+
'artist-list': [
|
21 |
+
{'name': artist}
|
22 |
+
]
|
23 |
+
}
|
24 |
+
|
25 |
+
mock_search_release_groups.return_value = {
|
26 |
+
'release-group-list': [
|
27 |
+
{
|
28 |
+
'title': title,
|
29 |
+
'artist-credit-phrase': artist,
|
30 |
+
'first-release-date': '2023-01-01',
|
31 |
+
'tag-list': [
|
32 |
+
{'name': 'Genre 1'},
|
33 |
+
{'name': 'Genre 2'}
|
34 |
+
]
|
35 |
+
}
|
36 |
+
]
|
37 |
+
}
|
38 |
+
|
39 |
+
# Call the function to test
|
40 |
+
title, artist, year, genre = get_music_infos(search)
|
41 |
+
|
42 |
+
# Assert the returned values
|
43 |
+
self.assertEqual(title, 'That\'s Rocking!')
|
44 |
+
self.assertEqual(artist, 'UltraSinger')
|
45 |
+
self.assertEqual(year, '2023-01-01')
|
46 |
+
self.assertEqual(genre, 'Genre 1,Genre 2,')
|
47 |
+
|
48 |
+
@patch('musicbrainzngs.search_artists')
|
49 |
+
@patch('musicbrainzngs.search_release_groups')
|
50 |
+
def test_get_music_infos_when_title_and_artist_are_the_same(self, mock_search_release_groups, mock_search_artists):
|
51 |
+
# Arrange
|
52 |
+
artist = "ArtistIsTitle"
|
53 |
+
title = "ArtistIsTitle"
|
54 |
+
search_not_same = "ArtistIsTitle - ArtistNotTitle"
|
55 |
+
search_is_same = f"{artist} - {title}"
|
56 |
+
|
57 |
+
# Set up mock return values for the MusicBrainz API calls
|
58 |
+
mock_search_artists.return_value = {
|
59 |
+
'artist-list': [
|
60 |
+
{'name': artist}
|
61 |
+
]
|
62 |
+
}
|
63 |
+
|
64 |
+
mock_search_release_groups.return_value = {
|
65 |
+
'release-group-list': [
|
66 |
+
{
|
67 |
+
'title': title,
|
68 |
+
'artist-credit-phrase': artist,
|
69 |
+
}
|
70 |
+
]
|
71 |
+
}
|
72 |
+
|
73 |
+
# Act search_not_same but musicbrainz returns the same artist and title
|
74 |
+
title, artist, year, genre = get_music_infos(search_not_same)
|
75 |
+
|
76 |
+
# Assert
|
77 |
+
self.assertEqual(title, None)
|
78 |
+
self.assertEqual(artist, None)
|
79 |
+
self.assertEqual(year, None)
|
80 |
+
self.assertEqual(genre, None)
|
81 |
+
|
82 |
+
# Act search_is_same and musicbrainz returns the same artist and title
|
83 |
+
title, artist, year, genre = get_music_infos(search_is_same)
|
84 |
+
|
85 |
+
# Assert
|
86 |
+
self.assertEqual(title, 'ArtistIsTitle')
|
87 |
+
self.assertEqual(artist, 'ArtistIsTitle')
|
88 |
+
self.assertEqual(year, None)
|
89 |
+
self.assertEqual(genre, None)
|
90 |
+
|
91 |
+
@patch('musicbrainzngs.search_artists')
|
92 |
+
@patch('musicbrainzngs.search_release_groups')
|
93 |
+
def test_get_music_infos(self, mock_search_release_groups, mock_search_artists):
|
94 |
+
# Arrange
|
95 |
+
artist = 'UltraSinger'
|
96 |
+
title = 'That\'s Rocking!'
|
97 |
+
search = f'{artist} - {title} (UltrStar 2023) FULL HD'
|
98 |
+
|
99 |
+
# Set up mock return values for the MusicBrainz API calls
|
100 |
+
mock_search_artists.return_value = {
|
101 |
+
'artist-list': [
|
102 |
+
{'name': f' {artist} '} # Also test leading and trailing whitespaces
|
103 |
+
]
|
104 |
+
}
|
105 |
+
|
106 |
+
mock_search_release_groups.return_value = {
|
107 |
+
'release-group-list': [
|
108 |
+
{
|
109 |
+
'title': f' {title} ', # Also test leading and trailing whitespaces
|
110 |
+
'artist-credit-phrase': f' {artist} ', # Also test leading and trailing whitespaces
|
111 |
+
'first-release-date': ' 2023-01-01 ', # Also test leading and trailing whitespaces
|
112 |
+
'tag-list': [
|
113 |
+
{'name': ' Genre 1 '}, # Also test leading and trailing whitespaces
|
114 |
+
{'name': ' Genre 2 '} # Also test leading and trailing whitespaces
|
115 |
+
]
|
116 |
+
}
|
117 |
+
]
|
118 |
+
}
|
119 |
+
|
120 |
+
# Act
|
121 |
+
title, artist, year, genre = get_music_infos(search)
|
122 |
+
|
123 |
+
# Assert
|
124 |
+
self.assertEqual(title, 'That\'s Rocking!')
|
125 |
+
self.assertEqual(artist, 'UltraSinger')
|
126 |
+
self.assertEqual(year, '2023-01-01')
|
127 |
+
self.assertEqual(genre, 'Genre 1,Genre 2,')
|
128 |
+
|
129 |
+
@patch('musicbrainzngs.search_artists')
|
130 |
+
@patch('musicbrainzngs.search_release_groups')
|
131 |
+
def test_get_empty_artist_music_infos(self, mock_search_release_groups, mock_search_artists):
|
132 |
+
# Arrange
|
133 |
+
artist = 'UltraSinger'
|
134 |
+
title = 'That\'s Rocking!'
|
135 |
+
search = f'{artist} - {title} (UltrStar 2023) FULL HD'
|
136 |
+
|
137 |
+
# Set up mock return values for the MusicBrainz API calls
|
138 |
+
mock_search_artists.return_value = {
|
139 |
+
'artist-list': []
|
140 |
+
}
|
141 |
+
|
142 |
+
mock_search_release_groups.return_value = {
|
143 |
+
'release-group-list': [
|
144 |
+
{
|
145 |
+
'title': f' {title} ', # Also test leading and trailing whitespaces
|
146 |
+
'artist-credit-phrase': f' {artist} ', # Also test leading and trailing whitespaces
|
147 |
+
'first-release-date': ' 2023-01-01 ', # Also test leading and trailing whitespaces
|
148 |
+
'tag-list': [
|
149 |
+
{'name': ' Genre 1 '}, # Also test leading and trailing whitespaces
|
150 |
+
{'name': ' Genre 2 '} # Also test leading and trailing whitespaces
|
151 |
+
]
|
152 |
+
}
|
153 |
+
]
|
154 |
+
}
|
155 |
+
|
156 |
+
# Act
|
157 |
+
title, artist, year, genre = get_music_infos(search)
|
158 |
+
|
159 |
+
# Assert
|
160 |
+
self.assertEqual(title, None)
|
161 |
+
self.assertEqual(artist, None)
|
162 |
+
self.assertEqual(year, None)
|
163 |
+
self.assertEqual(genre, None)
|
164 |
+
|
165 |
+
@patch('musicbrainzngs.search_artists')
|
166 |
+
@patch('musicbrainzngs.search_release_groups')
|
167 |
+
def test_get_empty_release_music_infos(self, mock_search_release_groups, mock_search_artists):
|
168 |
+
# Arrange
|
169 |
+
artist = 'UltraSinger'
|
170 |
+
title = 'That\'s Rocking!'
|
171 |
+
search = f'{artist} - {title} (UltrStar 2023) FULL HD'
|
172 |
+
|
173 |
+
# Set up mock return values for the MusicBrainz API calls
|
174 |
+
mock_search_artists.return_value = {
|
175 |
+
'artist-list': [
|
176 |
+
{'name': f' {artist} '} # Also test leading and trailing whitespaces
|
177 |
+
]
|
178 |
+
}
|
179 |
+
|
180 |
+
mock_search_release_groups.return_value = {
|
181 |
+
'release-group-list': []
|
182 |
+
}
|
183 |
+
|
184 |
+
# Act
|
185 |
+
title, artist, year, genre = get_music_infos(search)
|
186 |
+
|
187 |
+
# Assert
|
188 |
+
self.assertEqual(title, None)
|
189 |
+
self.assertEqual(artist, None)
|
190 |
+
self.assertEqual(year, None)
|
191 |
+
self.assertEqual(genre, None)
|
192 |
+
|
193 |
+
if __name__ == '__main__':
|
194 |
+
unittest.main()
|