test_api.py 16.5 KB
Newer Older
Mark Smith's avatar
Mark Smith committed
1
import unittest
2
import random
Mark Smith's avatar
Mark Smith committed
3
import shutil
4
import time
Josh Roesslein's avatar
Josh Roesslein committed
5
import os
6
from ast import literal_eval
7

Joshua Roesslein's avatar
Joshua Roesslein committed
8
9
from nose import SkipTest

10
11
from tweepy import Friendship, MemoryCache, FileCache, API
from tweepy.parsers import Parser
Mark Smith's avatar
Mark Smith committed
12
from .config import TweepyTestCase, username, use_replay, tape
Josh Roesslein's avatar
Josh Roesslein committed
13

14
test_tweet_id = '266367358078169089'
15
tweet_text = 'testing 1000'
16

17
18
"""Unit tests"""

Mark Smith's avatar
Mark Smith committed
19

20
21
22
23
24
25
26
27
28
29
30
31
class TweepyErrorTests(unittest.TestCase):

    def testpickle(self):
        """Verify exceptions can be pickled and unpickled."""
        import pickle
        from tweepy.error import TweepError

        e = TweepError('no reason', {'status': 200})
        e2 = pickle.loads(pickle.dumps(e))

        self.assertEqual(e.reason, e2.reason)
        self.assertEqual(e.response, e2.response)
Josh Roesslein's avatar
Josh Roesslein committed
32

Mark Smith's avatar
Mark Smith committed
33

34
class TweepyAPITests(TweepyTestCase):
Josh Roesslein's avatar
Josh Roesslein committed
35

36
37
38
39
40
41
42
43
44
45
46
    #@tape.use_cassette('testfailure.json')
    #def testapierror(self):
    #    from tweepy.error import TweepError
    #
    #    with self.assertRaises(TweepError) as cm:
    #        self.api.direct_messages()
    #
    #    reason, = literal_eval(cm.exception.reason)
    #    self.assertEqual(reason['message'], 'Bad Authentication data.')
    #    self.assertEqual(reason['code'], 215)
    #    self.assertEqual(cm.exception.api_code, 215)
47

Miriam Sexton's avatar
Miriam Sexton committed
48
    # TODO: Actually have some sort of better assertion
Mark Smith's avatar
Mark Smith committed
49
    @tape.use_cassette('testgetoembed.json')
Miriam Sexton's avatar
Miriam Sexton committed
50
    def testgetoembed(self):
Miriam Sexton's avatar
Miriam Sexton committed
51
        data = self.api.get_oembed(test_tweet_id)
Miriam Sexton's avatar
Miriam Sexton committed
52
53
        self.assertEqual(data['author_name'], "Twitter")

Mark Smith's avatar
Mark Smith committed
54
    @tape.use_cassette('testparserargumenthastobeaparserinstance.json')
55
56
57
    def testparserargumenthastobeaparserinstance(self):
        """ Testing the issue https://github.com/tweepy/tweepy/issues/421"""
        self.assertRaises(TypeError, API, self.auth, parser=Parser)
Miriam Sexton's avatar
Miriam Sexton committed
58

Mark Smith's avatar
Mark Smith committed
59
    @tape.use_cassette('testhometimeline.json')
60
61
62
    def testhometimeline(self):
        self.api.home_timeline()

Mark Smith's avatar
Mark Smith committed
63
    @tape.use_cassette('testusertimeline.json')
Josh Roesslein's avatar
Josh Roesslein committed
64
    def testusertimeline(self):
65
66
        self.api.user_timeline()
        self.api.user_timeline('twitter')
Josh Roesslein's avatar
Josh Roesslein committed
67

Mark Smith's avatar
Mark Smith committed
68
    @tape.use_cassette('testmentionstimeline.json')
Joshua Roesslein's avatar
Joshua Roesslein committed
69
70
    def testmentionstimeline(self):
        self.api.mentions_timeline()
71

Mark Smith's avatar
Mark Smith committed
72
    @tape.use_cassette('testretweetsofme.json')
73
74
75
76
    def testretweetsofme(self):
        self.api.retweets_of_me()

    def testretweet(self):
Joshua Roesslein's avatar
Joshua Roesslein committed
77
78
        # TODO(josh): Need a way to get random tweets to retweet.
        raise SkipTest()
79

Mark Smith's avatar
Mark Smith committed
80
    @tape.use_cassette('testretweets.json')
81
    def testretweets(self):
82
        self.api.retweets(test_tweet_id)
83

Mark Smith's avatar
Mark Smith committed
84
    @tape.use_cassette('testretweeters.json')
Joshua Roesslein's avatar
Joshua Roesslein committed
85
86
87
    def testretweeters(self):
        self.api.retweeters(test_tweet_id)

Mark Smith's avatar
Mark Smith committed
88
    @tape.use_cassette('testgetstatus.json')
Josh Roesslein's avatar
Josh Roesslein committed
89
    def testgetstatus(self):
90
        self.api.get_status(id=test_tweet_id)
Josh Roesslein's avatar
Josh Roesslein committed
91

Mark Smith's avatar
Mark Smith committed
92
    @tape.use_cassette('testupdateanddestroystatus.json')
Josh Roesslein's avatar
Josh Roesslein committed
93
94
    def testupdateanddestroystatus(self):
        # test update
95
96
        update = self.api.update_status(status=tweet_text)
        self.assertEqual(update.text, tweet_text)
Josh Roesslein's avatar
Josh Roesslein committed
97
98
99
100
101

        # test destroy
        deleted = self.api.destroy_status(id=update.id)
        self.assertEqual(deleted.id, update.id)

102
103
104
    @tape.use_cassette('testupdateanddestroystatus.json')
    def testupdateanddestroystatuswithoutkwarg(self):
        # test update, passing text as a positional argument (#554)
105
106
        update = self.api.update_status(tweet_text)
        self.assertEqual(update.text, tweet_text)
107
108
109
110
111

        # test destroy
        deleted = self.api.destroy_status(id=update.id)
        self.assertEqual(deleted.id, update.id)

Mark Smith's avatar
Mark Smith committed
112
    @tape.use_cassette('testupdatestatuswithmedia.yaml', serializer='yaml')
113
114
    def testupdatestatuswithmedia(self):
        update = self.api.update_with_media('examples/banner.png', status=tweet_text)
115
        self.assertIn(tweet_text + ' https://t.co', update.text)
116

Mark Smith's avatar
Mark Smith committed
117
    @tape.use_cassette('testgetuser.json')
Josh Roesslein's avatar
Josh Roesslein committed
118
    def testgetuser(self):
119
120
121
122
        u = self.api.get_user('twitter')
        self.assertEqual(u.screen_name, 'twitter')

        u = self.api.get_user(783214)
Josh Roesslein's avatar
Josh Roesslein committed
123
124
        self.assertEqual(u.screen_name, 'twitter')

Mark Smith's avatar
Mark Smith committed
125
    @tape.use_cassette('testlookupusers.json')
126
127
128
129
130
131
    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']))

Mark Smith's avatar
Mark Smith committed
132
    @tape.use_cassette('testsearchusers.json')
133
134
135
    def testsearchusers(self):
        self.api.search_users('twitter')

Mark Smith's avatar
Mark Smith committed
136
    @tape.use_cassette('testsuggestedcategories.json')
137
138
139
    def testsuggestedcategories(self):
        self.api.suggested_categories()

Mark Smith's avatar
Mark Smith committed
140
    @tape.use_cassette('testsuggestedusers.json')
141
142
143
144
145
    def testsuggestedusers(self):
        categories = self.api.suggested_categories()
        if len(categories) != 0:
            self.api.suggested_users(categories[0].slug)

Mark Smith's avatar
Mark Smith committed
146
    @tape.use_cassette('testsuggesteduserstweets.json')
147
148
149
150
    def testsuggesteduserstweets(self):
        categories = self.api.suggested_categories()
        if len(categories) != 0:
            self.api.suggested_users_tweets(categories[0].slug)
151

Mark Smith's avatar
Mark Smith committed
152
    @tape.use_cassette('testme.json')
Josh Roesslein's avatar
Josh Roesslein committed
153
154
155
156
    def testme(self):
        me = self.api.me()
        self.assertEqual(me.screen_name, username)

Mark Smith's avatar
Mark Smith committed
157
    @tape.use_cassette('testdirectmessages.json')
Josh Roesslein's avatar
Josh Roesslein committed
158
    def testdirectmessages(self):
159
160
        self.api.direct_messages()

Mark Smith's avatar
Mark Smith committed
161
    @tape.use_cassette('testsentdirectmessages.json')
162
163
    def testsentdirectmessages(self):
        self.api.sent_direct_messages()
Josh Roesslein's avatar
Josh Roesslein committed
164

Mark Smith's avatar
Mark Smith committed
165
    @tape.use_cassette('testsendanddestroydirectmessage.json')
Josh Roesslein's avatar
Josh Roesslein committed
166
167
    def testsendanddestroydirectmessage(self):
        # send
Joshua Roesslein's avatar
Joshua Roesslein committed
168
        sent_dm = self.api.send_direct_message(username, text='test message')
Josh Roesslein's avatar
Josh Roesslein committed
169
170
171
172
173
174
175
176
177
178
179
        self.assertEqual(sent_dm.text, 'test message')
        self.assertEqual(sent_dm.sender.screen_name, username)
        self.assertEqual(sent_dm.recipient.screen_name, username)

        # destroy
        destroyed_dm = self.api.destroy_direct_message(sent_dm.id)
        self.assertEqual(destroyed_dm.text, sent_dm.text)
        self.assertEqual(destroyed_dm.id, sent_dm.id)
        self.assertEqual(destroyed_dm.sender.screen_name, username)
        self.assertEqual(destroyed_dm.recipient.screen_name, username)

Mark Smith's avatar
Mark Smith committed
180
    @tape.use_cassette('testcreatedestroyfriendship.json')
181
    def testcreatedestroyfriendship(self):
Josh Roesslein's avatar
Josh Roesslein committed
182
183
184
        enemy = self.api.destroy_friendship('twitter')
        self.assertEqual(enemy.screen_name, 'twitter')

185
186
187
        friend = self.api.create_friendship('twitter')
        self.assertEqual(friend.screen_name, 'twitter')

Mark Smith's avatar
Mark Smith committed
188
    @tape.use_cassette('testshowfriendship.json')
Josh Roesslein's avatar
Josh Roesslein committed
189
    def testshowfriendship(self):
Joshua Roesslein's avatar
Joshua Roesslein committed
190
        source, target = self.api.show_friendship(target_screen_name='twitter')
Josh Roesslein's avatar
Josh Roesslein committed
191
192
193
        self.assert_(isinstance(source, Friendship))
        self.assert_(isinstance(target, Friendship))

Mark Smith's avatar
Mark Smith committed
194
    @tape.use_cassette('testfriendsids.json')
195
196
197
    def testfriendsids(self):
        self.api.friends_ids(username)

Mark Smith's avatar
Mark Smith committed
198
    @tape.use_cassette('testfollowersids.json')
199
200
201
    def testfollowersids(self):
        self.api.followers_ids(username)

Mark Smith's avatar
Mark Smith committed
202
    @tape.use_cassette('testfriends.json')
203
204
205
    def testfriends(self):
        self.api.friends(username)

Mark Smith's avatar
Mark Smith committed
206
    @tape.use_cassette('testfollowers.json')
207
208
209
    def testfollowers(self):
        self.api.followers(username)

Mark Smith's avatar
Mark Smith committed
210
    @tape.use_cassette('testverifycredentials.json')
211
    def testverifycredentials(self):
212
        self.assertNotEqual(self.api.verify_credentials(), False)
213

214
215
216
217
218
219
220
221
        # make sure that `me.status.entities` is not an empty dict
        me = self.api.verify_credentials(include_entities=True)
        self.assertTrue(me.status.entities)

        # `status` shouldn't be included
        me = self.api.verify_credentials(skip_status=True)
        self.assertFalse(hasattr(me, 'status'))

Mark Smith's avatar
Mark Smith committed
222
    @tape.use_cassette('testratelimitstatus.json')
223
224
225
    def testratelimitstatus(self):
        self.api.rate_limit_status()

226
    """ TODO(josh): Remove once this deprecated API is gone.
227
228
229
    def testsetdeliverydevice(self):
        self.api.set_delivery_device('im')
        self.api.set_delivery_device('none')
230
    """
231

Mark Smith's avatar
Mark Smith committed
232
    @tape.use_cassette('testupdateprofilecolors.json')
233
234
    def testupdateprofilecolors(self):
        original = self.api.me()
235
        updated = self.api.update_profile(profile_link_color='D0F900')
236
237

        # restore colors
238
239
        self.api.update_profile(
            profile_link_color=original.profile_link_color,
240
241
        )

Joshua Roesslein's avatar
Joshua Roesslein committed
242
243
        self.assertEqual(updated.profile_background_color, '000000')
        self.assertEqual(updated.profile_text_color, '000000')
244
        self.assertEqual(updated.profile_link_color, 'D0F900')
Joshua Roesslein's avatar
Joshua Roesslein committed
245
246
        self.assertEqual(updated.profile_sidebar_fill_color, '000000')
        self.assertEqual(updated.profile_sidebar_border_color, '000000')
247

248
    """
249
250
251
252
253
    def testupateprofileimage(self):
        self.api.update_profile_image('examples/profile.png')

    def testupdateprofilebg(self):
        self.api.update_profile_background_image('examples/bg.png')
254
    """
255

Mark Smith's avatar
Mark Smith committed
256
    @tape.use_cassette('testupdateprofilebannerimage.yaml', serializer='yaml')
257
258
259
    def testupdateprofilebannerimage(self):
        self.api.update_profile_banner('examples/banner.png')

Mark Smith's avatar
Mark Smith committed
260
    @tape.use_cassette('testupdateprofile.json')
261
262
263
264
265
266
267
268
269
    def testupdateprofile(self):
        original = self.api.me()
        profile = {
            'name': 'Tweepy test 123',
            'location': 'pytopia',
            'description': 'just testing things out'
        }
        updated = self.api.update_profile(**profile)
        self.api.update_profile(
270
            name = original.name, url = original.url,
271
272
273
274
            location = original.location, description = original.description
        )

        for k,v in profile.items():
Josh Roesslein's avatar
Josh Roesslein committed
275
            if k == 'email': continue
276
277
            self.assertEqual(getattr(updated, k), v)

Mark Smith's avatar
Mark Smith committed
278
    @tape.use_cassette('testfavorites.json')
279
280
281
    def testfavorites(self):
        self.api.favorites()

Mark Smith's avatar
Mark Smith committed
282
    @tape.use_cassette('testcreatedestroyfavorite.json')
283
    def testcreatedestroyfavorite(self):
Josh Roesslein's avatar
Josh Roesslein committed
284
285
        self.api.create_favorite(4901062372)
        self.api.destroy_favorite(4901062372)
286

Mark Smith's avatar
Mark Smith committed
287
    @tape.use_cassette('testcreatedestroyblock.json')
288
289
290
    def testcreatedestroyblock(self):
        self.api.create_block('twitter')
        self.api.destroy_block('twitter')
Josh Roesslein's avatar
Josh Roesslein committed
291
        self.api.create_friendship('twitter') # restore
292

Mark Smith's avatar
Mark Smith committed
293
    @tape.use_cassette('testblocks.json')
294
295
296
    def testblocks(self):
        self.api.blocks()

Mark Smith's avatar
Mark Smith committed
297
    @tape.use_cassette('testblocksids.json')
298
299
300
    def testblocksids(self):
        self.api.blocks_ids()

Joshua Roesslein's avatar
Joshua Roesslein committed
301
302
303
304
305
306
307
308
309
310
    # TODO: Rewrite test to be less brittle. It fails way too often.
    # def testcreateupdatedestroylist(self):
    #     params = {
    #         'owner_screen_name': username,
    #         'slug': 'tweeps'
    #     }
    #     l = self.api.create_list(name=params['slug'], **params)
    #     l = self.api.update_list(list_id=l.id, description='updated!')
    #     self.assertEqual(l.description, 'updated!')
    #     self.api.destroy_list(list_id=l.id)
Josh Roesslein's avatar
Josh Roesslein committed
311

Mark Smith's avatar
Mark Smith committed
312
    @tape.use_cassette('testlistsall.json')
Joshua Roesslein's avatar
Joshua Roesslein committed
313
314
    def testlistsall(self):
        self.api.lists_all()
Josh Roesslein's avatar
Josh Roesslein committed
315

Mark Smith's avatar
Mark Smith committed
316
    @tape.use_cassette('testlistsmemberships.json')
Josh Roesslein's avatar
Josh Roesslein committed
317
318
319
    def testlistsmemberships(self):
        self.api.lists_memberships()

Mark Smith's avatar
Mark Smith committed
320
    @tape.use_cassette('testlistssubscriptions.json')
Josh Roesslein's avatar
Josh Roesslein committed
321
322
323
    def testlistssubscriptions(self):
        self.api.lists_subscriptions()

Mark Smith's avatar
Mark Smith committed
324
    @tape.use_cassette('testlisttimeline.json')
Josh Roesslein's avatar
Josh Roesslein committed
325
    def testlisttimeline(self):
Joshua's avatar
Joshua committed
326
        self.api.list_timeline('applepie', 'stars')
Josh Roesslein's avatar
Josh Roesslein committed
327

Mark Smith's avatar
Mark Smith committed
328
    @tape.use_cassette('testgetlist.json')
Josh Roesslein's avatar
Josh Roesslein committed
329
    def testgetlist(self):
Joshua Roesslein's avatar
Joshua Roesslein committed
330
        self.api.get_list(owner_screen_name='applepie', slug='stars')
Josh Roesslein's avatar
Josh Roesslein committed
331

Mark Smith's avatar
Mark Smith committed
332
    @tape.use_cassette('testaddremovelistmember.json')
Josh Roesslein's avatar
Josh Roesslein committed
333
    def testaddremovelistmember(self):
334
335
336
337
338
339
340
341
342
343
344
        params = {
            'slug': 'test',
            'owner_screen_name': username,
            'screen_name': 'twitter'
        }

        def assert_list(l):
            self.assertEqual(l.name, params['slug'])

        assert_list(self.api.add_list_member(**params))
        assert_list(self.api.remove_list_member(**params))
Josh Roesslein's avatar
Josh Roesslein committed
345

Mark Smith's avatar
Mark Smith committed
346
    @tape.use_cassette('testaddremovelistmembers.json')
347
348
349
350
    def testaddremovelistmembers(self):
        params = {
            'slug': 'test',
            'owner_screen_name': username,
kk6's avatar
kk6 committed
351
            'screen_name': ['twitterapi', 'twittermobile']
352
353
354
355
356
357
358
359
        }

        def assert_list(l):
            self.assertEqual(l.name, params['slug'])

        assert_list(self.api.add_list_members(**params))
        assert_list(self.api.remove_list_members(**params))

Mark Smith's avatar
Mark Smith committed
360
    @tape.use_cassette('testlistmembers.json')
Josh Roesslein's avatar
Josh Roesslein committed
361
    def testlistmembers(self):
Joshua's avatar
Joshua committed
362
        self.api.list_members('applepie', 'stars')
Josh Roesslein's avatar
Josh Roesslein committed
363

Mark Smith's avatar
Mark Smith committed
364
    @tape.use_cassette('testshowlistmember.json')
Joshua Roesslein's avatar
Joshua Roesslein committed
365
366
    def testshowlistmember(self):
        self.assertTrue(self.api.show_list_member(owner_screen_name='applepie', slug='stars', screen_name='NathanFillion'))
Josh Roesslein's avatar
Josh Roesslein committed
367

Mark Smith's avatar
Mark Smith committed
368
    @tape.use_cassette('testsubscribeunsubscribelist.json')
Josh Roesslein's avatar
Josh Roesslein committed
369
    def testsubscribeunsubscribelist(self):
Joshua Roesslein's avatar
Joshua Roesslein committed
370
371
372
373
374
375
        params = {
            'owner_screen_name': 'applepie',
            'slug': 'stars'
        }
        self.api.subscribe_list(**params)
        self.api.unsubscribe_list(**params)
Josh Roesslein's avatar
Josh Roesslein committed
376

Mark Smith's avatar
Mark Smith committed
377
    @tape.use_cassette('testlistsubscribers.json')
Josh Roesslein's avatar
Josh Roesslein committed
378
    def testlistsubscribers(self):
Joshua's avatar
Joshua committed
379
        self.api.list_subscribers('applepie', 'stars')
Josh Roesslein's avatar
Josh Roesslein committed
380

Mark Smith's avatar
Mark Smith committed
381
    @tape.use_cassette('testshowlistsubscriber.json')
Joshua Roesslein's avatar
Joshua Roesslein committed
382
    def testshowlistsubscriber(self):
Joshua Roesslein's avatar
Joshua Roesslein committed
383
        self.assertTrue(self.api.show_list_subscriber('tweepytest', 'test', 'applepie'))
Josh Roesslein's avatar
Josh Roesslein committed
384

Mark Smith's avatar
Mark Smith committed
385
    @tape.use_cassette('testsavedsearches.json')
386
387
388
    def testsavedsearches(self):
        s = self.api.create_saved_search('test')
        self.api.saved_searches()
Josh Roesslein's avatar
Josh Roesslein committed
389
        self.assertEqual(self.api.get_saved_search(s.id).query, 'test')
390
391
        self.api.destroy_saved_search(s.id)

Mark Smith's avatar
Mark Smith committed
392
    @tape.use_cassette('testsearch.json')
393
    def testsearch(self):
Josh Roesslein's avatar
Josh Roesslein committed
394
        self.api.search('tweepy')
395

Mark Smith's avatar
Mark Smith committed
396
    @tape.use_cassette('testgeoapis.json')
397
    def testgeoapis(self):
398
399
400
401
        def place_name_in_list(place_name, place_list):
            """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])

402
403
        twitter_hq = self.api.geo_similar_places(lat='37.7821120598956',
                                                 long='-122.400612831116',
Mark Smith's avatar
Mark Smith committed
404
                                                 name='South of Market Child Care')
405
        # Assumes that twitter_hq is first Place returned...
406
        self.assertEqual(twitter_hq[0].id, '1d019624e6b4dcff')
407
        # Test various API functions using Austin, TX, USA
Mark Smith's avatar
Mark Smith committed
408
        self.assertEqual(self.api.geo_id(id='1ffd3558f2e98349').full_name, 'Dogpatch, San Francisco')
409
410
        self.assertTrue(place_name_in_list('Austin, TX',
            self.api.reverse_geocode(lat=30.267370168467806, long= -97.74261474609375))) # Austin, TX, USA
411

Mark Smith's avatar
Mark Smith committed
412
    @tape.use_cassette('testsupportedlanguages.json')
413
414
415
416
417
418
419
420
421
    def testsupportedlanguages(self):
        languages = self.api.supported_languages()
        expected_dict = {
            "name": "English",
            "code": "en",
            "status": "production"
        }
        self.assertTrue(expected_dict in languages)

Mark Smith's avatar
Mark Smith committed
422
    @tape.use_cassette('testcachedresult.json')
Joshua Roesslein's avatar
Joshua Roesslein committed
423
424
425
426
427
428
429
    def testcachedresult(self):
        self.api.cache = MemoryCache()
        self.api.home_timeline()
        self.assertFalse(self.api.cached_result)
        self.api.home_timeline()
        self.assertTrue(self.api.cached_result)

430

Mark Smith's avatar
Mark Smith committed
431
class TweepyCacheTests(unittest.TestCase):
432
    timeout = 0.5
Josh Roesslein's avatar
Josh Roesslein committed
433
434
435
436
437
438
439
440
441
    memcache_servers = ['127.0.0.1:11211']  # must be running for test to pass

    def _run_tests(self, do_cleanup=True):
        # test store and get
        self.cache.store('testkey', 'testvalue')
        self.assertEqual(self.cache.get('testkey'), 'testvalue',
            'Stored value does not match retrieved value')

        # test timeout
442
        time.sleep(self.timeout)
Josh Roesslein's avatar
Josh Roesslein committed
443
444
445
446
447
448
        self.assertEqual(self.cache.get('testkey'), None,
            'Cache entry should have expired')

        # test cleanup
        if do_cleanup:
            self.cache.store('testkey', 'testvalue')
449
            time.sleep(self.timeout)
Josh Roesslein's avatar
Josh Roesslein committed
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
            self.cache.cleanup()
            self.assertEqual(self.cache.count(), 0, 'Cache cleanup failed')

        # test count
        for i in range(0, 20):
            self.cache.store('testkey%i' % i, 'testvalue')
        self.assertEqual(self.cache.count(), 20, 'Count is wrong')

        # test flush
        self.cache.flush()
        self.assertEqual(self.cache.count(), 0, 'Cache failed to flush')

    def testmemorycache(self):
        self.cache = MemoryCache(timeout=self.timeout)
        self._run_tests()

    def testfilecache(self):
        os.mkdir('cache_test_dir')
Mark Smith's avatar
Mark Smith committed
468
469
470
471
472
473
474
475
        try:
            self.cache = FileCache('cache_test_dir', self.timeout)
            self._run_tests()
            self.cache.flush()
        finally:
            if os.path.exists('cache_test_dir'):
                shutil.rmtree('cache_test_dir')

476
if __name__ == '__main__':
Josh Roesslein's avatar
Josh Roesslein committed
477
    unittest.main()