Merge pull request #621 from jodal/feature/ref-model

models: Add lightweight Ref model with URI, name, and type
This commit is contained in:
Thomas Adamcik 2013-12-29 14:57:18 -08:00
commit cd261f9aaa
2 changed files with 60 additions and 1 deletions

View File

@ -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

View File

@ -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'