Move elevation to core config and clean up HTTP mocking in tests (#2378)
* Stick version numbers * Move elevation to core config * Migrate forecast test to requests-mock * Migrate YR tests to requests-mock * Add requests_mock to requirements_test.txt * Move conf code from bootstrap to config * More config fixes * Fix some more issues * Add test for set config and failing auto detect
This commit is contained in:
1 parent
dc75b28b90
commit
6714392e9c
26 files changed
+1779
-337
No files matched your search
@@ -1,17 +1,17 @@
|
||||
"""The tests for the forecast.io platform."""
|
||||
import json
|
||||
import re
|
||||
import os
|
||||
import unittest
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import forecastio
|
||||
import httpretty
|
||||
from requests.exceptions import HTTPError
|
||||
import requests_mock
|
||||
|
||||
from homeassistant.components.sensor import forecast
|
||||
from homeassistant import core as ha
|
||||
|
||||
from tests.common import load_fixture
|
||||
|
||||
|
||||
class TestForecastSetup(unittest.TestCase):
|
||||
"""Test the forecast.io platform."""
|
||||
@@ -48,29 +48,14 @@ class TestForecastSetup(unittest.TestCase):
|
||||
response = forecast.setup_platform(self.hass, self.config, MagicMock())
|
||||
self.assertFalse(response)
|
||||
|
||||
@httpretty.activate
|
||||
@requests_mock.Mocker()
|
||||
@patch('forecastio.api.get_forecast', wraps=forecastio.api.get_forecast)
|
||||
def test_setup(self, mock_get_forecast):
|
||||
def test_setup(self, m, mock_get_forecast):
|
||||
"""Test for successfully setting up the forecast.io platform."""
|
||||
def load_fixture_from_json():
|
||||
cwd = os.path.dirname(__file__)
|
||||
fixture_path = os.path.join(cwd, '..', 'fixtures', 'forecast.json')
|
||||
with open(fixture_path) as file:
|
||||
content = json.load(file)
|
||||
return json.dumps(content)
|
||||
|
||||
# Mock out any calls to the actual API and
|
||||
# return the fixture json instead
|
||||
uri = 'api.forecast.io\/forecast\/(\w+)\/(-?\d+\.?\d*),(-?\d+\.?\d*)'
|
||||
httpretty.register_uri(
|
||||
httpretty.GET,
|
||||
re.compile(uri),
|
||||
body=load_fixture_from_json(),
|
||||
)
|
||||
# The following will raise an error if the regex for the mock was
|
||||
# incorrect and we actually try to go out to the internet.
|
||||
httpretty.HTTPretty.allow_net_connect = False
|
||||
|
||||
uri = ('https://api.forecast.io\/forecast\/(\w+)\/'
|
||||
'(-?\d+\.?\d*),(-?\d+\.?\d*)')
|
||||
m.get(re.compile(uri),
|
||||
text=load_fixture('forecast.json'))
|
||||
forecast.setup_platform(self.hass, self.config, MagicMock())
|
||||
self.assertTrue(mock_get_forecast.called)
|
||||
self.assertEqual(mock_get_forecast.call_count, 1)
|
||||
@@ -1,39 +1,40 @@
|
||||
"""The tests for the Yr sensor platform."""
|
||||
from datetime import datetime
|
||||
from unittest import TestCase
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
import requests_mock
|
||||
|
||||
from homeassistant.bootstrap import _setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
from tests.common import get_test_home_assistant
|
||||
from tests.common import get_test_home_assistant, load_fixture
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('betamax_session')
|
||||
class TestSensorYr:
|
||||
class TestSensorYr(TestCase):
|
||||
"""Test the Yr sensor."""
|
||||
|
||||
def setup_method(self, method):
|
||||
def setUp(self):
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
self.hass.config.latitude = 32.87336
|
||||
self.hass.config.longitude = 117.22743
|
||||
|
||||
def teardown_method(self, method):
|
||||
def tearDown(self):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def test_default_setup(self, betamax_session):
|
||||
@requests_mock.Mocker()
|
||||
def test_default_setup(self, m):
|
||||
"""Test the default setup."""
|
||||
m.get('http://api.yr.no/weatherapi/locationforecast/1.9/',
|
||||
text=load_fixture('yr.no.json'))
|
||||
now = datetime(2016, 6, 9, 1, tzinfo=dt_util.UTC)
|
||||
|
||||
with patch('homeassistant.components.sensor.yr.requests.Session',
|
||||
return_value=betamax_session):
|
||||
with patch('homeassistant.components.sensor.yr.dt_util.utcnow',
|
||||
return_value=now):
|
||||
assert _setup_component(self.hass, 'sensor', {
|
||||
'sensor': {'platform': 'yr',
|
||||
'elevation': 0}})
|
||||
with patch('homeassistant.components.sensor.yr.dt_util.utcnow',
|
||||
return_value=now):
|
||||
assert _setup_component(self.hass, 'sensor', {
|
||||
'sensor': {'platform': 'yr',
|
||||
'elevation': 0}})
|
||||
|
||||
state = self.hass.states.get('sensor.yr_symbol')
|
||||
|
||||
@@ -41,23 +42,24 @@ class TestSensorYr:
|
||||
assert state.state.isnumeric()
|
||||
assert state.attributes.get('unit_of_measurement') is None
|
||||
|
||||
def test_custom_setup(self, betamax_session):
|
||||
@requests_mock.Mocker()
|
||||
def test_custom_setup(self, m):
|
||||
"""Test a custom setup."""
|
||||
m.get('http://api.yr.no/weatherapi/locationforecast/1.9/',
|
||||
text=load_fixture('yr.no.json'))
|
||||
now = datetime(2016, 6, 9, 1, tzinfo=dt_util.UTC)
|
||||
|
||||
with patch('homeassistant.components.sensor.yr.requests.Session',
|
||||
return_value=betamax_session):
|
||||
with patch('homeassistant.components.sensor.yr.dt_util.utcnow',
|
||||
return_value=now):
|
||||
assert _setup_component(self.hass, 'sensor', {
|
||||
'sensor': {'platform': 'yr',
|
||||
'elevation': 0,
|
||||
'monitored_conditions': [
|
||||
'pressure',
|
||||
'windDirection',
|
||||
'humidity',
|
||||
'fog',
|
||||
'windSpeed']}})
|
||||
with patch('homeassistant.components.sensor.yr.dt_util.utcnow',
|
||||
return_value=now):
|
||||
assert _setup_component(self.hass, 'sensor', {
|
||||
'sensor': {'platform': 'yr',
|
||||
'elevation': 0,
|
||||
'monitored_conditions': [
|
||||
'pressure',
|
||||
'windDirection',
|
||||
'humidity',
|
||||
'fog',
|
||||
'windSpeed']}})
|
||||
|
||||
state = self.hass.states.get('sensor.yr_pressure')
|
||||
assert 'hPa' == state.attributes.get('unit_of_measurement')
|
||||
|
||||
@@ -131,7 +131,7 @@ class TestComponentsCore(unittest.TestCase):
|
||||
assert state.attributes.get('hello') == 'world'
|
||||
|
||||
@patch('homeassistant.components._LOGGER.error')
|
||||
@patch('homeassistant.bootstrap.process_ha_core_config')
|
||||
@patch('homeassistant.config.process_ha_core_config')
|
||||
def test_reload_core_with_wrong_conf(self, mock_process, mock_error):
|
||||
"""Test reload core conf service."""
|
||||
with TemporaryDirectory() as conf_dir:
|
||||
|
||||
Reference in new issue
Block a user