Attempt to improve the error message if parser argument is not well set.
authorJordi Riera <kender.jr@gmail.com>
Sat, 3 May 2014 12:53:29 +0000 (14:53 +0200)
committerJordi Riera <kender.jr@gmail.com>
Sat, 3 May 2014 12:53:29 +0000 (14:53 +0200)
tests/test_api.py
tweepy/api.py

index 8e3660e95458c4779c169fc53602337692e9852b..adcbbc2883a50ee6bacd7cf395c00ca83eb3dbfd 100644 (file)
@@ -5,7 +5,8 @@ import os
 
 from nose import SkipTest
 
-from tweepy import Friendship, MemoryCache, FileCache
+from tweepy import Friendship, MemoryCache, FileCache, API
+from tweepy.parsers import Parser
 from config import TweepyTestCase, username, use_replay
 
 test_tweet_id = '266367358078169089'
@@ -13,8 +14,8 @@ tweet_text = 'testing 1000'
 
 """Unit tests"""
 
-class TweepyErrorTests(unittest.TestCase):
 
+class TweepyErrorTests(unittest.TestCase):
     def testpickle(self):
         """Verify exceptions can be pickled and unpickled."""
         import pickle
@@ -26,13 +27,16 @@ class TweepyErrorTests(unittest.TestCase):
         self.assertEqual(e.reason, e2.reason)
         self.assertEqual(e.response, e2.response)
 
-class TweepyAPITests(TweepyTestCase):
 
+class TweepyAPITests(TweepyTestCase):
     # TODO: Actually have some sort of better assertion
     def testgetoembed(self):
         data = self.api.get_oembed(test_tweet_id)
         self.assertEqual(data['author_name'], "Twitter")
 
+    def testparserargumenthastobeaparserinstance(self):
+        """ Testing the issue https://github.com/tweepy/tweepy/issues/421"""
+        self.assertRaises(TypeError, API, self.auth, parser=Parser)
 
     def testhometimeline(self):
         self.api.home_timeline()
@@ -80,6 +84,7 @@ class TweepyAPITests(TweepyTestCase):
     def testlookupusers(self):
         def check(users):
             self.assertEqual(len(users), 2)
+
         check(self.api.lookup_users(user_ids=[6844292, 6253282]))
         check(self.api.lookup_users(screen_names=['twitterapi', 'twitter']))
 
@@ -210,11 +215,11 @@ class TweepyAPITests(TweepyTestCase):
         }
         updated = self.api.update_profile(**profile)
         self.api.update_profile(
-            name = original.name, url = original.url,
-            location = original.location, description = original.description
+            name=original.name, url=original.url,
+            location=original.location, description=original.description
         )
 
-        for k,v in profile.items():
+        for k, v in profile.items():
             if k == 'email': continue
             self.assertEqual(getattr(updated, k), v)
 
@@ -228,7 +233,7 @@ class TweepyAPITests(TweepyTestCase):
     def testcreatedestroyblock(self):
         self.api.create_block('twitter')
         self.api.destroy_block('twitter')
-        self.api.create_friendship('twitter') # restore
+        self.api.create_friendship('twitter')  # restore
 
     def testblocks(self):
         self.api.blocks()
@@ -279,7 +284,8 @@ class TweepyAPITests(TweepyTestCase):
         self.api.list_members('applepie', 'stars')
 
     def testshowlistmember(self):
-        self.assertTrue(self.api.show_list_member(owner_screen_name='applepie', slug='stars', screen_name='NathanFillion'))
+        self.assertTrue(
+            self.api.show_list_member(owner_screen_name='applepie', slug='stars', screen_name='NathanFillion'))
 
     def testsubscribeunsubscribelist(self):
         params = {
@@ -309,16 +315,17 @@ class TweepyAPITests(TweepyTestCase):
             """Return True if a given place_name is in place_list."""
             return any([x.full_name.lower() == place_name.lower() for x in place_list])
 
-        twitter_hq = self.api.geo_similar_places(lat=37, long= -122, name='Twitter HQ')
+        twitter_hq = self.api.geo_similar_places(lat=37, long=-122, name='Twitter HQ')
         # Assumes that twitter_hq is first Place returned...
         self.assertEqual(twitter_hq[0].id, '3bdf30ed8b201f31')
         # Test various API functions using Austin, TX, USA
         self.assertEqual(self.api.geo_id(id='c3f37afa9efcf94b').full_name, 'Austin, TX')
         self.assertTrue(place_name_in_list('Austin, TX',
-            self.api.reverse_geocode(lat=30.267370168467806, long= -97.74261474609375))) # Austin, TX, USA
+                                           self.api.reverse_geocode(lat=30.267370168467806,
+                                                                    long=-97.74261474609375)))  # Austin, TX, USA
 
-class TweepyCacheTests(unittest.TestCase):
 
+class TweepyCacheTests(unittest.TestCase):
     timeout = 2.0
     memcache_servers = ['127.0.0.1:11211']  # must be running for test to pass
 
@@ -326,12 +333,12 @@ class TweepyCacheTests(unittest.TestCase):
         # test store and get
         self.cache.store('testkey', 'testvalue')
         self.assertEqual(self.cache.get('testkey'), 'testvalue',
-            'Stored value does not match retrieved value')
+                         'Stored value does not match retrieved value')
 
         # test timeout
         sleep(self.timeout)
         self.assertEqual(self.cache.get('testkey'), None,
-            'Cache entry should have expired')
+                         'Cache entry should have expired')
 
         # test cleanup
         if do_cleanup:
@@ -360,5 +367,6 @@ class TweepyCacheTests(unittest.TestCase):
         self.cache.flush()
         os.rmdir('cache_test_dir')
 
+
 if __name__ == '__main__':
     unittest.main()
index 22f08196e17628784205c65cad8d7d1678bf6430..0ab04d3960fd8706b4f5dc62b7fc846e7786f996 100644 (file)
@@ -7,7 +7,7 @@ import mimetypes
 
 from tweepy.binder import bind_api
 from tweepy.error import TweepError
-from tweepy.parsers import ModelParser
+from tweepy.parsers import ModelParser, Parser
 from tweepy.utils import list_to_csv
 
 
@@ -32,6 +32,16 @@ class API(object):
         self.retry_errors = retry_errors
         self.timeout = timeout
         self.parser = parser or ModelParser()
+        # Attempt to explain more clearly the parser argument requirements
+        # https://github.com/tweepy/tweepy/issues/421
+        #
+        parser_type = Parser
+        if not isinstance(self.parser, parser_type):
+            raise TypeError(
+                '"parser" argument has to be an instance of "{}". It is currently a {}.'.format(
+                    parser_type.__name__, type(self.parser)
+                )
+            )
 
     """ statuses/home_timeline """
     home_timeline = bind_api(