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:
parent
dc71b7421c
commit
c08f984f2b
1 changed files with 69 additions and 74 deletions
|
@ -1,90 +1,85 @@
|
|||
"""The tests for the uptime sensor platform."""
|
||||
import asyncio
|
||||
from datetime import timedelta
|
||||
import unittest
|
||||
|
||||
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.common import get_test_home_assistant
|
||||
|
||||
|
||||
class TestUptimeSensor(unittest.TestCase):
|
||||
"""Test the uptime sensor."""
|
||||
async def test_uptime_min_config(hass):
|
||||
"""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):
|
||||
"""Test minimum uptime configuration."""
|
||||
config = {"sensor": {"platform": "uptime"}}
|
||||
assert setup_component(self.hass, "sensor", config)
|
||||
async def test_uptime_sensor_name_change(hass):
|
||||
"""Test uptime sensor with different name."""
|
||||
config = {"sensor": {"platform": "uptime", "name": "foobar"}}
|
||||
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):
|
||||
"""Test uptime sensor with hours defined in config."""
|
||||
config = {"sensor": {"platform": "uptime", "unit_of_measurement": "hours"}}
|
||||
assert setup_component(self.hass, "sensor", config)
|
||||
async def test_uptime_sensor_config_hours(hass):
|
||||
"""Test uptime sensor with hours defined in config."""
|
||||
config = {"sensor": {"platform": "uptime", "unit_of_measurement": "hours"}}
|
||||
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):
|
||||
"""Test uptime sensor output data."""
|
||||
sensor = UptimeSensor("test", "days")
|
||||
assert sensor.unit_of_measurement == "days"
|
||||
new_time = sensor.initial + timedelta(days=1)
|
||||
with patch("homeassistant.util.dt.now", return_value=new_time):
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
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
|
||||
async def test_uptime_sensor_config_minutes(hass):
|
||||
"""Test uptime sensor with minutes defined in config."""
|
||||
config = {"sensor": {"platform": "uptime", "unit_of_measurement": "minutes"}}
|
||||
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") == "minutes"
|
||||
|
||||
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):
|
||||
"""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):
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
sensor.async_update(), self.hass.loop
|
||||
).result()
|
||||
assert sensor.state == 16.00
|
||||
new_time = sensor.initial + timedelta(minutes=12.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 == 12.50
|
||||
async def test_uptime_sensor_days_output(hass):
|
||||
"""Test uptime sensor output data."""
|
||||
sensor = UptimeSensor("test", "days")
|
||||
assert sensor.unit_of_measurement == "days"
|
||||
new_time = sensor.initial + timedelta(days=1)
|
||||
with patch("homeassistant.util.dt.now", return_value=new_time):
|
||||
await sensor.async_update()
|
||||
assert sensor.state == 1.00
|
||||
new_time = sensor.initial + timedelta(days=111.499)
|
||||
with patch("homeassistant.util.dt.now", return_value=new_time):
|
||||
await sensor.async_update()
|
||||
assert sensor.state == 111.50
|
||||
|
||||
|
||||
async def test_uptime_sensor_hours_output(hass):
|
||||
"""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
|
||||
|
|
Loading…
Add table
Reference in a new issue