def test_download_image__exception_raised(self, mocker): correct_url = 'http://example.com' now = datetime.now() e = Exception() dummy_request = {'requested_at': now, 'url': correct_url} dummy_downloader = mocker.Mock(side_effect=e) grabber = Grabber('http://bad-url.com') grabber.get_download_callable = mocker.Mock( grabber.get_download_callable, autospec=True, return_value=dummy_downloader, ) grabber.ignore_download_exception = mocker.Mock( grabber.ignore_download_exception, autospec=True, return_value=False) with pytest.raises(Exception): grabber.download_image(dummy_request) grabber.get_download_callable.assert_called_once_with() dummy_downloader.assert_called_once_with(correct_url, grabber) grabber.ignore_download_exception.assert_called_once_with(e)
def test_download_image__exception_ignored(self, mocker): correct_url = 'http://example.com' now = datetime.now() e = Exception() dummy_request = {'requested_at': now, 'url': correct_url} dummy_downloader = mocker.Mock(side_effect=e) grabber = Grabber('http://bad-url.com') grabber.get_download_callable = mocker.Mock( grabber.get_download_callable, autospec=True, return_value=dummy_downloader, ) grabber.ignore_download_exception = mocker.Mock( grabber.ignore_download_exception, autospec=True, return_value=True) expected_result = { 'requested_at': now, 'url': correct_url, 'image': None, 'error': e } result = grabber.download_image(dummy_request) assert result == expected_result grabber.get_download_callable.assert_called_once_with() dummy_downloader.assert_called_once_with(correct_url, grabber) grabber.ignore_download_exception.assert_called_once_with(e)
def test_download_image(self, mocker): correct_url = 'http://example.com' dummy_image = Image.Image() now = datetime.now() dummy_request = {'requested_at': now, 'url': correct_url} dummy_downloader = mocker.Mock(return_value=dummy_image) grabber = Grabber('http://bad-url.com', save_dir='some/dir') grabber.get_download_callable = mocker.Mock( grabber.get_download_callable, autospec=True, return_value=dummy_downloader) expected_result = { 'requested_at': now, 'url': correct_url, 'image': dummy_image, 'error': None, } result = grabber.download_image(dummy_request) assert result == expected_result dummy_downloader.assert_called_once_with(correct_url, grabber) grabber.get_download_callable.assert_called_once_with()
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)