1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Speed up tests

This commit is contained in:
Paulus Schoutsen
2015-09-12 22:56:49 -07:00
parent bb42e264cb
commit cfc23b0091
10 changed files with 97 additions and 106 deletions

View File

@@ -8,6 +8,8 @@ Tests the history component.
import time
import os
import unittest
from unittest.mock import patch
from datetime import timedelta
import homeassistant.core as ha
import homeassistant.util.dt as dt_util
@@ -68,11 +70,7 @@ class TestComponentHistory(unittest.TestCase):
self.init_recorder()
states = []
# Create 10 states for 5 different entities
# After the first 5, sleep a second and save the time
# history.get_states takes the latest states BEFORE point X
for i in range(10):
for i in range(5):
state = ha.State(
'test.point_in_time_{}'.format(i % 5),
"State {}".format(i),
@@ -80,19 +78,27 @@ class TestComponentHistory(unittest.TestCase):
mock_state_change_event(self.hass, state)
self.hass.pool.block_till_done()
recorder._INSTANCE.block_till_done()
if i < 5:
states.append(state)
states.append(state)
if i == 4:
time.sleep(1)
point = dt_util.utcnow()
recorder._INSTANCE.block_till_done()
self.assertEqual(
states,
sorted(
history.get_states(point), key=lambda state: state.entity_id))
point = dt_util.utcnow() + timedelta(seconds=1)
with patch('homeassistant.util.dt.utcnow', return_value=point):
for i in range(5):
state = ha.State(
'test.point_in_time_{}'.format(i % 5),
"State {}".format(i),
{'attribute_test': i})
mock_state_change_event(self.hass, state)
self.hass.pool.block_till_done()
# Get states returns everything before POINT
self.assertEqual(states,
sorted(history.get_states(point),
key=lambda state: state.entity_id))
# Test get_state here because we have a DB setup
self.assertEqual(
@@ -113,22 +119,20 @@ class TestComponentHistory(unittest.TestCase):
set_state('YouTube')
start = dt_util.utcnow()
point = start + timedelta(seconds=1)
end = point + timedelta(seconds=1)
time.sleep(1)
with patch('homeassistant.util.dt.utcnow', return_value=point):
states = [
set_state('idle'),
set_state('Netflix'),
set_state('Plex'),
set_state('YouTube'),
]
states = [
set_state('idle'),
set_state('Netflix'),
set_state('Plex'),
set_state('YouTube'),
]
time.sleep(1)
end = dt_util.utcnow()
set_state('Netflix')
set_state('Plex')
with patch('homeassistant.util.dt.utcnow', return_value=end):
set_state('Netflix')
set_state('Plex')
self.assertEqual(
{entity_id: states},