Skip to content

Commit 1ac2ae3

Browse files
bieniujoostlek
andauthored
Improve client mock for NextDNS tests (home-assistant#155036)
Co-authored-by: Joost Lekkerkerker <[email protected]>
1 parent 2fce7db commit 1ac2ae3

File tree

3 files changed

+57
-50
lines changed

3 files changed

+57
-50
lines changed

tests/components/nextdns/conftest.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,29 +59,31 @@ def mock_config_entry() -> MockConfigEntry:
5959

6060

6161
@pytest.fixture
62-
def mock_nextdns_client() -> Generator[AsyncMock]:
63-
"""Mock a NextDNS client."""
64-
62+
def mock_nextdns() -> Generator[AsyncMock]:
63+
"""Mock the NextDns class."""
6564
with (
66-
patch("homeassistant.components.nextdns.NextDns", autospec=True) as mock_client,
67-
patch(
68-
"homeassistant.components.nextdns.config_flow.NextDns",
69-
new=mock_client,
70-
),
65+
patch("homeassistant.components.nextdns.NextDns", autospec=True) as mock_class,
66+
patch("homeassistant.components.nextdns.config_flow.NextDns", new=mock_class),
7167
):
72-
client = mock_client.create.return_value
73-
client.clear_logs.return_value = True
74-
client.connection_status.return_value = CONNECTION_STATUS
75-
client.get_analytics_dnssec.return_value = ANALYTICS_DNSSEC
76-
client.get_analytics_encryption.return_value = ANALYTICS_ENCRYPTION
77-
client.get_analytics_ip_versions.return_value = ANALYTICS_IP_VERSIONS
78-
client.get_analytics_protocols.return_value = ANALYTICS_PROTOCOLS
79-
client.get_analytics_status.return_value = ANALYTICS_STATUS
80-
client.get_profile_id = Mock(return_value="xyz12")
81-
client.get_profile_name = Mock(return_value="Fake Profile")
82-
client.get_profiles.return_value = PROFILES
83-
client.get_settings.return_value = SETTINGS
84-
client.set_setting.return_value = True
85-
client.profiles = [ProfileInfo(**PROFILES[0])]
68+
yield mock_class
69+
70+
71+
@pytest.fixture
72+
def mock_nextdns_client(mock_nextdns: AsyncMock) -> AsyncMock:
73+
"""Mock a NextDNS client instance."""
74+
client = mock_nextdns.create.return_value
75+
client.clear_logs.return_value = True
76+
client.connection_status.return_value = CONNECTION_STATUS
77+
client.get_analytics_dnssec.return_value = ANALYTICS_DNSSEC
78+
client.get_analytics_encryption.return_value = ANALYTICS_ENCRYPTION
79+
client.get_analytics_ip_versions.return_value = ANALYTICS_IP_VERSIONS
80+
client.get_analytics_protocols.return_value = ANALYTICS_PROTOCOLS
81+
client.get_analytics_status.return_value = ANALYTICS_STATUS
82+
client.get_profile_id = Mock(return_value="xyz12")
83+
client.get_profile_name = Mock(return_value="Fake Profile")
84+
client.get_profiles.return_value = PROFILES
85+
client.get_settings.return_value = SETTINGS
86+
client.set_setting.return_value = True
87+
client.profiles = [ProfileInfo(**PROFILES[0])]
8688

87-
yield client
89+
return client

tests/components/nextdns/test_config_flow.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Define tests for the NextDNS config flow."""
22

3-
from unittest.mock import AsyncMock, patch
3+
from unittest.mock import AsyncMock
44

55
from nextdns import ApiError, InvalidApiKeyError
66
import pytest
@@ -21,6 +21,7 @@ async def test_form_create_entry(
2121
hass: HomeAssistant,
2222
mock_setup_entry: AsyncMock,
2323
mock_nextdns_client: AsyncMock,
24+
mock_nextdns: AsyncMock,
2425
) -> None:
2526
"""Test that the user step works."""
2627
result = await hass.config_entries.flow.async_init(
@@ -64,6 +65,7 @@ async def test_form_errors(
6465
hass: HomeAssistant,
6566
mock_setup_entry: AsyncMock,
6667
mock_nextdns_client: AsyncMock,
68+
mock_nextdns: AsyncMock,
6769
exc: Exception,
6870
base_error: str,
6971
) -> None:
@@ -74,18 +76,18 @@ async def test_form_errors(
7476
assert result["type"] is FlowResultType.FORM
7577
assert result["errors"] == {}
7678

77-
with patch(
78-
"homeassistant.components.nextdns.NextDns.create",
79-
side_effect=exc,
80-
):
81-
result = await hass.config_entries.flow.async_configure(
82-
result["flow_id"],
83-
{CONF_API_KEY: "fake_api_key"},
84-
)
79+
mock_nextdns.create.side_effect = exc
80+
81+
result = await hass.config_entries.flow.async_configure(
82+
result["flow_id"],
83+
{CONF_API_KEY: "fake_api_key"},
84+
)
8585

8686
assert result["type"] is FlowResultType.FORM
8787
assert result["errors"] == {"base": base_error}
8888

89+
mock_nextdns.create.side_effect = None
90+
8991
result = await hass.config_entries.flow.async_configure(
9092
result["flow_id"],
9193
{CONF_API_KEY: "fake_api_key"},
@@ -110,6 +112,7 @@ async def test_form_already_configured(
110112
hass: HomeAssistant,
111113
mock_config_entry: MockConfigEntry,
112114
mock_nextdns_client: AsyncMock,
115+
mock_nextdns: AsyncMock,
113116
) -> None:
114117
"""Test that errors are shown when duplicates are added."""
115118
await init_integration(hass, mock_config_entry)
@@ -135,6 +138,7 @@ async def test_reauth_successful(
135138
hass: HomeAssistant,
136139
mock_config_entry: MockConfigEntry,
137140
mock_nextdns_client: AsyncMock,
141+
mock_nextdns: AsyncMock,
138142
) -> None:
139143
"""Test starting a reauthentication flow."""
140144
await init_integration(hass, mock_config_entry)
@@ -168,6 +172,7 @@ async def test_reauth_errors(
168172
base_error: str,
169173
mock_config_entry: MockConfigEntry,
170174
mock_nextdns_client: AsyncMock,
175+
mock_nextdns: AsyncMock,
171176
) -> None:
172177
"""Test reauthentication flow with errors."""
173178
await init_integration(hass, mock_config_entry)
@@ -176,14 +181,17 @@ async def test_reauth_errors(
176181
assert result["type"] is FlowResultType.FORM
177182
assert result["step_id"] == "reauth_confirm"
178183

179-
with patch("homeassistant.components.nextdns.NextDns.create", side_effect=exc):
180-
result = await hass.config_entries.flow.async_configure(
181-
result["flow_id"],
182-
user_input={CONF_API_KEY: "new_api_key"},
183-
)
184+
mock_nextdns.create.side_effect = exc
185+
186+
result = await hass.config_entries.flow.async_configure(
187+
result["flow_id"],
188+
user_input={CONF_API_KEY: "new_api_key"},
189+
)
184190

185191
assert result["errors"] == {"base": base_error}
186192

193+
mock_nextdns.create.side_effect = None
194+
187195
result = await hass.config_entries.flow.async_configure(
188196
result["flow_id"],
189197
user_input={CONF_API_KEY: "new_api_key"},

tests/components/nextdns/test_init.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Test init of NextDNS integration."""
22

3-
from unittest.mock import AsyncMock, patch
3+
from unittest.mock import AsyncMock
44

55
from nextdns import ApiError, InvalidApiKeyError
66
import pytest
@@ -36,15 +36,13 @@ async def test_async_setup_entry(
3636
async def test_config_not_ready(
3737
hass: HomeAssistant,
3838
mock_config_entry: MockConfigEntry,
39-
mock_nextdns_client: AsyncMock,
39+
mock_nextdns: AsyncMock,
4040
exc: Exception,
4141
) -> None:
4242
"""Test for setup failure if the connection to the service fails."""
43-
with patch(
44-
"homeassistant.components.nextdns.NextDns.create",
45-
side_effect=exc,
46-
):
47-
await init_integration(hass, mock_config_entry)
43+
mock_nextdns.create.side_effect = exc
44+
45+
await init_integration(hass, mock_config_entry)
4846

4947
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
5048

@@ -53,6 +51,7 @@ async def test_unload_entry(
5351
hass: HomeAssistant,
5452
mock_config_entry: MockConfigEntry,
5553
mock_nextdns_client: AsyncMock,
54+
mock_nextdns: AsyncMock,
5655
) -> None:
5756
"""Test successful unload of entry."""
5857
await init_integration(hass, mock_config_entry)
@@ -70,14 +69,12 @@ async def test_unload_entry(
7069
async def test_config_auth_failed(
7170
hass: HomeAssistant,
7271
mock_config_entry: MockConfigEntry,
73-
mock_nextdns_client: AsyncMock,
72+
mock_nextdns: AsyncMock,
7473
) -> None:
7574
"""Test for setup failure if the auth fails."""
76-
with patch(
77-
"homeassistant.components.nextdns.NextDns.create",
78-
side_effect=InvalidApiKeyError,
79-
):
80-
await init_integration(hass, mock_config_entry)
75+
mock_nextdns.create.side_effect = InvalidApiKeyError
76+
77+
await init_integration(hass, mock_config_entry)
8178

8279
assert mock_config_entry.state is ConfigEntryState.SETUP_ERROR
8380

0 commit comments

Comments
 (0)