Skip to content

Commit 3a65d3c

Browse files
Add tests to Transmission (home-assistant#157355)
1 parent 7fe2622 commit 3a65d3c

File tree

10 files changed

+1159
-91
lines changed

10 files changed

+1159
-91
lines changed

homeassistant/components/transmission/quality_scale.yaml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ rules:
3232
log-when-unavailable: done
3333
parallel-updates: todo
3434
reauthentication-flow: done
35-
test-coverage:
36-
status: todo
37-
comment: |
38-
Change to mock_setup_entry to avoid repetition when expanding tests.
35+
test-coverage: done
3936

4037
# Gold
4138
devices:

tests/components/transmission/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
"""Tests for Transmission."""
22

3+
from homeassistant.core import HomeAssistant
4+
5+
from tests.common import MockConfigEntry
6+
7+
8+
async def setup_integration(hass: HomeAssistant, config_entry: MockConfigEntry) -> None:
9+
"""Fixture for setting up the component."""
10+
config_entry.add_to_hass(hass)
11+
12+
await hass.config_entries.async_setup(config_entry.entry_id)
13+
await hass.async_block_till_done()
14+
15+
316
OLD_MOCK_CONFIG_DATA = {
417
"name": "Transmission",
518
"host": "0.0.0.0",
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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

Comments
 (0)