async def test_upload_image_returns_submission(client): g = Gallery() g._gallery_token = { 'token_id': 'a', 'token_secret': 'b', 'gallery_id': 'c', 'gallery_secret': 'd' } g._client.headers[_const.CSRF_TOKEN_HEADER] = 'csrf_token' client.post.return_value = { 'files': [{ 'original_url': 'http://image_url', 'thumbnail_url': 'http://thumbnail_url', 'url': 'http://web_url', }] } sub = await g._upload_image('foo.jpg', 'mock filetuple', None) assert sub == Submission( filepath='foo.jpg', image_url='http://image_url', thumbnail_url='http://thumbnail_url', web_url='http://web_url', gallery_url=g.url, edit_url=g.edit_url, )
async def test_upload_image_catches_exception_from_create_request(client): g = Gallery() with patch.object(g, 'create', side_effect=ConnectionError('The Error')): for i in range(3): sub = await g._upload_image(f'foo{i}.jpg', 'mock filetuple', None) assert sub == Submission(filepath=f'foo{i}.jpg', error='The Error') assert g.create.call_args_list == [call()] * (i + 1)
async def test_json_encounters_no_exceptions(mock_io, mock_gallery, mocker): mocker.patch('imgbox._output._all_files_ok', return_value=True) mock_gallery.add.return_value = AsyncIterator( (Submission(filepath='path/to/foo.jpg', success=True, image_url='img/foo', thumbnail_url='thumb/foo', web_url='web/foo', gallery_url='gallery/foo', edit_url='edit/foo'), )) with mock_io() as cap: exit_code = await _output.json(mock_gallery, ['path/to/foo.jpg']) assert exit_code == 0 assert cap.stderr == '' assert json.loads(cap.stdout) == [ { 'filename': 'foo.jpg', 'filepath': 'path/to/foo.jpg', 'success': True, 'error': None, 'image_url': 'img/foo', 'thumbnail_url': 'thumb/foo', 'web_url': 'web/foo', 'gallery_url': 'gallery/foo', 'edit_url': 'edit/foo', }, ] assert mock_gallery.add.call_args_list == [call(['path/to/foo.jpg'])] assert mock_gallery.create.call_args_list == []
async def test_upload_image_gets_error_via_argument(client): g = Gallery() with patch.object(g, 'create'): sub = await g._upload_image('foo.jpg', None, 'Something went wrong') assert sub == Submission(filepath='foo.jpg', error='Something went wrong') assert g.create.call_args_list == []
def test_Submission_getattr_key_with_None_value(): s = Submission(error='Argh') assert s.filename is None assert s.filepath is None assert s.image_url is None assert s.thumbnail_url is None assert s.web_url is None assert s.gallery_url is None assert s.edit_url is None
async def test_text_handles_error_when_adding_to_gallery( mock_io, mock_gallery, mocker): mocker.patch('imgbox._output._all_files_ok', return_value=True) mock_gallery.add.return_value = AsyncIterator(( Submission(filepath='path/to/foo.jpg', success=True, image_url='img/foo', thumbnail_url='thumb/foo', web_url='web/foo', gallery_url='gallery/foo', edit_url='edit/foo'), Submission(filepath='path/to/bar.jpg', success=False, error='Oops'), Submission(filepath='path/to/baz.jpg', success=True, image_url='img/baz', thumbnail_url='thumb/baz', web_url='web/baz', gallery_url='gallery/baz', edit_url='edit/baz'), )) with mock_io() as cap: exit_code = await _output.text( mock_gallery, ['path/to/foo.jpg', 'path/to/bar.jpg', 'path/to/baz.jpg']) assert exit_code == 1 assert cap.stderr == '' assert cap.stdout == ('Gallery: <Gallery URL>\n' ' Edit: <Edit URL>\n' '* foo.jpg\n' ' Image: img/foo\n' ' Thumbnail: thumb/foo\n' ' Webpage: web/foo\n' '* bar.jpg\n' ' Oops\n' '* baz.jpg\n' ' Image: img/baz\n' ' Thumbnail: thumb/baz\n' ' Webpage: web/baz\n') assert mock_gallery.create.call_args_list == [call()] assert mock_gallery.add.call_args_list == [ call(['path/to/foo.jpg', 'path/to/bar.jpg', 'path/to/baz.jpg']) ]
def test_Submission_getattr_unknown_key(): s = Submission( filepath='path/to/Foo.jpg', image_url='https://foo.bar/asdf.jpg', thumbnail_url='https://foo.bar/asdf_t.jpg', web_url='https://foo.bar/asdf', gallery_url='https://foo.bar/fdsa', edit_url='https://foo.bar/fdsa/edit' ) with pytest.raises(AttributeError, match=r"^Submission object has no attribute 'foo'$"): s.foo
async def test_upload_image_catches_exception_from_upload_request(client): g = Gallery() g._gallery_token = { 'token_id': 'a', 'token_secret': 'b', 'gallery_id': 'c', 'gallery_secret': 'd' } g._client.headers[_const.CSRF_TOKEN_HEADER] = 'csrf_token' client.post.side_effect = ConnectionError('The Error') sub = await g._upload_image('foo.jpg', 'mock filetuple', None) assert sub == Submission(filepath='foo.jpg', error='The Error')
def test_Submission_no_required_arguments_on_error(): assert Submission( error='Trouble is afoot!', ) == {'success': False, 'error': 'Trouble is afoot!', 'filename': None, 'filepath': None, 'image_url': None, 'thumbnail_url': None, 'web_url': None, 'gallery_url': None, 'edit_url': None}
def test_Submission_required_argument_on_success(key): with pytest.raises(AssertionError, match=rf"^Missing key: '{key}'$"): kwargs = { 'filepath': 'path/to/Foo.jpg', 'image_url': 'https://foo.bar/asdf.jpg', 'thumbnail_url': 'https://foo.bar/asdf_t.jpg', 'web_url': 'https://foo.bar/asdf', 'gallery_url': 'https://foo.bar/asdf/gallery', 'edit_url': 'https://foo.bar/asdf/edit', } del kwargs[key] Submission(**kwargs)
def test_Submission_gets_valid_success_arguments(): assert Submission( filepath='path/to/Foo.jpg', image_url='https://foo.bar/asdf.jpg', thumbnail_url='https://foo.bar/asdf_t.jpg', web_url='https://foo.bar/asdf', gallery_url='https://foo.bar/fdsa', edit_url='https://foo.bar/fdsa/edit' ) == { 'success': True, 'error': None, 'filename': 'Foo.jpg', 'filepath': 'path/to/Foo.jpg', 'image_url': 'https://foo.bar/asdf.jpg', 'thumbnail_url': 'https://foo.bar/asdf_t.jpg', 'web_url': 'https://foo.bar/asdf', 'gallery_url': 'https://foo.bar/fdsa', 'edit_url': 'https://foo.bar/fdsa/edit', }
def test_Submission_repr(): s = Submission( filepath='path/to/Foo.jpg', image_url='https://foo.bar/asdf.jpg', thumbnail_url='https://foo.bar/asdf_t.jpg', web_url='https://foo.bar/asdf', gallery_url='https://foo.bar/fdsa', edit_url='https://foo.bar/fdsa/edit' ) assert repr(s) == ( "Submission(" "success=True, " "filepath='path/to/Foo.jpg', " "filename='Foo.jpg', " "image_url='https://foo.bar/asdf.jpg', " "thumbnail_url='https://foo.bar/asdf_t.jpg', " "web_url='https://foo.bar/asdf', " "gallery_url='https://foo.bar/fdsa', " "edit_url='https://foo.bar/fdsa/edit'" ")" )
async def test_json_handles_error_from_adding_to_gallery( mock_io, mock_gallery, mocker): mocker.patch('imgbox._output._all_files_ok', return_value=True) mock_gallery.add.return_value = AsyncIterator(( Submission(filepath='path/to/foo.jpg', success=True, image_url='img/foo', thumbnail_url='thumb/foo', web_url='web/foo', gallery_url='gallery/foo', edit_url='edit/foo'), Submission(filepath='path/to/bar.jpg', success=False, error='Oops'), Submission(filepath='path/to/baz.jpg', success=True, image_url='img/baz', thumbnail_url='thumb/baz', web_url='web/baz', gallery_url='gallery/baz', edit_url='edit/baz'), )) with mock_io() as cap: exit_code = await _output.json( mock_gallery, ['path/to/foo.jpg', 'path/to/bar.jpg', 'path/to/baz.jpg']) assert exit_code == 1 assert cap.stderr == '' assert json.loads(cap.stdout) == [ { 'filename': 'foo.jpg', 'filepath': 'path/to/foo.jpg', 'success': True, 'error': None, 'image_url': 'img/foo', 'thumbnail_url': 'thumb/foo', 'web_url': 'web/foo', 'gallery_url': 'gallery/foo', 'edit_url': 'edit/foo', }, { 'filename': 'bar.jpg', 'filepath': 'path/to/bar.jpg', 'success': False, 'error': 'Oops', 'image_url': None, 'thumbnail_url': None, 'web_url': None, 'gallery_url': None, 'edit_url': None, }, { 'filename': 'baz.jpg', 'filepath': 'path/to/baz.jpg', 'success': True, 'error': None, 'image_url': 'img/baz', 'thumbnail_url': 'thumb/baz', 'web_url': 'web/baz', 'gallery_url': 'gallery/baz', 'edit_url': 'edit/baz', }, ] assert mock_gallery.add.call_args_list == [ call(['path/to/foo.jpg', 'path/to/bar.jpg', 'path/to/baz.jpg']), ] assert mock_gallery.create.call_args_list == []
def test_Submission_gets_unknown_key(): with pytest.raises(AssertionError, match=r"^Unknown key: 'x'$"): Submission(x='y')