def test_search_failed(twitter): """ If there's an exception, fail gracefully """ twitter.search = Mock(side_effect=TwythonError("Can't reach Twitter")) query = search.search(['food']) assert query == []
def test_search(twitter): """ The correct query should be requested """ keywords = ['food', 'stomach', 'disease'] tweets = {'statuses': sample.tweets} twitter.search = Mock(return_value=tweets) query = search.search(keywords) twitter.search.assert_called_with(count=100,\ q='food OR stomach OR disease',\ since_id=None) assert query == sample.tweets
def backfill_tweets(self): """ Get all tweets from the author of the source tweet since the last time this method was called, adding them to the database. """ name = self.source.user.screen_name new_tweets = search(['@'+name, 'from:'+name], self.last_tweet_id) if not new_tweets: return logger.info("Backfilling %s tweets for %s, last id %s" % (len(new_tweets), name, self.last_tweet_id)) new_tweets = [db.merge(tweet_to_Tweet(t)) for t in new_tweets] new_ids = [t.id for t in new_tweets] stray = [db.merge(t) for t in self.stray_tweets if t.id not in new_ids] logger.info("%s other stray tweets" % len(stray)) # doing the db.merge already added the tweets self.tweets.extend(stray) self.tweets.extend(new_tweets) self.update_last_tweet_id() self.stray_tweets = [] logger.info("%s total tweets in %s" % (len(self.tweets), self))