From e87d5729e3c30cfe6771fe57b9712933bd72ea4b Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Sun, 29 Dec 2013 19:31:12 +0100 Subject: [PATCH 1/2] models: Add lightweight Ref model with URI, name, and type --- mopidy/models.py | 21 +++++++++++++++++++++ tests/models_test.py | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/mopidy/models.py b/mopidy/models.py index 04d71591..1aace8db 100644 --- a/mopidy/models.py +++ b/mopidy/models.py @@ -136,6 +136,27 @@ def model_json_decoder(dct): return dct +class Ref(ImmutableObject): + """ + :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' From d3dcceefc52f7ca417e84a47a6951d1b7b543fad Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Sun, 29 Dec 2013 21:08:46 +0100 Subject: [PATCH 2/2] models: Add description of Ref model --- mopidy/models.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mopidy/models.py b/mopidy/models.py index 1aace8db..0e40a8f6 100644 --- a/mopidy/models.py +++ b/mopidy/models.py @@ -138,6 +138,10 @@ def model_json_decoder(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