Fix object repr functions

This commit is contained in:
Pete 2013-08-29 14:23:09 -07:00
parent f742df27b4
commit 431e8fcc8a
2 changed files with 31 additions and 3 deletions

View file

@ -443,3 +443,31 @@ class TestPhotoTransform(TestPhotos):
photo.transform(rotate="90") photo.transform(rotate="90")
mock_post.assert_called_with("/photo/1a/transform.json", rotate="90") mock_post.assert_called_with("/photo/1a/transform.json", rotate="90")
self.assertEqual(photo.get_fields(), self.test_photos_dict[1]) self.assertEqual(photo.get_fields(), self.test_photos_dict[1])
class TestPhotoObject(TestPhotos):
def test_photo_object_repr_without_id_or_name(self):
"""
Ensure the string representation on an object includes its class name
if the ID and Name attributes don't exist.
"""
photo = trovebox.objects.photo.Photo(self.client, {})
self.assertEqual(repr(photo), "<Photo>")
def test_photo_object_repr_with_id(self):
""" Ensure the string representation on an object includes its id, if present """
photo = trovebox.objects.photo.Photo(self.client, {"id": "Test ID"})
self.assertEqual(repr(photo), "<Photo id='Test ID'>")
def test_photo_object_repr_with_id_and_name(self):
""" Ensure the string representation on an object includes its name, if present """
photo = trovebox.objects.photo.Photo(self.client, {"id": "Test ID",
"name": "Test Name"})
self.assertEqual(repr(photo), "<Photo name='Test Name'>")
def test_photo_object_illegal_attribute(self):
"""
Check that an exception is raised when creating an Photo object
with an illegal attribute
"""
with self.assertRaises(ValueError):
photo = trovebox.objects.photo.Photo(self.client, {"_illegal_attribute": "test"})

View file

@ -39,11 +39,11 @@ class TroveboxObject(object):
def __repr__(self): def __repr__(self):
if self.name is not None: if self.name is not None:
return "<%s name='%s'>" % (self.__class__, self.name) return "<%s name='%s'>" % (self.__class__.__name__, self.name)
elif self.id is not None: elif self.id is not None:
return "<%s id='%s'>" % (self.__class__, self.id) return "<%s id='%s'>" % (self.__class__.__name__, self.id)
else: else:
return "<%s>" % (self.__class__) return "<%s>" % (self.__class__.__name__)
def get_fields(self): def get_fields(self):
""" Returns this object's attributes """ """ Returns this object's attributes """