Esempio n. 1
0
 def test_with_config(self):
     config = ConfigParser.SafeConfigParser()
     config.add_section('twitter')
     config.set('twitter', 'consumer_key', 'test')
     config.set('twitter', 'consumer_secret', 'test')
     config.set('twitter', 'access_token', 'test')
     config.set('twitter', 'access_token_secret', 'test')
     self.stream = Stream(self.listener, config=config)
Esempio n. 2
0
 def setup(self):
     self.listener = QueueListener()
     self.stream = Stream(self.listener, 'test', 'test', 'test', 'test')
Esempio n. 3
0
class TestStream(object):
    def setup(self):
        self.listener = QueueListener()
        self.stream = Stream(self.listener, 'test', 'test', 'test', 'test')

    def test_with_config(self):
        config = ConfigParser.SafeConfigParser()
        config.add_section('twitter')
        config.set('twitter', 'consumer_key', 'test')
        config.set('twitter', 'consumer_secret', 'test')
        config.set('twitter', 'access_token', 'test')
        config.set('twitter', 'access_token_secret', 'test')
        self.stream = Stream(self.listener, config=config)

    def test_missing_credentials(self):
        assert_raises(ValueError, Stream, self.listener)

    def test_run_tcpip_exceptions(self):
        side_effects = [
                        socket.error,
                        httplib.HTTPException,
                        KeyboardInterrupt,
                       ]
        mock = MagicMock(side_effect=side_effects)
        self.stream._run(mock)
        assert self.stream.tcpip_delay == 0.25 * (len(side_effects) - 1) + 0.25

    def test_run_twitter_stream_error(self):
        side_effects = [
                        TwitterStreamError(47),
                       ]
        mock = MagicMock(side_effect=side_effects)
        self.stream._run(mock)
        expected = [call()]
        calls = mock.mock_calls
        assert calls == expected

    def test_sample(self):
        side_effects = [
                        send_listener(self.listener, EXAMPLE_TWEETS),
                        KeyboardInterrupt,
                       ]
        mock = MagicMock()
        self.stream._stream = mock
        self.stream._stream.sample = MagicMock(side_effect=side_effects)
        self.stream.sample()
        assert len(self.listener) == len(EXAMPLE_TWEETS)
        expected = [call.sample(), call.disconnect(), call.sample(), call.disconnect()]
        calls = mock.mock_calls
        assert calls == expected

    def test_sample_kwargs(self):
        side_effects = [
                        send_listener(self.listener, EXAMPLE_TWEETS),
                        KeyboardInterrupt,
                       ]
        mock = MagicMock(side_effect=side_effects)
        self.stream._stream.sample = mock
        self.stream.sample(unreal_kwarg=47)
        assert len(self.listener) == len(EXAMPLE_TWEETS)
        expected = [call(unreal_kwarg=47)] * len(side_effects)
        calls = mock.mock_calls
        assert calls == expected

    def test_filter_follow(self):
        side_effects = [
                        send_listener(self.listener, EXAMPLE_TWEETS),
                        KeyboardInterrupt,
                       ]
        self.stream._stream.filter = MagicMock(side_effect=side_effects)
        self.stream.filter(follow=['user1', 'user2'])
        assert len(self.listener) == len(EXAMPLE_TWEETS)

    def test_filter_track(self):
        side_effects = [
                        send_listener(self.listener, EXAMPLE_TWEETS),
                        KeyboardInterrupt,
                       ]
        self.stream._stream.filter = MagicMock(side_effect=side_effects)
        self.stream.filter(track=['keyword1', 'kw2'])
        assert len(self.listener) == len(EXAMPLE_TWEETS)

    def test_filter_locations(self):
        side_effects = [
                        send_listener(self.listener, EXAMPLE_TWEETS),
                        KeyboardInterrupt,
                       ]
        self.stream._stream.filter = MagicMock(side_effect=side_effects)
        self.stream.filter(locations=[1, 2, 3, 4])
        assert len(self.listener) == len(EXAMPLE_TWEETS)

    def test_filter_multiple(self):
        side_effects = [
                        send_listener(self.listener, EXAMPLE_TWEETS),
                        KeyboardInterrupt,
                       ]
        self.stream._stream.filter = MagicMock(side_effect=side_effects)
        self.stream.filter(follow=['user1', 'user2'],
                           track=['keyword1', 'kw2'],
                           locations=[1, 2, 3, 4])
        assert len(self.listener) == len(EXAMPLE_TWEETS)

    def test_filter_kwargs(self):
        side_effects = [
                        send_listener(self.listener, EXAMPLE_TWEETS),
                        KeyboardInterrupt,
                       ]
        mock = MagicMock(side_effect=side_effects)
        self.stream._stream.filter = mock
        self.stream.filter(track=['keyword1', 'kw2'],
                           unreal_kwarg=47)
        assert len(self.listener) == len(EXAMPLE_TWEETS)
        expected = [call(track=['keyword1', 'kw2'], follow=None,
                         unreal_kwarg=47, locations=None)] * len(side_effects)
        calls = mock.mock_calls
        assert calls == expected