From f67e55618cbbd9c121520df5402c7d664ec4e120 Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Sat, 21 Mar 2015 00:11:15 +0100 Subject: [PATCH] core: Make lookup(uris=...) return dict with all uris All uris given to lookup should be in the result even if there is no backend to handle the uri, and the lookup result thus is an empty list. As a side effect, future.get() is now called in the order of the URIs in the `uris` list, making it easier to mock out backend.library.lookup() in core layer tests. --- mopidy/core/library.py | 14 ++++++++++---- tests/core/test_library.py | 9 ++++++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/mopidy/core/library.py b/mopidy/core/library.py index f2a8b9bd..b8018b16 100644 --- a/mopidy/core/library.py +++ b/mopidy/core/library.py @@ -188,20 +188,26 @@ class LibraryController(object): if none_set or both_set: raise ValueError("One of 'uri' or 'uris' must be set") + if uri is not None: + uris = [uri] + futures = {} result = {} - backends = self._get_backends_to_uris([uri] if uri else uris) + backends = self._get_backends_to_uris(uris) # TODO: lookup(uris) to backend APIs for backend, backend_uris in backends.items(): for u in backend_uris or []: futures[u] = backend.library.lookup(u) - for u, future in futures.items(): - result[u] = future.get() + for u in uris: + if u in futures: + result[u] = futures[u].get() + else: + result[u] = [] if uri: - return result.get(uri, []) + return result[uri] return result def refresh(self, uri=None): diff --git a/tests/core/test_library.py b/tests/core/test_library.py index b71e5de5..9eacd1a2 100644 --- a/tests/core/test_library.py +++ b/tests/core/test_library.py @@ -168,13 +168,20 @@ class CoreLibraryTest(unittest.TestCase): result = self.core.library.lookup(uris=['dummy1:a', 'dummy2:a']) self.assertEqual(result, {'dummy2:a': [5678], 'dummy1:a': [1234]}) - def test_lookup_returns_nothing_for_dummy3_track(self): + def test_lookup_uri_returns_empty_list_for_dummy3_track(self): result = self.core.library.lookup('dummy3:a') self.assertEqual(result, []) self.assertFalse(self.library1.lookup.called) self.assertFalse(self.library2.lookup.called) + def test_lookup_uris_returns_empty_list_for_dummy3_track(self): + result = self.core.library.lookup(uris=['dummy3:a']) + + self.assertEqual(result, {'dummy3:a': []}) + self.assertFalse(self.library1.lookup.called) + self.assertFalse(self.library2.lookup.called) + def test_refresh_with_uri_selects_dummy1_backend(self): self.core.library.refresh('dummy1:a')