def remove_torrent_keeping_data(self, torrent_id: int) -> CommandResult: response: Response[TorrentAccessorResponse] = self.client.torrent.remove( torrent_id, delete_local_data=False ) if response.result != "success": return CommandResult(error=response.result, success=False) return CommandResult()
def verify(self, torrent_id: int) -> CommandResult: response: Response[TorrentAccessorResponse] = self.client.torrent.action( TorrentActionMethod.VERIFY, torrent_id ) if response.result != "success": return CommandResult(error=response.result, success=False) return CommandResult()
def test_linking_add_run_display_failed(mocker: MockerFixture, capsys): api = mocker.Mock(spec=TransmissionApi) api.add_torrent_with_files.return_value = CommandResult(error="unknown", success=False) api.add_torrent.return_value = CommandResult(error="unknown", success=False) add_service = AddService(api) fs = MockFilesystem({"test_path"}) locator: TorrentDataLocator = DefaultTorrentDataLocator(fs) find_service = FindService(locator) path = Path("/", "test_path") file = MetainfoFile( { "name": "meaningless", "info_hash": "meaningless and necessary", "info": { "length": 5 }, }, path, ) command = LinkingAddCommand(add_service, fs, {TorrentData(file, Path("/some/place"))}) output: LinkingAddOutput = command.run() output.display() result = capsys.readouterr().out assert result == "\n".join(["1 failed:", "meaningless because: unknown" ]) + "\n"
def change_torrent_location(self, torrent_id: int, new_path: Path) -> CommandResult: response: Response = self.client.torrent.move( ids=torrent_id, location=str(new_path), move=False ) if response.result != "success": return CommandResult(success=False) return CommandResult()
def test_add_linking_unknown(mocker: MockerFixture): api = mocker.Mock(spec=TransmissionApi) api.add_torrent_with_files.return_value = CommandResult(error="unknown", success=False) api.add_torrent.return_value = CommandResult(error="unknown", success=False) add_service = AddService(api) fs = MockFilesystem({"test_path"}) path = Path("/", "test_path") file = MetainfoFile( { "name": "meaningless", "info_hash": "meaningless and necessary", "info": { "length": 5 }, }, path, ) command = LinkingAddCommand(add_service, fs, {TorrentData(file, Path("/some/place"))}) output: LinkingAddOutput = command.run() assert fs.exists(path) assert output.failed_torrents == {file: "unknown"}
def test_linking_add_dry_run_display(mocker: MockerFixture, capsys): api = mocker.Mock(spec=TransmissionApi) api.add_torrent_with_files.return_value = CommandResult(success=True) api.add_torrent.return_value = CommandResult(success=True) add_service = AddService(api) fs = MockFilesystem({"test_path"}) path = Path("/", "test_path") file = MetainfoFile( { "name": "meaningless", "info_hash": "meaningless and necessary", "info": { "length": 5 }, }, path, ) torrent_data = {TorrentData(file, Path("/some/place")), TorrentData(file)} command = LinkingAddCommand(add_service, fs, torrent_data) output: LinkingAddOutput = command.dry_run() output.dry_run_display() result = capsys.readouterr().out assert (result == "\n".join([ "Would add 1 torrents with data:", "meaningless at /some/place", "Would add 1 torrents without data:", "meaningless", ]) + "\n")
def test_linking_add_run_display_duplicated(mocker: MockerFixture, capsys): api = mocker.Mock(spec=TransmissionApi) api.add_torrent_with_files.return_value = CommandResult(success=False, error="duplicate") api.add_torrent.return_value = CommandResult(success=False, error="duplicate") add_service = AddService(api) fs = MockFilesystem({"test_path"}) path = Path("/", "test_path") file = MetainfoFile( { "name": "meaningless", "info_hash": "meaningless and necessary", "info": { "length": 5 }, }, path, ) command = LinkingAddCommand(add_service, fs, {TorrentData(file, Path("/some/place"))}) output: LinkingAddOutput = command.run() output.display() result = capsys.readouterr().out assert result == "\n".join(["There are 1 duplicates:", "meaningless" ]) + "\n"
def test_add_linking_success(mocker: MockerFixture): api = mocker.Mock(spec=TransmissionApi) api.add_torrent_with_files.return_value = CommandResult(success=True) api.add_torrent.return_value = CommandResult(success=True) add_service = AddService(api) fs = MockFilesystem({"test_path"}) locator: TorrentDataLocator = DefaultTorrentDataLocator(fs) find_service = FindService(locator) path = Path("/", "test_path") file = MetainfoFile( { "name": "meaningless", "info_hash": "meaningless and necessary", "info": { "length": 5 }, }, path, ) command = LinkingAddCommand(add_service, fs, {TorrentData(file, Path("/some/place"))}) output: LinkingAddOutput = command.run() assert not fs.exists(path) assert output.linked_torrents == {file: Path("/some/place")}
def add_torrent(self, file: Path) -> CommandResult: arguments: TorrentAddArguments = { "filename": str(file), "paused": True, } response: Response[TorrentAdd] = self.client.torrent.add(arguments) if response.result != "success" or response.arguments is None: return CommandResult(error=response.result, success=False) if response.arguments.torrent_added: return CommandResult() elif response.arguments.torrent_duplicate: return CommandResult(error="duplicate torrent", success=False) return CommandResult(error="unknown error", success=False)
def test_add_run_display(mocker: MockerFixture, capsys): api = mocker.Mock(spec=TransmissionApi) api.add_torrent.return_value = CommandResult() service = AddService(api) fs = mocker.Mock(spec=Filesystem) metainfo_paths = {Path("/", "test_path", str(n)) for n in range(2)} metainfo_files = { MetainfoFile({ "info_hash": path, "name": "some_name" }, path) for path in metainfo_paths } command = AddCommand(service, fs, metainfo_files) output: AddOutput = command.run() output.display() result = capsys.readouterr().out assert (result == "\n".join([ "2 torrents were added:", "some_name", "some_name", "2 torrents were deleted:", "some_name at /test_path/0", "some_name at /test_path/1", ]) + "\n")
def test_add_run_unknown(mocker: MockerFixture): api = mocker.Mock(spec=TransmissionApi) api.add_torrent.return_value = CommandResult(error="unknown", success=False) service = AddService(api) fs = mocker.Mock(spec=Filesystem) metainfo_path = Path("/", "test_path") metainfo_file = MetainfoFile({"info_hash": "arbitrary"}, metainfo_path) command = AddCommand(service, fs, {metainfo_file}) output: AddOutput = command.run() fs.remove.assert_not_called() assert output.failed_torrents[metainfo_file] == "unknown"
def test_add_run_success(mocker: MockerFixture): api = mocker.Mock(spec=TransmissionApi) api.add_torrent.return_value = CommandResult() service = AddService(api) fs = mocker.Mock(spec=Filesystem) metainfo_paths = {Path("/", "test_path", str(n)) for n in range(10)} metainfo_files = { MetainfoFile({"info_hash": path}, path) for path in metainfo_paths } command = AddCommand(service, fs, metainfo_files) output: AddOutput = command.run() for path in metainfo_paths: fs.remove.assert_any_call(path) added_paths = {torrent.path for torrent in output.added_torrents} for path in metainfo_paths: assert path in added_paths
def test_add_run_display_failed(mocker: MockerFixture, capsys): api = mocker.Mock(spec=TransmissionApi) api.add_torrent.return_value = CommandResult(error="unknown", success=False) service = AddService(api) fs = mocker.Mock(spec=Filesystem) metainfo_path = Path("/", "test_path") metainfo_file = MetainfoFile( { "info_hash": "arbitrary", "name": "some_name" }, metainfo_path) command = AddCommand(service, fs, {metainfo_file}) output: AddOutput = command.run() output.display() result = capsys.readouterr().out assert (result == "\n".join([ "1 torrents failed:", "some_name because: unknown", ]) + "\n")