def test_when_send_dm_error_then_send_dm_called(self): with patch.object(Twitter, 'client'): twitter = Twitter() twitter.client = MagicMock() dm = "example" twitter.send_dm(dm) twitter.client.get_user.assert_called_once_with("aitormagan") twitter.client.send_direct_message( twitter.client.get_user.return_value.id, dm)
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_given_url_and_text_when_publish_with_media_then_file_downloaded_and_tweet_published( self, temp_file_mock): with patch.object(Twitter, 'client'): twitter = Twitter() twitter.client = MagicMock() twitter._download_file = 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.client.update_with_media.return_value.id, 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_called_once_with( temp_file.name, text, in_reply_to_status_id=in_response_to)
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)