mirror of
https://github.com/home-assistant/core.git
synced 2025-04-26 18:27:51 +00:00
Make tox run faster by using in-memory sqlite db. (#1752)
Two tests were invariably grinding my system to a halt as it was applying migrations to an empty sqlite database. This patch reduces the time of a pytest run from 50 to about 16 seconds on my desktop. The difference is even more noticable on a slower laptop drive, but is probably insignificant for all you fancy SSD users.
This commit is contained in:
parent
f5227e1de0
commit
c481cbce7e
@ -168,6 +168,7 @@ class RecorderRun(object):
|
|||||||
class Recorder(threading.Thread):
|
class Recorder(threading.Thread):
|
||||||
"""A threaded recorder class."""
|
"""A threaded recorder class."""
|
||||||
|
|
||||||
|
# pylint: disable=too-many-instance-attributes
|
||||||
def __init__(self, hass):
|
def __init__(self, hass):
|
||||||
"""Initialize the recorder."""
|
"""Initialize the recorder."""
|
||||||
threading.Thread.__init__(self)
|
threading.Thread.__init__(self)
|
||||||
@ -179,6 +180,7 @@ class Recorder(threading.Thread):
|
|||||||
self.lock = threading.Lock()
|
self.lock = threading.Lock()
|
||||||
self.recording_start = dt_util.utcnow()
|
self.recording_start = dt_util.utcnow()
|
||||||
self.utc_offset = dt_util.now().utcoffset().total_seconds()
|
self.utc_offset = dt_util.now().utcoffset().total_seconds()
|
||||||
|
self.db_path = self.hass.config.path(DB_FILE)
|
||||||
|
|
||||||
def start_recording(event):
|
def start_recording(event):
|
||||||
"""Start recording."""
|
"""Start recording."""
|
||||||
@ -302,8 +304,7 @@ class Recorder(threading.Thread):
|
|||||||
|
|
||||||
def _setup_connection(self):
|
def _setup_connection(self):
|
||||||
"""Ensure database is ready to fly."""
|
"""Ensure database is ready to fly."""
|
||||||
db_path = self.hass.config.path(DB_FILE)
|
self.conn = sqlite3.connect(self.db_path, check_same_thread=False)
|
||||||
self.conn = sqlite3.connect(db_path, check_same_thread=False)
|
|
||||||
self.conn.row_factory = sqlite3.Row
|
self.conn.row_factory = sqlite3.Row
|
||||||
|
|
||||||
# Make sure the database is closed whenever Python exits
|
# Make sure the database is closed whenever Python exits
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
"""The tests the History component."""
|
"""The tests the History component."""
|
||||||
# pylint: disable=protected-access,too-many-public-methods
|
# pylint: disable=protected-access,too-many-public-methods
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import os
|
|
||||||
import unittest
|
import unittest
|
||||||
from unittest.mock import patch, sentinel
|
from unittest.mock import patch, sentinel
|
||||||
|
|
||||||
@ -24,13 +23,10 @@ class TestComponentHistory(unittest.TestCase):
|
|||||||
"""Stop everything that was started."""
|
"""Stop everything that was started."""
|
||||||
self.hass.stop()
|
self.hass.stop()
|
||||||
|
|
||||||
db_path = self.hass.config.path(recorder.DB_FILE)
|
|
||||||
if os.path.isfile(db_path):
|
|
||||||
os.remove(db_path)
|
|
||||||
|
|
||||||
def init_recorder(self):
|
def init_recorder(self):
|
||||||
"""Initialize the recorder."""
|
"""Initialize the recorder."""
|
||||||
recorder.setup(self.hass, {})
|
with patch('homeassistant.core.Config.path', return_value=':memory:'):
|
||||||
|
recorder.setup(self.hass, {})
|
||||||
self.hass.start()
|
self.hass.start()
|
||||||
self.wait_recording_done()
|
self.wait_recording_done()
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
"""The tests for the Recorder component."""
|
"""The tests for the Recorder component."""
|
||||||
# pylint: disable=too-many-public-methods,protected-access
|
# pylint: disable=too-many-public-methods,protected-access
|
||||||
import unittest
|
import unittest
|
||||||
import os
|
from unittest.mock import patch
|
||||||
|
|
||||||
from homeassistant.const import MATCH_ALL
|
from homeassistant.const import MATCH_ALL
|
||||||
from homeassistant.components import recorder
|
from homeassistant.components import recorder
|
||||||
@ -15,7 +15,8 @@ class TestRecorder(unittest.TestCase):
|
|||||||
def setUp(self): # pylint: disable=invalid-name
|
def setUp(self): # pylint: disable=invalid-name
|
||||||
"""Setup things to be run when tests are started."""
|
"""Setup things to be run when tests are started."""
|
||||||
self.hass = get_test_home_assistant()
|
self.hass = get_test_home_assistant()
|
||||||
recorder.setup(self.hass, {})
|
with patch('homeassistant.core.Config.path', return_value=':memory:'):
|
||||||
|
recorder.setup(self.hass, {})
|
||||||
self.hass.start()
|
self.hass.start()
|
||||||
recorder._INSTANCE.block_till_done()
|
recorder._INSTANCE.block_till_done()
|
||||||
|
|
||||||
@ -23,7 +24,6 @@ class TestRecorder(unittest.TestCase):
|
|||||||
"""Stop everything that was started."""
|
"""Stop everything that was started."""
|
||||||
self.hass.stop()
|
self.hass.stop()
|
||||||
recorder._INSTANCE.block_till_done()
|
recorder._INSTANCE.block_till_done()
|
||||||
os.remove(self.hass.config.path(recorder.DB_FILE))
|
|
||||||
|
|
||||||
def test_saving_state(self):
|
def test_saving_state(self):
|
||||||
"""Test saving and restoring a state."""
|
"""Test saving and restoring a state."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user