8000 Test refactor WIP · home-assistant/core@77e6fd7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 77e6fd7

Browse files
committed
Test refactor WIP
1 parent 4b74c42 commit 77e6fd7

File tree

3 files changed

+166
-123
lines changed

3 files changed

+166
-123
lines changed

tests/components/amberelectric/conftest.py

Lines changed: 88 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from unittest.mock import AsyncMock, Mock, patch
66

77
from amberelectric.models.channel import Channel, ChannelType
8+
from amberelectric.models.interval import Interval
9+
from amberelectric.models.range import Range
810
from amberelectric.models.site import Site
911
from amberelectric.models.site_status import SiteStatus
1012
import pytest
@@ -15,8 +17,6 @@
1517
DOMAIN,
1618
)
1719
from homeassistant.const import CONF_API_TOKEN
18-
from homeassistant.core import HomeAssistant
19-
from homeassistant.setup import async_setup_component
2020

2121
from .helpers import (
2222
CONTROLLED_LOAD_CHANNEL,
@@ -33,23 +33,27 @@
3333
MOCK_API_TOKEN = "psk_0000000000000000"
3434

3535

36-
async def create_amber_config_entry(site_id: str) -> MockConfigEntry:
36+
def create_amber_config_entry(site_id: str, name: str) -> MockConfigEntry:
3737
"""Create an Amber config entry."""
3838
return MockConfigEntry(
3939
domain=DOMAIN,
4040
data={
41-
CONF_API_TOKEN: "TOKEN",
42-
CONF_SITE_NAME: "home",
41+
CONF_API_TOKEN: MOCK_API_TOKEN,
42+
CONF_SITE_NAME: name,
4343
CONF_SITE_ID: site_id,
4444
},
45-
entry_id=site_id,
45+
unique_id=site_id,
4646
)
4747

4848

4949
@pytest.fixture
50-
async def general_only_site_id_amber_config_entry():
51-
"""Generate the default Amber config entry."""
52-
return await create_amber_config_entry(GENERAL_ONLY_SITE_ID)
50+
def mock_amber_client() -> Generator[AsyncMock]:
51+
"""Mock the Amber API client."""
52+
with patch(
53+
"homeassistant.components.amberelectric.amberelectric.AmberApi",
54+
autospec=True,
55+
) as mock_client:
56+
yield mock_client
5357

5458

5559
@pytest.fixture
@@ -62,76 +66,90 @@ def mock_setup_entry() -> Generator[AsyncMock]:
6266

6367

6468
@pytest.fixture
65-
async def setup_general(hass: HomeAssistant) -> AsyncGenerator[Mock]:
66-
"""Set up general channel."""
67-
MockConfigEntry(
68-
domain="amberelectric",
69-
data={
70-
CONF_SITE_NAME: "mock_title",
71-
CONF_API_TOKEN: MOCK_API_TOKEN,
72-
CONF_SITE_ID: GENERAL_ONLY_SITE_ID,
73-
},
74-
).add_to_hass(hass)
69+
async def general_channel_config_entry():
70+
"""Generate the default Amber config entry."""
71+
return create_amber_config_entry(GENERAL_ONLY_SITE_ID, "home")
7572

76-
instance = Mock()
77-
with patch(
78-
"amberelectric.AmberApi",
79-
return_value=instance,
80-
) as mock_update:
81-
instance.get_current_prices = Mock(return_value=GENERAL_CHANNEL)
82-
assert await async_setup_component(hass, DOMAIN, {})
83-
await hass.async_block_till_done()
84-
yield mock_update.return_value
73+
74+
@pytest.fixture
75+
async def general_channel_and_controlled_load_config_entry():
76+
"""Generate the default Amber config entry for site with controlled load."""
77+
return create_amber_config_entry(GENERAL_AND_CONTROLLED_SITE_ID, "home")
8578

8679

8780
@pytest.fixture
88-
async def setup_general_and_controlled_load(
89-
hass: HomeAssistant,
90-
) -> AsyncGenerator[Mock]:
91-
"""Set up general channel and controller load channel."""
92-
MockConfigEntry(
93-
domain="amberelectric",
94-
data={
95-
CONF_API_TOKEN: MOCK_API_TOKEN,
96-
CONF_SITE_ID: GENERAL_AND_CONTROLLED_SITE_ID,
97-
},
98-
).add_to_hass(hass)
81+
async def general_channel_and_feed_in_config_entry():
82+
"""Generate the default Amber config entry for site with feed in."""
83+
return create_amber_config_entry(GENERAL_AND_FEED_IN_SITE_ID, "home")
9984

100-
instance = Mock()
101-
with patch(
102-
"amberelectric.AmberApi",
103-
return_value=instance,
104-
) as mock_update:
105-
instance.get_current_prices = Mock(
106-
return_value=GENERAL_CHANNEL + CONTROLLED_LOAD_CHANNEL
107-
)
108-
assert await async_setup_component(hass, DOMAIN, {})
109-
await hass.async_block_till_done()
110-
yield mock_update.return_value
85+
86+
@pytest.fixture
87+
def general_channel_prices() -> list[Interval]:
88+
"""List containing general channel prices."""
89+
return GENERAL_CHANNEL
11190

11291

11392
@pytest.fixture
114-
async def setup_general_and_feed_in(hass: HomeAssistant) -> AsyncGenerator[Mock]:
115-
"""Set up general channel and feed in channel."""
116-
MockConfigEntry(
117-
domain="amberelectric",
118-
data={
119-
CONF_API_TOKEN: MOCK_API_TOKEN,
120-
CONF_SITE_ID: GENERAL_AND_FEED_IN_SITE_ID,
121-
},
122-
).add_to_hass(hass)
93+
def controlled_load_channel_prices() -> list[Interval]:
94+
"""List containing controlled load channel prices."""
95+
return CONTROLLED_LOAD_CHANNEL
12396

124-
instance = Mock()
125-
with patch(
126-
"amberelectric.AmberApi",
127-
return_value=instance,
128-
) as mock_update:
129-
instance.get_current_prices = Mock(
130-
return_value=GENERAL_CHANNEL + FEED_IN_CHANNEL
131-
)
132-
assert await async_setup_component(hass, DOMAIN, {})
133-
await hass.async_block_till_done()
134-
yield mock_update.return_value
97+
98+
@pytest.fixture
99+
def feed_in_channel_prices() -> list[Interval]:
100+
"""List containing feed in channel prices."""
101+
return FEED_IN_CHANNEL
102+
103+
104+
@pytest.fixture
105+
def mock_amber_client_general_channel(
106+
mock_amber_client: AsyncMock, general_channel_prices: list[Interval]
107+
) -> Generator[AsyncMock]:
108+
"""Fake general channel prices."""
109+
client = mock_amber_client.return_value
110+
client.get_current_prices.return_value = general_channel_prices
111+
return mock_amber_client
112+
113+
114+
@pytest.fixture
115+
def mock_amber_client_general_channel_with_range(
116+
mock_amber_client: AsyncMock, general_channel_prices: list[Interval]
117+
) -> Generator[AsyncMock]:
118+
"""Fake general channel prices with a range."""
119+
for interval in general_channel_prices:
120+
interval.actual_instance.range = Range(min=7.8, max=12.4)
121+
122+
client = mock_amber_client.return_value
123+
client.get_current_prices.return_value = general_channel_prices
124+
return mock_amber_client
125+
126+
127+
@pytest.fixture
128+
def mock_amber_client_general_and_controlled_load(
129+
mock_amber_client: AsyncMock,
130+
general_channel_prices: list[Interval],
131+
controlled_load_channel_prices: list[Interval],
132+
) -> Generator[AsyncMock]:
133+
"""Fake general channel and controlled load channel prices."""
134+
client = mock_amber_client.return_value
135+
client.get_current_prices.return_value = (
136+
general_channel_prices + controlled_load_channel_prices
137+
)
138+
return mock_amber_client
139+
140+
141+
@pytest.fixture
142+
async def mock_amber_client_general_and_feed_in(
143+
mock_amber_client: AsyncMock,
144+
general_channel_prices: list[Interval],
145+
feed_in_channel_prices: list[Interval],
146+
) -> AsyncGenerator[Mock]:
147+
"""Set up general channel and feed in channel."""
148+
client = mock_amber_client.return_value
149+
client.get_current_prices.return_value = (
150+
general_channel_prices + feed_in_channel_prices
151+
)
152+
return mock_amber_client
135153

136154

137155
@pytest.fixture(name="forecast_prices")

0 commit comments

Comments
 (0)
0