Improve neato tests (#27578)

* Improve tests

* Rename account to configflow

* configflow to config_flow

* Patch pybotvac instead of own code
This commit is contained in:
Santobert 2019-10-13 22:56:34 +02:00 committed by Paulus Schoutsen
parent 0235487a22
commit 9fb0812ce5
2 changed files with 61 additions and 11 deletions

View file

@ -92,10 +92,7 @@ async def async_setup(hass, config):
async def async_setup_entry(hass, entry):
"""Set up config entry."""
if entry.data[CONF_VENDOR] == "neato":
hass.data[NEATO_LOGIN] = NeatoHub(hass, entry.data, Account, Neato)
elif entry.data[CONF_VENDOR] == "vorwerk":
hass.data[NEATO_LOGIN] = NeatoHub(hass, entry.data, Account, Vorwerk)
hass.data[NEATO_LOGIN] = NeatoHub(hass, entry.data, Account)
hub = hass.data[NEATO_LOGIN]
await hass.async_add_executor_job(hub.login)
@ -132,12 +129,16 @@ async def async_unload_entry(hass, entry):
class NeatoHub:
"""A My Neato hub wrapper class."""
def __init__(self, hass, domain_config, neato, vendor):
def __init__(self, hass, domain_config, neato):
"""Initialize the Neato hub."""
self.config = domain_config
self._neato = neato
self._hass = hass
self._vendor = vendor
if self.config[CONF_VENDOR] == "vorwerk":
self._vendor = Vorwerk()
else: # Neato
self._vendor = Neato()
self.my_neato = None
self.logged_in = False

View file

@ -2,6 +2,8 @@
import pytest
from unittest.mock import patch
from pybotvac.exceptions import NeatoLoginException
from homeassistant.components.neato.const import CONF_VENDOR, NEATO_DOMAIN
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.setup import async_setup_component
@ -20,6 +22,12 @@ VALID_CONFIG = {
CONF_VENDOR: VENDOR_NEATO,
}
DIFFERENT_CONFIG = {
CONF_USERNAME: "anotherUsername",
CONF_PASSWORD: "anotherPassword",
CONF_VENDOR: VENDOR_VORWERK,
}
INVALID_CONFIG = {
CONF_USERNAME: USERNAME,
CONF_PASSWORD: PASSWORD,
@ -27,20 +35,40 @@ INVALID_CONFIG = {
}
@pytest.fixture(name="account")
def mock_controller_login():
@pytest.fixture(name="config_flow")
def mock_config_flow_login():
"""Mock a successful login."""
with patch("homeassistant.components.neato.config_flow.Account", return_value=True):
yield
@pytest.fixture(name="hub")
def mock_controller_login():
"""Mock a successful login."""
with patch("homeassistant.components.neato.Account", return_value=True):
yield
async def test_no_config_entry(hass):
"""There is nothing in configuration.yaml."""
res = await async_setup_component(hass, NEATO_DOMAIN, {})
assert res is True
async def test_config_entries_in_sync(hass, account):
async def test_create_valid_config_entry(hass, config_flow, hub):
"""There is something in configuration.yaml."""
assert hass.config_entries.async_entries(NEATO_DOMAIN) == []
assert await async_setup_component(hass, NEATO_DOMAIN, {NEATO_DOMAIN: VALID_CONFIG})
await hass.async_block_till_done()
entries = hass.config_entries.async_entries(NEATO_DOMAIN)
assert entries
assert entries[0].data[CONF_USERNAME] == USERNAME
assert entries[0].data[CONF_PASSWORD] == PASSWORD
assert entries[0].data[CONF_VENDOR] == VENDOR_NEATO
async def test_config_entries_in_sync(hass, hub):
"""The config entry and configuration.yaml are in sync."""
MockConfigEntry(domain=NEATO_DOMAIN, data=VALID_CONFIG).add_to_hass(hass)
@ -55,9 +83,9 @@ async def test_config_entries_in_sync(hass, account):
assert entries[0].data[CONF_VENDOR] == VENDOR_NEATO
async def test_config_entries_not_in_sync(hass, account):
async def test_config_entries_not_in_sync(hass, config_flow, hub):
"""The config entry and configuration.yaml are not in sync."""
MockConfigEntry(domain=NEATO_DOMAIN, data=INVALID_CONFIG).add_to_hass(hass)
MockConfigEntry(domain=NEATO_DOMAIN, data=DIFFERENT_CONFIG).add_to_hass(hass)
assert hass.config_entries.async_entries(NEATO_DOMAIN)
assert await async_setup_component(hass, NEATO_DOMAIN, {NEATO_DOMAIN: VALID_CONFIG})
@ -68,3 +96,24 @@ async def test_config_entries_not_in_sync(hass, account):
assert entries[0].data[CONF_USERNAME] == USERNAME
assert entries[0].data[CONF_PASSWORD] == PASSWORD
assert entries[0].data[CONF_VENDOR] == VENDOR_NEATO
async def test_config_entries_not_in_sync_error(hass):
"""The config entry and configuration.yaml are not in sync, the new configuration is wrong."""
MockConfigEntry(domain=NEATO_DOMAIN, data=VALID_CONFIG).add_to_hass(hass)
assert hass.config_entries.async_entries(NEATO_DOMAIN)
with patch(
"homeassistant.components.neato.config_flow.Account",
side_effect=NeatoLoginException(),
):
assert not await async_setup_component(
hass, NEATO_DOMAIN, {NEATO_DOMAIN: DIFFERENT_CONFIG}
)
await hass.async_block_till_done()
entries = hass.config_entries.async_entries(NEATO_DOMAIN)
assert entries
assert entries[0].data[CONF_USERNAME] == USERNAME
assert entries[0].data[CONF_PASSWORD] == PASSWORD
assert entries[0].data[CONF_VENDOR] == VENDOR_NEATO