Rewrite mhz19 unittest tests to pytest style tests (#41441)

* Rewrite mhz19 unittest tests to pytest style tests

* Move mhz19 tests imports
This commit is contained in:
Alexander Pitkin 2020-10-10 14:05:08 +03:00 committed by GitHub
parent 66aa55dddf
commit 85b01a9b80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,6 @@
"""Tests for MH-Z19 sensor.""" """Tests for MH-Z19 sensor."""
import unittest from pmsensor import co2sensor
from pmsensor.co2sensor import read_mh_z19_with_temperature
import homeassistant.components.mhz19.sensor as mhz19 import homeassistant.components.mhz19.sensor as mhz19
from homeassistant.components.sensor import DOMAIN from homeassistant.components.sensor import DOMAIN
@ -8,52 +9,41 @@ from homeassistant.const import (
TEMP_CELSIUS, TEMP_CELSIUS,
TEMP_FAHRENHEIT, TEMP_FAHRENHEIT,
) )
from homeassistant.setup import setup_component from homeassistant.setup import async_setup_component
from tests.async_mock import DEFAULT, Mock, patch from tests.async_mock import DEFAULT, Mock, patch
from tests.common import assert_setup_component, get_test_home_assistant from tests.common import assert_setup_component
class TestMHZ19Sensor(unittest.TestCase): async def test_setup_missing_config(hass):
"""Test the MH-Z19 sensor."""
hass = None
def setup_method(self, method):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
def teardown_method(self, method):
"""Stop everything that was started."""
self.hass.stop()
def test_setup_missing_config(self):
"""Test setup with configuration missing required entries.""" """Test setup with configuration missing required entries."""
with assert_setup_component(0): with assert_setup_component(0):
assert setup_component(self.hass, DOMAIN, {"sensor": {"platform": "mhz19"}}) assert await async_setup_component(
hass, DOMAIN, {"sensor": {"platform": "mhz19"}}
)
@patch("pmsensor.co2sensor.read_mh_z19", side_effect=OSError("test error"))
def test_setup_failed_connect(self, mock_co2): @patch("pmsensor.co2sensor.read_mh_z19", side_effect=OSError("test error"))
async def test_setup_failed_connect(mock_co2, hass):
"""Test setup when connection error occurs.""" """Test setup when connection error occurs."""
assert not mhz19.setup_platform( assert not mhz19.setup_platform(
self.hass, hass,
{"platform": "mhz19", mhz19.CONF_SERIAL_DEVICE: "test.serial"}, {"platform": "mhz19", mhz19.CONF_SERIAL_DEVICE: "test.serial"},
None, None,
) )
def test_setup_connected(self):
async def test_setup_connected(hass):
"""Test setup when connection succeeds.""" """Test setup when connection succeeds."""
with patch.multiple( with patch.multiple(
"pmsensor.co2sensor", "pmsensor.co2sensor",
read_mh_z19=DEFAULT, read_mh_z19=DEFAULT,
read_mh_z19_with_temperature=DEFAULT, read_mh_z19_with_temperature=DEFAULT,
): ):
from pmsensor.co2sensor import read_mh_z19_with_temperature
read_mh_z19_with_temperature.return_value = None read_mh_z19_with_temperature.return_value = None
mock_add = Mock() mock_add = Mock()
assert mhz19.setup_platform( assert mhz19.setup_platform(
self.hass, hass,
{ {
"platform": "mhz19", "platform": "mhz19",
"monitored_conditions": ["co2", "temperature"], "monitored_conditions": ["co2", "temperature"],
@ -63,41 +53,37 @@ class TestMHZ19Sensor(unittest.TestCase):
) )
assert mock_add.call_count == 1 assert mock_add.call_count == 1
@patch(
@patch(
"pmsensor.co2sensor.read_mh_z19_with_temperature", "pmsensor.co2sensor.read_mh_z19_with_temperature",
side_effect=OSError("test error"), side_effect=OSError("test error"),
) )
def aiohttp_client_update_oserror(self, mock_function): async def aiohttp_client_update_oserror(mock_function):
"""Test MHZClient when library throws OSError.""" """Test MHZClient when library throws OSError."""
from pmsensor import co2sensor
client = mhz19.MHZClient(co2sensor, "test.serial") client = mhz19.MHZClient(co2sensor, "test.serial")
client.update() client.update()
assert {} == client.data assert {} == client.data
@patch("pmsensor.co2sensor.read_mh_z19_with_temperature", return_value=(5001, 24))
def aiohttp_client_update_ppm_overflow(self, mock_function):
"""Test MHZClient when ppm is too high."""
from pmsensor import co2sensor
@patch("pmsensor.co2sensor.read_mh_z19_with_temperature", return_value=(5001, 24))
async def aiohttp_client_update_ppm_overflow(mock_function):
"""Test MHZClient when ppm is too high."""
client = mhz19.MHZClient(co2sensor, "test.serial") client = mhz19.MHZClient(co2sensor, "test.serial")
client.update() client.update()
assert client.data.get("co2") is None assert client.data.get("co2") is None
@patch("pmsensor.co2sensor.read_mh_z19_with_temperature", return_value=(1000, 24))
def aiohttp_client_update_good_read(self, mock_function):
"""Test MHZClient when ppm is too high."""
from pmsensor import co2sensor
@patch("pmsensor.co2sensor.read_mh_z19_with_temperature", return_value=(1000, 24))
async def aiohttp_client_update_good_read(mock_function):
"""Test MHZClient when ppm is too high."""
client = mhz19.MHZClient(co2sensor, "test.serial") client = mhz19.MHZClient(co2sensor, "test.serial")
client.update() client.update()
assert {"temperature": 24, "co2": 1000} == client.data assert {"temperature": 24, "co2": 1000} == client.data
@patch("pmsensor.co2sensor.read_mh_z19_with_temperature", return_value=(1000, 24))
def test_co2_sensor(self, mock_function):
"""Test CO2 sensor."""
from pmsensor import co2sensor
@patch("pmsensor.co2sensor.read_mh_z19_with_temperature", return_value=(1000, 24))
async def test_co2_sensor(mock_function):
"""Test CO2 sensor."""
client = mhz19.MHZClient(co2sensor, "test.serial") client = mhz19.MHZClient(co2sensor, "test.serial")
sensor = mhz19.MHZ19Sensor(client, mhz19.SENSOR_CO2, None, "name") sensor = mhz19.MHZ19Sensor(client, mhz19.SENSOR_CO2, None, "name")
sensor.update() sensor.update()
@ -108,11 +94,10 @@ class TestMHZ19Sensor(unittest.TestCase):
assert sensor.should_poll assert sensor.should_poll
assert sensor.device_state_attributes == {"temperature": 24} assert sensor.device_state_attributes == {"temperature": 24}
@patch("pmsensor.co2sensor.read_mh_z19_with_temperature", return_value=(1000, 24))
def test_temperature_sensor(self, mock_function):
"""Test temperature sensor."""
from pmsensor import co2sensor
@patch("pmsensor.co2sensor.read_mh_z19_with_temperature", return_value=(1000, 24))
async def test_temperature_sensor(mock_function):
"""Test temperature sensor."""
client = mhz19.MHZClient(co2sensor, "test.serial") client = mhz19.MHZClient(co2sensor, "test.serial")
sensor = mhz19.MHZ19Sensor(client, mhz19.SENSOR_TEMPERATURE, None, "name") sensor = mhz19.MHZ19Sensor(client, mhz19.SENSOR_TEMPERATURE, None, "name")
sensor.update() sensor.update()
@ -123,11 +108,10 @@ class TestMHZ19Sensor(unittest.TestCase):
assert sensor.should_poll assert sensor.should_poll
assert sensor.device_state_attributes == {"co2_concentration": 1000} assert sensor.device_state_attributes == {"co2_concentration": 1000}
@patch("pmsensor.co2sensor.read_mh_z19_with_temperature", return_value=(1000, 24))
def test_temperature_sensor_f(self, mock_function):
"""Test temperature sensor."""
from pmsensor import co2sensor
@patch("pmsensor.co2sensor.read_mh_z19_with_temperature", return_value=(1000, 24))
async def test_temperature_sensor_f(mock_function):
"""Test temperature sensor."""
client = mhz19.MHZClient(co2sensor, "test.serial") client = mhz19.MHZClient(co2sensor, "test.serial")
sensor = mhz19.MHZ19Sensor( sensor = mhz19.MHZ19Sensor(
client, mhz19.SENSOR_TEMPERATURE, TEMP_FAHRENHEIT, "name" client, mhz19.SENSOR_TEMPERATURE, TEMP_FAHRENHEIT, "name"