|
| 1 | +"""Transmission tests configuration.""" |
| 2 | + |
| 3 | +from collections.abc import Generator |
| 4 | +from datetime import UTC, datetime |
| 5 | +from unittest.mock import AsyncMock, patch |
| 6 | + |
| 7 | +import pytest |
| 8 | +from transmission_rpc.session import Session, SessionStats |
| 9 | +from transmission_rpc.torrent import Torrent |
| 10 | + |
| 11 | +from homeassistant.components.transmission.const import DOMAIN |
| 12 | + |
| 13 | +from . import MOCK_CONFIG_DATA |
| 14 | + |
| 15 | +from tests.common import MockConfigEntry |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def mock_setup_entry() -> Generator[AsyncMock]: |
| 20 | + """Override async_setup_entry.""" |
| 21 | + with patch( |
| 22 | + "homeassistant.components.transmission.async_setup_entry", |
| 23 | + return_value=True, |
| 24 | + ) as mock_setup_entry: |
| 25 | + yield mock_setup_entry |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def mock_config_entry() -> MockConfigEntry: |
| 30 | + """Mock a config entry.""" |
| 31 | + return MockConfigEntry( |
| 32 | + domain=DOMAIN, |
| 33 | + title="Transmission", |
| 34 | + data=MOCK_CONFIG_DATA, |
| 35 | + entry_id="01J0BC4QM2YBRP6H5G933AETT7", |
| 36 | + ) |
| 37 | + |
| 38 | + |
| 39 | +@pytest.fixture |
| 40 | +def mock_transmission_client() -> Generator[AsyncMock]: |
| 41 | + """Mock a Transmission client.""" |
| 42 | + with ( |
| 43 | + patch( |
| 44 | + "homeassistant.components.transmission.transmission_rpc.Client", |
| 45 | + autospec=False, |
| 46 | + ) as mock_client_class, |
| 47 | + ): |
| 48 | + client = mock_client_class.return_value |
| 49 | + |
| 50 | + session_stats_data = { |
| 51 | + "uploadSpeed": 1, |
| 52 | + "downloadSpeed": 1, |
| 53 | + "activeTorrentCount": 0, |
| 54 | + "pausedTorrentCount": 0, |
| 55 | + "torrentCount": 0, |
| 56 | + } |
| 57 | + client.session_stats.return_value = SessionStats(fields=session_stats_data) |
| 58 | + |
| 59 | + session_data = {"alt-speed-enabled": False} |
| 60 | + client.get_session.return_value = Session(fields=session_data) |
| 61 | + |
| 62 | + client.get_torrents.return_value = [] |
| 63 | + |
| 64 | + yield mock_client_class |
| 65 | + |
| 66 | + |
| 67 | +@pytest.fixture |
| 68 | +def mock_torrent(): |
| 69 | + """Fixture that returns a factory function to create mock torrents.""" |
| 70 | + |
| 71 | + def _create_mock_torrent( |
| 72 | + torrent_id: int = 1, |
| 73 | + name: str = "Test Torrent", |
| 74 | + percent_done: float = 0.5, |
| 75 | + status: int = 4, |
| 76 | + download_dir: str = "/downloads", |
| 77 | + eta: int = 3600, |
| 78 | + added_date: datetime | None = None, |
| 79 | + ratio: float = 1.5, |
| 80 | + ) -> Torrent: |
| 81 | + """Create a mock torrent with all required attributes.""" |
| 82 | + if added_date is None: |
| 83 | + added_date = datetime(2025, 11, 26, 14, 18, 0, tzinfo=UTC) |
| 84 | + |
| 85 | + torrent_data = { |
| 86 | + "id": torrent_id, |
| 87 | + "name": name, |
| 88 | + "percentDone": percent_done, |
| 89 | + "status": status, |
| 90 | + "rateDownload": 0, |
| 91 | + "rateUpload": 0, |
| 92 | + "downloadDir": download_dir, |
| 93 | + "eta": eta, |
| 94 | + "addedDate": int(added_date.timestamp()), |
| 95 | + "uploadRatio": ratio, |
| 96 | + "error": 0, |
| 97 | + "errorString": "", |
| 98 | + } |
| 99 | + return Torrent(fields=torrent_data) |
| 100 | + |
| 101 | + return _create_mock_torrent |
0 commit comments