Replace global test markers with fixtures in Synology DSM tests (#87678)

This commit is contained in:
Franck Nijhof 2023-02-08 09:42:57 +01:00 committed by GitHub
parent c98b4e3204
commit e749521b29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 20 deletions

View File

@ -1,26 +1,17 @@
"""Configure Synology DSM tests."""
from collections.abc import Generator
from unittest.mock import AsyncMock, patch
import pytest
def pytest_configure(config):
"""Register custom marker for tests."""
config.addinivalue_line(
"markers", "no_bypass_setup: mark test to disable bypass_setup_fixture"
)
@pytest.fixture(name="bypass_setup", autouse=True)
def bypass_setup_fixture(request):
"""Mock component setup."""
if "no_bypass_setup" in request.keywords:
yield
else:
with patch(
"homeassistant.components.synology_dsm.async_setup_entry", return_value=True
):
yield
@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock, None, None]:
"""Mock setting up a config entry."""
with patch(
"homeassistant.components.synology_dsm.async_setup_entry", return_value=True
) as mock_setup:
yield mock_setup
@pytest.fixture(name="mock_dsm")

View File

@ -146,6 +146,7 @@ def mock_controller_service_failed():
yield dsm
@pytest.mark.usefixtures("mock_setup_entry")
async def test_user(hass: HomeAssistant, service: MagicMock):
"""Test user config."""
result = await hass.config_entries.flow.async_init(
@ -217,6 +218,7 @@ async def test_user(hass: HomeAssistant, service: MagicMock):
assert result["data"].get(CONF_VOLUMES) is None
@pytest.mark.usefixtures("mock_setup_entry")
async def test_user_2sa(hass: HomeAssistant, service_2sa: MagicMock):
"""Test user with 2sa authentication config."""
with patch(
@ -269,6 +271,7 @@ async def test_user_2sa(hass: HomeAssistant, service_2sa: MagicMock):
assert result["data"].get(CONF_VOLUMES) is None
@pytest.mark.usefixtures("mock_setup_entry")
async def test_user_vdsm(hass: HomeAssistant, service_vdsm: MagicMock):
"""Test user config."""
with patch(
@ -313,6 +316,7 @@ async def test_user_vdsm(hass: HomeAssistant, service_vdsm: MagicMock):
assert result["data"].get(CONF_VOLUMES) is None
@pytest.mark.usefixtures("mock_setup_entry")
async def test_reauth(hass: HomeAssistant, service: MagicMock):
"""Test reauthentication."""
entry = MockConfigEntry(
@ -362,6 +366,7 @@ async def test_reauth(hass: HomeAssistant, service: MagicMock):
assert result["reason"] == "reauth_successful"
@pytest.mark.usefixtures("mock_setup_entry")
async def test_reconfig_user(hass: HomeAssistant, service: MagicMock):
"""Test re-configuration of already existing entry by user."""
MockConfigEntry(
@ -390,6 +395,7 @@ async def test_reconfig_user(hass: HomeAssistant, service: MagicMock):
assert result["reason"] == "reconfigure_successful"
@pytest.mark.usefixtures("mock_setup_entry")
async def test_login_failed(hass: HomeAssistant, service: MagicMock):
"""Test when we have errors during login."""
service.return_value.login = Mock(
@ -405,6 +411,7 @@ async def test_login_failed(hass: HomeAssistant, service: MagicMock):
assert result["errors"] == {CONF_USERNAME: "invalid_auth"}
@pytest.mark.usefixtures("mock_setup_entry")
async def test_connection_failed(hass: HomeAssistant, service: MagicMock):
"""Test when we have errors during connection."""
service.return_value.login = Mock(
@ -421,6 +428,7 @@ async def test_connection_failed(hass: HomeAssistant, service: MagicMock):
assert result["errors"] == {CONF_HOST: "cannot_connect"}
@pytest.mark.usefixtures("mock_setup_entry")
async def test_unknown_failed(hass: HomeAssistant, service: MagicMock):
"""Test when we have an unknown error."""
service.return_value.login = Mock(side_effect=SynologyDSMException(None, None))
@ -435,6 +443,7 @@ async def test_unknown_failed(hass: HomeAssistant, service: MagicMock):
assert result["errors"] == {"base": "unknown"}
@pytest.mark.usefixtures("mock_setup_entry")
async def test_missing_data_after_login(hass: HomeAssistant, service_failed: MagicMock):
"""Test when we have errors during connection."""
with patch(
@ -450,6 +459,7 @@ async def test_missing_data_after_login(hass: HomeAssistant, service_failed: Mag
assert result["errors"] == {"base": "missing_data"}
@pytest.mark.usefixtures("mock_setup_entry")
async def test_form_ssdp(hass: HomeAssistant, service: MagicMock):
"""Test we can setup from ssdp."""
@ -493,6 +503,7 @@ async def test_form_ssdp(hass: HomeAssistant, service: MagicMock):
assert result["data"].get(CONF_VOLUMES) is None
@pytest.mark.usefixtures("mock_setup_entry")
async def test_reconfig_ssdp(hass: HomeAssistant, service: MagicMock):
"""Test re-configuration of already existing entry by ssdp."""
@ -525,6 +536,7 @@ async def test_reconfig_ssdp(hass: HomeAssistant, service: MagicMock):
assert result["reason"] == "reconfigure_successful"
@pytest.mark.usefixtures("mock_setup_entry")
async def test_skip_reconfig_ssdp(hass: HomeAssistant, service: MagicMock):
"""Test re-configuration of already existing entry by ssdp."""
@ -557,6 +569,7 @@ async def test_skip_reconfig_ssdp(hass: HomeAssistant, service: MagicMock):
assert result["reason"] == "already_configured"
@pytest.mark.usefixtures("mock_setup_entry")
async def test_existing_ssdp(hass: HomeAssistant, service: MagicMock):
"""Test abort of already existing entry by ssdp."""
@ -589,6 +602,7 @@ async def test_existing_ssdp(hass: HomeAssistant, service: MagicMock):
assert result["reason"] == "already_configured"
@pytest.mark.usefixtures("mock_setup_entry")
async def test_options_flow(hass: HomeAssistant, service: MagicMock):
"""Test config flow options."""
config_entry = MockConfigEntry(
@ -632,6 +646,7 @@ async def test_options_flow(hass: HomeAssistant, service: MagicMock):
assert config_entry.options[CONF_SNAPSHOT_QUALITY] == 0
@pytest.mark.usefixtures("mock_setup_entry")
async def test_discovered_via_zeroconf(hass: HomeAssistant, service: MagicMock):
"""Test we can setup from zeroconf."""
@ -677,6 +692,7 @@ async def test_discovered_via_zeroconf(hass: HomeAssistant, service: MagicMock):
assert result["data"].get(CONF_VOLUMES) is None
@pytest.mark.usefixtures("mock_setup_entry")
async def test_discovered_via_zeroconf_missing_mac(
hass: HomeAssistant, service: MagicMock
):

View File

@ -1,7 +1,6 @@
"""Tests for the Synology DSM component."""
from unittest.mock import MagicMock, patch
import pytest
from synology_dsm.exceptions import SynologyDSMLoginInvalidException
from homeassistant import data_entry_flow
@ -21,7 +20,6 @@ from .consts import HOST, MACS, PASSWORD, PORT, USE_SSL, USERNAME
from tests.common import MockConfigEntry
@pytest.mark.no_bypass_setup
async def test_services_registered(hass: HomeAssistant, mock_dsm: MagicMock):
"""Test if all services are registered."""
with patch(
@ -45,7 +43,6 @@ async def test_services_registered(hass: HomeAssistant, mock_dsm: MagicMock):
assert hass.services.has_service(DOMAIN, service)
@pytest.mark.no_bypass_setup
async def test_reauth_triggered(hass: HomeAssistant):
"""Test if reauthentication flow is triggered."""
with patch(