diff --git a/mopidy/models.py b/mopidy/models.py index 04d71591..0e40a8f6 100644 --- a/mopidy/models.py +++ b/mopidy/models.py @@ -136,6 +136,31 @@ def model_json_decoder(dct): return dct +class Ref(ImmutableObject): + """ + Model to represent URI references with a human friendly name and type + attached. This is intended for use a lightweight object "free" of metadata + that can be passed around instead of using full blown models. + + :param uri: object URI + :type uri: string + :param name: object name + :type name: string + :param type: object type + :type name: string + """ + + #: The object URI. Read-only. + uri = None + + #: The object name. Read-only. + name = None + + #: The object type, e.g. "artist", "album", "track", "playlist", + #: "directory". Read-only. + type = None + + class Artist(ImmutableObject): """ :param uri: artist URI diff --git a/tests/models_test.py b/tests/models_test.py index 9f43e624..50faf89e 100644 --- a/tests/models_test.py +++ b/tests/models_test.py @@ -5,7 +5,7 @@ import json import unittest from mopidy.models import ( - Artist, Album, TlTrack, Track, Playlist, SearchResult, + Ref, Artist, Album, TlTrack, Track, Playlist, SearchResult, ModelJSONEncoder, model_json_decoder) @@ -54,6 +54,40 @@ class GenericCopyTest(unittest.TestCase): self.assertRaises(TypeError, test) +class RefTest(unittest.TestCase): + def test_uri(self): + uri = 'an_uri' + ref = Ref(uri=uri) + self.assertEqual(ref.uri, uri) + self.assertRaises(AttributeError, setattr, ref, 'uri', None) + + def test_name(self): + name = 'a name' + ref = Ref(name=name) + self.assertEqual(ref.name, name) + self.assertRaises(AttributeError, setattr, ref, 'name', None) + + def test_invalid_kwarg(self): + test = lambda: SearchResult(foo='baz') + self.assertRaises(TypeError, test) + + def test_repr_without_results(self): + self.assertEquals( + "Ref(name=u'foo', type=u'artist', uri=u'uri')", + repr(Ref(uri='uri', name='foo', type='artist'))) + + def test_serialize_without_results(self): + self.assertDictEqual( + {'__model__': 'Ref', 'uri': 'uri'}, + Ref(uri='uri').serialize()) + + def test_to_json_and_back(self): + ref1 = Ref(uri='uri') + serialized = json.dumps(ref1, cls=ModelJSONEncoder) + ref2 = json.loads(serialized, object_hook=model_json_decoder) + self.assertEqual(ref1, ref2) + + class ArtistTest(unittest.TestCase): def test_uri(self): uri = 'an_uri'