From 9fb0812ce5bdfb3dd4f47656f3406ba0bc2cae69 Mon Sep 17 00:00:00 2001 From: Santobert Date: Sun, 13 Oct 2019 22:56:34 +0200 Subject: [PATCH] Improve neato tests (#27578) * Improve tests * Rename account to configflow * configflow to config_flow * Patch pybotvac instead of own code --- homeassistant/components/neato/__init__.py | 13 ++--- tests/components/neato/test_init.py | 59 ++++++++++++++++++++-- 2 files changed, 61 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/neato/__init__.py b/homeassistant/components/neato/__init__.py index 839c24568d8..ddf9789f678 100644 --- a/homeassistant/components/neato/__init__.py +++ b/homeassistant/components/neato/__init__.py @@ -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 diff --git a/tests/components/neato/test_init.py b/tests/components/neato/test_init.py index 361f9eab1db..444cbe8cc5d 100644 --- a/tests/components/neato/test_init.py +++ b/tests/components/neato/test_init.py @@ -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