Skip to content

Commit

Permalink
Small cleanup in conftest mocks of PVOutput (#103628)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck committed Nov 8, 2023
1 parent 91ffe4f commit 3e204ab
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
13 changes: 3 additions & 10 deletions tests/components/pvoutput/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,14 @@ def mock_setup_entry() -> Generator[AsyncMock, None, None]:
yield mock_setup


@pytest.fixture
def mock_pvoutput_config_flow() -> Generator[None, MagicMock, None]:
"""Return a mocked PVOutput client."""
with patch(
"homeassistant.components.pvoutput.config_flow.PVOutput", autospec=True
) as pvoutput_mock:
yield pvoutput_mock.return_value


@pytest.fixture
def mock_pvoutput() -> Generator[None, MagicMock, None]:
"""Return a mocked PVOutput client."""
with patch(
"homeassistant.components.pvoutput.coordinator.PVOutput", autospec=True
) as pvoutput_mock:
) as pvoutput_mock, patch(
"homeassistant.components.pvoutput.config_flow.PVOutput", new=pvoutput_mock
):
pvoutput = pvoutput_mock.return_value
pvoutput.status.return_value = Status.from_dict(
load_json_object_fixture("status.json", DOMAIN)
Expand Down
42 changes: 20 additions & 22 deletions tests/components/pvoutput/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

async def test_full_user_flow(
hass: HomeAssistant,
mock_pvoutput_config_flow: MagicMock,
mock_pvoutput: MagicMock,
mock_setup_entry: AsyncMock,
) -> None:
"""Test the full user configuration flow."""
Expand All @@ -42,12 +42,12 @@ async def test_full_user_flow(
}

assert len(mock_setup_entry.mock_calls) == 1
assert len(mock_pvoutput_config_flow.system.mock_calls) == 1
assert len(mock_pvoutput.system.mock_calls) == 1


async def test_full_flow_with_authentication_error(
hass: HomeAssistant,
mock_pvoutput_config_flow: MagicMock,
mock_pvoutput: MagicMock,
mock_setup_entry: AsyncMock,
) -> None:
"""Test the full user configuration flow with incorrect API key.
Expand All @@ -62,7 +62,7 @@ async def test_full_flow_with_authentication_error(
assert result.get("type") == FlowResultType.FORM
assert result.get("step_id") == "user"

mock_pvoutput_config_flow.system.side_effect = PVOutputAuthenticationError
mock_pvoutput.system.side_effect = PVOutputAuthenticationError
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
Expand All @@ -76,9 +76,9 @@ async def test_full_flow_with_authentication_error(
assert result2.get("errors") == {"base": "invalid_auth"}

assert len(mock_setup_entry.mock_calls) == 0
assert len(mock_pvoutput_config_flow.system.mock_calls) == 1
assert len(mock_pvoutput.system.mock_calls) == 1

mock_pvoutput_config_flow.system.side_effect = None
mock_pvoutput.system.side_effect = None
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
user_input={
Expand All @@ -95,14 +95,12 @@ async def test_full_flow_with_authentication_error(
}

assert len(mock_setup_entry.mock_calls) == 1
assert len(mock_pvoutput_config_flow.system.mock_calls) == 2
assert len(mock_pvoutput.system.mock_calls) == 2


async def test_connection_error(
hass: HomeAssistant, mock_pvoutput_config_flow: MagicMock
) -> None:
async def test_connection_error(hass: HomeAssistant, mock_pvoutput: MagicMock) -> None:
"""Test API connection error."""
mock_pvoutput_config_flow.system.side_effect = PVOutputConnectionError
mock_pvoutput.system.side_effect = PVOutputConnectionError

result = await hass.config_entries.flow.async_init(
DOMAIN,
Expand All @@ -116,13 +114,13 @@ async def test_connection_error(
assert result.get("type") == FlowResultType.FORM
assert result.get("errors") == {"base": "cannot_connect"}

assert len(mock_pvoutput_config_flow.system.mock_calls) == 1
assert len(mock_pvoutput.system.mock_calls) == 1


async def test_already_configured(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_pvoutput_config_flow: MagicMock,
mock_pvoutput: MagicMock,
) -> None:
"""Test we abort if the PVOutput system is already configured."""
mock_config_entry.add_to_hass(hass)
Expand All @@ -146,7 +144,7 @@ async def test_already_configured(
async def test_reauth_flow(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_pvoutput_config_flow: MagicMock,
mock_pvoutput: MagicMock,
mock_setup_entry: AsyncMock,
) -> None:
"""Test the reauthentication configuration flow."""
Expand Down Expand Up @@ -178,13 +176,13 @@ async def test_reauth_flow(
}

assert len(mock_setup_entry.mock_calls) == 1
assert len(mock_pvoutput_config_flow.system.mock_calls) == 1
assert len(mock_pvoutput.system.mock_calls) == 1


async def test_reauth_with_authentication_error(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_pvoutput_config_flow: MagicMock,
mock_pvoutput: MagicMock,
mock_setup_entry: AsyncMock,
) -> None:
"""Test the reauthentication configuration flow with an authentication error.
Expand All @@ -206,7 +204,7 @@ async def test_reauth_with_authentication_error(
assert result.get("type") == FlowResultType.FORM
assert result.get("step_id") == "reauth_confirm"

mock_pvoutput_config_flow.system.side_effect = PVOutputAuthenticationError
mock_pvoutput.system.side_effect = PVOutputAuthenticationError
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_API_KEY: "invalid_key"},
Expand All @@ -218,9 +216,9 @@ async def test_reauth_with_authentication_error(
assert result2.get("errors") == {"base": "invalid_auth"}

assert len(mock_setup_entry.mock_calls) == 0
assert len(mock_pvoutput_config_flow.system.mock_calls) == 1
assert len(mock_pvoutput.system.mock_calls) == 1

mock_pvoutput_config_flow.system.side_effect = None
mock_pvoutput.system.side_effect = None
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
user_input={CONF_API_KEY: "valid_key"},
Expand All @@ -235,12 +233,12 @@ async def test_reauth_with_authentication_error(
}

assert len(mock_setup_entry.mock_calls) == 1
assert len(mock_pvoutput_config_flow.system.mock_calls) == 2
assert len(mock_pvoutput.system.mock_calls) == 2


async def test_reauth_api_error(
hass: HomeAssistant,
mock_pvoutput_config_flow: MagicMock,
mock_pvoutput: MagicMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test API error during reauthentication."""
Expand All @@ -258,7 +256,7 @@ async def test_reauth_api_error(
assert result.get("type") == FlowResultType.FORM
assert result.get("step_id") == "reauth_confirm"

mock_pvoutput_config_flow.system.side_effect = PVOutputConnectionError
mock_pvoutput.system.side_effect = PVOutputConnectionError
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_API_KEY: "some_new_key"},
Expand Down

0 comments on commit 3e204ab

Please sign in to comment.