def test_incremental_search(self, twarc_class): message = base_message.copy() message["options"] = { # Incremental means that will only retrieve new results. "incremental": True } mock_twarc = MagicMock(spec=Twarc) # Expecting 2 searches. First returns 2 tweets. Second returns none. mock_twarc.search.side_effect = [(tweet2,), ()] # Return mock_twarc when instantiating a twarc. twarc_class.side_effect = [mock_twarc] harvester = TwitterHarvester() state_store = DictHarvestStateStore() state_store.set_state("twitter_harvester", "gwu.since_id", 605726286741434400) harvester.state_store = state_store harvester.message = message harvester.harvest_result = HarvestResult() harvester.stop_event = threading.Event() harvester.harvest_result_lock = threading.Lock() harvester.harvest_seeds() self.assertDictEqual({"tweet": 1}, harvester.harvest_result.summary) self.assertSetEqual({"http://bit.ly/1ipwd0B"}, harvester.harvest_result.urls_as_set()) twarc_class.assert_called_once_with(tests.TWITTER_CONSUMER_KEY, tests.TWITTER_CONSUMER_SECRET, tests.TWITTER_ACCESS_TOKEN, tests.TWITTER_ACCESS_TOKEN_SECRET) self.assertEqual([call("gwu", since_id=605726286741434400), call("gelman", since_id=None)], mock_twarc.search.mock_calls) # State updated self.assertEqual(660065173563158500, state_store.get_state("twitter_harvester", "gwu.since_id"))
def test_search(self, mock_twarc_class): mock_twarc = MagicMock(spec=Twarc) # Expecting 2 searches. First returns 2 tweets. Second returns none. mock_twarc.search.side_effect = [(tweet1, tweet2), ()] # Return mock_twarc when instantiating a twarc. mock_twarc_class.side_effect = [mock_twarc] harvester = TwitterHarvester() harvester.state_store = DictHarvestStateStore() harvester.message = base_message harvester.harvest_result = HarvestResult() harvester.stop_event = threading.Event() harvester.harvest_result_lock = threading.Lock() harvester.harvest_seeds() self.assertDictEqual({"tweet": 2}, harvester.harvest_result.summary) self.assertSetEqual({"http://bit.ly/1ipwd0B"}, harvester.harvest_result.urls_as_set()) mock_twarc_class.assert_called_once_with(tests.TWITTER_CONSUMER_KEY, tests.TWITTER_CONSUMER_SECRET, tests.TWITTER_ACCESS_TOKEN, tests.TWITTER_ACCESS_TOKEN_SECRET) self.assertEqual([call("gwu", since_id=None), call("gelman", since_id=None)], mock_twarc.search.mock_calls) # Nothing added to state self.assertEqual(0, len(harvester.state_store._state))