Update recorder. (#2549)

* Update recorder.

models.py:
 - Use scoped_session in models.py to fix shutdown error
__init__.py:
 - Session _commit & retry method
 - Single session var for purge_data
 - Ensure single _INSTANCE
 - repeat purge every 2 days
 - show correct time in log_error

* _commit

* Restore models to old functionality, swap purge, remove _INSTANCE cleanup from tests, typing ignore Base class

* pylint

* Remove recorder from model unit test
This commit is contained in:
Johann Kellerman authored and Paulus Schoutsen committed 2016-07-23 11:25:17 -07:00
1 parent d4f78e8552
commit 4cf618334c
4 files changed
+80 -65

No files matched your search

+21 -17
View File
@@ -1,5 +1,4 @@
"""The tests for the Recorder component."""
# pylint: disable=too-many-public-methods,protected-access
import unittest
from datetime import datetime
@@ -12,32 +11,35 @@ from homeassistant.util import dt
from homeassistant.components.recorder.models import (
Base, Events, States, RecorderRuns)
engine = None
Session = None
ENGINE = None
SESSION = None
def setUpModule():
def setUpModule(): # pylint: disable=invalid-name
"""Set up a database to use."""
global engine, Session
global ENGINE
global SESSION
engine = create_engine("sqlite://")
Base.metadata.create_all(engine)
session_factory = sessionmaker(bind=engine)
Session = scoped_session(session_factory)
ENGINE = create_engine("sqlite://")
Base.metadata.create_all(ENGINE)
session_factory = sessionmaker(bind=ENGINE)
SESSION = scoped_session(session_factory)
def tearDownModule():
def tearDownModule(): # pylint: disable=invalid-name
"""Close database."""
global engine, Session
global ENGINE
global SESSION
engine.dispose()
engine = None
Session = None
ENGINE.dispose()
ENGINE = None
SESSION = None
class TestEvents(unittest.TestCase):
"""Test Events model."""
# pylint: disable=no-self-use
def test_from_event(self):
"""Test converting event to db event."""
event = ha.Event('test_event', {
@@ -49,6 +51,8 @@ class TestEvents(unittest.TestCase):
class TestStates(unittest.TestCase):
"""Test States model."""
# pylint: disable=no-self-use
def test_from_event(self):
"""Test converting event to db state."""
state = ha.State('sensor.temperature', '18')
@@ -78,14 +82,14 @@ class TestStates(unittest.TestCase):
class TestRecorderRuns(unittest.TestCase):
"""Test recorder run model."""
def setUp(self):
def setUp(self): # pylint: disable=invalid-name
"""Set up recorder runs."""
self.session = session = Session()
self.session = session = SESSION()
session.query(Events).delete()
session.query(States).delete()
session.query(RecorderRuns).delete()
def tearDown(self):
def tearDown(self): # pylint: disable=invalid-name
"""Clean up."""
self.session.rollback()