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

Code before:
    )
    await hass.async_block_till_done()

    assert len(instance.update_dns_record.mock_calls) == 4
    assert "All target records are up to date" not in caplog.text


@pytest.mark.usefixtures("location_info")
async def test_integration_services_with_issue(
    hass: HomeAssistant, cfupdate: MagicMock, caplog: pytest.LogCaptureFixture
) -> None:
    """Test integration services with issue."""
    instance = cfupdate.return_value

    entry = await init_integration(hass)
    assert entry.state is ConfigEntryState.LOADED

    with patch(LOCATION_PATCH_TARGET, return_value=None):
        await hass.services.async_call(
            DOMAIN,
            SERVICE_UPDATE_RECORDS,
            {},
            blocking=True,
        )

    assert len(instance.update_dns_record.mock_calls) == 2
    assert "Could not get external IPv4 address" in caplog.text


@pytest.mark.usefixtures("location_info")
async def test_integration_services_with_nonexisting_record(
    hass: HomeAssistant, cfupdate: MagicMock, caplog: pytest.LogCaptureFixture
) -> None:
    """Test integration services."""
    instance = cfupdate.return_value

    entry = await init_integration(
        hass, data={**ENTRY_CONFIG, CONF_RECORDS: ["nonexisting.example.com"]}
    )
    assert entry.state is ConfigEntryState.LOADED

    await hass.services.async_call(
        DOMAIN,
        SERVICE_UPDATE_RECORDS,
        {},
        blocking=True,
    )
    await hass.async_block_till_done()

    instance.update_dns_record.assert_not_called()
    assert "All target records are up to date" in caplog.text

Review comment (suggestion):
I'm not sure if this is correct. This test shouldn't update records if the public IP address can't be determined.. Why do we have two function calls here?


Here is related code:
https://github.com/home-assistant/core/blob/3ab350dc5e2820190809bc9ed4dd0244eb8c199e/homeassistant/components/cloudflare/coordinator.py#L80-L81

Code after:
        SERVICE_UPDATE_RECORDS,
        {},
        blocking=True,
    )
    await hass.async_block_till_done()

    assert len(instance.update_dns_record.mock_calls) == 2
    assert "All target records are up to date" not in caplog.text


@pytest.mark.usefixtures("location_info")
async def test_integration_services_with_issue(
    hass: HomeAssistant, cfupdate: MagicMock, caplog: pytest.LogCaptureFixture
) -> None:
    """Test integration services with issue."""
    instance = cfupdate.return_value

    entry = await init_integration(hass)
    assert entry.state is ConfigEntryState.LOADED

    assert len(instance.update_dns_record.mock_calls) == 2
    instance.update_dns_record.reset_mock()

    with patch(LOCATION_PATCH_TARGET, return_value=None):
        await hass.services.async_call(
            DOMAIN,
            SERVICE_UPDATE_RECORDS,
            {},
            blocking=True,
        )

    instance.update_dns_record.assert_not_called()
    assert "Could not get external IPv4 address" in caplog.text


@pytest.mark.usefixtures("location_info")
async def test_integration_services_with_nonexisting_record(
    hass: HomeAssistant, cfupdate: MagicMock, caplog: pytest.LogCaptureFixture
) -> None:
    """Test integration services."""
    instance = cfupdate.return_value

    entry = await init_integration(
        hass, data={**ENTRY_CONFIG, CONF_RECORDS: ["nonexisting.example.com"]}
    )
    assert entry.state is ConfigEntryState.LOADED

    await hass.services.async_call(
        DOMAIN,
        SERVICE_UPDATE_RECORDS,
        {},