Skip to content

Commit

Permalink
081
Browse files Browse the repository at this point in the history
  • Loading branch information
bbelderbos committed Jun 16, 2017
1 parent 460e05c commit e8fa789
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 1 deletion.
Binary file added 081/data/844092059988508673
Binary file not shown.
Binary file added 081/data/846302762736504833
Binary file not shown.
Binary file added 081/data/875382808897835013
Binary file not shown.
Binary file added 081/data/875594400872538112
Binary file not shown.
Binary file added 081/data/875632151990947840
Binary file not shown.
Binary file added 081/data/875639674244444160
Binary file not shown.
Binary file added 081/data/875669971954806784
Binary file not shown.
58 changes: 58 additions & 0 deletions 081/test_whotweeted.py
@@ -0,0 +1,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()
1 change: 1 addition & 0 deletions 081/whotweeted.py
2 changes: 1 addition & 1 deletion LOG.md
Expand Up @@ -82,7 +82,7 @@
| 078 | Jun 15, 2017 | [Use a context manager to rollback a transaction](078) | An example for the use of the with statement = resource management. |
| 079 | Jun 16, 2017 | [#Python script to capture exceptions when creating an #sqlite db](079) | A simple but useful script I use to create an sqlite db and check to see if it exists on subsequent runs. The use of `exception` allows me to capture any other errors too. |
| 080 | Jun 17, 2017 | ["Is this Bob or Julian?" - script to reveal who of @pybites tweets](080) | Fun little exercise that started with [Anthony Shaw asking who he was talking to](https://twitter.com/anthonypjshaw/status/875275923930480641) - it actually became more involved turning/testing out location on our tweets and adding exception handling ... nice practice! (and a funny new service for our PyBites community) |
| 081 | Jun 18, 2017 | [TITLE](081) | LEARNING |
| 081 | Jun 18, 2017 | [Using unittest mock patch to test Tweepy code without calling the API](081) | I took my code of day 80 and wrote tests for it. But as it involves an API we don't want to call it and depend on the network. So you can use the mock patch decorator. I did have to save some test data (cache decorator in main script), but once that was done, it was pretty easy. [Here](https://github.com/bbelderbos/quotes_on_design) is another Twitter API mock I did some time ago |
| 082 | Jun 19, 2017 | [TITLE](082) | LEARNING |
| 083 | Jun 20, 2017 | [TITLE](083) | LEARNING |
| 084 | Jun 21, 2017 | [TITLE](084) | LEARNING |
Expand Down

0 comments on commit e8fa789

Please sign in to comment.