mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 14:17:45 +00:00
Rewrite the kira/test_init.py unittests to pytest style test functions (#42753)
This commit is contained in:
parent
ce056656f8
commit
7cd17dd94f
@ -3,13 +3,13 @@
|
|||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
import homeassistant.components.kira as kira
|
import homeassistant.components.kira as kira
|
||||||
from homeassistant.setup import setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
from tests.async_mock import MagicMock, patch
|
from tests.async_mock import MagicMock, patch
|
||||||
from tests.common import get_test_home_assistant
|
|
||||||
|
|
||||||
TEST_CONFIG = {
|
TEST_CONFIG = {
|
||||||
kira.DOMAIN: {
|
kira.DOMAIN: {
|
||||||
@ -31,57 +31,58 @@ KIRA_CODES = """
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class TestKiraSetup(unittest.TestCase):
|
@pytest.fixture(autouse=True)
|
||||||
"""Test class for kira."""
|
def setup_comp():
|
||||||
|
"""Set up things to be run when tests are started."""
|
||||||
|
_base_mock = MagicMock()
|
||||||
|
pykira = _base_mock.pykira
|
||||||
|
pykira.__file__ = "test"
|
||||||
|
_module_patcher = patch.dict("sys.modules", {"pykira": pykira})
|
||||||
|
_module_patcher.start()
|
||||||
|
yield
|
||||||
|
_module_patcher.stop()
|
||||||
|
|
||||||
# pylint: disable=invalid-name
|
|
||||||
def setUp(self):
|
|
||||||
"""Set up things to be run when tests are started."""
|
|
||||||
self.hass = get_test_home_assistant()
|
|
||||||
_base_mock = MagicMock()
|
|
||||||
pykira = _base_mock.pykira
|
|
||||||
pykira.__file__ = "test"
|
|
||||||
self._module_patcher = patch.dict("sys.modules", {"pykira": pykira})
|
|
||||||
self._module_patcher.start()
|
|
||||||
|
|
||||||
self.work_dir = tempfile.mkdtemp()
|
@pytest.fixture(scope="module")
|
||||||
self.addCleanup(self.tear_down_cleanup)
|
def work_dir():
|
||||||
|
"""Set up temporary workdir."""
|
||||||
|
work_dir = tempfile.mkdtemp()
|
||||||
|
yield work_dir
|
||||||
|
shutil.rmtree(work_dir, ignore_errors=True)
|
||||||
|
|
||||||
def tear_down_cleanup(self):
|
|
||||||
"""Stop everything that was started."""
|
|
||||||
self.hass.stop()
|
|
||||||
self._module_patcher.stop()
|
|
||||||
shutil.rmtree(self.work_dir, ignore_errors=True)
|
|
||||||
|
|
||||||
def test_kira_empty_config(self):
|
async def test_kira_empty_config(hass):
|
||||||
"""Kira component should load a default sensor."""
|
"""Kira component should load a default sensor."""
|
||||||
setup_component(self.hass, kira.DOMAIN, {})
|
await async_setup_component(hass, kira.DOMAIN, {kira.DOMAIN: {}})
|
||||||
assert len(self.hass.data[kira.DOMAIN]["sensor"]) == 1
|
assert len(hass.data[kira.DOMAIN]["sensor"]) == 1
|
||||||
|
|
||||||
def test_kira_setup(self):
|
|
||||||
"""Ensure platforms are loaded correctly."""
|
|
||||||
setup_component(self.hass, kira.DOMAIN, TEST_CONFIG)
|
|
||||||
assert len(self.hass.data[kira.DOMAIN]["sensor"]) == 2
|
|
||||||
assert sorted(self.hass.data[kira.DOMAIN]["sensor"].keys()) == [
|
|
||||||
"kira",
|
|
||||||
"kira_1",
|
|
||||||
]
|
|
||||||
assert len(self.hass.data[kira.DOMAIN]["remote"]) == 2
|
|
||||||
assert sorted(self.hass.data[kira.DOMAIN]["remote"].keys()) == [
|
|
||||||
"kira",
|
|
||||||
"kira_1",
|
|
||||||
]
|
|
||||||
|
|
||||||
def test_kira_creates_codes(self):
|
async def test_kira_setup(hass):
|
||||||
"""Kira module should create codes file if missing."""
|
"""Ensure platforms are loaded correctly."""
|
||||||
code_path = os.path.join(self.work_dir, "codes.yaml")
|
await async_setup_component(hass, kira.DOMAIN, TEST_CONFIG)
|
||||||
kira.load_codes(code_path)
|
assert len(hass.data[kira.DOMAIN]["sensor"]) == 2
|
||||||
assert os.path.exists(code_path), "Kira component didn't create codes file"
|
assert sorted(hass.data[kira.DOMAIN]["sensor"].keys()) == [
|
||||||
|
"kira",
|
||||||
|
"kira_1",
|
||||||
|
]
|
||||||
|
assert len(hass.data[kira.DOMAIN]["remote"]) == 2
|
||||||
|
assert sorted(hass.data[kira.DOMAIN]["remote"].keys()) == [
|
||||||
|
"kira",
|
||||||
|
"kira_1",
|
||||||
|
]
|
||||||
|
|
||||||
def test_load_codes(self):
|
|
||||||
"""Kira should ignore invalid codes."""
|
async def test_kira_creates_codes(work_dir):
|
||||||
code_path = os.path.join(self.work_dir, "codes.yaml")
|
"""Kira module should create codes file if missing."""
|
||||||
with open(code_path, "w") as code_file:
|
code_path = os.path.join(work_dir, "codes.yaml")
|
||||||
code_file.write(KIRA_CODES)
|
kira.load_codes(code_path)
|
||||||
res = kira.load_codes(code_path)
|
assert os.path.exists(code_path), "Kira component didn't create codes file"
|
||||||
assert len(res) == 1, "Expected exactly 1 valid Kira code"
|
|
||||||
|
|
||||||
|
async def test_load_codes(work_dir):
|
||||||
|
"""Kira should ignore invalid codes."""
|
||||||
|
code_path = os.path.join(work_dir, "codes.yaml")
|
||||||
|
with open(code_path, "w") as code_file:
|
||||||
|
code_file.write(KIRA_CODES)
|
||||||
|
res = kira.load_codes(code_path)
|
||||||
|
assert len(res) == 1, "Expected exactly 1 valid Kira code"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user