From 9da5ccbb79a05cec6e1dcd4f6058e85451630488 Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Thu, 9 Jan 2014 08:50:26 +0100 Subject: [PATCH] models: Add type specific constructors to Ref --- mopidy/models.py | 30 ++++++++++++++++++++++++++++++ tests/models_test.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/mopidy/models.py b/mopidy/models.py index b3d2a1b8..53083eb7 100644 --- a/mopidy/models.py +++ b/mopidy/models.py @@ -175,6 +175,36 @@ class Ref(ImmutableObject): #: Constant used for comparision with the :attr:`type` field. TRACK = 'track' + @classmethod + def album(cls, **kwargs): + """Create a :class:`Ref` with ``type`` :attr:`ALBUM`.""" + kwargs['type'] = Ref.ALBUM + return cls(**kwargs) + + @classmethod + def artist(cls, **kwargs): + """Create a :class:`Ref` with ``type`` :attr:`ARTIST`.""" + kwargs['type'] = Ref.ARTIST + return cls(**kwargs) + + @classmethod + def directory(cls, **kwargs): + """Create a :class:`Ref` with ``type`` :attr:`DIRECTORY`.""" + kwargs['type'] = Ref.DIRECTORY + return cls(**kwargs) + + @classmethod + def playlist(cls, **kwargs): + """Create a :class:`Ref` with ``type`` :attr:`PLAYLIST`.""" + kwargs['type'] = Ref.PLAYLIST + return cls(**kwargs) + + @classmethod + def track(cls, **kwargs): + """Create a :class:`Ref` with ``type`` :attr:`TRACK`.""" + kwargs['type'] = Ref.TRACK + return cls(**kwargs) + class Artist(ImmutableObject): """ diff --git a/tests/models_test.py b/tests/models_test.py index b2b72ea4..02cba8f4 100644 --- a/tests/models_test.py +++ b/tests/models_test.py @@ -94,6 +94,36 @@ class RefTest(unittest.TestCase): self.assertEqual(Ref.PLAYLIST, 'playlist') self.assertEqual(Ref.TRACK, 'track') + def test_album_constructor(self): + ref = Ref.album(uri='foo', name='bar') + self.assertEqual(ref.uri, 'foo') + self.assertEqual(ref.name, 'bar') + self.assertEqual(ref.type, Ref.ALBUM) + + def test_artist_constructor(self): + ref = Ref.artist(uri='foo', name='bar') + self.assertEqual(ref.uri, 'foo') + self.assertEqual(ref.name, 'bar') + self.assertEqual(ref.type, Ref.ARTIST) + + def test_directory_constructor(self): + ref = Ref.directory(uri='foo', name='bar') + self.assertEqual(ref.uri, 'foo') + self.assertEqual(ref.name, 'bar') + self.assertEqual(ref.type, Ref.DIRECTORY) + + def test_playlist_constructor(self): + ref = Ref.playlist(uri='foo', name='bar') + self.assertEqual(ref.uri, 'foo') + self.assertEqual(ref.name, 'bar') + self.assertEqual(ref.type, Ref.PLAYLIST) + + def test_track_constructor(self): + ref = Ref.track(uri='foo', name='bar') + self.assertEqual(ref.uri, 'foo') + self.assertEqual(ref.name, 'bar') + self.assertEqual(ref.type, Ref.TRACK) + class ArtistTest(unittest.TestCase): def test_uri(self):