Rewrite dte_energy_bridge unittest tests to pytest style test functions (#41116)

This commit is contained in:
Eliot Wong 2020-10-04 07:50:03 -04:00 committed by GitHub
parent 346be6fc3f
commit 9ff8f03322
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,65 +1,56 @@
"""The tests for the DTE Energy Bridge.""" """The tests for the DTE Energy Bridge."""
import unittest
import requests_mock import requests_mock
from homeassistant.setup import setup_component from homeassistant.setup import async_setup_component
from tests.common import get_test_home_assistant
DTE_ENERGY_BRIDGE_CONFIG = {"platform": "dte_energy_bridge", "ip": "192.168.1.1"} DTE_ENERGY_BRIDGE_CONFIG = {"platform": "dte_energy_bridge", "ip": "192.168.1.1"}
class TestDteEnergyBridgeSetup(unittest.TestCase): async def test_setup_with_config(hass):
"""Test the DTE Energy Bridge platform.""" """Test the platform setup with configuration."""
assert await async_setup_component(
hass, "sensor", {"dte_energy_bridge": DTE_ENERGY_BRIDGE_CONFIG}
)
await hass.async_block_till_done()
def setUp(self):
"""Initialize values for this testcase class."""
self.hass = get_test_home_assistant()
self.addCleanup(self.hass.stop)
def test_setup_with_config(self): async def test_setup_correct_reading(hass):
"""Test the platform setup with configuration.""" """Test DTE Energy bridge returns a correct value."""
assert setup_component( with requests_mock.Mocker() as mock_req:
self.hass, "sensor", {"dte_energy_bridge": DTE_ENERGY_BRIDGE_CONFIG}
)
@requests_mock.Mocker()
def test_setup_correct_reading(self, mock_req):
"""Test DTE Energy bridge returns a correct value."""
mock_req.get( mock_req.get(
"http://{}/instantaneousdemand".format(DTE_ENERGY_BRIDGE_CONFIG["ip"]), "http://{}/instantaneousdemand".format(DTE_ENERGY_BRIDGE_CONFIG["ip"]),
text=".411 kW", text=".411 kW",
) )
assert setup_component( assert await async_setup_component(
self.hass, "sensor", {"sensor": DTE_ENERGY_BRIDGE_CONFIG} hass, "sensor", {"sensor": DTE_ENERGY_BRIDGE_CONFIG}
) )
self.hass.block_till_done() await hass.async_block_till_done()
assert "0.411" == self.hass.states.get("sensor.current_energy_usage").state assert hass.states.get("sensor.current_energy_usage").state == "0.411"
@requests_mock.Mocker()
def test_setup_incorrect_units_reading(self, mock_req): async def test_setup_incorrect_units_reading(hass):
"""Test DTE Energy bridge handles a value with incorrect units.""" """Test DTE Energy bridge handles a value with incorrect units."""
with requests_mock.Mocker() as mock_req:
mock_req.get( mock_req.get(
"http://{}/instantaneousdemand".format(DTE_ENERGY_BRIDGE_CONFIG["ip"]), "http://{}/instantaneousdemand".format(DTE_ENERGY_BRIDGE_CONFIG["ip"]),
text="411 kW", text="411 kW",
) )
assert setup_component( assert await async_setup_component(
self.hass, "sensor", {"sensor": DTE_ENERGY_BRIDGE_CONFIG} hass, "sensor", {"sensor": DTE_ENERGY_BRIDGE_CONFIG}
) )
self.hass.block_till_done() await hass.async_block_till_done()
assert "0.411" == self.hass.states.get("sensor.current_energy_usage").state assert hass.states.get("sensor.current_energy_usage").state == "0.411"
@requests_mock.Mocker()
def test_setup_bad_format_reading(self, mock_req): async def test_setup_bad_format_reading(hass):
"""Test DTE Energy bridge handles an invalid value.""" """Test DTE Energy bridge handles an invalid value."""
with requests_mock.Mocker() as mock_req:
mock_req.get( mock_req.get(
"http://{}/instantaneousdemand".format(DTE_ENERGY_BRIDGE_CONFIG["ip"]), "http://{}/instantaneousdemand".format(DTE_ENERGY_BRIDGE_CONFIG["ip"]),
text="411", text="411",
) )
assert setup_component( assert await async_setup_component(
self.hass, "sensor", {"sensor": DTE_ENERGY_BRIDGE_CONFIG} hass, "sensor", {"sensor": DTE_ENERGY_BRIDGE_CONFIG}
) )
self.hass.block_till_done() await hass.async_block_till_done()
assert "unknown" == self.hass.states.get("sensor.current_energy_usage").state assert hass.states.get("sensor.current_energy_usage").state == "unknown"