def test_given_tweet_when_publish_tweet_then_client_called(self): with patch.object(Twitter, 'client'): twitter = Twitter() twitter.client = MagicMock() tweet = MagicMock() in_response_to = MagicMock() tweet_id = twitter.publish_tweet(tweet, in_response_to) self.assertEqual(twitter.client.update_status.return_value.id, tweet_id) twitter.client.update_status.assert_called_once_with( tweet, in_response_to)
def test_when_publish_tweets_then_split_and_publish(self): twitter = Twitter() tweet1_id = 123 tweet2_id = 456 previous_id = 789 tweet1 = "test1" tweet2 = "test2" twitter.publish_tweet = MagicMock(side_effect=[tweet1_id, tweet2_id]) tweets = [tweet1, tweet2] last_tweet_id = twitter.publish_tweets(tweets, previous_id) self.assertEqual(tweet2_id, last_tweet_id) twitter.publish_tweet.assert_has_calls( [call(tweet1, previous_id), call(tweet2, tweet1_id)])
def test_given_exception_downloading_file_when_publish_with_media_then_tweet_without_media_published( self, temp_file_mock): with patch.object(Twitter, 'client'): twitter = Twitter() twitter.client = MagicMock() twitter._download_file = MagicMock( side_effect=MediaNotAccessibleError()) twitter.publish_tweet = MagicMock() url = "http://example.com/file.jpg" text = "this is an example" in_response_to = MagicMock() tweet_id = twitter.publish_tweet_with_media( text, url, in_response_to) self.assertEqual(twitter.publish_tweet.return_value, tweet_id) temp_file_mock.assert_called_once_with(suffix=".png") with temp_file_mock.return_value as temp_file: twitter._download_file.assert_called_once_with(url, temp_file) twitter.client.update_with_media.assert_not_called() twitter.publish_tweet.assert_called_once_with( text, in_response_to)