mirror of
https://github.com/home-assistant/core.git
synced 2025-04-24 01:08:12 +00:00
Axis - Improve tests based on feedback (#30430)
* Use MockConfigEntry, add_to_hass and hass.config_entries.async_setup * Mock method instead of handling paths
This commit is contained in:
parent
0944d02262
commit
b57da2f862
@ -8,6 +8,8 @@ import pytest
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components import axis
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
MAC = "00408C12345"
|
||||
MODEL = "model"
|
||||
NAME = "name"
|
||||
@ -55,7 +57,7 @@ root.Properties.Image.Format=jpeg,mjpeg,h264
|
||||
root.Properties.Image.NbrOfViews=2
|
||||
root.Properties.Image.Resolution=1920x1080,1280x960,1280x720,1024x768,1024x576,800x600,640x480,640x360,352x240,320x240
|
||||
root.Properties.Image.Rotation=0,180
|
||||
root.Properties.System.SerialNumber=ACCC12345678
|
||||
root.Properties.System.SerialNumber=00408C12345
|
||||
"""
|
||||
|
||||
|
||||
@ -68,41 +70,34 @@ async def setup_axis_integration(
|
||||
properties=DEFAULT_PROPERTIES,
|
||||
):
|
||||
"""Create the Axis device."""
|
||||
config_entry = config_entries.ConfigEntry(
|
||||
version=1,
|
||||
config_entry = MockConfigEntry(
|
||||
domain=axis.DOMAIN,
|
||||
title="Mock Title",
|
||||
data=deepcopy(config),
|
||||
source="test",
|
||||
connection_class=config_entries.CONN_CLASS_LOCAL_PUSH,
|
||||
system_options={},
|
||||
options=deepcopy(options),
|
||||
entry_id="1",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
def mock_request(self, method, path, json=None):
|
||||
if method == "get":
|
||||
if path == "/axis-cgi/param.cgi?action=list&group=root.Brand":
|
||||
return brand
|
||||
if path in [
|
||||
"/axis-cgi/param.cgi?action=list&group=root.Input",
|
||||
"/axis-cgi/param.cgi?action=list&group=root.IOPort",
|
||||
"/axis-cgi/param.cgi?action=list&group=root.Output",
|
||||
]:
|
||||
return ports
|
||||
if path == "/axis-cgi/param.cgi?action=list&group=root.Properties":
|
||||
return properties
|
||||
def mock_update_brand(self):
|
||||
self.process_raw(brand)
|
||||
|
||||
return None
|
||||
def mock_update_ports(self):
|
||||
self.process_raw(ports)
|
||||
|
||||
with patch("axis.vapix.Vapix.request", new=mock_request), patch(
|
||||
def mock_update_properties(self):
|
||||
self.process_raw(properties)
|
||||
|
||||
with patch("axis.param_cgi.Brand.update_brand", new=mock_update_brand), patch(
|
||||
"axis.param_cgi.Ports.update_ports", new=mock_update_ports
|
||||
), patch(
|
||||
"axis.param_cgi.Properties.update_properties", new=mock_update_properties
|
||||
), patch(
|
||||
"axis.AxisDevice.start", return_value=True
|
||||
):
|
||||
await axis.async_setup_entry(hass, config_entry)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
hass.config_entries._entries.append(config_entry)
|
||||
|
||||
return hass.data[axis.DOMAIN].get(config[axis.CONF_MAC])
|
||||
|
||||
|
||||
@ -163,9 +158,7 @@ async def test_device_reset(hass):
|
||||
|
||||
async def test_device_not_accessible(hass):
|
||||
"""Failed setup schedules a retry of setup."""
|
||||
with patch.object(
|
||||
axis.device, "get_device", side_effect=axis.errors.CannotConnect
|
||||
), pytest.raises(axis.device.ConfigEntryNotReady):
|
||||
with patch.object(axis.device, "get_device", side_effect=axis.errors.CannotConnect):
|
||||
await setup_axis_integration(hass)
|
||||
assert hass.data[axis.DOMAIN] == {}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user