Merge pull request #840 from tkem/feature/local-library-lookup-multiple

local: Return multiple tracks from lookup()
This commit is contained in:
Stein Magnus Jodal 2014-09-23 14:14:22 +02:00
commit 1fcc75ba1e
4 changed files with 27 additions and 9 deletions

View File

@ -99,11 +99,9 @@ class Library(object):
"""
Lookup the given URI.
Unlike the core APIs, local tracks uris can only be resolved to a
single track.
:param string uri: track URI
:rtype: :class:`~mopidy.models.Track`
:rtype: List of :class:`~mopidy.models.Track` or single
:class:`~mopidy.models.Track` for backward compatibility
"""
raise NotImplementedError

View File

@ -151,9 +151,9 @@ class JsonLibrary(local.Library):
def lookup(self, uri):
try:
return self._tracks[uri]
return [self._tracks[uri]]
except KeyError:
return None
return []
def search(self, query=None, limit=100, offset=0, uris=None, exact=False):
tracks = self._tracks.values()

View File

@ -33,11 +33,13 @@ class LocalLibraryProvider(backend.LibraryProvider):
def lookup(self, uri):
if not self._library:
return []
track = self._library.lookup(uri)
if track is None:
tracks = self._library.lookup(uri)
if tracks is None:
logger.debug('Failed to lookup %r', uri)
return []
return [track]
if isinstance(tracks, models.Track):
tracks = [tracks]
return tracks
def find_exact(self, query=None, uris=None):
if not self._library:

View File

@ -5,6 +5,8 @@ import shutil
import tempfile
import unittest
import mock
import pykka
from mopidy import core
@ -129,6 +131,22 @@ class LocalLibraryProviderTest(unittest.TestCase):
tracks = self.library.lookup('fake uri')
self.assertEqual(tracks, [])
# test backward compatibility with local libraries returning a
# single Track
@mock.patch.object(json.JsonLibrary, 'lookup')
def test_lookup_return_single_track(self, mock_lookup):
backend = actor.LocalBackend(config=self.config, audio=None)
mock_lookup.return_value = self.tracks[0]
tracks = backend.library.lookup(self.tracks[0].uri)
mock_lookup.assert_called_with(self.tracks[0].uri)
self.assertEqual(tracks, self.tracks[0:1])
mock_lookup.return_value = None
tracks = backend.library.lookup('fake uri')
mock_lookup.assert_called_with('fake uri')
self.assertEqual(tracks, [])
# TODO: move to search_test module
def test_find_exact_no_hits(self):
result = self.library.find_exact(track_name=['unknown track'])