From 611dc44c46dd3bdee7e50008df7fc4999673a048 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Wed, 29 May 2013 14:51:16 -0700 Subject: [PATCH] fix tests for ID parsing (#291) The previous tests accessed the network. This refactoring lets us test the ID parsing in isolation. --- beets/autotag/mb.py | 25 ++++++++++++++++++------- test/test_mb.py | 22 ++++++++++++---------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/beets/autotag/mb.py b/beets/autotag/mb.py index 6fe7854d3..8dd4b4688 100644 --- a/beets/autotag/mb.py +++ b/beets/autotag/mb.py @@ -322,24 +322,32 @@ def match_track(artist, title, limit=SEARCH_LIMIT): for recording in res['recording-list']: yield track_info(recording) +def _parse_id(s): + """Search for a MusicBrainz ID in the given string and return it. If + no ID can be found, return None. + """ + # Find the first thing that looks like a UUID/MBID. + match = re.search('[a-f0-9]{8}(-[a-f0-9]{4}){3}-[a-f0-9]{12}', s) + if match: + log.error('Invalid MBID.') + return match.group() + def album_for_id(albumid): """Fetches an album by its MusicBrainz ID and returns an AlbumInfo object or None if the album is not found. May raise a MusicBrainzAPIError. """ - # Find the first thing that looks like a UUID/MBID. - match = re.search('[a-f0-9]{8}(-[a-f0-9]{4}){3}-[a-f0-9]{12}', albumid) - if not match: - log.error('Invalid MBID.') - return None + albumid = _parse_id(albumid) + if not albumid: + return try: - res = musicbrainzngs.get_release_by_id(match.group(), + res = musicbrainzngs.get_release_by_id(albumid, RELEASE_INCLUDES) except musicbrainzngs.ResponseError: log.debug('Album ID match failed.') return None except musicbrainzngs.MusicBrainzError as exc: - raise MusicBrainzAPIError(exc, 'get release by ID', match.group(), + raise MusicBrainzAPIError(exc, 'get release by ID', albumid, traceback.format_exc()) return album_info(res['release']) @@ -347,6 +355,9 @@ def track_for_id(trackid): """Fetches a track by its MusicBrainz ID. Returns a TrackInfo object or None if no track is found. May raise a MusicBrainzAPIError. """ + trackid = _parse_id(trackid) + if not trackid: + return try: res = musicbrainzngs.get_recording_by_id(trackid, TRACK_INCLUDES) except musicbrainzngs.ResponseError: diff --git a/test/test_mb.py b/test/test_mb.py index 278e5cd1f..877e4a567 100644 --- a/test/test_mb.py +++ b/test/test_mb.py @@ -14,11 +14,12 @@ """Tests for MusicBrainz API wrapper. """ +import _common from _common import unittest from beets.autotag import mb from beets import config -class MBAlbumInfoTest(unittest.TestCase): +class MBAlbumInfoTest(_common.TestCase): def _make_release(self, date_str='2009', tracks=None): release = { 'title': 'ALBUM TITLE', @@ -277,23 +278,24 @@ class MBAlbumInfoTest(unittest.TestCase): self.assertEqual(track.artist_sort, 'TRACK ARTIST SORT NAME') self.assertEqual(track.artist_credit, 'TRACK ARTIST CREDIT') - def test_album_for_id_correct(self): +class ParseIDTest(_common.TestCase): + def test_parse_id_correct(self): id_string = "28e32c71-1450-463e-92bf-e0a46446fc11" - out = mb.album_for_id(id_string) - self.assertEqual(out.album_id, id_string) + out = mb._parse_id(id_string) + self.assertEqual(out, id_string) - def test_album_for_id_non_id_returns_none(self): + def test_parse_id_non_id_returns_none(self): id_string = "blah blah" - out = mb.album_for_id(id_string) + out = mb._parse_id(id_string) self.assertEqual(out, None) - def test_album_for_id_url_finds_id(self): + def test_parse_id_url_finds_id(self): id_string = "28e32c71-1450-463e-92bf-e0a46446fc11" id_url = "http://musicbrainz.org/entity/%s" % id_string - out = mb.album_for_id(id_url) - self.assertEqual(out.album_id, id_string) + out = mb._parse_id(id_url) + self.assertEqual(out, id_string) -class ArtistFlatteningTest(unittest.TestCase): +class ArtistFlatteningTest(_common.TestCase): def _credit_dict(self, suffix=''): return { 'artist': {