Skip to content
This repository was archived by the owner on Aug 7, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions tests/test_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ def testEq(self):

self.assertEqual(media, self._GetSampleMedia())

def testHash(self):
'''Test the twitter.Media __hash__ method'''
media = self._GetSampleMedia()
self.assertEqual(hash(media), hash(media.id))

def testNewFromJsonDict(self):
'''Test the twitter.Media NewFromJsonDict method'''
data = json.loads(MediaTest.RAW_JSON)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ def testEq(self):
status.user = self._GetSampleUser()
self.assertEqual(status, self._GetSampleStatus())

def testHash(self):
'''Test the twitter.Status __hash__ method'''
status = self._GetSampleStatus()
self.assertEqual(hash(status), hash(status.id))

def testNewFromJsonDict(self):
'''Test the twitter.Status NewFromJsonDict method'''
data = json.loads(StatusTest.SAMPLE_JSON)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_trend.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ def testEq(self):
trend.query = 'Kesuke Miyagi'
trend.timestamp = 'Fri Jan 26 23:17:14 +0000 2007'
self.assertEqual(trend, self._GetSampleTrend())

def testHash(self):
'''Test the twitter.Trent __hash__ method'''
trend = self._GetSampleTrend()
with self.assertRaises(TypeError) as context:
hash(trend)
self.assertIn('unhashable type: {} (no id attribute)'
.format(type(trend)), str(context.exception))
5 changes: 5 additions & 0 deletions tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ def testEq(self):
user.status = self._GetSampleStatus()
self.assertEqual(user, self._GetSampleUser())

def testHash(self):
'''Test the twitter.User __hash__ method'''
user = self._GetSampleUser()
self.assertEqual(hash(user), hash(user.id))

def testNewFromJsonDict(self):
'''Test the twitter.User NewFromJsonDict method'''
data = json.loads(UserTest.SAMPLE_JSON)
Expand Down
7 changes: 7 additions & 0 deletions twitter/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ def __eq__(self, other):
def __ne__(self, other):
return not self.__eq__(other)

def __hash__(self):
if hasattr(self, 'id'):
return hash(self.id)
else:
raise TypeError('unhashable type: {} (no id attribute)'
.format(type(self)))

def AsJsonString(self):
""" Returns the TwitterModel as a JSON string based on key/value
pairs returned from the AsDict() method. """
Expand Down