Exemple #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)
Exemple #2
0
    def test_handle_received_image__no_result_return(self, mocker):
        dummy_result_original = {
            'image': Image.Image(),
            'requested_at': datetime.now(),
            'url': 'http://example.com',
            'error': None
        }

        dummy_result_after_1 = dummy_result_original.copy()
        dummy_result_after_1['handler_1'] = 'done'

        dummy_result_after_3 = dummy_result_after_1.copy()
        dummy_result_after_3['handler_3'] = 'done'

        mock_handler_1 = mocker.Mock(return_value=dummy_result_after_1)
        mock_handler_2 = mocker.Mock(return_value=None)
        mock_handler_3 = mocker.Mock(return_value=dummy_result_after_3)

        grabber = Grabber('http://example.com')

        mocker.patch.object(grabber,
                            'get_result_handlers',
                            return_value=(mock_handler_1, mock_handler_2,
                                          mock_handler_3))

        result = grabber.handle_received_image(dummy_result_original)

        grabber.get_result_handlers.assert_called_once_with()

        mock_handler_1.assert_called_once_with(dummy_result_original, grabber)
        mock_handler_2.assert_called_once_with(dummy_result_after_1, grabber)
        mock_handler_3.assert_called_once_with(dummy_result_after_1, grabber)

        assert result == dummy_result_after_3
Exemple #3
0
    def test_tick(self, mocker):
        im = Image.Image()

        dummy_request = {
            'url': 'http://example.com',
            'requested_at': datetime.now()
        }

        grabber = Grabber('http://example.com')
        grabber.create_request = mocker.Mock(grabber.create_request,
                                             autospec=True,
                                             return_value=dummy_request)

        dummy_result = {'image': im, 'requested_at': datetime.now()}

        grabber.download_image = mocker.Mock(grabber.download_image,
                                             autospec=True,
                                             return_value=dummy_result)
        grabber.handle_received_image = mocker.Mock(
            grabber.handle_received_image, autospec=True)

        grabber.tick()

        grabber.create_request.assert_called_once_with()
        grabber.download_image.assert_called_once_with(dummy_request)
        grabber.handle_received_image.assert_called_once_with(dummy_result)