def test_build_with_default_content_when_no_lang_given( self, mocked_build_with_default_content, mocked_build_from_lang, mocked_warning): diff = get_mocked_diff() type(diff.new).title = PropertyMock(return_value="Test") type(diff).url = PropertyMock(return_value="https://this.is/a-test") build_text(diff) mocked_warning.assert_not_called() mocked_build_with_default_content.assert_called_once() mocked_build_from_lang.assert_not_called()
def test_build_with_default_content_when_lang_is_incomplete( self, mocked_build_with_default_content, mocked_build_from_lang, mocked_warning): diff = get_mocked_diff() type(diff.new).title = PropertyMock(return_value="Test") type(diff).url = PropertyMock(return_value="https://this.is/a-test") lang = { "change_in": "change in", "the_url": "the URL", "the_title": "the title", } build_text(diff, lang) mocked_warning.assert_called_once() mocked_build_with_default_content.assert_called_once() mocked_build_from_lang.assert_not_called()
def test_default_content_text(self, mocked_build_from_lang): diff = get_mocked_diff() type(diff.new).title = "Test" type(diff).url = "https://this.is/a-test" text = build_text(diff) mocked_build_from_lang.assert_not_called() self.assertEqual(text, "%s %s" % (diff.new.title, diff.url)) lang = { "change_in": "change in", "the_url": "the URL", "the_title": "the title", } text = build_text(diff, lang) mocked_build_from_lang.assert_not_called() self.assertEqual(text, "%s %s" % (diff.new.title, diff.url))
def tweet_diff(self, diff, token=None, lang={}): if not token: raise TokenNotFoundError() elif diff.tweeted: raise AlreadyTweetedError(diff) elif not (diff.old.archive_url and diff.new.archive_url): raise TwitterAchiveUrlNotFoundError(diff) twitter = self.api(token) text = build_text(diff, lang) # Check if the thread exists thread_status_id_str = None if diff.old.entry.tweet_status_id_str == "": try: thread_status_id_str = self.create_thread( diff.old.entry, diff.old, token ) logging.info( "created thread https://twitter.com/%s/status/%s" % (self.auth.get_username(), thread_status_id_str) ) except UpdateStatusError as e: logging.error(str(e)) else: thread_status_id_str = diff.old.tweet_status_id_str try: status = twitter.update_with_media( diff.thumbnail_path, status=text, in_reply_to_status_id=thread_status_id_str, ) logging.info( "tweeted diff https://twitter.com/%s/status/%s" % (self.auth.get_username(), status.id_str) ) # Save the tweet status id inside the new version diff.new.tweet_status_id_str = status.id_str diff.new.save() # And save that the diff has been tweeted diff.tweeted = datetime.utcnow() diff.save() except Exception as e: logging.error("unable to tweet: %s", e)
def test_lang_content_text(self): diff = get_mocked_diff() lang = { "change_in": "change in", "the_url": "the URL", "the_title": "the title", "and": "and", "the_summary": "the summary", } type(diff).url_changed = True type(diff).title_changed = False type(diff).summary_changed = False type(diff).url = "https://this.is/a-test" text = build_text(diff, lang) self.assertEqual(text, "change in the URL\n%s" % diff.url) type(diff).url_changed = False type(diff).title_changed = True type(diff).summary_changed = False text = build_text(diff, lang) self.assertEqual(text, "change in the title\n%s" % diff.url) type(diff).url_changed = False type(diff).title_changed = False type(diff).summary_changed = True text = build_text(diff, lang) self.assertEqual(text, "change in the summary\n%s" % diff.url) type(diff).url_changed = True type(diff).title_changed = True type(diff).summary_changed = False text = build_text(diff, lang) self.assertEqual(text, "change in the URL and the title\n%s" % diff.url) type(diff).url_changed = True type(diff).title_changed = False type(diff).summary_changed = True text = build_text(diff, lang) self.assertEqual(text, "change in the URL and the summary\n%s" % diff.url) type(diff).url_changed = False type(diff).title_changed = True type(diff).summary_changed = True text = build_text(diff, lang) self.assertEqual(text, "change in the title and the summary\n%s" % diff.url) type(diff).url_changed = True type(diff).title_changed = True type(diff).summary_changed = True text = build_text(diff, lang) self.assertEqual( text, "change in the URL, the title and the summary\n%s" % diff.url)