Rewrite the kira/test_init.py unittests to pytest style test functions (#42753)
parent
ce056656f8
commit
7cd17dd94f
|
@ -3,13 +3,13 @@
|
|||
import os
|
||||
import shutil
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
import pytest
|
||||
|
||||
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.common import get_test_home_assistant
|
||||
|
||||
TEST_CONFIG = {
|
||||
kira.DOMAIN: {
|
||||
|
@ -31,57 +31,58 @@ KIRA_CODES = """
|
|||
"""
|
||||
|
||||
|
||||
class TestKiraSetup(unittest.TestCase):
|
||||
"""Test class for kira."""
|
||||
@pytest.fixture(autouse=True)
|
||||
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()
|
||||
self.addCleanup(self.tear_down_cleanup)
|
||||
@pytest.fixture(scope="module")
|
||||
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):
|
||||
"""Kira component should load a default sensor."""
|
||||
setup_component(self.hass, kira.DOMAIN, {})
|
||||
assert len(self.hass.data[kira.DOMAIN]["sensor"]) == 1
|
||||
async def test_kira_empty_config(hass):
|
||||
"""Kira component should load a default sensor."""
|
||||
await async_setup_component(hass, kira.DOMAIN, {kira.DOMAIN: {}})
|
||||
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):
|
||||
"""Kira module should create codes file if missing."""
|
||||
code_path = os.path.join(self.work_dir, "codes.yaml")
|
||||
kira.load_codes(code_path)
|
||||
assert os.path.exists(code_path), "Kira component didn't create codes file"
|
||||
async def test_kira_setup(hass):
|
||||
"""Ensure platforms are loaded correctly."""
|
||||
await async_setup_component(hass, kira.DOMAIN, TEST_CONFIG)
|
||||
assert len(hass.data[kira.DOMAIN]["sensor"]) == 2
|
||||
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."""
|
||||
code_path = os.path.join(self.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"
|
||||
|
||||
async def test_kira_creates_codes(work_dir):
|
||||
"""Kira module should create codes file if missing."""
|
||||
code_path = os.path.join(work_dir, "codes.yaml")
|
||||
kira.load_codes(code_path)
|
||||
assert os.path.exists(code_path), "Kira component didn't create codes file"
|
||||
|
||||
|
||||
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…
Reference in New Issue