From 9b9cdc3ade91e63db6e40ec6f2009062acce2281 Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Mon, 22 Dec 2014 22:29:36 +0100 Subject: [PATCH] stream: Fix track conversion bug and add tests This adds basic checks for the library provider lookup: - Check that uri schemes are respected - Check that blacklisting and globbing works - Check uri successfully gets converted to a track --- mopidy/stream/actor.py | 3 ++- tests/stream/__init__.py | 0 tests/stream/test_library.py | 43 ++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 tests/stream/__init__.py create mode 100644 tests/stream/test_library.py diff --git a/mopidy/stream/actor.py b/mopidy/stream/actor.py index 96d405e6..9599d9d3 100644 --- a/mopidy/stream/actor.py +++ b/mopidy/stream/actor.py @@ -45,7 +45,8 @@ class StreamLibraryProvider(backend.LibraryProvider): try: tags, duration = self._scanner.scan(uri) - track = utils.tags_to_track(tags).copy(uri=uri, length=duration) + track = utils.convert_tags_to_track(tags).copy( + uri=uri, length=duration) except exceptions.ScannerError as e: logger.warning('Problem looking up %s: %s', uri, e) track = Track(uri=uri) diff --git a/tests/stream/__init__.py b/tests/stream/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/stream/test_library.py b/tests/stream/test_library.py new file mode 100644 index 00000000..d90610d2 --- /dev/null +++ b/tests/stream/test_library.py @@ -0,0 +1,43 @@ +from __future__ import absolute_import, unicode_literals + +import unittest + +import gobject +gobject.threads_init() + +import pygst +pygst.require('0.10') +import gst # noqa: pygst magic is needed to import correct gst + +import mock + +from mopidy.models import Album, Track +from mopidy.stream import actor +from mopidy.utils.path import path_to_uri + +from tests import path_to_data_dir + + +class LibraryProviderTest(unittest.TestCase): + def setUp(self): # noqa: ignore method must be lowercase + self.backend = mock.Mock() + self.backend.uri_schemes = ['file'] + self.uri = path_to_uri(path_to_data_dir('song1.wav')) + + def test_lookup_ignores_unknown_scheme(self): + library = actor.StreamLibraryProvider(self.backend, 1000, []) + self.assertFalse(library.lookup('http://example.com')) + + def test_lookup_respects_blacklist(self): + library = actor.StreamLibraryProvider(self.backend, 100, [self.uri]) + self.assertEqual([Track(uri=self.uri)], library.lookup(self.uri)) + + def test_lookup_respects_blacklist_globbing(self): + blacklist = [path_to_uri(path_to_data_dir('')) + '*'] + library = actor.StreamLibraryProvider(self.backend, 100, blacklist) + self.assertEqual([Track(uri=self.uri)], library.lookup(self.uri)) + + def test_lookup_converts_uri_metadata_to_track(self): + library = actor.StreamLibraryProvider(self.backend, 100, []) + self.assertEqual([Track(length=4406, uri=self.uri, album=Album())], + library.lookup(self.uri))