Ejemplo n.º 1
0
    def test_handle_received_image(self, mocker):
        grabber = Grabber('http://example.com')

        dummy_result = {
            'image': Image.Image(),
            'requested_at': datetime.now(),
            'url': 'http://example.com',
            'error': None
        }

        # If no handlers, then should just return a dict of data
        grabber.get_result_handlers = mocker.Mock(
            'grabber.get_result_handlers', return_value=())
        assert grabber.handle_received_image(dummy_result) == dummy_result
        grabber.get_result_handlers.assert_called_once_with()

        mock_handler_1_return = dummy_result.copy()
        mock_handler_1_return['some_key'] = 'value'
        mock_handler_1 = mocker.Mock(return_value=mock_handler_1_return)

        mock_handler_2_return = dummy_result.copy()
        mock_handler_2_return['another_key'] = 'value'
        mock_handler_2 = mocker.Mock(return_value=mock_handler_2_return)

        grabber.get_result_handlers = mocker.Mock(
            'grabber.get_result_handlers',
            return_value=(mock_handler_1, mock_handler_2),
        )

        result = grabber.handle_received_image(dummy_result)

        assert result == mock_handler_2_return
        mock_handler_1.assert_called_once_with(dummy_result, grabber)
        mock_handler_2.assert_called_once_with(mock_handler_1_return, grabber)
Ejemplo n.º 2
0
    def test_get_result_handlers(self, mocker):
        grabber = Grabber('http://example.com')

        # When nothing is set, should still return an iterable
        grabber.default_result_handlers = None
        grabber.extra_result_handlers = None
        grabber.result_handlers = None
        assert grabber.get_result_handlers() == ()

        grabber.default_result_handlers = ()
        grabber.extra_result_handlers = ()
        grabber.result_handlers = None
        assert grabber.get_result_handlers() == ()

        grabber.default_result_handlers = ()
        grabber.extra_result_handlers = ()
        grabber.result_handlers = ()
        assert grabber.get_result_handlers() == ()

        mock_handler_1 = mocker.Mock()
        mock_handler_2 = mocker.Mock()
        mock_handler_3 = mocker.Mock()
        mock_handler_4 = mocker.Mock()

        grabber.default_result_handlers = (mock_handler_1, )
        grabber.extra_result_handlers = ()
        grabber.result_handlers = None
        assert grabber.get_result_handlers() == (mock_handler_1, )

        grabber.default_result_handlers = ()
        grabber.extra_result_handlers = (mock_handler_1, )
        grabber.result_handlers = None
        assert grabber.get_result_handlers() == (mock_handler_1, )

        grabber.default_result_handlers = (mock_handler_1, )
        grabber.extra_result_handlers = (mock_handler_1, )
        grabber.result_handlers = None
        assert grabber.get_result_handlers() == (mock_handler_1,
                                                 mock_handler_1)

        grabber.default_result_handlers = (mock_handler_1, )
        grabber.extra_result_handlers = (mock_handler_1, )
        grabber.result_handlers = (mock_handler_3, mock_handler_4)
        assert grabber.get_result_handlers() == (mock_handler_3,
                                                 mock_handler_4)

        grabber.default_result_handlers = (mock_handler_1, mock_handler_2)
        grabber.extra_result_handlers = (mock_handler_1, mock_handler_3)
        grabber.result_handlers = None
        assert grabber.get_result_handlers() == (mock_handler_1,
                                                 mock_handler_2,
                                                 mock_handler_1,
                                                 mock_handler_3)