Repository: home-assistant/core
File: tests/components/duckdns/test_init.py (Python)

Code before:
    await hass.async_block_till_done()

    assert config_entry.state is ConfigEntryState.LOADED


@pytest.mark.usefixtures("setup_duckdns")
async def test_setup(hass: HomeAssistant, aioclient_mock: AiohttpClientMocker) -> None:
    """Test setup works if update passes."""
    aioclient_mock.get(
        UPDATE_URL,
        params={"domains": TEST_SUBDOMAIN, "token": TEST_TOKEN},
        text="OK",
    )

    assert aioclient_mock.call_count == 1

    async_fire_time_changed(hass, utcnow() + timedelta(minutes=5))
    await hass.async_block_till_done()
    assert aioclient_mock.call_count == 2


@pytest.mark.parametrize(
    "side_effect",
    [
        [False] * len(BACKOFF_INTERVALS),
        [ClientError] * len(BACKOFF_INTERVALS),
    ],
)
@pytest.mark.freeze_time
async def test_setup_backoff(
    hass: HomeAssistant,
    config_entry: MockConfigEntry,
    side_effect: list[Exception | bool],
) -> None:
    """Test update fails with backoffs and recovers."""

    with patch(
        "homeassistant.components.duckdns.coordinator.update_duckdns",
        side_effect=side_effect,
    ) as client_mock:
        config_entry.add_to_hass(hass)
        await hass.config_entries.async_setup(config_entry.entry_id)
        await hass.async_block_till_done()

        assert config_entry.state is ConfigEntryState.SETUP_RETRY
        assert client_mock.call_count == 1

        tme = utcnow()
        await hass.async_block_till_done()

        _LOGGER.debug("Backoff")

Review comment (suggestion):
Why don't you make the `* len()` part of the test instead of doing it here

Code after:
    await hass.async_block_till_done()

    assert config_entry.state is ConfigEntryState.LOADED


@pytest.mark.usefixtures("setup_duckdns")
async def test_setup(hass: HomeAssistant, aioclient_mock: AiohttpClientMocker) -> None:
    """Test setup works if update passes."""
    aioclient_mock.get(
        UPDATE_URL,
        params={"domains": TEST_SUBDOMAIN, "token": TEST_TOKEN},
        text="OK",
    )

    assert aioclient_mock.call_count == 1

    async_fire_time_changed(hass, utcnow() + timedelta(minutes=5))
    await hass.async_block_till_done()
    assert aioclient_mock.call_count == 2


@pytest.mark.parametrize(
    "side_effect",
    [False, ClientError],
)
@pytest.mark.freeze_time
async def test_setup_backoff(
    hass: HomeAssistant,
    config_entry: MockConfigEntry,
    side_effect: Exception | bool,
) -> None:
    """Test update fails with backoffs and recovers."""

    with patch(
        "homeassistant.components.duckdns.coordinator.update_duckdns",
        side_effect=[side_effect] * len(BACKOFF_INTERVALS),
    ) as client_mock:
        config_entry.add_to_hass(hass)
        await hass.config_entries.async_setup(config_entry.entry_id)
        await hass.async_block_till_done()

        assert config_entry.state is ConfigEntryState.SETUP_RETRY
        assert client_mock.call_count == 1

        tme = utcnow()
        await hass.async_block_till_done()

        _LOGGER.debug("Backoff")
        for idx in range(1, len(BACKOFF_INTERVALS)):
            tme += BACKOFF_INTERVALS[idx]
            async_fire_time_changed(hass, tme)