From 97a272bd3a3e8a1b27a09e74bea11a44c7d3dcd0 Mon Sep 17 00:00:00 2001 From: Philipp Wolfer Date: Thu, 27 Jan 2022 17:01:15 +0100 Subject: [PATCH] Added tests for FileLookup.mbid_lookup --- test/test_browser.py | 122 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 99 insertions(+), 23 deletions(-) diff --git a/test/test_browser.py b/test/test_browser.py index 9add56953..6330f9c46 100644 --- a/test/test_browser.py +++ b/test/test_browser.py @@ -22,7 +22,10 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -from unittest.mock import patch +from unittest.mock import ( + Mock, + patch, +) from urllib.parse import ( parse_qs, urlparse, @@ -45,33 +48,106 @@ class BrowserLookupTest(PicardTestCase): super().setUp() self.lookup = FileLookup(None, SERVER, PORT, LOCAL_PORT) + def assert_mbid_url_matches(self, url, entity, mbid, expected_query_args=None): + url = urlparse(url) + path = url.path.split('/')[1:] + query_args = parse_qs(url.query) + + self.assertEqual(url.netloc, "%s:%s" % (SERVER, PORT)) + + self.assertEqual(entity, path[0]) + self.assertEqual(mbid, path[1]) + + if expected_query_args: + for key, value in expected_query_args.items(): + self.assertIn(key, query_args) + self.assertEqual(value, query_args['tport'][0]) + + + def test_entity_lookups(self): + lookups = ( + {'function': self.lookup.recording_lookup, 'entity': 'recording'}, + {'function': self.lookup.track_lookup, 'entity': 'track'}, + {'function': self.lookup.album_lookup, 'entity': 'release'}, + {'function': self.lookup.work_lookup, 'entity': 'work'}, + {'function': self.lookup.artist_lookup, 'entity': 'artist'}, + {'function': self.lookup.artist_lookup, 'entity': 'artist'}, + {'function': self.lookup.release_group_lookup, 'entity': 'release-group'}, + {'function': self.lookup.discid_lookup, 'entity': 'cdtoc'}, + ) + for case in lookups: + with patch.object(webbrowser2, 'open') as mock_open: + case['function']("123") + mock_open.assert_called_once() + url = mock_open.call_args[0][0] + query_args = {'tport': '8000'} + self.assert_mbid_url_matches(url, case['entity'], '123', query_args) + + def test_mbid_lookup_invalid_url(self): + self.assertFalse(self.lookup.mbid_lookup('noentity:123')) + + def test_mbid_lookup_no_entity(self): + self.assertFalse(self.lookup.mbid_lookup('F03D09B3-39DC-4083-AFD6-159E3F0D462F')) + @patch.object(webbrowser2, 'open') - def test_entity_lookups(self, mock_open): - lookups = { - "recording": {'function': self.lookup.recording_lookup, 'path': 'recording'}, - "track": {'function': self.lookup.track_lookup, 'path': 'track'}, - "album": {'function': self.lookup.album_lookup, 'path': 'release'}, - "work": {'function': self.lookup.work_lookup, 'path': 'work'}, - "artist": {'function': self.lookup.artist_lookup, 'path': 'artist'}, - "albumartist": {'function': self.lookup.artist_lookup, 'path': 'artist'}, - "releasegroup": {'function': self.lookup.release_group_lookup, 'path': 'release-group'}, - "cdtoc": {'function': self.lookup.discid_lookup, 'path': 'cdtoc'}, - } - for i, type_ in enumerate(lookups): - lookups[type_]['function']("123") + def test_mbid_lookup_set_type(self, mock_open): + result = self.lookup.mbid_lookup('bd55aeb7-19d1-4607-a500-14b8479d3fed', 'place') + self.assertTrue(result) + mock_open.assert_called_once() + url = mock_open.call_args.args[0] + self.assert_mbid_url_matches(url, 'place', 'bd55aeb7-19d1-4607-a500-14b8479d3fed') - url = urlparse(mock_open.call_args[0][0]) - path = url.path.split('/')[1:] - query_args = parse_qs(url.query) + @patch.object(webbrowser2, 'open') + def test_mbid_lookup_matched_callback(self, mock_open): + mock_matched_callback = Mock() + result = self.lookup.mbid_lookup('area:F03D09B3-39DC-4083-AFD6-159E3F0D462F', mbid_matched_callback=mock_matched_callback) + self.assertTrue(result) + mock_open.assert_called_once() + url = mock_open.call_args.args[0] + self.assert_mbid_url_matches(url, 'area', 'f03d09b3-39dc-4083-afd6-159e3f0d462f') - self.assertEqual(mock_open.call_count, i + 1) - self.assertEqual(url.netloc, "%s:%s" % (SERVER, PORT)) + @patch('PyQt5.QtCore.QObject.tagger') + def test_mbid_lookup_release(self, mock_tagger): + url = 'https://musicbrainz.org/release/60dbf818-3058-41b9-bb53-25dbdb9d9bad' + result = self.lookup.mbid_lookup(url) + self.assertTrue(result) + mock_tagger.load_album.assert_called_once_with('60dbf818-3058-41b9-bb53-25dbdb9d9bad') - self.assertEqual(lookups[type_]['path'], path[0]) - self.assertEqual("123", path[1]) + @patch('PyQt5.QtCore.QObject.tagger') + def test_mbid_lookup_recording(self, mock_tagger): + url = 'https://musicbrainz.org/recording/511f3a33-ded8-4dc7-92d2-b913ec420dfc' + result = self.lookup.mbid_lookup(url) + self.assertTrue(result) + mock_tagger.load_nat.assert_called_once_with('511f3a33-ded8-4dc7-92d2-b913ec420dfc') - self.assertIn('tport', query_args) - self.assertEqual(query_args['tport'][0], '8000') + @patch('PyQt5.QtCore.QObject.tagger') + @patch('picard.browser.filelookup.AlbumSearchDialog') + def test_mbid_lookup_release_group(self, mock_dialog, mock_tagger): + url = 'https://musicbrainz.org/release-group/168615bf-f841-49f7-ac98-36a4eb25479c' + result = self.lookup.mbid_lookup(url) + self.assertTrue(result) + mock_dialog.assert_called_once_with(mock_tagger.window, force_advanced_search=True) + instance = mock_dialog.return_value + instance.search.assert_called_once_with('rgid:168615bf-f841-49f7-ac98-36a4eb25479c') + instance.exec_.assert_called_once() + + def test_mbid_lookup_browser_fallback(self): + mbid = '4836aa50-a9ae-490a-983b-cfc8efca92de' + for entity in {'area', 'artist', 'label', 'url', 'work'}: + with patch.object(webbrowser2, 'open') as mock_open: + uri = '%s:%s' % (entity, mbid) + result = self.lookup.mbid_lookup(uri) + self.assertTrue(result, 'lookup failed for %s' % uri) + mock_open.assert_called_once() + url = mock_open.call_args.args[0] + self.assert_mbid_url_matches(url, entity, mbid) + + @patch.object(webbrowser2, 'open') + def test_mbid_lookup_browser_fallback_disabled(self, mock_open): + url = 'https://musicbrainz.org/artist/4836aa50-a9ae-490a-983b-cfc8efca92de' + result = self.lookup.mbid_lookup(url, browser_fallback=False) + self.assertFalse(result) + mock_open.assert_not_called() @patch('picard.browser.filelookup.Disc') def test_mbid_lookup_cdtoc(self, mock_disc):