Commit 0c3441b3 authored by Josh Roesslein's avatar Josh Roesslein
Browse files

Merge branch 'master' into streaming

parents e2523089 1f9f35e2
......@@ -20,79 +20,64 @@ class TweepyAPITests(unittest.TestCase):
self.api = API(BasicAuthHandler(self.username, self.password), self.username)
def testpublictimeline(self):
s = self.api.public_timeline()
self.assert_(len(s) == 20)
self.assert_(isinstance(s[0],Status))
self.assertEqual(len(self.api.public_timeline()), 20)
def testfriendstimeline(self):
s = self.api.friends_timeline(count=1)
self.assert_(len(s) == 1)
self.assert_(isinstance(s[0],Status))
self.assert_(len(self.api.friends_timeline()) > 0)
def testusertimeline(self):
s = self.api.user_timeline(screen_name='twitter')
self.assert_(len(s) > 0)
self.assert_(isinstance(s[0],Status))
self.assertEqual(s[0].user.screen_name, 'twitter')
def testmentions(self):
s = self.api.mentions()
self.assert_(len(s) > 0)
self.assert_(isinstance(s[0],Status))
self.assert_(s[0].text.find(self.username) >= 0)
def testgetstatus(self):
s = self.api.get_status(id=123)
self.assert_(isinstance(s,Status))
self.assertEqual(s.user.id, 17)
def testupdateanddestroystatus(self):
# test update
text = 'testing %i' % random.randint(0,1000)
update = self.api.update_status(status=text)
self.assert_(isinstance(update,Status))
self.assertEqual(update.text, text)
# test destroy
deleted = self.api.destroy_status(id=update.id)
self.assert_(isinstance(deleted,Status))
self.assertEqual(deleted.id, update.id)
def testgetuser(self):
u = self.api.get_user(screen_name='twitter')
self.assert_(isinstance(u,User))
self.assertEqual(u.screen_name, 'twitter')
def testme(self):
me = self.api.me()
self.assert_(isinstance(me,User))
self.assertEqual(me.screen_name, self.username)
def testfriends(self):
friends = self.api.friends()
self.assert_(len(friends) > 0)
self.assert_(isinstance(friends[0], User))
def testfollowers(self):
followers = self.api.followers()
self.assert_(len(followers) > 0)
self.assert_(isinstance(followers[0], User))
def testdirectmessages(self):
dms = self.api.direct_messages()
self.assert_(len(dms) > 0)
self.assert_(isinstance(dms[0], DirectMessage))
def testsendanddestroydirectmessage(self):
# send
sent_dm = self.api.send_direct_message(self.username, 'test message')
self.assert_(isinstance(sent_dm, DirectMessage))
self.assertEqual(sent_dm.text, 'test message')
self.assertEqual(sent_dm.sender.screen_name, self.username)
self.assertEqual(sent_dm.recipient.screen_name, self.username)
# destroy
destroyed_dm = self.api.destroy_direct_message(sent_dm.id)
self.assert_(isinstance(destroyed_dm, DirectMessage))
self.assertEqual(destroyed_dm.text, sent_dm.text)
self.assertEqual(destroyed_dm.id, sent_dm.id)
self.assertEqual(destroyed_dm.sender.screen_name, self.username)
......@@ -100,13 +85,11 @@ class TweepyAPITests(unittest.TestCase):
def testcreatefriendship(self):
friend = self.api.create_friendship('twitter')
self.assert_(isinstance(friend, User))
self.assertEqual(friend.screen_name, 'twitter')
self.assertTrue(self.api.exists_friendship(self.username, 'twitter'))
def testdestroyfriendship(self):
enemy = self.api.destroy_friendship('twitter')
self.assert_(isinstance(enemy, User))
self.assertEqual(enemy.screen_name, 'twitter')
self.assertFalse(self.api.exists_friendship(self.username, 'twitter'))
......
......@@ -11,7 +11,8 @@ from error import TweepError
class API(object):
def __init__(self, auth_handler=None, username=None, host='twitter.com', cache=None,
secure=False, api_root='', classes={'user': User, 'status': Status,
secure=False, api_root='', validate=True,
classes={'user': User, 'status': Status,
'direct_message': DirectMessage, 'friendship': Friendship,
'saved_search': SavedSearch, 'search_result': SearchResult}):
self.auth_handler = auth_handler
......@@ -20,6 +21,7 @@ class API(object):
self.api_root = api_root
self.cache = cache
self.secure = secure
self.validate = validate
self.classes = classes
"""Get public timeline"""
......
......@@ -79,13 +79,24 @@ def bind_api(path, parser, allowed_param=None, method='GET', require_auth=False,
# Pass returned body into parser and return parser output
out = parser(resp.read(), api)
conn.close()
# validate result
if api.validate:
# list of results
if isinstance(out, list) and len(out) > 0:
if hasattr(out[0], 'validate'):
for result in out:
result.validate()
# single result
else:
if hasattr(out, 'validate'):
out.validate()
# store result in cache
if api.cache and method == 'GET':
api.cache.store(url, out)
# close connection and return data
conn.close()
return out
return _call
......@@ -2,6 +2,8 @@
# Copyright 2009 Joshua Roesslein
# See LICENSE
from error import TweepError
class Model(object):
def __getstate__(self):
......@@ -12,13 +14,48 @@ class Model(object):
pickle[k] = v
return pickle
@staticmethod
def _validate(model, attributes):
missing = []
for attr in attributes:
if not hasattr(model, attr):
missing.append(attr)
if len(missing) > 0:
raise TweepError('Missing required attribute(s) %s' % str(missing).strip('[]'))
class Status(Model):
@staticmethod
def _validate(status):
Model._validate(status, [
'created_at', 'id', 'text', 'source', 'truncated', 'in_reply_to_status_id',
'in_reply_to_user_id', 'favorited', 'in_reply_to_screen_name'
])
if hasattr(status, 'user'):
User._validate(status.user)
def validate(self):
Status._validate(self)
def destroy(self):
return self._api.destroy_status(id=self.id)
class User(Model):
@staticmethod
def _validate(user):
Model._validate(user, [
'id', 'name', 'screen_name', 'location', 'description', 'profile_image_url',
'url', 'protected', 'followers_count', 'profile_background_color',
'profile_text_color', 'profile_sidebar_fill_color', 'profile_sidebar_border_color',
'friends_count', 'created_at', 'favourites_count', 'utc_offset', 'time_zone',
'profile_background_image_url', 'statuses_count', 'notifications', 'following',
'verified'
])
if hasattr(user, 'status'):
Status._validate(user.status)
def validate(self):
User._validate(self)
def timeline(self, **kargs):
return self._api.user_timeline(**kargs)
def mentions(self, **kargs):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment