def test_archive_second_query_failure(mocker: MockerFixture): archive_path = Path("/", "test_path") fs = mocker.Mock(spec=Filesystem) client = mocker.Mock(spec=TransmissionApi) client.get_torrent_files_by_id.return_value = QueryResult({1: Path("/", "file_1")}) client.get_torrent_name_by_id.return_value = QueryResult( error="some_error", success=False ) command = ArchiveCommand(archive_path, fs, client) result: ArchiveOutput = command.run() assert result.query_failure == "query failed: get_torrent_name_by_id"
def test_archive_success(mocker: MockerFixture): archive_path = Path("/", "test_path") fs = mocker.Mock(spec=Filesystem) client = mocker.Mock(spec=TransmissionApi) client.get_torrent_files_by_id.return_value = QueryResult({1: Path("/", "file_1")}) client.get_torrent_name_by_id.return_value = QueryResult({1: "test_name"}) command = ArchiveCommand(archive_path, fs, client) result: ArchiveOutput = command.run() assert result.copied == {ArchiveAction(1, "test_name", Path("/", "file_1"))} fs.create_dir.assert_called_once_with(Path("/", "test_path")) fs.copy.assert_called_once_with(Path("/", "file_1"), Path("/", "test_path"))
def test_query_failure_output(mocker: MockerFixture, capsys): archive_path = Path("/", "test_path") fs = MockFilesystem({}) client = mocker.Mock(spec=TransmissionApi) client.get_torrent_files_by_id.return_value = QueryResult( error="some_error", success=False ) client.get_torrent_name_by_id.return_value = QueryResult({1: "test_name"}) command = ArchiveCommand(archive_path, fs, client) output: ArchiveOutput = command.run() output.display() result = capsys.readouterr().out assert result == "Query failed: get_torrent_files_by_id\n"
def test_dry_run_display(mocker: MockerFixture, capsys): archive_path = Path("/", "test_path") fs = mocker.Mock(spec=Filesystem) client = mocker.Mock(spec=TransmissionApi) client.get_torrent_files_by_id.return_value = QueryResult({1: Path("/", "file_1")}) client.get_torrent_name_by_id.return_value = QueryResult({1: "test_name"}) command = ArchiveCommand(archive_path, fs, client) output: ArchiveOutput = command.dry_run() output.dry_run_display() result = capsys.readouterr().out assert ( result == "\n".join(["Found 1 duplicate metainfo files", "No metainfo files to move"]) + "\n" )
def test_run_display_copy_failure(mocker: MockerFixture, capsys): archive_path = Path("/", "test_path") fs = MockFilesystem({"file_1", "test_path"}) client = mocker.Mock(spec=TransmissionApi) client.get_torrent_files_by_id.return_value = QueryResult({1: Path("/", "file_1")}) client.get_torrent_name_by_id.return_value = QueryResult({1: "test_name"}) command = ArchiveCommand(archive_path, fs, client) output: ArchiveOutput = command.run() output.display() result = capsys.readouterr().out assert ( result == "\n".join( [ "Failed to move 1 metainfo files:", "\x1b[31m✗ failed to move /file_1 because:destination is a file", ] ) + "\n" )
def archive_factory(argv: Sequence[str], dependencies: Mapping) -> CommandFactoryResult: client = dependencies["client"] fs = dependencies["fs"] # parse from clutchless.spec import archive as archive_command archive_args = docopt(doc=archive_command.__doc__, argv=argv) location = Path(archive_args.get("<destination>")) errors_option = archive_args.get("--errors") if location: if errors_option: return ErrorArchiveCommand(location, fs, client), archive_args else: return ArchiveCommand(location, fs, client), archive_args return MissingCommand(), archive_args