Files
Server-scripts/torrent_api/tests/test_main.py
T

67 lines
2.4 KiB
Python

import contextlib
import io
import subprocess
import sys
import unittest
from pathlib import Path
from unittest.mock import patch
from torrent_api.cli import main
class MainTest(unittest.TestCase):
@patch("torrent_api.cli.setup_logging")
@patch("torrent_api.cli.Qbittorrent")
@patch.dict(
"os.environ",
{"QBITTORRENT_PRIVATE_API_KEY": "private", "QBITTORRENT_PUBLIC_API_KEY": "public"},
clear=True,
)
@patch(
"sys.argv",
[
"torrent-api", "--sort", "ratio", "--asc", "--force-start",
"--show-id", "--id", "example", "--progress-threshold", "0.5",
"--copy-to", "/tmp/copied", "--dry-run", "--log-level", "DEBUG",
],
)
def test_cli_calls_both_instances(self, client, setup_logging):
private, public = client.side_effect = [unittest.mock.Mock(), unittest.mock.Mock()]
main()
private.show_torrents.assert_called_once_with(sort_by="ratio", reverse=False, id=True)
public.show_torrents.assert_called_once_with(sort_by="ratio", reverse=False, id=True)
public.show_and_copy_torrent_contents.assert_called_once_with(
"example", progress_threshold=0.5, copy_to=Path("/tmp/copied"), dry_run=True
)
private.force_start_by_tracker.assert_called_once_with(
{"myanonamouse": True, "leo-test": True, "nordicq": True}
)
public.force_start_by_tracker.assert_not_called()
setup_logging.assert_called_once_with("DEBUG")
@patch.dict("os.environ", {}, clear=True)
@patch("sys.argv", ["torrent-api"])
def test_cli_requires_keys(self):
with contextlib.redirect_stderr(io.StringIO()) as stderr:
with self.assertRaises(SystemExit) as error:
main()
self.assertEqual(error.exception.code, 2)
self.assertIn("Set QBITTORRENT_PRIVATE_API_KEY", stderr.getvalue())
def test_debug_entry_point_shows_help(self):
project_dir = Path(__file__).resolve().parents[1]
result = subprocess.run(
[sys.executable, str(project_dir / "main.py"), "--help"],
cwd=project_dir,
capture_output=True,
text=True,
check=True,
)
self.assertIn("--show-id", result.stdout)
self.assertIn("--progress-threshold", result.stdout)
self.assertIn("--copy-to", result.stdout)
if __name__ == "__main__":
unittest.main()