Files
homeassistant-core/tests/components/folder_watcher/test_init.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

97 lines
3.1 KiB
Python
Raw Normal View History

2018-03-30 02:10:20 +01:00
"""The tests for the folder_watcher component."""
2018-03-30 02:10:20 +01:00
import os
from types import SimpleNamespace
2021-01-01 22:31:56 +01:00
from unittest.mock import Mock, patch
2018-03-30 02:10:20 +01:00
from homeassistant.components import folder_watcher
from homeassistant.core import HomeAssistant
2018-04-01 08:30:14 -07:00
from homeassistant.setup import async_setup_component
2018-03-30 02:10:20 +01:00
async def test_invalid_path_setup(hass: HomeAssistant) -> None:
2018-08-19 22:29:08 +02:00
"""Test that an invalid path is not set up."""
2018-04-01 08:30:14 -07:00
assert not await async_setup_component(
hass,
folder_watcher.DOMAIN,
{folder_watcher.DOMAIN: {folder_watcher.CONF_FOLDER: "invalid_path"}},
)
2018-03-30 02:10:20 +01:00
async def test_valid_path_setup(hass: HomeAssistant) -> None:
2018-04-01 08:30:14 -07:00
"""Test that a valid path is setup."""
cwd = os.path.join(os.path.dirname(__file__))
2020-07-13 08:43:11 -07:00
hass.config.allowlist_external_dirs = {cwd}
2018-04-01 08:30:14 -07:00
with patch.object(folder_watcher, "Watcher"):
assert await async_setup_component(
hass,
folder_watcher.DOMAIN,
{folder_watcher.DOMAIN: {folder_watcher.CONF_FOLDER: cwd}},
)
2018-03-30 02:10:20 +01:00
def test_event() -> None:
"""Check that Home Assistant events are fired correctly on watchdog event."""
2019-07-31 12:25:30 -07:00
2018-04-01 08:30:14 -07:00
class MockPatternMatchingEventHandler:
"""Mock base class for the pattern matcher event handler."""
2018-03-30 02:10:20 +01:00
2018-04-01 08:30:14 -07:00
def __init__(self, patterns):
pass
2018-03-30 02:10:20 +01:00
2020-04-30 16:31:00 -07:00
with patch(
"homeassistant.components.folder_watcher.PatternMatchingEventHandler",
MockPatternMatchingEventHandler,
):
hass = Mock()
handler = folder_watcher.create_event_handler(["*"], hass, "1")
2020-04-30 16:31:00 -07:00
handler.on_created(
SimpleNamespace(
is_directory=False, src_path="/hello/world.txt", event_type="created"
)
2020-04-30 16:31:00 -07:00
)
assert hass.bus.fire.called
assert hass.bus.fire.mock_calls[0][1][0] == folder_watcher.DOMAIN
assert hass.bus.fire.mock_calls[0][1][1] == {
"event_type": "created",
"path": "/hello/world.txt",
"file": "world.txt",
"folder": "/hello",
}
def test_move_event() -> None:
"""Check that Home Assistant events are fired correctly on watchdog event."""
class MockPatternMatchingEventHandler:
"""Mock base class for the pattern matcher event handler."""
def __init__(self, patterns):
pass
with patch(
"homeassistant.components.folder_watcher.PatternMatchingEventHandler",
MockPatternMatchingEventHandler,
):
hass = Mock()
handler = folder_watcher.create_event_handler(["*"], hass, "1")
handler.on_moved(
SimpleNamespace(
is_directory=False,
src_path="/hello/world.txt",
dest_path="/hello/earth.txt",
event_type="moved",
)
)
assert hass.bus.fire.called
assert hass.bus.fire.mock_calls[0][1][0] == folder_watcher.DOMAIN
assert hass.bus.fire.mock_calls[0][1][1] == {
"event_type": "moved",
"path": "/hello/world.txt",
"dest_path": "/hello/earth.txt",
"file": "world.txt",
"dest_file": "earth.txt",
"folder": "/hello",
"dest_folder": "/hello",
}