core/tests/components/bring/test_init.py

90 lines
2.7 KiB
Python
Raw Normal View History

Add bring integration (#108027) * add bring integration * fix typings and remove from strictly typed - wait for python-bring-api to be ready for strictly typed * make entity unique to user and list - before it was only list, therefore the same list imported by two users would have failed * simplify bring attribute Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * cleanup and code simplification * remove empty fields in manifest * __init__.py aktualisieren Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * __init__.py aktualisieren Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * strings.json aktualisieren Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * streamline async calls * use coordinator refresh * fix order in update call and simplify bring list * simplify the config_flow * Update homeassistant/components/bring/manifest.json Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> * add unit testing for __init__.py * cleanup comments * use dict instead of list * Update homeassistant/components/bring/todo.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * clean up * update attribute name * update more attribute name * improve unit tests - remove patch and use mock in conftest * clean up tests even more * more unit test inprovements * remove optional type * minor unit test cleanup * Update .coveragerc Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com>
2024-01-29 14:08:11 +00:00
"""Unit tests for the bring integration."""
from unittest.mock import AsyncMock
Add bring integration (#108027) * add bring integration * fix typings and remove from strictly typed - wait for python-bring-api to be ready for strictly typed * make entity unique to user and list - before it was only list, therefore the same list imported by two users would have failed * simplify bring attribute Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * cleanup and code simplification * remove empty fields in manifest * __init__.py aktualisieren Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * __init__.py aktualisieren Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * strings.json aktualisieren Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * streamline async calls * use coordinator refresh * fix order in update call and simplify bring list * simplify the config_flow * Update homeassistant/components/bring/manifest.json Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> * add unit testing for __init__.py * cleanup comments * use dict instead of list * Update homeassistant/components/bring/todo.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * clean up * update attribute name * update more attribute name * improve unit tests - remove patch and use mock in conftest * clean up tests even more * more unit test inprovements * remove optional type * minor unit test cleanup * Update .coveragerc Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com>
2024-01-29 14:08:11 +00:00
import pytest
from homeassistant.components.bring import (
BringAuthException,
BringParseException,
BringRequestException,
async_setup_entry,
Add bring integration (#108027) * add bring integration * fix typings and remove from strictly typed - wait for python-bring-api to be ready for strictly typed * make entity unique to user and list - before it was only list, therefore the same list imported by two users would have failed * simplify bring attribute Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * cleanup and code simplification * remove empty fields in manifest * __init__.py aktualisieren Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * __init__.py aktualisieren Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * strings.json aktualisieren Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * streamline async calls * use coordinator refresh * fix order in update call and simplify bring list * simplify the config_flow * Update homeassistant/components/bring/manifest.json Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> * add unit testing for __init__.py * cleanup comments * use dict instead of list * Update homeassistant/components/bring/todo.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * clean up * update attribute name * update more attribute name * improve unit tests - remove patch and use mock in conftest * clean up tests even more * more unit test inprovements * remove optional type * minor unit test cleanup * Update .coveragerc Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com>
2024-01-29 14:08:11 +00:00
)
from homeassistant.components.bring.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryError, ConfigEntryNotReady
Add bring integration (#108027) * add bring integration * fix typings and remove from strictly typed - wait for python-bring-api to be ready for strictly typed * make entity unique to user and list - before it was only list, therefore the same list imported by two users would have failed * simplify bring attribute Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * cleanup and code simplification * remove empty fields in manifest * __init__.py aktualisieren Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * __init__.py aktualisieren Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * strings.json aktualisieren Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * streamline async calls * use coordinator refresh * fix order in update call and simplify bring list * simplify the config_flow * Update homeassistant/components/bring/manifest.json Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> * add unit testing for __init__.py * cleanup comments * use dict instead of list * Update homeassistant/components/bring/todo.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * clean up * update attribute name * update more attribute name * improve unit tests - remove patch and use mock in conftest * clean up tests even more * more unit test inprovements * remove optional type * minor unit test cleanup * Update .coveragerc Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com>
2024-01-29 14:08:11 +00:00
from tests.common import MockConfigEntry
async def setup_integration(
hass: HomeAssistant,
bring_config_entry: MockConfigEntry,
) -> None:
"""Mock setup of the bring integration."""
bring_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(bring_config_entry.entry_id)
await hass.async_block_till_done()
async def test_load_unload(
hass: HomeAssistant,
mock_bring_client: AsyncMock,
Add bring integration (#108027) * add bring integration * fix typings and remove from strictly typed - wait for python-bring-api to be ready for strictly typed * make entity unique to user and list - before it was only list, therefore the same list imported by two users would have failed * simplify bring attribute Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * cleanup and code simplification * remove empty fields in manifest * __init__.py aktualisieren Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * __init__.py aktualisieren Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * strings.json aktualisieren Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * streamline async calls * use coordinator refresh * fix order in update call and simplify bring list * simplify the config_flow * Update homeassistant/components/bring/manifest.json Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> * add unit testing for __init__.py * cleanup comments * use dict instead of list * Update homeassistant/components/bring/todo.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * clean up * update attribute name * update more attribute name * improve unit tests - remove patch and use mock in conftest * clean up tests even more * more unit test inprovements * remove optional type * minor unit test cleanup * Update .coveragerc Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com>
2024-01-29 14:08:11 +00:00
bring_config_entry: MockConfigEntry,
) -> None:
"""Test loading and unloading of the config entry."""
await setup_integration(hass, bring_config_entry)
entries = hass.config_entries.async_entries(DOMAIN)
assert len(entries) == 1
assert bring_config_entry.state is ConfigEntryState.LOADED
Add bring integration (#108027) * add bring integration * fix typings and remove from strictly typed - wait for python-bring-api to be ready for strictly typed * make entity unique to user and list - before it was only list, therefore the same list imported by two users would have failed * simplify bring attribute Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * cleanup and code simplification * remove empty fields in manifest * __init__.py aktualisieren Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * __init__.py aktualisieren Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * strings.json aktualisieren Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * streamline async calls * use coordinator refresh * fix order in update call and simplify bring list * simplify the config_flow * Update homeassistant/components/bring/manifest.json Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> * add unit testing for __init__.py * cleanup comments * use dict instead of list * Update homeassistant/components/bring/todo.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * clean up * update attribute name * update more attribute name * improve unit tests - remove patch and use mock in conftest * clean up tests even more * more unit test inprovements * remove optional type * minor unit test cleanup * Update .coveragerc Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com>
2024-01-29 14:08:11 +00:00
assert await hass.config_entries.async_unload(bring_config_entry.entry_id)
assert bring_config_entry.state is ConfigEntryState.NOT_LOADED
Add bring integration (#108027) * add bring integration * fix typings and remove from strictly typed - wait for python-bring-api to be ready for strictly typed * make entity unique to user and list - before it was only list, therefore the same list imported by two users would have failed * simplify bring attribute Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * cleanup and code simplification * remove empty fields in manifest * __init__.py aktualisieren Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * __init__.py aktualisieren Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * strings.json aktualisieren Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * streamline async calls * use coordinator refresh * fix order in update call and simplify bring list * simplify the config_flow * Update homeassistant/components/bring/manifest.json Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> * add unit testing for __init__.py * cleanup comments * use dict instead of list * Update homeassistant/components/bring/todo.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * clean up * update attribute name * update more attribute name * improve unit tests - remove patch and use mock in conftest * clean up tests even more * more unit test inprovements * remove optional type * minor unit test cleanup * Update .coveragerc Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com>
2024-01-29 14:08:11 +00:00
@pytest.mark.parametrize(
("exception", "status"),
[
(BringRequestException, ConfigEntryState.SETUP_RETRY),
(BringAuthException, ConfigEntryState.SETUP_ERROR),
(BringParseException, ConfigEntryState.SETUP_RETRY),
],
)
async def test_init_failure(
hass: HomeAssistant,
mock_bring_client: AsyncMock,
Add bring integration (#108027) * add bring integration * fix typings and remove from strictly typed - wait for python-bring-api to be ready for strictly typed * make entity unique to user and list - before it was only list, therefore the same list imported by two users would have failed * simplify bring attribute Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * cleanup and code simplification * remove empty fields in manifest * __init__.py aktualisieren Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * __init__.py aktualisieren Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * strings.json aktualisieren Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * streamline async calls * use coordinator refresh * fix order in update call and simplify bring list * simplify the config_flow * Update homeassistant/components/bring/manifest.json Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> * add unit testing for __init__.py * cleanup comments * use dict instead of list * Update homeassistant/components/bring/todo.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * clean up * update attribute name * update more attribute name * improve unit tests - remove patch and use mock in conftest * clean up tests even more * more unit test inprovements * remove optional type * minor unit test cleanup * Update .coveragerc Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com>
2024-01-29 14:08:11 +00:00
status: ConfigEntryState,
exception: Exception,
bring_config_entry: MockConfigEntry | None,
) -> None:
"""Test an initialization error on integration load."""
mock_bring_client.login.side_effect = exception
Add bring integration (#108027) * add bring integration * fix typings and remove from strictly typed - wait for python-bring-api to be ready for strictly typed * make entity unique to user and list - before it was only list, therefore the same list imported by two users would have failed * simplify bring attribute Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * cleanup and code simplification * remove empty fields in manifest * __init__.py aktualisieren Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * __init__.py aktualisieren Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * strings.json aktualisieren Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * streamline async calls * use coordinator refresh * fix order in update call and simplify bring list * simplify the config_flow * Update homeassistant/components/bring/manifest.json Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> * add unit testing for __init__.py * cleanup comments * use dict instead of list * Update homeassistant/components/bring/todo.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * clean up * update attribute name * update more attribute name * improve unit tests - remove patch and use mock in conftest * clean up tests even more * more unit test inprovements * remove optional type * minor unit test cleanup * Update .coveragerc Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com>
2024-01-29 14:08:11 +00:00
await setup_integration(hass, bring_config_entry)
assert bring_config_entry.state == status
@pytest.mark.parametrize(
("exception", "expected"),
[
(BringRequestException, ConfigEntryNotReady),
(BringAuthException, ConfigEntryError),
(BringParseException, ConfigEntryNotReady),
],
)
async def test_init_exceptions(
hass: HomeAssistant,
mock_bring_client: AsyncMock,
exception: Exception,
expected: Exception,
bring_config_entry: MockConfigEntry | None,
) -> None:
"""Test an initialization error on integration load."""
bring_config_entry.add_to_hass(hass)
mock_bring_client.login.side_effect = exception
with pytest.raises(expected):
await async_setup_entry(hass, bring_config_entry)