mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 04:37:06 +00:00
Rewrite filesize unittest tests to pytest style test functions (#41421)
This commit is contained in:
parent
a57d1c1bf0
commit
6b409dc811
@ -1,15 +1,15 @@
|
|||||||
"""The tests for the filesize sensor."""
|
"""The tests for the filesize sensor."""
|
||||||
import os
|
import os
|
||||||
import unittest
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
from homeassistant import config as hass_config
|
from homeassistant import config as hass_config
|
||||||
from homeassistant.components.filesize import DOMAIN
|
from homeassistant.components.filesize import DOMAIN
|
||||||
from homeassistant.components.filesize.sensor import CONF_FILE_PATHS
|
from homeassistant.components.filesize.sensor import CONF_FILE_PATHS
|
||||||
from homeassistant.const import SERVICE_RELOAD
|
from homeassistant.const import SERVICE_RELOAD
|
||||||
from homeassistant.setup import async_setup_component, setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
from tests.async_mock import patch
|
from tests.async_mock import patch
|
||||||
from tests.common import get_test_home_assistant
|
|
||||||
|
|
||||||
TEST_DIR = os.path.join(os.path.dirname(__file__))
|
TEST_DIR = os.path.join(os.path.dirname(__file__))
|
||||||
TEST_FILE = os.path.join(TEST_DIR, "mock_file_test_filesize.txt")
|
TEST_FILE = os.path.join(TEST_DIR, "mock_file_test_filesize.txt")
|
||||||
@ -21,40 +21,36 @@ def create_file(path):
|
|||||||
test_file.write("test")
|
test_file.write("test")
|
||||||
|
|
||||||
|
|
||||||
class TestFileSensor(unittest.TestCase):
|
@pytest.fixture(autouse=True)
|
||||||
"""Test the filesize sensor."""
|
def remove_file():
|
||||||
|
"""Remove test file."""
|
||||||
|
if os.path.isfile(TEST_FILE):
|
||||||
|
os.remove(TEST_FILE)
|
||||||
|
|
||||||
def setup_method(self, method):
|
|
||||||
"""Set up things to be run when tests are started."""
|
|
||||||
self.hass = get_test_home_assistant()
|
|
||||||
self.hass.config.allowlist_external_dirs = {TEST_DIR}
|
|
||||||
|
|
||||||
def teardown_method(self, method):
|
async def test_invalid_path(hass):
|
||||||
"""Stop everything that was started."""
|
"""Test that an invalid path is caught."""
|
||||||
if os.path.isfile(TEST_FILE):
|
config = {"sensor": {"platform": "filesize", CONF_FILE_PATHS: ["invalid_path"]}}
|
||||||
os.remove(TEST_FILE)
|
assert await async_setup_component(hass, "sensor", config)
|
||||||
self.hass.stop()
|
await hass.async_block_till_done()
|
||||||
|
assert len(hass.states.async_entity_ids()) == 0
|
||||||
|
|
||||||
def test_invalid_path(self):
|
|
||||||
"""Test that an invalid path is caught."""
|
|
||||||
config = {"sensor": {"platform": "filesize", CONF_FILE_PATHS: ["invalid_path"]}}
|
|
||||||
assert setup_component(self.hass, "sensor", config)
|
|
||||||
assert len(self.hass.states.entity_ids()) == 0
|
|
||||||
|
|
||||||
def test_valid_path(self):
|
async def test_valid_path(hass):
|
||||||
"""Test for a valid path."""
|
"""Test for a valid path."""
|
||||||
create_file(TEST_FILE)
|
create_file(TEST_FILE)
|
||||||
config = {"sensor": {"platform": "filesize", CONF_FILE_PATHS: [TEST_FILE]}}
|
config = {"sensor": {"platform": "filesize", CONF_FILE_PATHS: [TEST_FILE]}}
|
||||||
assert setup_component(self.hass, "sensor", config)
|
hass.config.allowlist_external_dirs = {TEST_DIR}
|
||||||
self.hass.block_till_done()
|
assert await async_setup_component(hass, "sensor", config)
|
||||||
assert len(self.hass.states.entity_ids()) == 1
|
await hass.async_block_till_done()
|
||||||
state = self.hass.states.get("sensor.mock_file_test_filesize_txt")
|
assert len(hass.states.async_entity_ids()) == 1
|
||||||
assert state.state == "0.0"
|
state = hass.states.get("sensor.mock_file_test_filesize_txt")
|
||||||
assert state.attributes.get("bytes") == 4
|
assert state.state == "0.0"
|
||||||
|
assert state.attributes.get("bytes") == 4
|
||||||
|
|
||||||
|
|
||||||
async def test_reload(hass, tmpdir):
|
async def test_reload(hass, tmpdir):
|
||||||
"""Verify we can reload filter sensors."""
|
"""Verify we can reload filesize sensors."""
|
||||||
testfile = f"{tmpdir}/file"
|
testfile = f"{tmpdir}/file"
|
||||||
await hass.async_add_executor_job(create_file, testfile)
|
await hass.async_add_executor_job(create_file, testfile)
|
||||||
with patch.object(hass.config, "is_allowed_path", return_value=True):
|
with patch.object(hass.config, "is_allowed_path", return_value=True):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user