Add test for bootstrap

This commit is contained in:
Paulus Schoutsen 2015-08-11 08:20:13 -07:00
parent 27ca611689
commit 60abaa585c
3 changed files with 60 additions and 19 deletions

View file

@ -9,6 +9,7 @@ from datetime import timedelta
from unittest import mock
import homeassistant as ha
import homeassistant.util.location as location_util
import homeassistant.util.dt as dt_util
from homeassistant.helpers.entity import ToggleEntity
from homeassistant.const import (
@ -40,6 +41,23 @@ def get_test_home_assistant(num_threads=None):
return hass
def mock_detect_location_info():
""" Mock implementation of util.detect_location_info. """
return location_util.LocationInfo(
ip='1.1.1.1',
country_code='US',
country_name='United States',
region_code='CA',
region_name='California',
city='San Diego',
zip_code='92122',
time_zone='America/Los_Angeles',
latitude='2.0',
longitude='1.0',
use_fahrenheit=True,
)
def mock_service(hass, domain, service):
"""
Sets up a fake service.

41
tests/test_bootstrap.py Normal file
View file

@ -0,0 +1,41 @@
"""
tests.test_bootstrap
~~~~~~~~~~~~~~~~~~~~
Tests bootstrap.
"""
# pylint: disable=too-many-public-methods,protected-access
import tempfile
import unittest
from unittest import mock
from homeassistant import bootstrap
import homeassistant.util.dt as dt_util
from tests.common import mock_detect_location_info
class TestBootstrap(unittest.TestCase):
""" Test the bootstrap utils. """
def setUp(self):
self.orig_timezone = dt_util.DEFAULT_TIME_ZONE
def tearDown(self):
dt_util.DEFAULT_TIME_ZONE = self.orig_timezone
def test_from_config_file(self):
components = ['browser', 'conversation', 'script']
with tempfile.NamedTemporaryFile() as fp:
for comp in components:
fp.write('{}:\n'.format(comp).encode('utf-8'))
fp.flush()
with mock.patch('homeassistant.util.location.detect_location_info',
mock_detect_location_info):
hass = bootstrap.from_config_file(fp.name)
components.append('group')
self.assertEqual(sorted(components),
sorted(hass.config.components))

View file

@ -10,13 +10,12 @@ import unittest.mock as mock
import os
from homeassistant import DOMAIN, HomeAssistantError
import homeassistant.util.location as location_util
import homeassistant.config as config_util
from homeassistant.const import (
CONF_LATITUDE, CONF_LONGITUDE, CONF_TEMPERATURE_UNIT, CONF_NAME,
CONF_TIME_ZONE)
from common import get_test_config_dir
from common import get_test_config_dir, mock_detect_location_info
CONFIG_DIR = get_test_config_dir()
YAML_PATH = os.path.join(CONFIG_DIR, config_util.YAML_CONFIG_FILE)
@ -28,23 +27,6 @@ def create_file(path):
pass
def mock_detect_location_info():
""" Mock implementation of util.detect_location_info. """
return location_util.LocationInfo(
ip='1.1.1.1',
country_code='US',
country_name='United States',
region_code='CA',
region_name='California',
city='San Diego',
zip_code='92122',
time_zone='America/Los_Angeles',
latitude='2.0',
longitude='1.0',
use_fahrenheit=True,
)
class TestConfig(unittest.TestCase):
""" Test the config utils. """