diff --git a/mopidy/models.py b/mopidy/models.py index aab69a3f..e1a1270f 100644 --- a/mopidy/models.py +++ b/mopidy/models.py @@ -278,8 +278,8 @@ class Album(ImmutableObject): # actual usage of this field with more than one image. def __init__(self, *args, **kwargs): - self.__dict__['artists'] = frozenset(kwargs.pop('artists', [])) - self.__dict__['images'] = frozenset(kwargs.pop('images', [])) + self.__dict__['artists'] = frozenset(kwargs.pop('artists', None) or []) + self.__dict__['images'] = frozenset(kwargs.pop('images', None) or []) super(Album, self).__init__(*args, **kwargs) @@ -365,9 +365,10 @@ class Track(ImmutableObject): last_modified = 0 def __init__(self, *args, **kwargs): - self.__dict__['artists'] = frozenset(kwargs.pop('artists', [])) - self.__dict__['composers'] = frozenset(kwargs.pop('composers', [])) - self.__dict__['performers'] = frozenset(kwargs.pop('performers', [])) + get = lambda key: frozenset(kwargs.pop(key, None) or []) + self.__dict__['artists'] = get('artists') + self.__dict__['composers'] = get('composers') + self.__dict__['performers'] = get('performers') super(Track, self).__init__(*args, **kwargs) @@ -436,7 +437,7 @@ class Playlist(ImmutableObject): last_modified = None def __init__(self, *args, **kwargs): - self.__dict__['tracks'] = tuple(kwargs.pop('tracks', [])) + self.__dict__['tracks'] = tuple(kwargs.pop('tracks', None) or []) super(Playlist, self).__init__(*args, **kwargs) # TODO: def insert(self, pos, track): ... ? @@ -472,7 +473,7 @@ class SearchResult(ImmutableObject): albums = tuple() def __init__(self, *args, **kwargs): - self.__dict__['tracks'] = tuple(kwargs.pop('tracks', [])) - self.__dict__['artists'] = tuple(kwargs.pop('artists', [])) - self.__dict__['albums'] = tuple(kwargs.pop('albums', [])) + self.__dict__['tracks'] = tuple(kwargs.pop('tracks', None) or []) + self.__dict__['artists'] = tuple(kwargs.pop('artists', None) or []) + self.__dict__['albums'] = tuple(kwargs.pop('albums', None) or []) super(SearchResult, self).__init__(*args, **kwargs) diff --git a/tests/test_models.py b/tests/test_models.py index 5872e0e6..9a4f97b7 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -276,6 +276,9 @@ class AlbumTest(unittest.TestCase): self.assertIn(artist, album.artists) self.assertRaises(AttributeError, setattr, album, 'artists', None) + def test_artists_none(self): + self.assertEqual(set(), Album(artists=None).artists) + def test_num_tracks(self): num_tracks = 11 album = Album(num_tracks=num_tracks) @@ -307,6 +310,9 @@ class AlbumTest(unittest.TestCase): self.assertIn(image, album.images) self.assertRaises(AttributeError, setattr, album, 'images', None) + def test_images_none(self): + self.assertEqual(set(), Album(images=None).images) + def test_invalid_kwarg(self): test = lambda: Album(foo='baz') self.assertRaises(TypeError, test) @@ -476,6 +482,27 @@ class TrackTest(unittest.TestCase): self.assertEqual(set(track.artists), set(artists)) self.assertRaises(AttributeError, setattr, track, 'artists', None) + def test_artists_none(self): + self.assertEqual(set(), Track(artists=None).artists) + + def test_composers(self): + artists = [Artist(name='name1'), Artist(name='name2')] + track = Track(composers=artists) + self.assertEqual(set(track.composers), set(artists)) + self.assertRaises(AttributeError, setattr, track, 'composers', None) + + def test_composers_none(self): + self.assertEqual(set(), Track(composers=None).composers) + + def test_performers(self): + artists = [Artist(name='name1'), Artist(name='name2')] + track = Track(performers=artists) + self.assertEqual(set(track.performers), set(artists)) + self.assertRaises(AttributeError, setattr, track, 'performers', None) + + def test_performers_none(self): + self.assertEqual(set(), Track(performers=None).performers) + def test_album(self): album = Album() track = Track(album=album)