Adjust invalid test values in rest (#86009)

This commit is contained in:
epenet 2023-01-16 13:37:38 +01:00 committed by GitHub
parent 6859a9ebdd
commit c1589d3c89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -182,7 +182,9 @@ async def test_setup_duplicate_resource_template(hass: HomeAssistant) -> None:
@respx.mock @respx.mock
async def test_setup_get(hass: HomeAssistant) -> None: async def test_setup_get(hass: HomeAssistant) -> None:
"""Test setup with valid configuration.""" """Test setup with valid configuration."""
respx.get("http://localhost").respond(status_code=HTTPStatus.OK, json={}) respx.get("http://localhost").respond(
status_code=HTTPStatus.OK, json={"key": "123"}
)
assert await async_setup_component( assert await async_setup_component(
hass, hass,
"sensor", "sensor",
@ -210,7 +212,7 @@ async def test_setup_get(hass: HomeAssistant) -> None:
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(hass.states.async_all("sensor")) == 1 assert len(hass.states.async_all("sensor")) == 1
assert hass.states.get("sensor.foo").state == "" assert hass.states.get("sensor.foo").state == "123"
await hass.services.async_call( await hass.services.async_call(
"homeassistant", "homeassistant",
SERVICE_UPDATE_ENTITY, SERVICE_UPDATE_ENTITY,
@ -219,7 +221,7 @@ async def test_setup_get(hass: HomeAssistant) -> None:
) )
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get("sensor.foo") state = hass.states.get("sensor.foo")
assert state.state == "" assert state.state == "123"
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfTemperature.CELSIUS assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfTemperature.CELSIUS
assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.TEMPERATURE assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.TEMPERATURE
assert state.attributes[ATTR_STATE_CLASS] is SensorStateClass.MEASUREMENT assert state.attributes[ATTR_STATE_CLASS] is SensorStateClass.MEASUREMENT
@ -325,7 +327,9 @@ async def test_setup_get_templated_headers_params(hass: HomeAssistant) -> None:
@respx.mock @respx.mock
async def test_setup_get_digest_auth(hass: HomeAssistant) -> None: async def test_setup_get_digest_auth(hass: HomeAssistant) -> None:
"""Test setup with valid configuration.""" """Test setup with valid configuration."""
respx.get("http://localhost").respond(status_code=HTTPStatus.OK, json={}) respx.get("http://localhost").respond(
status_code=HTTPStatus.OK, json={"key": "123"}
)
assert await async_setup_component( assert await async_setup_component(
hass, hass,
"sensor", "sensor",
@ -354,7 +358,9 @@ async def test_setup_get_digest_auth(hass: HomeAssistant) -> None:
@respx.mock @respx.mock
async def test_setup_post(hass: HomeAssistant) -> None: async def test_setup_post(hass: HomeAssistant) -> None:
"""Test setup with valid configuration.""" """Test setup with valid configuration."""
respx.post("http://localhost").respond(status_code=HTTPStatus.OK, json={}) respx.post("http://localhost").respond(
status_code=HTTPStatus.OK, json={"key": "123"}
)
assert await async_setup_component( assert await async_setup_component(
hass, hass,
"sensor", "sensor",
@ -386,7 +392,7 @@ async def test_setup_get_xml(hass: HomeAssistant) -> None:
respx.get("http://localhost").respond( respx.get("http://localhost").respond(
status_code=HTTPStatus.OK, status_code=HTTPStatus.OK,
headers={"content-type": "text/xml"}, headers={"content-type": "text/xml"},
content="<dog>abc</dog>", content="<dog>123</dog>",
) )
assert await async_setup_component( assert await async_setup_component(
hass, hass,
@ -408,7 +414,7 @@ async def test_setup_get_xml(hass: HomeAssistant) -> None:
assert len(hass.states.async_all("sensor")) == 1 assert len(hass.states.async_all("sensor")) == 1
state = hass.states.get("sensor.foo") state = hass.states.get("sensor.foo")
assert state.state == "abc" assert state.state == "123"
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfInformation.MEGABYTES assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfInformation.MEGABYTES
@ -438,7 +444,7 @@ async def test_update_with_json_attrs(hass: HomeAssistant) -> None:
respx.get("http://localhost").respond( respx.get("http://localhost").respond(
status_code=HTTPStatus.OK, status_code=HTTPStatus.OK,
json={"key": "some_json_value"}, json={"key": "123", "other_key": "some_json_value"},
) )
assert await async_setup_component( assert await async_setup_component(
hass, hass,
@ -449,7 +455,7 @@ async def test_update_with_json_attrs(hass: HomeAssistant) -> None:
"resource": "http://localhost", "resource": "http://localhost",
"method": "GET", "method": "GET",
"value_template": "{{ value_json.key }}", "value_template": "{{ value_json.key }}",
"json_attributes": ["key"], "json_attributes": ["other_key"],
"name": "foo", "name": "foo",
"unit_of_measurement": UnitOfInformation.MEGABYTES, "unit_of_measurement": UnitOfInformation.MEGABYTES,
"verify_ssl": "true", "verify_ssl": "true",
@ -461,8 +467,8 @@ async def test_update_with_json_attrs(hass: HomeAssistant) -> None:
assert len(hass.states.async_all("sensor")) == 1 assert len(hass.states.async_all("sensor")) == 1
state = hass.states.get("sensor.foo") state = hass.states.get("sensor.foo")
assert state.state == "some_json_value" assert state.state == "123"
assert state.attributes["key"] == "some_json_value" assert state.attributes["other_key"] == "some_json_value"
@respx.mock @respx.mock
@ -483,7 +489,6 @@ async def test_update_with_no_template(hass: HomeAssistant) -> None:
"method": "GET", "method": "GET",
"json_attributes": ["key"], "json_attributes": ["key"],
"name": "foo", "name": "foo",
"unit_of_measurement": UnitOfInformation.MEGABYTES,
"verify_ssl": "true", "verify_ssl": "true",
"timeout": 30, "timeout": 30,
"headers": {"Accept": "text/xml"}, "headers": {"Accept": "text/xml"},
@ -556,7 +561,6 @@ async def test_update_with_json_attrs_not_dict(
"value_template": "{{ value_json.key }}", "value_template": "{{ value_json.key }}",
"json_attributes": ["key"], "json_attributes": ["key"],
"name": "foo", "name": "foo",
"unit_of_measurement": UnitOfInformation.MEGABYTES,
"verify_ssl": "true", "verify_ssl": "true",
"timeout": 30, "timeout": 30,
"headers": {"Accept": "text/xml"}, "headers": {"Accept": "text/xml"},
@ -568,7 +572,7 @@ async def test_update_with_json_attrs_not_dict(
state = hass.states.get("sensor.foo") state = hass.states.get("sensor.foo")
assert state.state == "" assert state.state == ""
assert state.attributes == {"unit_of_measurement": "MB", "friendly_name": "foo"} assert state.attributes == {"friendly_name": "foo"}
assert "not a dictionary or list" in caplog.text assert "not a dictionary or list" in caplog.text
@ -618,7 +622,7 @@ async def test_update_with_json_attrs_with_json_attrs_path(hass: HomeAssistant)
status_code=HTTPStatus.OK, status_code=HTTPStatus.OK,
json={ json={
"toplevel": { "toplevel": {
"master_value": "master", "master_value": "123",
"second_level": { "second_level": {
"some_json_key": "some_json_value", "some_json_key": "some_json_value",
"some_json_key2": "some_json_value2", "some_json_key2": "some_json_value2",
@ -649,7 +653,7 @@ async def test_update_with_json_attrs_with_json_attrs_path(hass: HomeAssistant)
assert len(hass.states.async_all("sensor")) == 1 assert len(hass.states.async_all("sensor")) == 1
state = hass.states.get("sensor.foo") state = hass.states.get("sensor.foo")
assert state.state == "master" assert state.state == "123"
assert state.attributes["some_json_key"] == "some_json_value" assert state.attributes["some_json_key"] == "some_json_value"
assert state.attributes["some_json_key2"] == "some_json_value2" assert state.attributes["some_json_key2"] == "some_json_value2"
@ -663,7 +667,7 @@ async def test_update_with_xml_convert_json_attrs_with_json_attrs_path(
respx.get("http://localhost").respond( respx.get("http://localhost").respond(
status_code=HTTPStatus.OK, status_code=HTTPStatus.OK,
headers={"content-type": "text/xml"}, headers={"content-type": "text/xml"},
content="<toplevel><master_value>master</master_value><second_level><some_json_key>some_json_value</some_json_key><some_json_key2>some_json_value2</some_json_key2></second_level></toplevel>", content="<toplevel><master_value>123</master_value><second_level><some_json_key>some_json_value</some_json_key><some_json_key2>some_json_value2</some_json_key2></second_level></toplevel>",
) )
assert await async_setup_component( assert await async_setup_component(
hass, hass,
@ -687,7 +691,7 @@ async def test_update_with_xml_convert_json_attrs_with_json_attrs_path(
assert len(hass.states.async_all("sensor")) == 1 assert len(hass.states.async_all("sensor")) == 1
state = hass.states.get("sensor.foo") state = hass.states.get("sensor.foo")
assert state.state == "master" assert state.state == "123"
assert state.attributes["some_json_key"] == "some_json_value" assert state.attributes["some_json_key"] == "some_json_value"
assert state.attributes["some_json_key2"] == "some_json_value2" assert state.attributes["some_json_key2"] == "some_json_value2"
@ -701,7 +705,7 @@ async def test_update_with_xml_convert_json_attrs_with_jsonattr_template(
respx.get("http://localhost").respond( respx.get("http://localhost").respond(
status_code=HTTPStatus.OK, status_code=HTTPStatus.OK,
headers={"content-type": "text/xml"}, headers={"content-type": "text/xml"},
content='<?xml version="1.0" encoding="utf-8"?><response><scan>0</scan><ver>12556</ver><count>48</count><ssid>alexander</ssid><bss><valid>0</valid><name>0</name><privacy>0</privacy><wlan>bogus</wlan><strength>0</strength></bss><led0>0</led0><led1>0</led1><led2>0</led2><led3>0</led3><led4>0</led4><led5>0</led5><led6>0</led6><led7>0</led7><btn0>up</btn0><btn1>up</btn1><btn2>up</btn2><btn3>up</btn3><pot0>0</pot0><usr0>0</usr0><temp0>0x0XF0x0XF</temp0><time0> 0</time0></response>', content='<?xml version="1.0" encoding="utf-8"?><response><scan>0</scan><ver>12556</ver><count>48</count><ssid>alexander</ssid><bss><valid>0</valid><name>0</name><privacy>0</privacy><wlan>123</wlan><strength>0</strength></bss><led0>0</led0><led1>0</led1><led2>0</led2><led3>0</led3><led4>0</led4><led5>0</led5><led6>0</led6><led7>0</led7><btn0>up</btn0><btn1>up</btn1><btn2>up</btn2><btn3>up</btn3><pot0>0</pot0><usr0>0</usr0><temp0>0x0XF0x0XF</temp0><time0> 0</time0></response>',
) )
assert await async_setup_component( assert await async_setup_component(
hass, hass,
@ -725,7 +729,7 @@ async def test_update_with_xml_convert_json_attrs_with_jsonattr_template(
assert len(hass.states.async_all("sensor")) == 1 assert len(hass.states.async_all("sensor")) == 1
state = hass.states.get("sensor.foo") state = hass.states.get("sensor.foo")
assert state.state == "bogus" assert state.state == "123"
assert state.attributes["led0"] == "0" assert state.attributes["led0"] == "0"
assert state.attributes["led1"] == "0" assert state.attributes["led1"] == "0"
assert state.attributes["temp0"] == "0x0XF0x0XF" assert state.attributes["temp0"] == "0x0XF0x0XF"
@ -906,7 +910,7 @@ async def test_entity_config(hass: HomeAssistant) -> None:
}, },
} }
respx.get("http://localhost") % HTTPStatus.OK respx.get("http://localhost").respond(status_code=HTTPStatus.OK, text="123")
assert await async_setup_component(hass, DOMAIN, config) assert await async_setup_component(hass, DOMAIN, config)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -914,7 +918,7 @@ async def test_entity_config(hass: HomeAssistant) -> None:
assert entity_registry.async_get("sensor.rest_sensor").unique_id == "very_unique" assert entity_registry.async_get("sensor.rest_sensor").unique_id == "very_unique"
state = hass.states.get("sensor.rest_sensor") state = hass.states.get("sensor.rest_sensor")
assert state.state == "" assert state.state == "123"
assert state.attributes == { assert state.attributes == {
"device_class": "temperature", "device_class": "temperature",
"entity_picture": "blabla.png", "entity_picture": "blabla.png",