Clean up RainMachine tests (#64785)

* Clean up RainMachine tests

* Remove old stuff
This commit is contained in:
Aaron Bach 2022-01-23 13:17:07 -07:00 committed by GitHub
parent 8736b0649b
commit d2eda91588
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 103 additions and 158 deletions

View File

@ -0,0 +1,59 @@
"""Define test fixtures for RainMachine."""
from unittest.mock import AsyncMock, patch
import pytest
from homeassistant.components.rainmachine import DOMAIN
from homeassistant.const import CONF_IP_ADDRESS, CONF_PASSWORD, CONF_PORT, CONF_SSL
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry
@pytest.fixture(name="client")
def client_fixture(controller_mac):
"""Define a regenmaschine client."""
controller = AsyncMock()
controller.name = "My RainMachine"
controller.mac = controller_mac
return AsyncMock(load_local=AsyncMock(), controllers={controller_mac: controller})
@pytest.fixture(name="config")
def config_fixture(hass):
"""Define a config entry data fixture."""
return {
CONF_IP_ADDRESS: "192.168.1.100",
CONF_PASSWORD: "password",
CONF_PORT: 8080,
CONF_SSL: True,
}
@pytest.fixture(name="config_entry")
def config_entry_fixture(hass, config, controller_mac):
"""Define a config entry fixture."""
entry = MockConfigEntry(domain=DOMAIN, unique_id=controller_mac, data=config)
entry.add_to_hass(hass)
return entry
@pytest.fixture(name="controller_mac")
def controller_mac_fixture():
"""Define a controller MAC address."""
return "aa:bb:cc:dd:ee:ff"
@pytest.fixture(name="setup_rainmachine")
async def setup_rainmachine_fixture(hass, client, config):
"""Define a fixture to set up RainMachine."""
with patch(
"homeassistant.components.rainmachine.Client", return_value=client
), patch(
"homeassistant.components.rainmachine.config_flow.Client", return_value=client
), patch(
"homeassistant.components.rainmachine.PLATFORMS", []
):
assert await async_setup_component(hass, DOMAIN, config)
await hass.async_block_till_done()
yield

View File

@ -1,5 +1,5 @@
"""Define tests for the OpenUV config flow.""" """Define tests for the OpenUV config flow."""
from unittest.mock import AsyncMock, Mock, patch from unittest.mock import patch
import pytest import pytest
from regenmaschine.errors import RainMachineError from regenmaschine.errors import RainMachineError
@ -10,64 +10,22 @@ from homeassistant.components.rainmachine import CONF_ZONE_RUN_TIME, DOMAIN
from homeassistant.const import CONF_IP_ADDRESS, CONF_PASSWORD, CONF_PORT, CONF_SSL from homeassistant.const import CONF_IP_ADDRESS, CONF_PASSWORD, CONF_PORT, CONF_SSL
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from tests.common import MockConfigEntry
async def test_duplicate_error(hass, config, config_entry, setup_rainmachine):
def _get_mock_client():
mock_controller = Mock()
mock_controller.name = "My Rain Machine"
mock_controller.mac = "aa:bb:cc:dd:ee:ff"
return Mock(
load_local=AsyncMock(), controllers={"aa:bb:cc:dd:ee:ff": mock_controller}
)
async def test_duplicate_error(hass):
"""Test that errors are shown when duplicates are added.""" """Test that errors are shown when duplicates are added."""
conf = {
CONF_IP_ADDRESS: "192.168.1.100",
CONF_PASSWORD: "password",
CONF_PORT: 8080,
CONF_SSL: True,
}
MockConfigEntry(
domain=DOMAIN, unique_id="aa:bb:cc:dd:ee:ff", data=conf
).add_to_hass(hass)
with patch(
"homeassistant.components.rainmachine.config_flow.Client",
return_value=_get_mock_client(),
):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN, context={"source": config_entries.SOURCE_USER}, data=config
context={"source": config_entries.SOURCE_USER},
data=conf,
) )
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "already_configured" assert result["reason"] == "already_configured"
async def test_invalid_password(hass): async def test_invalid_password(hass, config):
"""Test that an invalid password throws an error.""" """Test that an invalid password throws an error."""
conf = { with patch("regenmaschine.client.Client.load_local", side_effect=RainMachineError):
CONF_IP_ADDRESS: "192.168.1.100",
CONF_PASSWORD: "bad_password",
CONF_PORT: 8080,
CONF_SSL: True,
}
with patch(
"regenmaschine.client.Client.load_local",
side_effect=RainMachineError,
):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN, context={"source": config_entries.SOURCE_USER}, data=config
context={"source": config_entries.SOURCE_USER},
data=conf,
) )
await hass.async_block_till_done()
assert result["errors"] == {CONF_PASSWORD: "invalid_auth"} assert result["errors"] == {CONF_PASSWORD: "invalid_auth"}
@ -91,19 +49,17 @@ async def test_invalid_password(hass):
], ],
) )
async def test_migrate_1_2( async def test_migrate_1_2(
hass, platform, entity_name, entity_id, old_unique_id, new_unique_id hass,
client,
config,
config_entry,
entity_id,
entity_name,
old_unique_id,
new_unique_id,
platform,
): ):
"""Test migration from version 1 to 2 (consistent unique IDs).""" """Test migration from version 1 to 2 (consistent unique IDs)."""
conf = {
CONF_IP_ADDRESS: "192.168.1.100",
CONF_PASSWORD: "password",
CONF_PORT: 8080,
CONF_SSL: True,
}
entry = MockConfigEntry(domain=DOMAIN, unique_id="aa:bb:cc:dd:ee:ff", data=conf)
entry.add_to_hass(hass)
ent_reg = er.async_get(hass) ent_reg = er.async_get(hass)
# Create entity RegistryEntry using old unique ID format: # Create entity RegistryEntry using old unique ID format:
@ -112,7 +68,7 @@ async def test_migrate_1_2(
DOMAIN, DOMAIN,
old_unique_id, old_unique_id,
suggested_object_id=entity_name, suggested_object_id=entity_name,
config_entry=entry, config_entry=config_entry,
original_name=entity_name, original_name=entity_name,
) )
assert entity_entry.entity_id == entity_id assert entity_entry.entity_id == entity_id
@ -121,8 +77,7 @@ async def test_migrate_1_2(
with patch( with patch(
"homeassistant.components.rainmachine.async_setup_entry", return_value=True "homeassistant.components.rainmachine.async_setup_entry", return_value=True
), patch( ), patch(
"homeassistant.components.rainmachine.config_flow.Client", "homeassistant.components.rainmachine.config_flow.Client", return_value=client
return_value=_get_mock_client(),
): ):
await setup.async_setup_component(hass, DOMAIN, {}) await setup.async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done() await hass.async_block_till_done()
@ -133,36 +88,19 @@ async def test_migrate_1_2(
assert ent_reg.async_get_entity_id(platform, DOMAIN, old_unique_id) is None assert ent_reg.async_get_entity_id(platform, DOMAIN, old_unique_id) is None
async def test_options_flow(hass): async def test_options_flow(hass, config, config_entry):
"""Test config flow options.""" """Test config flow options."""
conf = {
CONF_IP_ADDRESS: "192.168.1.100",
CONF_PASSWORD: "password",
CONF_PORT: 8080,
CONF_SSL: True,
}
config_entry = MockConfigEntry(
domain=DOMAIN,
unique_id="abcde12345",
data=conf,
options={CONF_ZONE_RUN_TIME: 900},
)
config_entry.add_to_hass(hass)
with patch( with patch(
"homeassistant.components.rainmachine.async_setup_entry", return_value=True "homeassistant.components.rainmachine.async_setup_entry", return_value=True
): ):
await hass.config_entries.async_setup(config_entry.entry_id) await hass.config_entries.async_setup(config_entry.entry_id)
result = await hass.config_entries.options.async_init(config_entry.entry_id) result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "init" assert result["step_id"] == "init"
result = await hass.config_entries.options.async_configure( result = await hass.config_entries.options.async_configure(
result["flow_id"], user_input={CONF_ZONE_RUN_TIME: 600} result["flow_id"], user_input={CONF_ZONE_RUN_TIME: 600}
) )
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert config_entry.options == {CONF_ZONE_RUN_TIME: 600} assert config_entry.options == {CONF_ZONE_RUN_TIME: 600}
@ -174,32 +112,17 @@ async def test_show_form(hass):
context={"source": config_entries.SOURCE_USER}, context={"source": config_entries.SOURCE_USER},
data=None, data=None,
) )
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user" assert result["step_id"] == "user"
async def test_step_user(hass): async def test_step_user(hass, config, setup_rainmachine):
"""Test that the user step works.""" """Test that the user step works."""
conf = {
CONF_IP_ADDRESS: "192.168.1.100",
CONF_PASSWORD: "password",
CONF_PORT: 8080,
CONF_SSL: True,
}
with patch(
"homeassistant.components.rainmachine.async_setup_entry", return_value=True
) as mock_setup_entry, patch(
"homeassistant.components.rainmachine.config_flow.Client",
return_value=_get_mock_client(),
):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": config_entries.SOURCE_USER}, context={"source": config_entries.SOURCE_USER},
data=conf, data=config,
) )
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "My RainMachine" assert result["title"] == "My RainMachine"
assert result["data"] == { assert result["data"] == {
@ -209,28 +132,17 @@ async def test_step_user(hass):
CONF_SSL: True, CONF_SSL: True,
CONF_ZONE_RUN_TIME: 600, CONF_ZONE_RUN_TIME: 600,
} }
assert mock_setup_entry.called
@pytest.mark.parametrize( @pytest.mark.parametrize(
"source", [config_entries.SOURCE_ZEROCONF, config_entries.SOURCE_HOMEKIT] "source", [config_entries.SOURCE_ZEROCONF, config_entries.SOURCE_HOMEKIT]
) )
async def test_step_homekit_zeroconf_ip_already_exists(hass, source): async def test_step_homekit_zeroconf_ip_already_exists(
hass, client, config, config_entry, source
):
"""Test homekit and zeroconf with an ip that already exists.""" """Test homekit and zeroconf with an ip that already exists."""
conf = {
CONF_IP_ADDRESS: "192.168.1.100",
CONF_PASSWORD: "password",
CONF_PORT: 8080,
CONF_SSL: True,
}
MockConfigEntry(
domain=DOMAIN, unique_id="aa:bb:cc:dd:ee:ff", data=conf
).add_to_hass(hass)
with patch( with patch(
"homeassistant.components.rainmachine.config_flow.Client", "homeassistant.components.rainmachine.config_flow.Client", return_value=client
return_value=_get_mock_client(),
): ):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
@ -252,21 +164,10 @@ async def test_step_homekit_zeroconf_ip_already_exists(hass, source):
@pytest.mark.parametrize( @pytest.mark.parametrize(
"source", [config_entries.SOURCE_ZEROCONF, config_entries.SOURCE_HOMEKIT] "source", [config_entries.SOURCE_ZEROCONF, config_entries.SOURCE_HOMEKIT]
) )
async def test_step_homekit_zeroconf_ip_change(hass, source): async def test_step_homekit_zeroconf_ip_change(hass, client, config_entry, source):
"""Test zeroconf with an ip change.""" """Test zeroconf with an ip change."""
conf = {
CONF_IP_ADDRESS: "192.168.1.100",
CONF_PASSWORD: "password",
CONF_PORT: 8080,
CONF_SSL: True,
}
entry = MockConfigEntry(domain=DOMAIN, unique_id="aa:bb:cc:dd:ee:ff", data=conf)
entry.add_to_hass(hass)
with patch( with patch(
"homeassistant.components.rainmachine.config_flow.Client", "homeassistant.components.rainmachine.config_flow.Client", return_value=client
return_value=_get_mock_client(),
): ):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
@ -283,28 +184,18 @@ async def test_step_homekit_zeroconf_ip_change(hass, source):
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "already_configured" assert result["reason"] == "already_configured"
assert entry.data[CONF_IP_ADDRESS] == "192.168.1.2" assert config_entry.data[CONF_IP_ADDRESS] == "192.168.1.2"
@pytest.mark.parametrize( @pytest.mark.parametrize(
"source", [config_entries.SOURCE_ZEROCONF, config_entries.SOURCE_HOMEKIT] "source", [config_entries.SOURCE_ZEROCONF, config_entries.SOURCE_HOMEKIT]
) )
async def test_step_homekit_zeroconf_new_controller_when_some_exist(hass, source): async def test_step_homekit_zeroconf_new_controller_when_some_exist(
hass, client, config, source
):
"""Test homekit and zeroconf for a new controller when one already exists.""" """Test homekit and zeroconf for a new controller when one already exists."""
existing_conf = {
CONF_IP_ADDRESS: "192.168.1.3",
CONF_PASSWORD: "password",
CONF_PORT: 8080,
CONF_SSL: True,
}
entry = MockConfigEntry(
domain=DOMAIN, unique_id="zz:bb:cc:dd:ee:ff", data=existing_conf
)
entry.add_to_hass(hass)
with patch( with patch(
"homeassistant.components.rainmachine.config_flow.Client", "homeassistant.components.rainmachine.config_flow.Client", return_value=client
return_value=_get_mock_client(),
): ):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
@ -324,9 +215,8 @@ async def test_step_homekit_zeroconf_new_controller_when_some_exist(hass, source
with patch( with patch(
"homeassistant.components.rainmachine.async_setup_entry", return_value=True "homeassistant.components.rainmachine.async_setup_entry", return_value=True
) as mock_setup_entry, patch( ), patch(
"homeassistant.components.rainmachine.config_flow.Client", "homeassistant.components.rainmachine.config_flow.Client", return_value=client
return_value=_get_mock_client(),
): ):
result2 = await hass.config_entries.flow.async_configure( result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
@ -347,15 +237,12 @@ async def test_step_homekit_zeroconf_new_controller_when_some_exist(hass, source
CONF_SSL: True, CONF_SSL: True,
CONF_ZONE_RUN_TIME: 600, CONF_ZONE_RUN_TIME: 600,
} }
assert mock_setup_entry.called
async def test_discovery_by_homekit_and_zeroconf_same_time(hass): async def test_discovery_by_homekit_and_zeroconf_same_time(hass, client):
"""Test the same controller gets discovered by two different methods.""" """Test the same controller gets discovered by two different methods."""
with patch( with patch(
"homeassistant.components.rainmachine.config_flow.Client", "homeassistant.components.rainmachine.config_flow.Client", return_value=client
return_value=_get_mock_client(),
): ):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
@ -374,8 +261,7 @@ async def test_discovery_by_homekit_and_zeroconf_same_time(hass):
assert result["step_id"] == "user" assert result["step_id"] == "user"
with patch( with patch(
"homeassistant.components.rainmachine.config_flow.Client", "homeassistant.components.rainmachine.config_flow.Client", return_value=client
return_value=_get_mock_client(),
): ):
result2 = await hass.config_entries.flow.async_init( result2 = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,