mirror of https://github.com/home-assistant/core
Unflake folder watcher test (#13569)
* Unflake folder watcher test * Fix tests * Lint
This commit is contained in:
parent
0c0e0c36af
commit
ff9f500c51
|
@ -202,8 +202,5 @@ wakeonlan==1.0.0
|
|||
# homeassistant.components.cloud
|
||||
warrant==0.6.1
|
||||
|
||||
# homeassistant.components.folder_watcher
|
||||
watchdog==0.8.3
|
||||
|
||||
# homeassistant.components.sensor.yahoo_finance
|
||||
yahoo-finance==1.4.0
|
||||
|
|
|
@ -91,7 +91,6 @@ TEST_REQUIREMENTS = (
|
|||
'yahoo-finance',
|
||||
'pythonwhois',
|
||||
'wakeonlan',
|
||||
'watchdog',
|
||||
'vultr'
|
||||
)
|
||||
|
||||
|
|
|
@ -1,64 +1,56 @@
|
|||
"""The tests for the folder_watcher component."""
|
||||
import unittest
|
||||
from unittest.mock import MagicMock
|
||||
from unittest.mock import Mock, patch
|
||||
import os
|
||||
|
||||
from homeassistant.components import folder_watcher
|
||||
from homeassistant.setup import setup_component
|
||||
from tests.common import get_test_home_assistant
|
||||
|
||||
CWD = os.path.join(os.path.dirname(__file__))
|
||||
FILE = 'file.txt'
|
||||
from homeassistant.setup import async_setup_component
|
||||
from tests.common import MockDependency
|
||||
|
||||
|
||||
class TestFolderWatcher(unittest.TestCase):
|
||||
"""Test the file_watcher component."""
|
||||
async def test_invalid_path_setup(hass):
|
||||
"""Test that a invalid path is not setup."""
|
||||
assert not await async_setup_component(
|
||||
hass, folder_watcher.DOMAIN, {
|
||||
folder_watcher.DOMAIN: {
|
||||
folder_watcher.CONF_FOLDER: 'invalid_path'
|
||||
}
|
||||
})
|
||||
|
||||
def setup_method(self, method):
|
||||
"""Set up things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
self.hass.config.whitelist_external_dirs = set((CWD))
|
||||
|
||||
def teardown_method(self, method):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
async def test_valid_path_setup(hass):
|
||||
"""Test that a valid path is setup."""
|
||||
cwd = os.path.join(os.path.dirname(__file__))
|
||||
hass.config.whitelist_external_dirs = set((cwd))
|
||||
with patch.object(folder_watcher, 'Watcher'):
|
||||
assert await async_setup_component(
|
||||
hass, folder_watcher.DOMAIN, {
|
||||
folder_watcher.DOMAIN: {folder_watcher.CONF_FOLDER: cwd}
|
||||
})
|
||||
|
||||
def test_invalid_path_setup(self):
|
||||
"""Test that a invalid path is not setup."""
|
||||
config = {
|
||||
folder_watcher.DOMAIN: [{
|
||||
folder_watcher.CONF_FOLDER: 'invalid_path'
|
||||
}]
|
||||
}
|
||||
self.assertFalse(
|
||||
setup_component(self.hass, folder_watcher.DOMAIN, config))
|
||||
|
||||
def test_valid_path_setup(self):
|
||||
"""Test that a valid path is setup."""
|
||||
config = {
|
||||
folder_watcher.DOMAIN: [{folder_watcher.CONF_FOLDER: CWD}]
|
||||
}
|
||||
@MockDependency('watchdog', 'events')
|
||||
def test_event(mock_watchdog):
|
||||
"""Check that HASS events are fired correctly on watchdog event."""
|
||||
class MockPatternMatchingEventHandler:
|
||||
"""Mock base class for the pattern matcher event handler."""
|
||||
|
||||
self.assertTrue(setup_component(
|
||||
self.hass, folder_watcher.DOMAIN, config))
|
||||
def __init__(self, patterns):
|
||||
pass
|
||||
|
||||
def test_event(self):
|
||||
"""Check that HASS events are fired correctly on watchdog event."""
|
||||
from watchdog.events import FileModifiedEvent
|
||||
|
||||
# Cant use setup_component as need to retrieve Watcher object.
|
||||
w = folder_watcher.Watcher(CWD,
|
||||
folder_watcher.DEFAULT_PATTERN,
|
||||
self.hass)
|
||||
w.startup(None)
|
||||
|
||||
self.hass.bus.fire = MagicMock()
|
||||
|
||||
# Trigger a fake filesystem event through the Watcher Observer emitter.
|
||||
(emitter,) = w._observer.emitters
|
||||
emitter.queue_event(FileModifiedEvent(FILE))
|
||||
|
||||
# Wait for the event to propagate.
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert self.hass.bus.fire.called
|
||||
mock_watchdog.events.PatternMatchingEventHandler = \
|
||||
MockPatternMatchingEventHandler
|
||||
hass = Mock()
|
||||
handler = folder_watcher.create_event_handler(['*'], hass)
|
||||
handler.on_created(Mock(
|
||||
is_directory=False,
|
||||
src_path='/hello/world.txt',
|
||||
event_type='created'
|
||||
))
|
||||
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',
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue