def export(ctx, entry_id, format, output, filename_with_id): """ Export entry to file. Default output to current directory. """ output = PurePath(output) if output else None format = FormatType.get(format) params = ExportCommandParams(entry_id, format, output) params.filename_with_id = filename_with_id run_command(ExportCommand(ctx.obj, params))
def test_export(self, monkeypatch, tmp_path): def export_entry(self): return Response(200, None, b'123', 'application/epub') monkeypatch.setattr(ExportEntry, 'request', export_entry) params = ExportCommandParams(10, FormatType.get('epub'), PurePath(f'{tmp_path}/file.epub')) params.filename_with_id = False result, msg = ExportCommand(self.config, params).execute() assert result assert msg == f'Exported to: {tmp_path}/file.epub'
def test_wrong_entry_id(self, values): result, msg = ExportCommand( self.config, ExportCommandParams(values[0], FormatType.get('json'))).execute() assert not result assert msg == values[1]
def test_wrong_format_type(self, monkeypatch, tmpdir): command = ExportCommand( self.config, ExportCommandParams(10, FormatType.get('epub2'), PurePath(tmpdir))) result, msg = command.execute() assert not result assert msg == 'Unsupported type'
def test_empty_output(self, monkeypatch, tmpdir): def export_entry(self): return Response(200, None, b'123', 'application/epub') monkeypatch.setattr(ExportEntry, 'request', export_entry) result, msg = ExportCommand( self.config, ExportCommandParams(10, FormatType.get('epub'), PurePath(tmpdir))).execute() assert result assert re.search(f'Exported to: {PurePath(tmpdir)}/.+.epub', msg)
def test_wrong_format_type(self, monkeypatch, tmpdir): def export_entry(self): return Response(200, None, b'123', 'application/epub') monkeypatch.setattr(ExportEntry, 'request', export_entry) command = ExportCommand( self.config, ExportCommandParams(10, FormatType.get('epub2'), PurePath(tmpdir))) result, msg = command.execute() assert result assert command.params.format == FormatType.get('json')
def test_empty_output_cdisposition(self, monkeypatch): def export_entry(self): return Response(200, None, b'123', 'application/epub', 'attachment; filename="file.epub"') monkeypatch.setattr(ExportEntry, 'request', export_entry) result, msg = ExportCommand( self.config, ExportCommandParams(10, FormatType.get('epub'))).execute() assert result assert msg == f'Exported to: {Path.cwd()}/10. file.epub' os.remove(f'{Path.cwd()}/10. file.epub')
def test_empty_output(self, monkeypatch, tmpdir): def export_entry(self): return Response(200, None, b'123', 'application/epub') def get_entry(self): return Response( 200, '{"id": 10, "title": "title", "content": "content",\ "url": "url", "is_archived": 0, "is_starred": 1}') monkeypatch.setattr(ExportEntry, 'request', export_entry) monkeypatch.setattr(GetEntry, 'request', get_entry) result, msg = ExportCommand( self.config, ExportCommandParams(10, FormatType.get('epub'), PurePath(tmpdir))).execute() assert result assert f'Exported to: {PurePath(tmpdir)}/10. title.epub' == msg
def test_format_not_specified(self): result, msg = ExportCommand(self.config, ExportCommandParams(10, None)).execute() assert not result assert msg == 'Type not specified'