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
@@ -0,0 +1,61 @@
|
||||
"""The tests for the forecast.io platform."""
|
||||
import re
|
||||
import unittest
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import forecastio
|
||||
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."""
|
||||
|
||||
def setUp(self):
|
||||
"""Initialize values for this testcase class."""
|
||||
self.hass = ha.HomeAssistant()
|
||||
self.key = 'foo'
|
||||
self.config = {
|
||||
'api_key': 'foo',
|
||||
'monitored_conditions': ['summary', 'icon']
|
||||
}
|
||||
self.lat = 37.8267
|
||||
self.lon = -122.423
|
||||
self.hass.config.latitude = self.lat
|
||||
self.hass.config.longitude = self.lon
|
||||
|
||||
def test_setup_no_latitude(self):
|
||||
"""Test that the component is not loaded without required config."""
|
||||
self.hass.config.latitude = None
|
||||
self.assertFalse(forecast.setup_platform(self.hass, {}, MagicMock()))
|
||||
|
||||
@patch('forecastio.api.get_forecast')
|
||||
def test_setup_bad_api_key(self, mock_get_forecast):
|
||||
"""Test for handling a bad API key."""
|
||||
# The forecast API wrapper that we use raises an HTTP error
|
||||
# when you try to use a bad (or no) API key.
|
||||
url = 'https://api.forecast.io/forecast/{}/{},{}?units=auto'.format(
|
||||
self.key, str(self.lat), str(self.lon)
|
||||
)
|
||||
msg = '400 Client Error: Bad Request for url: {}'.format(url)
|
||||
mock_get_forecast.side_effect = HTTPError(msg,)
|
||||
|
||||
response = forecast.setup_platform(self.hass, self.config, MagicMock())
|
||||
self.assertFalse(response)
|
||||
|
||||
@requests_mock.Mocker()
|
||||
@patch('forecastio.api.get_forecast', wraps=forecastio.api.get_forecast)
|
||||
def test_setup(self, m, mock_get_forecast):
|
||||
"""Test for successfully setting up the forecast.io platform."""
|
||||
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')
|
||||
|
||||
Reference in new issue
Block a user