Ensure that models don't accept unknown kwarg keys
This commit is contained in:
parent
0bcda9920b
commit
1da9dced77
@ -10,7 +10,11 @@ class ImmutableObject(object):
|
||||
"""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.__dict__.update(kwargs)
|
||||
for key, value in kwargs.items():
|
||||
if not hasattr(self, key):
|
||||
raise TypeError('__init__() got an unexpected keyword ' + \
|
||||
'argument \'%s\'' % key)
|
||||
self.__dict__[key] = value
|
||||
|
||||
def __setattr__(self, name, value):
|
||||
if name.startswith('_'):
|
||||
|
||||
@ -18,6 +18,10 @@ class ArtistTest(unittest.TestCase):
|
||||
self.assertEqual(artist.name, name)
|
||||
self.assertRaises(AttributeError, setattr, artist, 'name', None)
|
||||
|
||||
def test_invalid_kwarg(self):
|
||||
test = lambda: Artist(foo='baz')
|
||||
self.assertRaises(TypeError, test)
|
||||
|
||||
def test_eq_name(self):
|
||||
artist1 = Artist(name=u'name')
|
||||
artist2 = Artist(name=u'name')
|
||||
@ -86,6 +90,10 @@ class AlbumTest(unittest.TestCase):
|
||||
self.assertEqual(album.num_tracks, num_tracks)
|
||||
self.assertRaises(AttributeError, setattr, album, 'num_tracks', None)
|
||||
|
||||
def test_invalid_kwarg(self):
|
||||
test = lambda: Album(foo='baz')
|
||||
self.assertRaises(TypeError, test)
|
||||
|
||||
def test_eq_name(self):
|
||||
album1 = Album(name=u'name')
|
||||
album2 = Album(name=u'name')
|
||||
@ -257,6 +265,10 @@ class TrackTest(unittest.TestCase):
|
||||
track = Track(artists=[Artist(name=u'ABBA'), Artist(name=u'Beatles')])
|
||||
self.assertEqual(track.mpd_format_artists(), u'ABBA, Beatles')
|
||||
|
||||
def test_invalid_kwarg(self):
|
||||
test = lambda: Track(foo='baz')
|
||||
self.assertRaises(TypeError, test)
|
||||
|
||||
def test_eq_uri(self):
|
||||
track1 = Track(uri=u'uri1')
|
||||
track2 = Track(uri=u'uri1')
|
||||
@ -509,6 +521,10 @@ class PlaylistTest(unittest.TestCase):
|
||||
self.assertEqual(new_playlist.tracks, tracks)
|
||||
self.assertEqual(new_playlist.last_modified, new_last_modified)
|
||||
|
||||
def test_invalid_kwarg(self):
|
||||
test = lambda: Playlist(foo='baz')
|
||||
self.assertRaises(TypeError, test)
|
||||
|
||||
def test_eq(self):
|
||||
# FIXME missing all equal and hash tests
|
||||
raise SkipTest
|
||||
|
||||
Loading…
Reference in New Issue
Block a user