def test_fetch_tweets_returns_after_cannot_acquire_lock( self, mock_twitter_init): twitter_account = TwitterAccount(name="Strassengezwitscher") twitter_account.fetch_tweets() # ToDo: This is not optimal, but I don't know of any better way to ensure that the body of fetch_tweets was not # executed self.assertFalse(mock_twitter_init.called)
def test_fetch_tweets_unlocks_if_get_utc_offset_throws_key_error( self, logger, unlock_mock): twitter_account = TwitterAccount(name="Strassengezwitscher") twitter_account.fetch_tweets() logger.assert_called_once_with( "Got unexpected result while fetching tweets.") self.assertEqual(unlock_mock.call_count, 1)
def test_fetch_tweets_unlocks_if_get_utc_offset_throws_unknown_error( self, logger, unlock_mock): twitter_account = TwitterAccount(name="Strassengezwitscher") twitter_account.fetch_tweets() logger.assert_called_once_with( "Got unexpected exception while fetching tweets: <class 'Exception'> - wow, much error, such bad" ) self.assertEqual(unlock_mock.call_count, 1)
def test_add_account_with_same_name_but_different_capitalization(self): TwitterAccount.objects.create(name="Strassengezwitscher", account_id=1337) twitter_account_new = TwitterAccount(name="STRASSENGEZWITSCHER") self.assertRaisesMessage( ValidationError, 'Twitter account with this name already exists.', twitter_account_new.clean)
def test_fetch_tweets_tweet_already_in_db(self): twitter_account = TwitterAccount(name="Strassengezwitscher", account_id=1337) twitter_account.last_known_tweet_id = 10 twitter_account.save() twitter_account.fetch_tweets() self.assertEqual(twitter_account.tweet_set.count(), 0)
def test_string_representation(self): twitter_account = TwitterAccount(name='Peter') tweet = Tweet( content='Erster Tweet, #cool!', created_at=datetime(2012, 12, 21, 19, 9, 0), account=twitter_account, ) self.assertEqual( str(tweet), 'Peter at 2012-12-21 19:09:00 - Erster Tweet, #cool!')
def do_send_tweet(tweet): """Called by send_tweets_task to make the API call into Twitter. """ params = {"status": tweet.message} url = "http://api.twitter.com/1/statuses/update.json" method = "POST" twitter_account = TwitterAccount.get_twitter_account() if twitter_account is None: logging.error(("No twitter account object was created." "Can't send tweets!")) return False consumer = oauth.Consumer(key=twitter_account.consumer_key, secret=twitter_account.consumer_secret) token = oauth.Token(key=twitter_account.access_token, secret=twitter_account.access_secret) auth_params = { "oauth_version": "1.0", "oauth_nonce": oauth.generate_nonce(), "oauth_timestamp": int(time.time()), "oauth_signature_method": "HMAC-SHA1", "oauth_token": token.key, "oauth_consumer_key": consumer.key, } client = oauth.Client(consumer, token) req = oauth.Request(method=method, url=url, parameters=params) resp, content = client.request( url, method=method, body=urllib.urlencode(params, True).replace("+", "%20"), headers=req.to_header(), force_auth_header=True, ) if resp["status"][0] == "5": return False return True
def test_clean_adds_account_id(self): twitter_account = TwitterAccount(name="Strassengezwitscher") twitter_account.clean() self.assertEqual(twitter_account.account_id, 1337)
def test_clean_removes_leading_at(self): twitter_account = TwitterAccount(name="@Strassengezwitscher") twitter_account.clean() self.assertEqual(twitter_account.name, "Strassengezwitscher")
def test_clean_raises_validation_error_on_user_not_found(self, _): twitter_account = TwitterAccount(name="Strassengezwitscher") self.assertRaisesMessage(ValidationError, 'Could not find user with provided name.', twitter_account.clean)
def test_string_representation(self): twitter_account = TwitterAccount(name="Strassengezwitscher") self.assertEqual(str(twitter_account), 'Strassengezwitscher')
def test_get_utc_offset_to_return_correctly(self): twitter_account = TwitterAccount(name="Strassengezwitscher") self.assertEqual(twitter_account._get_utc_offset(None), 3600)
def test_get_utc_offset_to_return_nothing_on_no_tweets(self): twitter_account = TwitterAccount(name="Strassengezwitscher") self.assertEqual(twitter_account._get_utc_offset(None), None)
def test_fetch_tweets_unlocks_if_get_utc_offset_throws_connection_error( self, logger, unlock_mock): twitter_account = TwitterAccount(name="Strassengezwitscher") twitter_account.fetch_tweets() logger.assert_called_once_with("Could not connect to Twitter.") self.assertEqual(unlock_mock.call_count, 1)
def test_fetch_tweets_unlocks_if_success(self, unlock_mock): twitter_account = TwitterAccount(name="Strassengezwitscher") twitter_account.fetch_tweets() self.assertEqual(unlock_mock.call_count, 1)
def test_representation(self): twitter_account = TwitterAccount(name="Strassengezwitscher") self.assertEqual(repr(twitter_account), '<TwitterAccount Strassengezwitscher>')
def test_fetch_tweets_from_api_max_since_id_set(self, request_mock): twitter_account = TwitterAccount(name="Strassengezwitscher") twitter = TwitterAPI('a', 'b', auth_type='oAuth2') twitter_account._fetch_tweets_from_api(twitter, max_id=99, since_id=17) self.assertEqual('since_id' in request_mock.call_args[0][1], True) self.assertEqual('max_id' in request_mock.call_args[0][1], True)
def test_get_absolute_url(self): twitter_account = TwitterAccount(name='PeterTheUnique', account_id=1337) twitter_account.save() self.assertEqual(twitter_account.get_absolute_url(), '/intern/twitter_accounts/1/')
def test_clean_raises_validation_error_on_twitter_connection_error(self): twitter_account = TwitterAccount(name="Strassengezwitscher") self.assertRaisesMessage( ValidationError, 'Could not connect to Twitter to retrieve user_id.', twitter_account.clean)