Rewrite aurora tests to pytest style (#41144)

This commit is contained in:
Claudio Catterina 2020-10-04 13:58:16 +02:00 committed by GitHub
parent 9ff8f03322
commit 6765d395f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,85 +1,60 @@
"""The tests for the Aurora sensor platform.""" """The tests for the Aurora sensor platform."""
import re import re
import unittest
import requests_mock
from homeassistant.components.aurora import binary_sensor as aurora from homeassistant.components.aurora import binary_sensor as aurora
from tests.common import get_test_home_assistant, load_fixture from tests.common import load_fixture
class TestAuroraSensorSetUp(unittest.TestCase): def test_setup_and_initial_state(hass, requests_mock):
"""Test the aurora platform.""" """Test that the component is created and initialized as expected."""
uri = re.compile(r"http://services\.swpc\.noaa\.gov/text/aurora-nowcast-map\.txt")
requests_mock.get(uri, text=load_fixture("aurora.txt"))
def setUp(self): entities = []
"""Initialize values for this testcase class."""
self.hass = get_test_home_assistant()
self.lat = 37.8267
self.lon = -122.423
self.hass.config.latitude = self.lat
self.hass.config.longitude = self.lon
self.entities = []
self.addCleanup(self.tear_down_cleanup)
def tear_down_cleanup(self):
"""Stop everything that was started."""
self.hass.stop()
@requests_mock.Mocker()
def test_setup_and_initial_state(self, mock_req):
"""Test that the component is created and initialized as expected."""
uri = re.compile(
r"http://services\.swpc\.noaa\.gov/text/aurora-nowcast-map\.txt"
)
mock_req.get(uri, text=load_fixture("aurora.txt"))
entities = []
def mock_add_entities(new_entities, update_before_add=False):
"""Mock add entities."""
if update_before_add:
for entity in new_entities:
entity.update()
def mock_add_entities(new_entities, update_before_add=False):
"""Mock add entities."""
if update_before_add:
for entity in new_entities: for entity in new_entities:
entities.append(entity) entity.update()
config = {"name": "Test", "forecast_threshold": 75} for entity in new_entities:
aurora.setup_platform(self.hass, config, mock_add_entities) entities.append(entity)
aurora_component = entities[0] config = {"name": "Test", "forecast_threshold": 75}
assert len(entities) == 1 aurora.setup_platform(hass, config, mock_add_entities)
assert aurora_component.name == "Test"
assert aurora_component.device_state_attributes["visibility_level"] == "0"
assert aurora_component.device_state_attributes["message"] == "nothing's out"
assert not aurora_component.is_on
@requests_mock.Mocker() aurora_component = entities[0]
def test_custom_threshold_works(self, mock_req): assert len(entities) == 1
"""Test that the config can take a custom forecast threshold.""" assert aurora_component.name == "Test"
uri = re.compile( assert aurora_component.device_state_attributes["visibility_level"] == "0"
r"http://services\.swpc\.noaa\.gov/text/aurora-nowcast-map\.txt" assert aurora_component.device_state_attributes["message"] == "nothing's out"
) assert not aurora_component.is_on
mock_req.get(uri, text=load_fixture("aurora.txt"))
entities = []
def mock_add_entities(new_entities, update_before_add=False): def test_custom_threshold_works(hass, requests_mock):
"""Mock add entities.""" """Test that the config can take a custom forecast threshold."""
if update_before_add: uri = re.compile(r"http://services\.swpc\.noaa\.gov/text/aurora-nowcast-map\.txt")
for entity in new_entities: requests_mock.get(uri, text=load_fixture("aurora.txt"))
entity.update()
entities = []
def mock_add_entities(new_entities, update_before_add=False):
"""Mock add entities."""
if update_before_add:
for entity in new_entities: for entity in new_entities:
entities.append(entity) entity.update()
config = {"name": "Test", "forecast_threshold": 1} for entity in new_entities:
self.hass.config.longitude = 18.987 entities.append(entity)
self.hass.config.latitude = 69.648
aurora.setup_platform(self.hass, config, mock_add_entities) config = {"name": "Test", "forecast_threshold": 1}
hass.config.longitude = 18.987
hass.config.latitude = 69.648
aurora_component = entities[0] aurora.setup_platform(hass, config, mock_add_entities)
assert aurora_component.aurora_data.visibility_level == "16"
assert aurora_component.is_on aurora_component = entities[0]
assert aurora_component.aurora_data.visibility_level == "16"
assert aurora_component.is_on