mirror of
https://github.com/beetbox/beets.git
synced 2025-12-30 20:42:37 +01:00
fix tests for ID parsing (#291)
The previous tests accessed the network. This refactoring lets us test the ID parsing in isolation.
This commit is contained in:
parent
303cd9ba00
commit
611dc44c46
2 changed files with 30 additions and 17 deletions
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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': {
|
||||
|
|
|
|||
Loading…
Reference in a new issue