This commit is contained in:
Paulus Schoutsen committed 2019-07-31 12:25:30 -07:00
1 parent da05dfe708
commit 4de97abc3a
2676 files changed
+163166 -140084

No files matched your search

+47 -47
View File
@@ -31,9 +31,9 @@ class TestRecorder(unittest.TestCase):
def test_saving_state(self):
"""Test saving and restoring a state."""
entity_id = 'test.recorder'
state = 'restoring_from_db'
attributes = {'test_attr': 5, 'test_attr_10': 'nice'}
entity_id = "test.recorder"
state = "restoring_from_db"
attributes = {"test_attr": 5, "test_attr_10": "nice"}
self.hass.states.set(entity_id, state, attributes)
@@ -50,8 +50,8 @@ class TestRecorder(unittest.TestCase):
def test_saving_event(self):
"""Test saving and restoring an event."""
event_type = 'EVENT_TEST'
event_data = {'test_attr': 5, 'test_attr_10': 'nice'}
event_type = "EVENT_TEST"
event_data = {"test_attr": 5, "test_attr_10": "nice"}
events = []
@@ -73,8 +73,7 @@ class TestRecorder(unittest.TestCase):
self.hass.data[DATA_INSTANCE].block_till_done()
with session_scope(hass=self.hass) as session:
db_events = list(session.query(Events).filter_by(
event_type=event_type))
db_events = list(session.query(Events).filter_by(event_type=event_type))
assert len(db_events) == 1
db_event = db_events[0].to_native()
@@ -83,8 +82,9 @@ class TestRecorder(unittest.TestCase):
assert event.origin == db_event.origin
# Recorder uses SQLite and stores datetimes as integer unix timestamps
assert event.time_fired.replace(microsecond=0) == \
db_event.time_fired.replace(microsecond=0)
assert event.time_fired.replace(microsecond=0) == db_event.time_fired.replace(
microsecond=0
)
@pytest.fixture
@@ -106,9 +106,9 @@ def hass_recorder():
def _add_entities(hass, entity_ids):
"""Add entities."""
attributes = {'test_attr': 5, 'test_attr_10': 'nice'}
attributes = {"test_attr": 5, "test_attr_10": "nice"}
for idx, entity_id in enumerate(entity_ids):
hass.states.set(entity_id, 'state{}'.format(idx), attributes)
hass.states.set(entity_id, "state{}".format(idx), attributes)
hass.block_till_done()
hass.data[DATA_INSTANCE].block_till_done()
@@ -131,74 +131,75 @@ def _add_events(hass, events):
# pylint: disable=redefined-outer-name,invalid-name
def test_saving_state_include_domains(hass_recorder):
"""Test saving and restoring a state."""
hass = hass_recorder({'include': {'domains': 'test2'}})
states = _add_entities(hass, ['test.recorder', 'test2.recorder'])
hass = hass_recorder({"include": {"domains": "test2"}})
states = _add_entities(hass, ["test.recorder", "test2.recorder"])
assert len(states) == 1
assert hass.states.get('test2.recorder') == states[0]
assert hass.states.get("test2.recorder") == states[0]
def test_saving_state_incl_entities(hass_recorder):
"""Test saving and restoring a state."""
hass = hass_recorder({'include': {'entities': 'test2.recorder'}})
states = _add_entities(hass, ['test.recorder', 'test2.recorder'])
hass = hass_recorder({"include": {"entities": "test2.recorder"}})
states = _add_entities(hass, ["test.recorder", "test2.recorder"])
assert len(states) == 1
assert hass.states.get('test2.recorder') == states[0]
assert hass.states.get("test2.recorder") == states[0]
def test_saving_event_exclude_event_type(hass_recorder):
"""Test saving and restoring an event."""
hass = hass_recorder({'exclude': {'event_types': 'test'}})
events = _add_events(hass, ['test', 'test2'])
hass = hass_recorder({"exclude": {"event_types": "test"}})
events = _add_events(hass, ["test", "test2"])
assert len(events) == 1
assert events[0].event_type == 'test2'
assert events[0].event_type == "test2"
def test_saving_state_exclude_domains(hass_recorder):
"""Test saving and restoring a state."""
hass = hass_recorder({'exclude': {'domains': 'test'}})
states = _add_entities(hass, ['test.recorder', 'test2.recorder'])
hass = hass_recorder({"exclude": {"domains": "test"}})
states = _add_entities(hass, ["test.recorder", "test2.recorder"])
assert len(states) == 1
assert hass.states.get('test2.recorder') == states[0]
assert hass.states.get("test2.recorder") == states[0]
def test_saving_state_exclude_entities(hass_recorder):
"""Test saving and restoring a state."""
hass = hass_recorder({'exclude': {'entities': 'test.recorder'}})
states = _add_entities(hass, ['test.recorder', 'test2.recorder'])
hass = hass_recorder({"exclude": {"entities": "test.recorder"}})
states = _add_entities(hass, ["test.recorder", "test2.recorder"])
assert len(states) == 1
assert hass.states.get('test2.recorder') == states[0]
assert hass.states.get("test2.recorder") == states[0]
def test_saving_state_exclude_domain_include_entity(hass_recorder):
"""Test saving and restoring a state."""
hass = hass_recorder({
'include': {'entities': 'test.recorder'},
'exclude': {'domains': 'test'}})
states = _add_entities(hass, ['test.recorder', 'test2.recorder'])
hass = hass_recorder(
{"include": {"entities": "test.recorder"}, "exclude": {"domains": "test"}}
)
states = _add_entities(hass, ["test.recorder", "test2.recorder"])
assert len(states) == 2
def test_saving_state_include_domain_exclude_entity(hass_recorder):
"""Test saving and restoring a state."""
hass = hass_recorder({
'exclude': {'entities': 'test.recorder'},
'include': {'domains': 'test'}})
states = _add_entities(hass, ['test.recorder', 'test2.recorder',
'test.ok'])
hass = hass_recorder(
{"exclude": {"entities": "test.recorder"}, "include": {"domains": "test"}}
)
states = _add_entities(hass, ["test.recorder", "test2.recorder", "test.ok"])
assert len(states) == 1
assert hass.states.get('test.ok') == states[0]
assert hass.states.get('test.ok').state == 'state2'
assert hass.states.get("test.ok") == states[0]
assert hass.states.get("test.ok").state == "state2"
def test_recorder_setup_failure():
"""Test some exceptions."""
hass = get_test_home_assistant()
with patch.object(Recorder, '_setup_connection') as setup, \
patch('homeassistant.components.recorder.time.sleep'):
with patch.object(Recorder, "_setup_connection") as setup, patch(
"homeassistant.components.recorder.time.sleep"
):
setup.side_effect = ImportError("driver not found")
rec = Recorder(hass, keep_days=7, purge_interval=2,
uri='sqlite://', include={}, exclude={})
rec = Recorder(
hass, keep_days=7, purge_interval=2, uri="sqlite://", include={}, exclude={}
)
rec.start()
rec.join()
@@ -212,13 +213,12 @@ async def test_defaults_set(hass):
async def mock_setup(hass, config):
"""Mock setup."""
nonlocal recorder_config
recorder_config = config['recorder']
recorder_config = config["recorder"]
return True
with patch('homeassistant.components.recorder.async_setup',
side_effect=mock_setup):
assert await async_setup_component(hass, 'history', {})
with patch("homeassistant.components.recorder.async_setup", side_effect=mock_setup):
assert await async_setup_component(hass, "history", {})
assert recorder_config is not None
assert recorder_config['purge_keep_days'] == 10
assert recorder_config['purge_interval'] == 1
assert recorder_config["purge_keep_days"] == 10
assert recorder_config["purge_interval"] == 1