Improved test runtime (#11553)

* Remove redundent assert statements and cleanup

* Added 'get_date' function

* Replace 'freeze_time' with 'mock.patch'
* Tox in 185s (py35)

* Removed obsolete 'freeze_time' from test_updater
* Tox 162s (py35)

* Remove test requirement 'freezegun'

* Fixed flake8 errors

* Added 'mock.patch' for 'feedparser.parse'

* Made 'FUNCTION_PATH' a constant

* Remove debug statements.
This commit is contained in:
cdce8p 2018-01-10 01:00:49 +01:00 committed by Paulus Schoutsen
parent f56b3d8e9c
commit cba55402b1
6 changed files with 66 additions and 70 deletions

View File

@ -64,7 +64,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
excludes = config.get(CONF_EXCLUDES)
days_offset = config.get(CONF_OFFSET)
year = (datetime.now() + timedelta(days=days_offset)).year
year = (get_date(datetime.today()) + timedelta(days=days_offset)).year
obj_holidays = getattr(holidays, country)(years=year)
if province:
@ -99,6 +99,11 @@ def day_to_string(day):
return None
def get_date(date):
"""Return date. Needed for testing."""
return date
class IsWorkdaySensor(BinarySensorDevice):
"""Implementation of a Workday sensor."""
@ -156,7 +161,7 @@ class IsWorkdaySensor(BinarySensorDevice):
self._state = False
# Get iso day of the week (1 = Monday, 7 = Sunday)
date = datetime.today() + timedelta(days=self._days_offset)
date = get_date(datetime.today()) + timedelta(days=self._days_offset)
day = date.isoweekday() - 1
day_of_week = day_to_string(day)

View File

@ -15,4 +15,3 @@ requests_mock==1.4
mock-open==1.3.1
flake8-docstrings==1.0.2
asynctest>=0.11.1
freezegun==0.3.9

View File

@ -16,7 +16,6 @@ requests_mock==1.4
mock-open==1.3.1
flake8-docstrings==1.0.2
asynctest>=0.11.1
freezegun==0.3.9
# homeassistant.components.notify.html5

View File

@ -1,5 +1,7 @@
"""Tests the HASS workday binary sensor."""
from freezegun import freeze_time
from datetime import date
from unittest.mock import patch
from homeassistant.components.binary_sensor.workday import day_to_string
from homeassistant.setup import setup_component
@ -7,6 +9,9 @@ from tests.common import (
get_test_home_assistant, assert_setup_component)
FUNCTION_PATH = 'homeassistant.components.binary_sensor.workday.get_date'
class TestWorkdaySetup(object):
"""Test class for workday sensor."""
@ -94,46 +99,45 @@ class TestWorkdaySetup(object):
def test_setup_component_province(self):
"""Setup workday component."""
with assert_setup_component(1, 'binary_sensor'):
setup_component(self.hass, 'binary_sensor', self.config_province)
setup_component(self.hass, 'binary_sensor',
self.config_province)
assert self.hass.states.get('binary_sensor.workday_sensor') is not None
entity = self.hass.states.get('binary_sensor.workday_sensor')
assert entity is not None
# Freeze time to a workday
@freeze_time("Mar 15th, 2017")
def test_workday_province(self):
# Freeze time to a workday - Mar 15th, 2017
@patch(FUNCTION_PATH, return_value=date(2017, 3, 15))
def test_workday_province(self, mock_date):
"""Test if workdays are reported correctly."""
with assert_setup_component(1, 'binary_sensor'):
setup_component(self.hass, 'binary_sensor', self.config_province)
assert self.hass.states.get('binary_sensor.workday_sensor') is not None
setup_component(self.hass, 'binary_sensor',
self.config_province)
self.hass.start()
entity = self.hass.states.get('binary_sensor.workday_sensor')
assert entity.state == 'on'
# Freeze time to a weekend
@freeze_time("Mar 12th, 2017")
def test_weekend_province(self):
# Freeze time to a weekend - Mar 12th, 2017
@patch(FUNCTION_PATH, return_value=date(2017, 3, 12))
def test_weekend_province(self, mock_date):
"""Test if weekends are reported correctly."""
with assert_setup_component(1, 'binary_sensor'):
setup_component(self.hass, 'binary_sensor', self.config_province)
assert self.hass.states.get('binary_sensor.workday_sensor') is not None
setup_component(self.hass, 'binary_sensor',
self.config_province)
self.hass.start()
entity = self.hass.states.get('binary_sensor.workday_sensor')
assert entity.state == 'off'
# Freeze time to a public holiday in province BW
@freeze_time("Jan 6th, 2017")
def test_public_holiday_province(self):
# Freeze time to a public holiday in province BW - Jan 6th, 2017
@patch(FUNCTION_PATH, return_value=date(2017, 1, 6))
def test_public_holiday_province(self, mock_date):
"""Test if public holidays are reported correctly."""
with assert_setup_component(1, 'binary_sensor'):
setup_component(self.hass, 'binary_sensor', self.config_province)
assert self.hass.states.get('binary_sensor.workday_sensor') is not None
setup_component(self.hass, 'binary_sensor',
self.config_province)
self.hass.start()
@ -143,47 +147,44 @@ class TestWorkdaySetup(object):
def test_setup_component_noprovince(self):
"""Setup workday component."""
with assert_setup_component(1, 'binary_sensor'):
setup_component(self.hass, 'binary_sensor', self.config_noprovince)
setup_component(self.hass, 'binary_sensor',
self.config_noprovince)
assert self.hass.states.get('binary_sensor.workday_sensor') is not None
entity = self.hass.states.get('binary_sensor.workday_sensor')
assert entity is not None
# Freeze time to a public holiday in province BW
@freeze_time("Jan 6th, 2017")
def test_public_holiday_noprovince(self):
# Freeze time to a public holiday in province BW - Jan 6th, 2017
@patch(FUNCTION_PATH, return_value=date(2017, 1, 6))
def test_public_holiday_noprovince(self, mock_date):
"""Test if public holidays are reported correctly."""
with assert_setup_component(1, 'binary_sensor'):
setup_component(self.hass, 'binary_sensor', self.config_noprovince)
assert self.hass.states.get('binary_sensor.workday_sensor') is not None
setup_component(self.hass, 'binary_sensor',
self.config_noprovince)
self.hass.start()
entity = self.hass.states.get('binary_sensor.workday_sensor')
assert entity.state == 'on'
# Freeze time to a public holiday in state CA
@freeze_time("Mar 31st, 2017")
def test_public_holiday_state(self):
# Freeze time to a public holiday in state CA - Mar 31st, 2017
@patch(FUNCTION_PATH, return_value=date(2017, 3, 31))
def test_public_holiday_state(self, mock_date):
"""Test if public holidays are reported correctly."""
with assert_setup_component(1, 'binary_sensor'):
setup_component(self.hass, 'binary_sensor', self.config_state)
assert self.hass.states.get('binary_sensor.workday_sensor') is not None
self.hass.start()
entity = self.hass.states.get('binary_sensor.workday_sensor')
assert entity.state == 'off'
# Freeze time to a public holiday in state CA
@freeze_time("Mar 31st, 2017")
def test_public_holiday_nostate(self):
# Freeze time to a public holiday in state CA - Mar 31st, 2017
@patch(FUNCTION_PATH, return_value=date(2017, 3, 31))
def test_public_holiday_nostate(self, mock_date):
"""Test if public holidays are reported correctly."""
with assert_setup_component(1, 'binary_sensor'):
setup_component(self.hass, 'binary_sensor', self.config_nostate)
assert self.hass.states.get('binary_sensor.workday_sensor') is not None
self.hass.start()
entity = self.hass.states.get('binary_sensor.workday_sensor')
@ -195,63 +196,56 @@ class TestWorkdaySetup(object):
setup_component(self.hass, 'binary_sensor',
self.config_invalidprovince)
assert self.hass.states.get('binary_sensor.workday_sensor') is None
entity = self.hass.states.get('binary_sensor.workday_sensor')
assert entity is None
# Freeze time to a public holiday in province BW
@freeze_time("Jan 6th, 2017")
def test_public_holiday_includeholiday(self):
# Freeze time to a public holiday in province BW - Jan 6th, 2017
@patch(FUNCTION_PATH, return_value=date(2017, 1, 6))
def test_public_holiday_includeholiday(self, mock_date):
"""Test if public holidays are reported correctly."""
with assert_setup_component(1, 'binary_sensor'):
setup_component(self.hass, 'binary_sensor',
self.config_includeholiday)
assert self.hass.states.get('binary_sensor.workday_sensor') is not None
self.hass.start()
entity = self.hass.states.get('binary_sensor.workday_sensor')
assert entity.state == 'on'
# Freeze time to a saturday to test offset
@freeze_time("Aug 5th, 2017")
def test_tomorrow(self):
# Freeze time to a saturday to test offset - Aug 5th, 2017
@patch(FUNCTION_PATH, return_value=date(2017, 8, 5))
def test_tomorrow(self, mock_date):
"""Test if tomorrow are reported correctly."""
with assert_setup_component(1, 'binary_sensor'):
setup_component(self.hass, 'binary_sensor',
self.config_tomorrow)
assert self.hass.states.get('binary_sensor.workday_sensor') is not None
self.hass.start()
entity = self.hass.states.get('binary_sensor.workday_sensor')
assert entity.state == 'off'
# Freeze time to a saturday to test offset
@freeze_time("Aug 5th, 2017")
def test_day_after_tomorrow(self):
# Freeze time to a saturday to test offset - Aug 5th, 2017
@patch(FUNCTION_PATH, return_value=date(2017, 8, 5))
def test_day_after_tomorrow(self, mock_date):
"""Test if the day after tomorrow are reported correctly."""
with assert_setup_component(1, 'binary_sensor'):
setup_component(self.hass, 'binary_sensor',
self.config_day_after_tomorrow)
assert self.hass.states.get('binary_sensor.workday_sensor') is not None
self.hass.start()
entity = self.hass.states.get('binary_sensor.workday_sensor')
assert entity.state == 'on'
# Freeze time to a saturday to test offset
@freeze_time("Aug 5th, 2017")
def test_yesterday(self):
# Freeze time to a saturday to test offset - Aug 5th, 2017
@patch(FUNCTION_PATH, return_value=date(2017, 8, 5))
def test_yesterday(self, mock_date):
"""Test if yesterday are reported correctly."""
with assert_setup_component(1, 'binary_sensor'):
setup_component(self.hass, 'binary_sensor',
self.config_yesterday)
assert self.hass.states.get('binary_sensor.workday_sensor') is not None
self.hass.start()
entity = self.hass.states.get('binary_sensor.workday_sensor')

View File

@ -1,6 +1,7 @@
"""The test for the geo rss events sensor platform."""
import unittest
from unittest import mock
import feedparser
from homeassistant.setup import setup_component
from tests.common import load_fixture, get_test_home_assistant
@ -33,7 +34,8 @@ class TestGeoRssServiceUpdater(unittest.TestCase):
"""Stop everything that was started."""
self.hass.stop()
def test_setup_with_categories(self):
@mock.patch('feedparser.parse', return_value=feedparser.parse(""))
def test_setup_with_categories(self, mock_parse):
"""Test the general setup of this sensor."""
self.config = VALID_CONFIG_WITH_CATEGORIES
self.assertTrue(
@ -43,7 +45,8 @@ class TestGeoRssServiceUpdater(unittest.TestCase):
self.assertIsNotNone(
self.hass.states.get('sensor.event_service_category_2'))
def test_setup_without_categories(self):
@mock.patch('feedparser.parse', return_value=feedparser.parse(""))
def test_setup_without_categories(self, mock_parse):
"""Test the general setup of this sensor."""
self.assertTrue(
setup_component(self.hass, 'sensor', {'sensor': self.config}))

View File

@ -3,7 +3,6 @@ import asyncio
from datetime import timedelta
from unittest.mock import patch, Mock
from freezegun import freeze_time
import pytest
from homeassistant.setup import async_setup_component
@ -39,7 +38,6 @@ def mock_get_uuid():
@asyncio.coroutine
@freeze_time("Mar 15th, 2017")
def test_new_version_shows_entity_after_hour(
hass, mock_get_uuid, mock_get_newest_version):
"""Test if new entity is created if new version is available."""
@ -59,7 +57,6 @@ def test_new_version_shows_entity_after_hour(
@asyncio.coroutine
@freeze_time("Mar 15th, 2017")
def test_same_version_not_show_entity(
hass, mock_get_uuid, mock_get_newest_version):
"""Test if new entity is created if new version is available."""
@ -79,7 +76,6 @@ def test_same_version_not_show_entity(
@asyncio.coroutine
@freeze_time("Mar 15th, 2017")
def test_disable_reporting(hass, mock_get_uuid, mock_get_newest_version):
"""Test if new entity is created if new version is available."""
mock_get_uuid.return_value = MOCK_HUUID