forked from pybites/100DaysOfCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_whotweeted.py
More file actions
58 lines (46 loc) · 1.94 KB
/
test_whotweeted.py
File metadata and controls
58 lines (46 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import unittest
from unittest.mock import patch
import tweepy
from whotweeted import get_country_code, who_is_output
from whotweeted import load_cache
DATA = dict(AU='875639674244444160',
ES='875669971954806784',
nopb='846302762736504833',
noloc='844092059988508673',
badid='8756396742444441da'
)
get_tweet = lambda x: load_cache(DATA.get(x)) # noqa E731
class WhoTweetedTestCase(unittest.TestCase):
@patch.object(tweepy.API, 'get_status', return_value=get_tweet('AU'))
def test_julian(self, mock_method):
tweetid = DATA.get('AU')
country = get_country_code(tweetid)
who_is_out = who_is_output(country)
self.assertEqual(country, 'AU')
self.assertIn('Julian', who_is_out)
@patch.object(tweepy.API, 'get_status', return_value=get_tweet('ES'))
def test_bob(self, mock_method):
tweetid = DATA.get('ES')
country = get_country_code(tweetid)
who_is_out = who_is_output(country)
self.assertEqual(country, 'ES')
self.assertIn('Bob', who_is_out)
@patch.object(tweepy.API, 'get_status', return_value=get_tweet('nopb'))
def test_no_pybites_account(self, mock_method):
tweetid = DATA.get('nopb')
with self.assertRaises(ValueError):
get_country_code(tweetid)
@patch.object(tweepy.API, 'get_status', return_value=get_tweet('noloc'))
def test_no_location_in_tweet(self, mock_method):
tweetid = DATA.get('noloc')
with self.assertRaises(AttributeError):
get_country_code(tweetid)
# not really a return value, it crashes before decorator can cash tweet
@patch.object(tweepy.API, 'get_status', return_value=get_tweet('nopb'))
def test_bad_tweet_id(self, mock_method):
tweetid = DATA.get('badid')
print(tweetid)
with self.assertRaises(ValueError):
get_country_code(tweetid)
if __name__ == '__main__':
unittest.main()