Rewrite folder unittest tests to pytest style test functions (#41672)
This commit is contained in:
parent
0f2dfeb33f
commit
30c20f362e
1 changed files with 27 additions and 33 deletions
|
@ -1,11 +1,8 @@
|
||||||
"""The tests for the folder sensor."""
|
"""The tests for the folder sensor."""
|
||||||
import os
|
import os
|
||||||
import unittest
|
|
||||||
|
|
||||||
from homeassistant.components.folder.sensor import CONF_FOLDER_PATHS
|
from homeassistant.components.folder.sensor import CONF_FOLDER_PATHS
|
||||||
from homeassistant.setup import setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
from tests.common import get_test_home_assistant
|
|
||||||
|
|
||||||
CWD = os.path.join(os.path.dirname(__file__))
|
CWD = os.path.join(os.path.dirname(__file__))
|
||||||
TEST_FOLDER = "test_folder"
|
TEST_FOLDER = "test_folder"
|
||||||
|
@ -20,36 +17,33 @@ def create_file(path):
|
||||||
test_file.write("test")
|
test_file.write("test")
|
||||||
|
|
||||||
|
|
||||||
class TestFolderSensor(unittest.TestCase):
|
def remove_test_file():
|
||||||
"""Test the filesize sensor."""
|
"""Remove test file."""
|
||||||
|
|
||||||
def setup_method(self, method):
|
|
||||||
"""Set up things to be run when tests are started."""
|
|
||||||
self.hass = get_test_home_assistant()
|
|
||||||
if not os.path.isdir(TEST_DIR):
|
|
||||||
os.mkdir(TEST_DIR)
|
|
||||||
self.hass.config.allowlist_external_dirs = {TEST_DIR}
|
|
||||||
|
|
||||||
def teardown_method(self, method):
|
|
||||||
"""Stop everything that was started."""
|
|
||||||
if os.path.isfile(TEST_FILE):
|
if os.path.isfile(TEST_FILE):
|
||||||
os.remove(TEST_FILE)
|
os.remove(TEST_FILE)
|
||||||
os.rmdir(TEST_DIR)
|
os.rmdir(TEST_DIR)
|
||||||
self.hass.stop()
|
|
||||||
|
|
||||||
def test_invalid_path(self):
|
|
||||||
|
async def test_invalid_path(hass):
|
||||||
"""Test that an invalid path is caught."""
|
"""Test that an invalid path is caught."""
|
||||||
config = {"sensor": {"platform": "folder", CONF_FOLDER_PATHS: "invalid_path"}}
|
config = {"sensor": {"platform": "folder", CONF_FOLDER_PATHS: "invalid_path"}}
|
||||||
assert setup_component(self.hass, "sensor", config)
|
assert await async_setup_component(hass, "sensor", config)
|
||||||
assert len(self.hass.states.entity_ids()) == 0
|
assert len(hass.states.async_entity_ids()) == 0
|
||||||
|
|
||||||
def test_valid_path(self):
|
|
||||||
|
async def test_valid_path(hass):
|
||||||
"""Test for a valid path."""
|
"""Test for a valid path."""
|
||||||
|
if not os.path.isdir(TEST_DIR):
|
||||||
|
os.mkdir(TEST_DIR)
|
||||||
create_file(TEST_FILE)
|
create_file(TEST_FILE)
|
||||||
|
|
||||||
|
hass.config.allowlist_external_dirs = {TEST_DIR}
|
||||||
config = {"sensor": {"platform": "folder", CONF_FOLDER_PATHS: TEST_DIR}}
|
config = {"sensor": {"platform": "folder", CONF_FOLDER_PATHS: TEST_DIR}}
|
||||||
assert setup_component(self.hass, "sensor", config)
|
assert await async_setup_component(hass, "sensor", config)
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert len(self.hass.states.entity_ids()) == 1
|
assert len(hass.states.async_entity_ids()) == 1
|
||||||
state = self.hass.states.get("sensor.test_folder")
|
state = hass.states.get("sensor.test_folder")
|
||||||
assert state.state == "0.0"
|
assert state.state == "0.0"
|
||||||
assert state.attributes.get("number_of_files") == 1
|
assert state.attributes.get("number_of_files") == 1
|
||||||
|
|
||||||
|
remove_test_file()
|
||||||
|
|
Loading…
Add table
Reference in a new issue