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) excludes = config.get(CONF_EXCLUDES)
days_offset = config.get(CONF_OFFSET) 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) obj_holidays = getattr(holidays, country)(years=year)
if province: if province:
@ -99,6 +99,11 @@ def day_to_string(day):
return None return None
def get_date(date):
"""Return date. Needed for testing."""
return date
class IsWorkdaySensor(BinarySensorDevice): class IsWorkdaySensor(BinarySensorDevice):
"""Implementation of a Workday sensor.""" """Implementation of a Workday sensor."""
@ -156,7 +161,7 @@ class IsWorkdaySensor(BinarySensorDevice):
self._state = False self._state = False
# Get iso day of the week (1 = Monday, 7 = Sunday) # 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 = date.isoweekday() - 1
day_of_week = day_to_string(day) day_of_week = day_to_string(day)

View File

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

View File

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

View File

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

View File

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

View File

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