Rewrite hddtemp unittest tests to pytest (#42513)

This commit is contained in:
Adrian Suwała 2021-01-27 10:24:04 +01:00 committed by GitHub
parent 26e266181d
commit e12e2377af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,12 +1,11 @@
"""The tests for the hddtemp platform.""" """The tests for the hddtemp platform."""
import socket import socket
import unittest
from unittest.mock import patch from unittest.mock import patch
from homeassistant.const import TEMP_CELSIUS import pytest
from homeassistant.setup import setup_component
from tests.common import get_test_home_assistant from homeassistant.const import TEMP_CELSIUS
from homeassistant.setup import async_setup_component
VALID_CONFIG_MINIMAL = {"sensor": {"platform": "hddtemp"}} VALID_CONFIG_MINIMAL = {"sensor": {"platform": "hddtemp"}}
@ -24,10 +23,37 @@ VALID_CONFIG_MULTIPLE_DISKS = {
} }
} }
VALID_CONFIG_HOST = {"sensor": {"platform": "hddtemp", "host": "alice.local"}} VALID_CONFIG_HOST_REFUSED = {"sensor": {"platform": "hddtemp", "host": "alice.local"}}
VALID_CONFIG_HOST_UNREACHABLE = {"sensor": {"platform": "hddtemp", "host": "bob.local"}} VALID_CONFIG_HOST_UNREACHABLE = {"sensor": {"platform": "hddtemp", "host": "bob.local"}}
REFERENCE = {
"/dev/sda1": {
"device": "/dev/sda1",
"temperature": "29",
"unit_of_measurement": TEMP_CELSIUS,
"model": "WDC WD30EZRX-12DC0B0",
},
"/dev/sdb1": {
"device": "/dev/sdb1",
"temperature": "32",
"unit_of_measurement": TEMP_CELSIUS,
"model": "WDC WD15EADS-11P7B2",
},
"/dev/sdc1": {
"device": "/dev/sdc1",
"temperature": "29",
"unit_of_measurement": TEMP_CELSIUS,
"model": "WDC WD20EARX-22MMMB0",
},
"/dev/sdd1": {
"device": "/dev/sdd1",
"temperature": "32",
"unit_of_measurement": TEMP_CELSIUS,
"model": "WDC WD15EARS-00Z5B1",
},
}
class TelnetMock: class TelnetMock:
"""Mock class for the telnetlib.Telnet object.""" """Mock class for the telnetlib.Telnet object."""
@ -54,114 +80,81 @@ class TelnetMock:
return self.sample_data return self.sample_data
class TestHDDTempSensor(unittest.TestCase): @pytest.fixture
"""Test the hddtemp sensor.""" def telnetmock():
"""Mock telnet."""
with patch("telnetlib.Telnet", new=TelnetMock):
yield
def setUp(self):
"""Set up things to run when tests begin."""
self.hass = get_test_home_assistant()
self.config = VALID_CONFIG_ONE_DISK
self.reference = {
"/dev/sda1": {
"device": "/dev/sda1",
"temperature": "29",
"unit_of_measurement": TEMP_CELSIUS,
"model": "WDC WD30EZRX-12DC0B0",
},
"/dev/sdb1": {
"device": "/dev/sdb1",
"temperature": "32",
"unit_of_measurement": TEMP_CELSIUS,
"model": "WDC WD15EADS-11P7B2",
},
"/dev/sdc1": {
"device": "/dev/sdc1",
"temperature": "29",
"unit_of_measurement": TEMP_CELSIUS,
"model": "WDC WD20EARX-22MMMB0",
},
"/dev/sdd1": {
"device": "/dev/sdd1",
"temperature": "32",
"unit_of_measurement": TEMP_CELSIUS,
"model": "WDC WD15EARS-00Z5B1",
},
}
self.addCleanup(self.hass.stop)
@patch("telnetlib.Telnet", new=TelnetMock) async def test_hddtemp_min_config(hass, telnetmock):
def test_hddtemp_min_config(self):
"""Test minimal hddtemp configuration.""" """Test minimal hddtemp configuration."""
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()
entity = self.hass.states.all()[0].entity_id entity_id = hass.states.async_all()[0].entity_id
state = self.hass.states.get(entity) state = hass.states.get(entity_id)
reference = self.reference[state.attributes.get("device")] reference = REFERENCE[state.attributes.get("device")]
assert state.state == reference["temperature"] assert state.state == reference["temperature"]
assert state.attributes.get("device") == reference["device"] assert state.attributes.get("device") == reference["device"]
assert state.attributes.get("model") == reference["model"] assert state.attributes.get("model") == reference["model"]
assert ( assert (
state.attributes.get("unit_of_measurement") state.attributes.get("unit_of_measurement") == reference["unit_of_measurement"]
== reference["unit_of_measurement"]
) )
assert ( assert (
state.attributes.get("friendly_name") state.attributes.get("friendly_name") == f"HD Temperature {reference['device']}"
== f"HD Temperature {reference['device']}"
) )
@patch("telnetlib.Telnet", new=TelnetMock)
def test_hddtemp_rename_config(self): async def test_hddtemp_rename_config(hass, telnetmock):
"""Test hddtemp configuration with different name.""" """Test hddtemp configuration with different name."""
assert setup_component(self.hass, "sensor", VALID_CONFIG_NAME) assert await async_setup_component(hass, "sensor", VALID_CONFIG_NAME)
self.hass.block_till_done() await hass.async_block_till_done()
entity = self.hass.states.all()[0].entity_id entity_id = hass.states.async_all()[0].entity_id
state = self.hass.states.get(entity) state = hass.states.get(entity_id)
reference = self.reference[state.attributes.get("device")] reference = REFERENCE[state.attributes.get("device")]
assert state.attributes.get("friendly_name") == f"FooBar {reference['device']}" assert state.attributes.get("friendly_name") == f"FooBar {reference['device']}"
@patch("telnetlib.Telnet", new=TelnetMock)
def test_hddtemp_one_disk(self): async def test_hddtemp_one_disk(hass, telnetmock):
"""Test hddtemp one disk configuration.""" """Test hddtemp one disk configuration."""
assert setup_component(self.hass, "sensor", VALID_CONFIG_ONE_DISK) assert await async_setup_component(hass, "sensor", VALID_CONFIG_ONE_DISK)
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get("sensor.hd_temperature_dev_sdd1") state = hass.states.get("sensor.hd_temperature_dev_sdd1")
reference = self.reference[state.attributes.get("device")] reference = REFERENCE[state.attributes.get("device")]
assert state.state == reference["temperature"] assert state.state == reference["temperature"]
assert state.attributes.get("device") == reference["device"] assert state.attributes.get("device") == reference["device"]
assert state.attributes.get("model") == reference["model"] assert state.attributes.get("model") == reference["model"]
assert ( assert (
state.attributes.get("unit_of_measurement") state.attributes.get("unit_of_measurement") == reference["unit_of_measurement"]
== reference["unit_of_measurement"]
) )
assert ( assert (
state.attributes.get("friendly_name") state.attributes.get("friendly_name") == f"HD Temperature {reference['device']}"
== f"HD Temperature {reference['device']}"
) )
@patch("telnetlib.Telnet", new=TelnetMock)
def test_hddtemp_wrong_disk(self):
"""Test hddtemp wrong disk configuration."""
assert setup_component(self.hass, "sensor", VALID_CONFIG_WRONG_DISK)
self.hass.block_till_done()
assert len(self.hass.states.all()) == 1 async def test_hddtemp_wrong_disk(hass, telnetmock):
state = self.hass.states.get("sensor.hd_temperature_dev_sdx1") """Test hddtemp wrong disk configuration."""
assert await async_setup_component(hass, "sensor", VALID_CONFIG_WRONG_DISK)
await hass.async_block_till_done()
assert len(hass.states.async_all()) == 1
state = hass.states.get("sensor.hd_temperature_dev_sdx1")
assert state.attributes.get("friendly_name") == "HD Temperature /dev/sdx1" assert state.attributes.get("friendly_name") == "HD Temperature /dev/sdx1"
@patch("telnetlib.Telnet", new=TelnetMock)
def test_hddtemp_multiple_disks(self): async def test_hddtemp_multiple_disks(hass, telnetmock):
"""Test hddtemp multiple disk configuration.""" """Test hddtemp multiple disk configuration."""
assert setup_component(self.hass, "sensor", VALID_CONFIG_MULTIPLE_DISKS) assert await async_setup_component(hass, "sensor", VALID_CONFIG_MULTIPLE_DISKS)
self.hass.block_till_done() await hass.async_block_till_done()
for sensor in [ for sensor in [
"sensor.hd_temperature_dev_sda1", "sensor.hd_temperature_dev_sda1",
@ -169,9 +162,9 @@ class TestHDDTempSensor(unittest.TestCase):
"sensor.hd_temperature_dev_sdc1", "sensor.hd_temperature_dev_sdc1",
]: ]:
state = self.hass.states.get(sensor) state = hass.states.get(sensor)
reference = self.reference[state.attributes.get("device")] reference = REFERENCE[state.attributes.get("device")]
assert state.state == reference["temperature"] assert state.state == reference["temperature"]
assert state.attributes.get("device") == reference["device"] assert state.attributes.get("device") == reference["device"]
@ -185,16 +178,16 @@ class TestHDDTempSensor(unittest.TestCase):
== f"HD Temperature {reference['device']}" == f"HD Temperature {reference['device']}"
) )
@patch("telnetlib.Telnet", new=TelnetMock)
def test_hddtemp_host_refused(self):
"""Test hddtemp if host unreachable."""
assert setup_component(self.hass, "sensor", VALID_CONFIG_HOST)
self.hass.block_till_done()
assert len(self.hass.states.all()) == 0
@patch("telnetlib.Telnet", new=TelnetMock) async def test_hddtemp_host_refused(hass, telnetmock):
def test_hddtemp_host_unreachable(self): """Test hddtemp if host is refused."""
assert await async_setup_component(hass, "sensor", VALID_CONFIG_HOST_REFUSED)
await hass.async_block_till_done()
assert len(hass.states.async_all()) == 0
async def test_hddtemp_host_unreachable(hass, telnetmock):
"""Test hddtemp if host unreachable.""" """Test hddtemp if host unreachable."""
assert setup_component(self.hass, "sensor", VALID_CONFIG_HOST_UNREACHABLE) assert await async_setup_component(hass, "sensor", VALID_CONFIG_HOST_UNREACHABLE)
self.hass.block_till_done() await hass.async_block_till_done()
assert len(self.hass.states.all()) == 0 assert len(hass.states.async_all()) == 0