def test_link_dry_run_failure_output(mocker: MockerFixture, capsys): metainfo_file = MetainfoFile({ "info_hash": "meaningless", "name": "some_name" }) location = Path() link_service = mocker.Mock(spec=LinkService) link_service.get_incomplete_id_by_metainfo_file.return_value = { metainfo_file: 1 } link_service.change_location.side_effect = RuntimeError("something") find_service = mocker.Mock(spec=FindService) torrent_data = TorrentData(metainfo_file, location) missing_torrent_data = TorrentData(metainfo_file) find_service.find.return_value = {torrent_data, missing_torrent_data} command = LinkCommand(link_service, find_service) output = command.dry_run() output.dry_run_display() result = capsys.readouterr().out assert (result == "\n".join([ "Found the following torrents:", "some_name at .", "Couldn't find data for the following torrents:", "some_name", ]) + "\n")
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_link_success(mocker: MockerFixture): metainfo_file = MetainfoFile({"info_hash": "meaningless"}) location = Path() link_service = mocker.Mock(spec=LinkService) link_service.get_incomplete_id_by_metainfo_file.return_value = { metainfo_file: 1 } find_service = mocker.Mock(spec=FindService) find_service.find.return_value = {TorrentData(metainfo_file, location)} command = LinkCommand(link_service, find_service) output = command.run() assert output.success == [TorrentData(metainfo_file, location)]
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_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 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 test_find_found(): metainfo_path = Path("/", "metainfo.torrent") search_path = Path("/", "data") properties = { "info_hash": "meaningless and necessary", "name": "test_name", "info": { "files": [ { "path": ["file1"], "length": 0 }, { "path": ["file2"], "length": 0 }, ] }, } metainfo_file = MetainfoFile(properties, metainfo_path) fs = MockFilesystem({"data": {"test_name": {"file1", "file2"}}}) locator = DefaultTorrentDataLocator(fs) find_service = FindService(locator) command = FindCommand(find_service, {metainfo_file}) output = command.run() assert output.found == {TorrentData(metainfo_file, search_path)}
async def test_async_data_locator_multi_file(datadir): reader: MetainfoIO = DefaultMetainfoIO() path = datadir / "being_earnest.torrent" file = reader.from_path(path) fs = DefaultFilesystem() locator = DefaultTorrentDataLocator(fs, datadir) result = await locator.find(file) assert result == TorrentData(file, datadir)
def test_link_failure(mocker: MockerFixture): metainfo_file = MetainfoFile({"info_hash": "meaningless"}) location = Path() link_service = mocker.Mock(spec=LinkService) link_service.get_incomplete_id_by_metainfo_file.return_value = { metainfo_file: 1 } link_service.change_location.side_effect = RuntimeError("something") find_service = mocker.Mock(spec=FindService) torrent_data = TorrentData(metainfo_file, location) find_service.find.return_value = {torrent_data} command = LinkCommand(link_service, find_service) output = command.run() assert output.success == [] assert output.no_matching_data == set() assert output.fail == [LinkFailure(torrent_data, "something")]
def test_link_dry_run_success_output(mocker: MockerFixture, capsys): metainfo_file = MetainfoFile({ "info_hash": "meaningless", "name": "some_name" }) location = Path() link_service = mocker.Mock(spec=LinkService) link_service.get_incomplete_id_by_metainfo_file.return_value = { metainfo_file: 1 } find_service = mocker.Mock(spec=FindService) find_service.find.return_value = {TorrentData(metainfo_file, location)} command = LinkCommand(link_service, find_service) output = command.dry_run() output.dry_run_display() result = capsys.readouterr().out assert (result == "\n".join( ["Found the following torrents:", "some_name at ."]) + "\n")