Convert darksky unittest tests to pytest (#79868)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Jose Ramirez 2022-10-17 10:09:37 -04:00 committed by GitHub
parent c0fc23f794
commit f730f96024
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 98 additions and 158 deletions

View File

@ -1,17 +1,14 @@
"""The tests for the Dark Sky platform.""" """The tests for the Dark Sky platform."""
from datetime import timedelta from datetime import timedelta
import re import re
import unittest from unittest.mock import patch
from unittest.mock import MagicMock, patch
import forecastio import forecastio
from requests.exceptions import HTTPError from requests.exceptions import ConnectionError as ConnectError
import requests_mock
from homeassistant.components.darksky import sensor as darksky from homeassistant.setup import async_setup_component
from homeassistant.setup import setup_component
from tests.common import get_test_home_assistant, load_fixture from tests.common import load_fixture
VALID_CONFIG_MINIMAL = { VALID_CONFIG_MINIMAL = {
"sensor": { "sensor": {
@ -69,140 +66,100 @@ INVALID_CONFIG_LANG = {
} }
} }
VALID_CONFIG_ALERTS = {
"sensor": {
"platform": "darksky",
"api_key": "foo",
"forecast": [1, 2],
"hourly_forecast": [1, 2],
"monitored_conditions": ["summary", "icon", "temperature_high", "alerts"],
"scan_interval": timedelta(seconds=120),
}
}
async def test_setup_with_config(hass, requests_mock):
"""Test the platform setup with configuration."""
with patch("homeassistant.components.darksky.sensor.forecastio.load_forecast"):
assert await async_setup_component(hass, "sensor", VALID_CONFIG_MINIMAL)
await hass.async_block_till_done()
def load_forecastMock(key, lat, lon, units, lang): # pylint: disable=invalid-name state = hass.states.get("sensor.dark_sky_summary")
"""Mock darksky forecast loading."""
return ""
class TestDarkSkySetup(unittest.TestCase):
"""Test the Dark Sky platform."""
def add_entities(self, new_entities, update_before_add=False):
"""Mock add entities."""
if update_before_add:
for entity in new_entities:
entity.update()
for entity in new_entities:
self.entities.append(entity)
def setUp(self):
"""Initialize values for this testcase class."""
self.hass = get_test_home_assistant()
self.key = "foo"
self.lat = self.hass.config.latitude = 37.8267
self.lon = self.hass.config.longitude = -122.423
self.entities = []
self.addCleanup(self.tear_down_cleanup)
def tear_down_cleanup(self):
"""Stop everything that was started."""
self.hass.stop()
@patch(
"homeassistant.components.darksky.sensor.forecastio.load_forecast",
new=load_forecastMock,
)
def test_setup_with_config(self):
"""Test the platform setup with configuration."""
setup_component(self.hass, "sensor", VALID_CONFIG_MINIMAL)
self.hass.block_till_done()
state = self.hass.states.get("sensor.dark_sky_summary")
assert state is not None assert state is not None
def test_setup_with_invalid_config(self):
"""Test the platform setup with invalid configuration."""
setup_component(self.hass, "sensor", INVALID_CONFIG_MINIMAL)
self.hass.block_till_done()
state = self.hass.states.get("sensor.dark_sky_summary") async def test_setup_with_invalid_config(hass):
assert state is None """Test the platform setup with invalid configuration."""
assert await async_setup_component(hass, "sensor", INVALID_CONFIG_MINIMAL)
await hass.async_block_till_done()
@patch( state = hass.states.get("sensor.dark_sky_summary")
"homeassistant.components.darksky.sensor.forecastio.load_forecast", assert state is None
new=load_forecastMock,
)
def test_setup_with_language_config(self):
"""Test the platform setup with language configuration."""
setup_component(self.hass, "sensor", VALID_CONFIG_LANG_DE)
self.hass.block_till_done()
state = self.hass.states.get("sensor.dark_sky_summary")
async def test_setup_with_language_config(hass):
"""Test the platform setup with language configuration."""
with patch("homeassistant.components.darksky.sensor.forecastio.load_forecast"):
assert await async_setup_component(hass, "sensor", VALID_CONFIG_LANG_DE)
await hass.async_block_till_done()
state = hass.states.get("sensor.dark_sky_summary")
assert state is not None assert state is not None
def test_setup_with_invalid_language_config(self):
"""Test the platform setup with language configuration."""
setup_component(self.hass, "sensor", INVALID_CONFIG_LANG)
self.hass.block_till_done()
state = self.hass.states.get("sensor.dark_sky_summary") async def test_setup_with_invalid_language_config(hass):
"""Test the platform setup with language configuration."""
assert await async_setup_component(hass, "sensor", INVALID_CONFIG_LANG)
await hass.async_block_till_done()
state = hass.states.get("sensor.dark_sky_summary")
assert state is None
async def test_setup_bad_api_key(hass, requests_mock):
"""Test for handling a bad API key."""
# The Dark Sky API wrapper that we use raises an HTTP error
# when you try to use a bad (or no) API key.
url = "https://api.darksky.net/forecast/{}/{},{}?units=auto".format(
"foo", str(hass.config.latitude), str(hass.config.longitude)
)
msg = f"400 Client Error: Bad Request for url: {url}"
requests_mock.get(url, text=msg, status_code=400)
assert await async_setup_component(
hass, "sensor", {"sensor": {"platform": "darksky", "api_key": "foo"}}
)
await hass.async_block_till_done()
assert hass.states.get("sensor.dark_sky_summary") is None
async def test_connection_error(hass):
"""Test setting up with a connection error."""
with patch(
"homeassistant.components.darksky.sensor.forecastio.load_forecast",
side_effect=ConnectError(),
):
await async_setup_component(hass, "sensor", VALID_CONFIG_MINIMAL)
await hass.async_block_till_done()
state = hass.states.get("sensor.dark_sky_summary")
assert state is None assert state is None
@patch("forecastio.api.get_forecast")
def test_setup_bad_api_key(self, mock_get_forecast):
"""Test for handling a bad API key."""
# The Dark Sky API wrapper that we use raises an HTTP error
# when you try to use a bad (or no) API key.
url = "https://api.darksky.net/forecast/{}/{},{}?units=auto".format(
self.key, str(self.lat), str(self.lon)
)
msg = f"400 Client Error: Bad Request for url: {url}"
mock_get_forecast.side_effect = HTTPError(msg)
response = darksky.setup_platform( async def test_setup(hass, requests_mock):
self.hass, VALID_CONFIG_MINIMAL["sensor"], MagicMock() """Test for successfully setting up the forecast.io platform."""
) with patch(
assert not response "forecastio.api.get_forecast", wraps=forecastio.api.get_forecast
) as mock_get_forecast:
@patch(
"homeassistant.components.darksky.sensor.forecastio.load_forecast",
new=load_forecastMock,
)
def test_setup_with_alerts_config(self):
"""Test the platform setup with alert configuration."""
setup_component(self.hass, "sensor", VALID_CONFIG_ALERTS)
self.hass.block_till_done()
state = self.hass.states.get("sensor.dark_sky_alerts")
assert state.state == "0"
@requests_mock.Mocker()
@patch("forecastio.api.get_forecast", wraps=forecastio.api.get_forecast)
def test_setup(self, mock_req, mock_get_forecast):
"""Test for successfully setting up the forecast.io platform."""
uri = ( uri = (
r"https://api.(darksky.net|forecast.io)\/forecast\/(\w+)\/" r"https://api.(darksky.net|forecast.io)\/forecast\/(\w+)\/"
r"(-?\d+\.?\d*),(-?\d+\.?\d*)" r"(-?\d+\.?\d*),(-?\d+\.?\d*)"
) )
mock_req.get(re.compile(uri), text=load_fixture("darksky.json")) requests_mock.get(re.compile(uri), text=load_fixture("darksky.json"))
assert setup_component(self.hass, "sensor", VALID_CONFIG_MINIMAL) assert await async_setup_component(hass, "sensor", VALID_CONFIG_MINIMAL)
self.hass.block_till_done() await hass.async_block_till_done()
assert mock_get_forecast.called
assert mock_get_forecast.call_count == 1 assert mock_get_forecast.call_count == 1
assert len(self.hass.states.entity_ids()) == 13 assert len(hass.states.async_entity_ids()) == 13
state = self.hass.states.get("sensor.dark_sky_summary") state = hass.states.get("sensor.dark_sky_summary")
assert state is not None assert state is not None
assert state.state == "Clear" assert state.state == "Clear"
assert state.attributes.get("friendly_name") == "Dark Sky Summary" assert state.attributes.get("friendly_name") == "Dark Sky Summary"
state = self.hass.states.get("sensor.dark_sky_alerts") state = hass.states.get("sensor.dark_sky_alerts")
assert state.state == "2" assert state.state == "2"
state = self.hass.states.get("sensor.dark_sky_daytime_high_temperature_1d") state = hass.states.get("sensor.dark_sky_daytime_high_temperature_1d")
assert state is not None assert state is not None
assert state.attributes.get("device_class") == "temperature" assert state.attributes.get("device_class") == "temperature"

View File

@ -1,67 +1,50 @@
"""The tests for the Dark Sky weather component.""" """The tests for the Dark Sky weather component."""
import re import re
import unittest
from unittest.mock import patch from unittest.mock import patch
import forecastio import forecastio
from requests.exceptions import ConnectionError from requests.exceptions import ConnectionError as ConnectError
import requests_mock
from homeassistant.components import weather from homeassistant.components import weather
from homeassistant.setup import setup_component from homeassistant.setup import async_setup_component
from homeassistant.util.unit_system import METRIC_SYSTEM
from tests.common import get_test_home_assistant, load_fixture from tests.common import load_fixture
class TestDarkSky(unittest.TestCase): async def test_setup(hass, requests_mock):
"""Test the Dark Sky weather component.""" """Test for successfully setting up the forecast.io platform."""
with patch(
def setUp(self): "forecastio.api.get_forecast", wraps=forecastio.api.get_forecast
"""Set up things to be run when tests are started.""" ) as mock_get_forecast:
self.hass = get_test_home_assistant() requests_mock.get(
self.hass.config.units = METRIC_SYSTEM re.compile(
self.lat = self.hass.config.latitude = 37.8267 r"https://api.(darksky.net|forecast.io)\/forecast\/(\w+)\/"
self.lon = self.hass.config.longitude = -122.423 r"(-?\d+\.?\d*),(-?\d+\.?\d*)"
self.addCleanup(self.tear_down_cleanup) ),
text=load_fixture("darksky.json"),
def tear_down_cleanup(self):
"""Stop down everything that was started."""
self.hass.stop()
@requests_mock.Mocker()
@patch("forecastio.api.get_forecast", wraps=forecastio.api.get_forecast)
def test_setup(self, mock_req, mock_get_forecast):
"""Test for successfully setting up the forecast.io platform."""
uri = (
r"https://api.(darksky.net|forecast.io)\/forecast\/(\w+)\/"
r"(-?\d+\.?\d*),(-?\d+\.?\d*)"
) )
mock_req.get(re.compile(uri), text=load_fixture("darksky.json"))
assert setup_component( assert await async_setup_component(
self.hass, hass,
weather.DOMAIN, weather.DOMAIN,
{"weather": {"name": "test", "platform": "darksky", "api_key": "foo"}}, {"weather": {"name": "test", "platform": "darksky", "api_key": "foo"}},
) )
self.hass.block_till_done() await hass.async_block_till_done()
assert mock_get_forecast.called
assert mock_get_forecast.call_count == 1 assert mock_get_forecast.call_count == 1
state = hass.states.get("weather.test")
state = self.hass.states.get("weather.test")
assert state.state == "sunny" assert state.state == "sunny"
@patch("forecastio.load_forecast", side_effect=ConnectionError())
def test_failed_setup(self, mock_load_forecast):
"""Test to ensure that a network error does not break component state."""
assert setup_component( async def test_failed_setup(hass):
self.hass, """Test to ensure that a network error does not break component state."""
with patch("forecastio.load_forecast", side_effect=ConnectError()):
assert await async_setup_component(
hass,
weather.DOMAIN, weather.DOMAIN,
{"weather": {"name": "test", "platform": "darksky", "api_key": "foo"}}, {"weather": {"name": "test", "platform": "darksky", "api_key": "foo"}},
) )
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("weather.test") state = hass.states.get("weather.test")
assert state.state == "unavailable" assert state.state == "unavailable"