mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Rewrite mochad unittest to pytest style (#42164)
This commit is contained in:
parent
9a54c31c34
commit
fcc895f294
@ -1,14 +1,12 @@
|
|||||||
"""The tests for the mochad light platform."""
|
"""The tests for the mochad light platform."""
|
||||||
import unittest
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components import light
|
from homeassistant.components import light
|
||||||
from homeassistant.components.mochad import light as mochad
|
from homeassistant.components.mochad import light as mochad
|
||||||
from homeassistant.setup import setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
import tests.async_mock as mock
|
import tests.async_mock as mock
|
||||||
from tests.common import get_test_home_assistant
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
@ -18,122 +16,51 @@ def pymochad_mock():
|
|||||||
yield device
|
yield device
|
||||||
|
|
||||||
|
|
||||||
class TestMochadSwitchSetup(unittest.TestCase):
|
@pytest.fixture
|
||||||
"""Test the mochad light."""
|
def light_mock(hass, brightness):
|
||||||
|
"""Mock light."""
|
||||||
PLATFORM = mochad
|
controller_mock = mock.MagicMock()
|
||||||
COMPONENT = light
|
dev_dict = {"address": "a1", "name": "fake_light", "brightness_levels": brightness}
|
||||||
THING = "light"
|
return mochad.MochadLight(hass, controller_mock, dev_dict)
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
"""Set up things to be run when tests are started."""
|
|
||||||
self.hass = get_test_home_assistant()
|
|
||||||
self.addCleanup(self.hass.stop)
|
|
||||||
|
|
||||||
@mock.patch("homeassistant.components.mochad.light.MochadLight")
|
|
||||||
def test_setup_adds_proper_devices(self, mock_light):
|
|
||||||
"""Test if setup adds devices."""
|
|
||||||
good_config = {
|
|
||||||
"mochad": {},
|
|
||||||
"light": {
|
|
||||||
"platform": "mochad",
|
|
||||||
"devices": [{"name": "Light1", "address": "a1"}],
|
|
||||||
},
|
|
||||||
}
|
|
||||||
assert setup_component(self.hass, light.DOMAIN, good_config)
|
|
||||||
|
|
||||||
|
|
||||||
class TestMochadLight(unittest.TestCase):
|
async def test_setup_adds_proper_devices(hass):
|
||||||
"""Test for mochad light platform."""
|
"""Test if setup adds devices."""
|
||||||
|
good_config = {
|
||||||
def setUp(self):
|
"mochad": {},
|
||||||
"""Set up things to be run when tests are started."""
|
"light": {
|
||||||
self.hass = get_test_home_assistant()
|
"platform": "mochad",
|
||||||
controller_mock = mock.MagicMock()
|
"devices": [{"name": "Light1", "address": "a1"}],
|
||||||
dev_dict = {"address": "a1", "name": "fake_light", "brightness_levels": 32}
|
},
|
||||||
self.light = mochad.MochadLight(self.hass, controller_mock, dev_dict)
|
}
|
||||||
|
assert await async_setup_component(hass, light.DOMAIN, good_config)
|
||||||
def teardown_method(self, method):
|
|
||||||
"""Stop everything that was started."""
|
|
||||||
self.hass.stop()
|
|
||||||
|
|
||||||
def test_name(self):
|
|
||||||
"""Test the name."""
|
|
||||||
assert "fake_light" == self.light.name
|
|
||||||
|
|
||||||
def test_turn_on_with_no_brightness(self):
|
|
||||||
"""Test turn_on."""
|
|
||||||
self.light.turn_on()
|
|
||||||
self.light.light.send_cmd.assert_called_once_with("on")
|
|
||||||
|
|
||||||
def test_turn_on_with_brightness(self):
|
|
||||||
"""Test turn_on."""
|
|
||||||
self.light.turn_on(brightness=45)
|
|
||||||
self.light.light.send_cmd.assert_has_calls(
|
|
||||||
[mock.call("on"), mock.call("dim 25")]
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_turn_off(self):
|
|
||||||
"""Test turn_off."""
|
|
||||||
self.light.turn_off()
|
|
||||||
self.light.light.send_cmd.assert_called_once_with("off")
|
|
||||||
|
|
||||||
|
|
||||||
class TestMochadLight256Levels(unittest.TestCase):
|
@pytest.mark.parametrize(
|
||||||
"""Test for mochad light platform."""
|
"brightness,expected", [(32, "on"), (256, "xdim 255"), (64, "xdim 63")]
|
||||||
|
)
|
||||||
def setUp(self):
|
async def test_turn_on_with_no_brightness(light_mock, expected):
|
||||||
"""Set up things to be run when tests are started."""
|
"""Test turn_on."""
|
||||||
self.hass = get_test_home_assistant()
|
light_mock.turn_on()
|
||||||
controller_mock = mock.MagicMock()
|
light_mock.light.send_cmd.assert_called_once_with(expected)
|
||||||
dev_dict = {"address": "a1", "name": "fake_light", "brightness_levels": 256}
|
|
||||||
self.light = mochad.MochadLight(self.hass, controller_mock, dev_dict)
|
|
||||||
|
|
||||||
def teardown_method(self, method):
|
|
||||||
"""Stop everything that was started."""
|
|
||||||
self.hass.stop()
|
|
||||||
|
|
||||||
def test_turn_on_with_no_brightness(self):
|
|
||||||
"""Test turn_on."""
|
|
||||||
self.light.turn_on()
|
|
||||||
self.light.light.send_cmd.assert_called_once_with("xdim 255")
|
|
||||||
|
|
||||||
def test_turn_on_with_brightness(self):
|
|
||||||
"""Test turn_on."""
|
|
||||||
self.light.turn_on(brightness=45)
|
|
||||||
self.light.light.send_cmd.assert_called_once_with("xdim 45")
|
|
||||||
|
|
||||||
def test_turn_off(self):
|
|
||||||
"""Test turn_off."""
|
|
||||||
self.light.turn_off()
|
|
||||||
self.light.light.send_cmd.assert_called_once_with("off")
|
|
||||||
|
|
||||||
|
|
||||||
class TestMochadLight64Levels(unittest.TestCase):
|
@pytest.mark.parametrize(
|
||||||
"""Test for mochad light platform."""
|
"brightness,expected",
|
||||||
|
[
|
||||||
|
(32, [mock.call("on"), mock.call("dim 25")]),
|
||||||
|
(256, [mock.call("xdim 45")]),
|
||||||
|
(64, [mock.call("xdim 11")]),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_turn_on_with_brightness(light_mock, expected):
|
||||||
|
"""Test turn_on."""
|
||||||
|
light_mock.turn_on(brightness=45)
|
||||||
|
light_mock.light.send_cmd.assert_has_calls(expected)
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
"""Set up things to be run when tests are started."""
|
|
||||||
self.hass = get_test_home_assistant()
|
|
||||||
controller_mock = mock.MagicMock()
|
|
||||||
dev_dict = {"address": "a1", "name": "fake_light", "brightness_levels": 64}
|
|
||||||
self.light = mochad.MochadLight(self.hass, controller_mock, dev_dict)
|
|
||||||
|
|
||||||
def teardown_method(self, method):
|
@pytest.mark.parametrize("brightness", [32])
|
||||||
"""Stop everything that was started."""
|
async def test_turn_off(light_mock):
|
||||||
self.hass.stop()
|
"""Test turn_off."""
|
||||||
|
light_mock.turn_off()
|
||||||
def test_turn_on_with_no_brightness(self):
|
light_mock.light.send_cmd.assert_called_once_with("off")
|
||||||
"""Test turn_on."""
|
|
||||||
self.light.turn_on()
|
|
||||||
self.light.light.send_cmd.assert_called_once_with("xdim 63")
|
|
||||||
|
|
||||||
def test_turn_on_with_brightness(self):
|
|
||||||
"""Test turn_on."""
|
|
||||||
self.light.turn_on(brightness=45)
|
|
||||||
self.light.light.send_cmd.assert_called_once_with("xdim 11")
|
|
||||||
|
|
||||||
def test_turn_off(self):
|
|
||||||
"""Test turn_off."""
|
|
||||||
self.light.turn_off()
|
|
||||||
self.light.light.send_cmd.assert_called_once_with("off")
|
|
||||||
|
@ -1,14 +1,11 @@
|
|||||||
"""The tests for the mochad switch platform."""
|
"""The tests for the mochad switch platform."""
|
||||||
import unittest
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components import switch
|
from homeassistant.components import switch
|
||||||
from homeassistant.components.mochad import switch as mochad
|
from homeassistant.components.mochad import switch as mochad
|
||||||
from homeassistant.setup import setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
import tests.async_mock as mock
|
import tests.async_mock as mock
|
||||||
from tests.common import get_test_home_assistant
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
@ -20,55 +17,38 @@ def pymochad_mock():
|
|||||||
yield
|
yield
|
||||||
|
|
||||||
|
|
||||||
class TestMochadSwitchSetup(unittest.TestCase):
|
@pytest.fixture
|
||||||
"""Test the mochad switch."""
|
def switch_mock(hass):
|
||||||
|
"""Mock switch."""
|
||||||
PLATFORM = mochad
|
controller_mock = mock.MagicMock()
|
||||||
COMPONENT = switch
|
dev_dict = {"address": "a1", "name": "fake_switch"}
|
||||||
THING = "switch"
|
return mochad.MochadSwitch(hass, controller_mock, dev_dict)
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
"""Set up things to be run when tests are started."""
|
|
||||||
self.hass = get_test_home_assistant()
|
|
||||||
self.addCleanup(self.hass.stop)
|
|
||||||
|
|
||||||
@mock.patch("homeassistant.components.mochad.switch.MochadSwitch")
|
|
||||||
def test_setup_adds_proper_devices(self, mock_switch):
|
|
||||||
"""Test if setup adds devices."""
|
|
||||||
good_config = {
|
|
||||||
"mochad": {},
|
|
||||||
"switch": {
|
|
||||||
"platform": "mochad",
|
|
||||||
"devices": [{"name": "Switch1", "address": "a1"}],
|
|
||||||
},
|
|
||||||
}
|
|
||||||
assert setup_component(self.hass, switch.DOMAIN, good_config)
|
|
||||||
|
|
||||||
|
|
||||||
class TestMochadSwitch(unittest.TestCase):
|
async def test_setup_adds_proper_devices(hass):
|
||||||
"""Test for mochad switch platform."""
|
"""Test if setup adds devices."""
|
||||||
|
good_config = {
|
||||||
|
"mochad": {},
|
||||||
|
"switch": {
|
||||||
|
"platform": "mochad",
|
||||||
|
"devices": [{"name": "Switch1", "address": "a1"}],
|
||||||
|
},
|
||||||
|
}
|
||||||
|
assert await async_setup_component(hass, switch.DOMAIN, good_config)
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
"""Set up things to be run when tests are started."""
|
|
||||||
self.hass = get_test_home_assistant()
|
|
||||||
controller_mock = mock.MagicMock()
|
|
||||||
dev_dict = {"address": "a1", "name": "fake_switch"}
|
|
||||||
self.switch = mochad.MochadSwitch(self.hass, controller_mock, dev_dict)
|
|
||||||
|
|
||||||
def teardown_method(self, method):
|
async def test_name(switch_mock):
|
||||||
"""Stop everything that was started."""
|
"""Test the name."""
|
||||||
self.hass.stop()
|
assert "fake_switch" == switch_mock.name
|
||||||
|
|
||||||
def test_name(self):
|
|
||||||
"""Test the name."""
|
|
||||||
assert "fake_switch" == self.switch.name
|
|
||||||
|
|
||||||
def test_turn_on(self):
|
async def test_turn_on(switch_mock):
|
||||||
"""Test turn_on."""
|
"""Test turn_on."""
|
||||||
self.switch.turn_on()
|
switch_mock.turn_on()
|
||||||
self.switch.switch.send_cmd.assert_called_once_with("on")
|
switch_mock.switch.send_cmd.assert_called_once_with("on")
|
||||||
|
|
||||||
def test_turn_off(self):
|
|
||||||
"""Test turn_off."""
|
async def test_turn_off(switch_mock):
|
||||||
self.switch.turn_off()
|
"""Test turn_off."""
|
||||||
self.switch.switch.send_cmd.assert_called_once_with("off")
|
switch_mock.turn_off()
|
||||||
|
switch_mock.switch.send_cmd.assert_called_once_with("off")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user