mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +00:00
Remove classes in nx584 tests (#42628)
This commit is contained in:
parent
8f22c5917f
commit
9fc92ab04e
@ -46,93 +46,97 @@ def client(fake_zones):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures("client")
|
@pytest.mark.usefixtures("client")
|
||||||
class TestNX584SensorSetup:
|
@mock.patch("homeassistant.components.nx584.binary_sensor.NX584Watcher")
|
||||||
"""Test the NX584 sensor platform."""
|
@mock.patch("homeassistant.components.nx584.binary_sensor.NX584ZoneSensor")
|
||||||
|
def test_nx584_sensor_setup_defaults(mock_nx, mock_watcher, hass, fake_zones):
|
||||||
|
"""Test the setup with no configuration."""
|
||||||
|
add_entities = mock.MagicMock()
|
||||||
|
config = {
|
||||||
|
"host": nx584.DEFAULT_HOST,
|
||||||
|
"port": nx584.DEFAULT_PORT,
|
||||||
|
"exclude_zones": [],
|
||||||
|
"zone_types": {},
|
||||||
|
}
|
||||||
|
assert nx584.setup_platform(hass, config, add_entities)
|
||||||
|
mock_nx.assert_has_calls([mock.call(zone, "opening") for zone in fake_zones])
|
||||||
|
assert add_entities.called
|
||||||
|
assert nx584_client.Client.call_count == 1
|
||||||
|
assert nx584_client.Client.call_args == mock.call("http://localhost:5007")
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
@mock.patch("homeassistant.components.nx584.binary_sensor.NX584Watcher")
|
|
||||||
@mock.patch("homeassistant.components.nx584.binary_sensor.NX584ZoneSensor")
|
|
||||||
def test_setup_defaults(mock_nx, mock_watcher, hass, fake_zones):
|
|
||||||
"""Test the setup with no configuration."""
|
|
||||||
add_entities = mock.MagicMock()
|
|
||||||
config = {
|
|
||||||
"host": nx584.DEFAULT_HOST,
|
|
||||||
"port": nx584.DEFAULT_PORT,
|
|
||||||
"exclude_zones": [],
|
|
||||||
"zone_types": {},
|
|
||||||
}
|
|
||||||
assert nx584.setup_platform(hass, config, add_entities)
|
|
||||||
mock_nx.assert_has_calls([mock.call(zone, "opening") for zone in fake_zones])
|
|
||||||
assert add_entities.called
|
|
||||||
assert nx584_client.Client.call_count == 1
|
|
||||||
assert nx584_client.Client.call_args == mock.call("http://localhost:5007")
|
|
||||||
|
|
||||||
@staticmethod
|
@pytest.mark.usefixtures("client")
|
||||||
@mock.patch("homeassistant.components.nx584.binary_sensor.NX584Watcher")
|
@mock.patch("homeassistant.components.nx584.binary_sensor.NX584Watcher")
|
||||||
@mock.patch("homeassistant.components.nx584.binary_sensor.NX584ZoneSensor")
|
@mock.patch("homeassistant.components.nx584.binary_sensor.NX584ZoneSensor")
|
||||||
def test_setup_full_config(mock_nx, mock_watcher, hass, fake_zones):
|
def test_nx584_sensor_setup_full_config(mock_nx, mock_watcher, hass, fake_zones):
|
||||||
"""Test the setup with full configuration."""
|
"""Test the setup with full configuration."""
|
||||||
config = {
|
config = {
|
||||||
"host": "foo",
|
"host": "foo",
|
||||||
"port": 123,
|
"port": 123,
|
||||||
"exclude_zones": [2],
|
"exclude_zones": [2],
|
||||||
"zone_types": {3: "motion"},
|
"zone_types": {3: "motion"},
|
||||||
}
|
}
|
||||||
add_entities = mock.MagicMock()
|
add_entities = mock.MagicMock()
|
||||||
assert nx584.setup_platform(hass, config, add_entities)
|
assert nx584.setup_platform(hass, config, add_entities)
|
||||||
mock_nx.assert_has_calls(
|
mock_nx.assert_has_calls(
|
||||||
[
|
|
||||||
mock.call(fake_zones[0], "opening"),
|
|
||||||
mock.call(fake_zones[2], "motion"),
|
|
||||||
]
|
|
||||||
)
|
|
||||||
assert add_entities.called
|
|
||||||
assert nx584_client.Client.call_count == 1
|
|
||||||
assert nx584_client.Client.call_args == mock.call("http://foo:123")
|
|
||||||
assert mock_watcher.called
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
async def _test_assert_graceful_fail(hass, config):
|
|
||||||
"""Test the failing."""
|
|
||||||
assert not await async_setup_component(hass, "nx584", config)
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
|
||||||
"config",
|
|
||||||
[
|
[
|
||||||
({"exclude_zones": ["a"]}),
|
mock.call(fake_zones[0], "opening"),
|
||||||
({"zone_types": {"a": "b"}}),
|
mock.call(fake_zones[2], "motion"),
|
||||||
({"zone_types": {1: "notatype"}}),
|
]
|
||||||
({"zone_types": {"notazone": "motion"}}),
|
|
||||||
],
|
|
||||||
)
|
)
|
||||||
async def test_setup_bad_config(self, hass, config):
|
assert add_entities.called
|
||||||
"""Test the setup with bad configuration."""
|
assert nx584_client.Client.call_count == 1
|
||||||
await self._test_assert_graceful_fail(hass, config)
|
assert nx584_client.Client.call_args == mock.call("http://foo:123")
|
||||||
|
assert mock_watcher.called
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
|
||||||
"exception_type",
|
|
||||||
[
|
|
||||||
pytest.param(requests.exceptions.ConnectionError, id="connect_failed"),
|
|
||||||
pytest.param(IndexError, id="no_partitions"),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
async def test_setup_with_exceptions(self, hass, exception_type):
|
|
||||||
"""Test the setup handles exceptions."""
|
|
||||||
nx584_client.Client.return_value.list_zones.side_effect = exception_type
|
|
||||||
await self._test_assert_graceful_fail(hass, {})
|
|
||||||
|
|
||||||
async def test_setup_version_too_old(self, hass):
|
async def _test_assert_graceful_fail(hass, config):
|
||||||
"""Test if version is too old."""
|
"""Test the failing."""
|
||||||
nx584_client.Client.return_value.get_version.return_value = "1.0"
|
assert not await async_setup_component(hass, "nx584", config)
|
||||||
await self._test_assert_graceful_fail(hass, {})
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def test_setup_no_zones(hass):
|
@pytest.mark.usefixtures("client")
|
||||||
"""Test the setup with no zones."""
|
@pytest.mark.parametrize(
|
||||||
nx584_client.Client.return_value.list_zones.return_value = []
|
"config",
|
||||||
add_entities = mock.MagicMock()
|
[
|
||||||
assert nx584.setup_platform(hass, {}, add_entities)
|
({"exclude_zones": ["a"]}),
|
||||||
assert not add_entities.called
|
({"zone_types": {"a": "b"}}),
|
||||||
|
({"zone_types": {1: "notatype"}}),
|
||||||
|
({"zone_types": {"notazone": "motion"}}),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_nx584_sensor_setup_bad_config(hass, config):
|
||||||
|
"""Test the setup with bad configuration."""
|
||||||
|
await _test_assert_graceful_fail(hass, config)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("client")
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"exception_type",
|
||||||
|
[
|
||||||
|
pytest.param(requests.exceptions.ConnectionError, id="connect_failed"),
|
||||||
|
pytest.param(IndexError, id="no_partitions"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_nx584_sensor_setup_with_exceptions(hass, exception_type):
|
||||||
|
"""Test the setup handles exceptions."""
|
||||||
|
nx584_client.Client.return_value.list_zones.side_effect = exception_type
|
||||||
|
await _test_assert_graceful_fail(hass, {})
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("client")
|
||||||
|
async def test_nx584_sensor_setup_version_too_old(hass):
|
||||||
|
"""Test if version is too old."""
|
||||||
|
nx584_client.Client.return_value.get_version.return_value = "1.0"
|
||||||
|
await _test_assert_graceful_fail(hass, {})
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("client")
|
||||||
|
def test_nx584_sensor_setup_no_zones(hass):
|
||||||
|
"""Test the setup with no zones."""
|
||||||
|
nx584_client.Client.return_value.list_zones.return_value = []
|
||||||
|
add_entities = mock.MagicMock()
|
||||||
|
assert nx584.setup_platform(hass, {}, add_entities)
|
||||||
|
assert not add_entities.called
|
||||||
|
|
||||||
|
|
||||||
def test_nx584_zone_sensor_normal():
|
def test_nx584_zone_sensor_normal():
|
||||||
@ -148,81 +152,77 @@ def test_nx584_zone_sensor_normal():
|
|||||||
assert not sensor.is_on
|
assert not sensor.is_on
|
||||||
|
|
||||||
|
|
||||||
class TestNX584Watcher:
|
@mock.patch.object(nx584.NX584ZoneSensor, "schedule_update_ha_state")
|
||||||
"""Test the NX584 watcher."""
|
def test_nx584_watcher_process_zone_event(mock_update):
|
||||||
|
"""Test the processing of zone events."""
|
||||||
|
zone1 = {"number": 1, "name": "foo", "state": True}
|
||||||
|
zone2 = {"number": 2, "name": "bar", "state": True}
|
||||||
|
zones = {
|
||||||
|
1: nx584.NX584ZoneSensor(zone1, "motion"),
|
||||||
|
2: nx584.NX584ZoneSensor(zone2, "motion"),
|
||||||
|
}
|
||||||
|
watcher = nx584.NX584Watcher(None, zones)
|
||||||
|
watcher._process_zone_event({"zone": 1, "zone_state": False})
|
||||||
|
assert not zone1["state"]
|
||||||
|
assert mock_update.call_count == 1
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
@mock.patch.object(nx584.NX584ZoneSensor, "schedule_update_ha_state")
|
|
||||||
def test_process_zone_event(mock_update):
|
|
||||||
"""Test the processing of zone events."""
|
|
||||||
zone1 = {"number": 1, "name": "foo", "state": True}
|
|
||||||
zone2 = {"number": 2, "name": "bar", "state": True}
|
|
||||||
zones = {
|
|
||||||
1: nx584.NX584ZoneSensor(zone1, "motion"),
|
|
||||||
2: nx584.NX584ZoneSensor(zone2, "motion"),
|
|
||||||
}
|
|
||||||
watcher = nx584.NX584Watcher(None, zones)
|
|
||||||
watcher._process_zone_event({"zone": 1, "zone_state": False})
|
|
||||||
assert not zone1["state"]
|
|
||||||
assert mock_update.call_count == 1
|
|
||||||
|
|
||||||
@staticmethod
|
@mock.patch.object(nx584.NX584ZoneSensor, "schedule_update_ha_state")
|
||||||
@mock.patch.object(nx584.NX584ZoneSensor, "schedule_update_ha_state")
|
def test_nx584_watcher_process_zone_event_missing_zone(mock_update):
|
||||||
def test_process_zone_event_missing_zone(mock_update):
|
"""Test the processing of zone events with missing zones."""
|
||||||
"""Test the processing of zone events with missing zones."""
|
watcher = nx584.NX584Watcher(None, {})
|
||||||
watcher = nx584.NX584Watcher(None, {})
|
watcher._process_zone_event({"zone": 1, "zone_state": False})
|
||||||
watcher._process_zone_event({"zone": 1, "zone_state": False})
|
assert not mock_update.called
|
||||||
assert not mock_update.called
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def test_run_with_zone_events():
|
|
||||||
"""Test the zone events."""
|
|
||||||
empty_me = [1, 2]
|
|
||||||
|
|
||||||
def fake_get_events():
|
def test_nx584_watcher_run_with_zone_events():
|
||||||
"""Return nothing twice, then some events."""
|
"""Test the zone events."""
|
||||||
if empty_me:
|
empty_me = [1, 2]
|
||||||
empty_me.pop()
|
|
||||||
else:
|
|
||||||
return fake_events
|
|
||||||
|
|
||||||
client = mock.MagicMock()
|
def fake_get_events():
|
||||||
fake_events = [
|
"""Return nothing twice, then some events."""
|
||||||
{"zone": 1, "zone_state": True, "type": "zone_status"},
|
if empty_me:
|
||||||
{"zone": 2, "foo": False},
|
empty_me.pop()
|
||||||
]
|
else:
|
||||||
client.get_events.side_effect = fake_get_events
|
return fake_events
|
||||||
watcher = nx584.NX584Watcher(client, {})
|
|
||||||
|
|
||||||
@mock.patch.object(watcher, "_process_zone_event")
|
client = mock.MagicMock()
|
||||||
def run(fake_process):
|
fake_events = [
|
||||||
"""Run a fake process."""
|
{"zone": 1, "zone_state": True, "type": "zone_status"},
|
||||||
fake_process.side_effect = StopMe
|
{"zone": 2, "foo": False},
|
||||||
with pytest.raises(StopMe):
|
]
|
||||||
watcher._run()
|
client.get_events.side_effect = fake_get_events
|
||||||
assert fake_process.call_count == 1
|
watcher = nx584.NX584Watcher(client, {})
|
||||||
assert fake_process.call_args == mock.call(fake_events[0])
|
|
||||||
|
|
||||||
run()
|
@mock.patch.object(watcher, "_process_zone_event")
|
||||||
assert 3 == client.get_events.call_count
|
def run(fake_process):
|
||||||
|
"""Run a fake process."""
|
||||||
|
fake_process.side_effect = StopMe
|
||||||
|
with pytest.raises(StopMe):
|
||||||
|
watcher._run()
|
||||||
|
assert fake_process.call_count == 1
|
||||||
|
assert fake_process.call_args == mock.call(fake_events[0])
|
||||||
|
|
||||||
@staticmethod
|
run()
|
||||||
@mock.patch("time.sleep")
|
assert 3 == client.get_events.call_count
|
||||||
def test_run_retries_failures(mock_sleep):
|
|
||||||
"""Test the retries with failures."""
|
|
||||||
empty_me = [1, 2]
|
|
||||||
|
|
||||||
def fake_run():
|
|
||||||
"""Fake runner."""
|
|
||||||
if empty_me:
|
|
||||||
empty_me.pop()
|
|
||||||
raise requests.exceptions.ConnectionError()
|
|
||||||
raise StopMe()
|
|
||||||
|
|
||||||
watcher = nx584.NX584Watcher(None, {})
|
@mock.patch("time.sleep")
|
||||||
with mock.patch.object(watcher, "_run") as mock_inner:
|
def test_nx584_watcher_run_retries_failures(mock_sleep):
|
||||||
mock_inner.side_effect = fake_run
|
"""Test the retries with failures."""
|
||||||
with pytest.raises(StopMe):
|
empty_me = [1, 2]
|
||||||
watcher.run()
|
|
||||||
assert 3 == mock_inner.call_count
|
def fake_run():
|
||||||
mock_sleep.assert_has_calls([mock.call(10), mock.call(10)])
|
"""Fake runner."""
|
||||||
|
if empty_me:
|
||||||
|
empty_me.pop()
|
||||||
|
raise requests.exceptions.ConnectionError()
|
||||||
|
raise StopMe()
|
||||||
|
|
||||||
|
watcher = nx584.NX584Watcher(None, {})
|
||||||
|
with mock.patch.object(watcher, "_run") as mock_inner:
|
||||||
|
mock_inner.side_effect = fake_run
|
||||||
|
with pytest.raises(StopMe):
|
||||||
|
watcher.run()
|
||||||
|
assert 3 == mock_inner.call_count
|
||||||
|
mock_sleep.assert_has_calls([mock.call(10), mock.call(10)])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user