Update uptime tests to pytest style (#41077)

* Update uptime tests to pytest style

First attempt

* Test that the sensor is actually configured

* Swap the rest of the tests to pytest

* fixup! Swap the rest of the tests to pytest
This commit is contained in:
Sharif Nassar 2020-10-13 13:18:31 -07:00 committed by GitHub
parent dc71b7421c
commit c08f984f2b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,90 +1,85 @@
"""The tests for the uptime sensor platform.""" """The tests for the uptime sensor platform."""
import asyncio
from datetime import timedelta from datetime import timedelta
import unittest
from homeassistant.components.uptime.sensor import UptimeSensor from homeassistant.components.uptime.sensor import UptimeSensor
from homeassistant.setup import setup_component from homeassistant.setup import async_setup_component
from tests.async_mock import patch from tests.async_mock import patch
from tests.common import get_test_home_assistant
class TestUptimeSensor(unittest.TestCase): async def test_uptime_min_config(hass):
"""Test the uptime sensor.""" """Test minimum uptime configuration."""
config = {"sensor": {"platform": "uptime"}}
assert await async_setup_component(hass, "sensor", config)
await hass.async_block_till_done()
state = hass.states.get("sensor.uptime")
assert state.attributes.get("unit_of_measurement") == "days"
def setUp(self):
"""Set up things to run when tests begin."""
self.hass = get_test_home_assistant()
self.addCleanup(self.hass.stop)
def test_uptime_min_config(self): async def test_uptime_sensor_name_change(hass):
"""Test minimum uptime configuration.""" """Test uptime sensor with different name."""
config = {"sensor": {"platform": "uptime"}} config = {"sensor": {"platform": "uptime", "name": "foobar"}}
assert setup_component(self.hass, "sensor", config) assert await async_setup_component(hass, "sensor", config)
await hass.async_block_till_done()
state = hass.states.get("sensor.foobar")
assert state.attributes.get("unit_of_measurement") == "days"
def test_uptime_sensor_name_change(self):
"""Test uptime sensor with different name."""
config = {"sensor": {"platform": "uptime", "name": "foobar"}}
assert setup_component(self.hass, "sensor", config)
def test_uptime_sensor_config_hours(self): async def test_uptime_sensor_config_hours(hass):
"""Test uptime sensor with hours defined in config.""" """Test uptime sensor with hours defined in config."""
config = {"sensor": {"platform": "uptime", "unit_of_measurement": "hours"}} config = {"sensor": {"platform": "uptime", "unit_of_measurement": "hours"}}
assert setup_component(self.hass, "sensor", config) assert await async_setup_component(hass, "sensor", config)
await hass.async_block_till_done()
state = hass.states.get("sensor.uptime")
assert state.attributes.get("unit_of_measurement") == "hours"
def test_uptime_sensor_config_minutes(self):
"""Test uptime sensor with minutes defined in config."""
config = {"sensor": {"platform": "uptime", "unit_of_measurement": "minutes"}}
assert setup_component(self.hass, "sensor", config)
def test_uptime_sensor_days_output(self): async def test_uptime_sensor_config_minutes(hass):
"""Test uptime sensor output data.""" """Test uptime sensor with minutes defined in config."""
sensor = UptimeSensor("test", "days") config = {"sensor": {"platform": "uptime", "unit_of_measurement": "minutes"}}
assert sensor.unit_of_measurement == "days" assert await async_setup_component(hass, "sensor", config)
new_time = sensor.initial + timedelta(days=1) await hass.async_block_till_done()
with patch("homeassistant.util.dt.now", return_value=new_time): state = hass.states.get("sensor.uptime")
asyncio.run_coroutine_threadsafe( assert state.attributes.get("unit_of_measurement") == "minutes"
sensor.async_update(), self.hass.loop
).result()
assert sensor.state == 1.00
new_time = sensor.initial + timedelta(days=111.499)
with patch("homeassistant.util.dt.now", return_value=new_time):
asyncio.run_coroutine_threadsafe(
sensor.async_update(), self.hass.loop
).result()
assert sensor.state == 111.50
def test_uptime_sensor_hours_output(self):
"""Test uptime sensor output data."""
sensor = UptimeSensor("test", "hours")
assert sensor.unit_of_measurement == "hours"
new_time = sensor.initial + timedelta(hours=16)
with patch("homeassistant.util.dt.now", return_value=new_time):
asyncio.run_coroutine_threadsafe(
sensor.async_update(), self.hass.loop
).result()
assert sensor.state == 16.00
new_time = sensor.initial + timedelta(hours=72.499)
with patch("homeassistant.util.dt.now", return_value=new_time):
asyncio.run_coroutine_threadsafe(
sensor.async_update(), self.hass.loop
).result()
assert sensor.state == 72.50
def test_uptime_sensor_minutes_output(self): async def test_uptime_sensor_days_output(hass):
"""Test uptime sensor output data.""" """Test uptime sensor output data."""
sensor = UptimeSensor("test", "minutes") sensor = UptimeSensor("test", "days")
assert sensor.unit_of_measurement == "minutes" assert sensor.unit_of_measurement == "days"
new_time = sensor.initial + timedelta(minutes=16) new_time = sensor.initial + timedelta(days=1)
with patch("homeassistant.util.dt.now", return_value=new_time): with patch("homeassistant.util.dt.now", return_value=new_time):
asyncio.run_coroutine_threadsafe( await sensor.async_update()
sensor.async_update(), self.hass.loop assert sensor.state == 1.00
).result() new_time = sensor.initial + timedelta(days=111.499)
assert sensor.state == 16.00 with patch("homeassistant.util.dt.now", return_value=new_time):
new_time = sensor.initial + timedelta(minutes=12.499) await sensor.async_update()
with patch("homeassistant.util.dt.now", return_value=new_time): assert sensor.state == 111.50
asyncio.run_coroutine_threadsafe(
sensor.async_update(), self.hass.loop
).result() async def test_uptime_sensor_hours_output(hass):
assert sensor.state == 12.50 """Test uptime sensor output data."""
sensor = UptimeSensor("test", "hours")
assert sensor.unit_of_measurement == "hours"
new_time = sensor.initial + timedelta(hours=16)
with patch("homeassistant.util.dt.now", return_value=new_time):
await sensor.async_update()
assert sensor.state == 16.00
new_time = sensor.initial + timedelta(hours=72.499)
with patch("homeassistant.util.dt.now", return_value=new_time):
await sensor.async_update()
assert sensor.state == 72.50
async def test_uptime_sensor_minutes_output(hass):
"""Test uptime sensor output data."""
sensor = UptimeSensor("test", "minutes")
assert sensor.unit_of_measurement == "minutes"
new_time = sensor.initial + timedelta(minutes=16)
with patch("homeassistant.util.dt.now", return_value=new_time):
await sensor.async_update()
assert sensor.state == 16.00
new_time = sensor.initial + timedelta(minutes=12.499)
with patch("homeassistant.util.dt.now", return_value=new_time):
await sensor.async_update()
assert sensor.state == 12.50