Upgrade respx to 0.16.2 (#43892)

* Bump respx to 0.16.2

* Align sensor tests to use new respx mock api
This commit is contained in:
Jonas Lundberg 2020-12-04 04:27:25 +01:00 committed by GitHub
parent 52edf6719d
commit a5279cc279
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 76 deletions

View file

@ -24,6 +24,6 @@ pytest-xdist==2.1.0
pytest==6.1.2
requests_mock==1.8.0
responses==0.12.0
respx==0.14.0
respx==0.16.2
stdlib-list==0.7.0
tqdm==4.49.0

View file

@ -18,7 +18,7 @@ from homeassistant.const import (
)
from homeassistant.setup import async_setup_component
from tests.async_mock import Mock, patch
from tests.async_mock import patch
async def test_setup_missing_basic_config(hass):
@ -50,9 +50,7 @@ async def test_setup_missing_config(hass):
@respx.mock
async def test_setup_failed_connect(hass):
"""Test setup when connection error occurs."""
respx.get(
"http://localhost", content=httpx.RequestError(message="any", request=Mock())
)
respx.get("http://localhost").mock(side_effect=httpx.RequestError)
assert await async_setup_component(
hass,
binary_sensor.DOMAIN,
@ -71,7 +69,7 @@ async def test_setup_failed_connect(hass):
@respx.mock
async def test_setup_timeout(hass):
"""Test setup when connection timeout occurs."""
respx.get("http://localhost", content=asyncio.TimeoutError())
respx.get("http://localhost").mock(side_effect=asyncio.TimeoutError())
assert await async_setup_component(
hass,
binary_sensor.DOMAIN,
@ -90,7 +88,7 @@ async def test_setup_timeout(hass):
@respx.mock
async def test_setup_minimum(hass):
"""Test setup with minimum configuration."""
respx.get("http://localhost", status_code=200)
respx.get("http://localhost") % 200
assert await async_setup_component(
hass,
binary_sensor.DOMAIN,
@ -109,7 +107,7 @@ async def test_setup_minimum(hass):
@respx.mock
async def test_setup_minimum_resource_template(hass):
"""Test setup with minimum configuration (resource_template)."""
respx.get("http://localhost", status_code=200)
respx.get("http://localhost") % 200
assert await async_setup_component(
hass,
binary_sensor.DOMAIN,
@ -127,7 +125,7 @@ async def test_setup_minimum_resource_template(hass):
@respx.mock
async def test_setup_duplicate_resource_template(hass):
"""Test setup with duplicate resources."""
respx.get("http://localhost", status_code=200)
respx.get("http://localhost") % 200
assert await async_setup_component(
hass,
binary_sensor.DOMAIN,
@ -146,7 +144,7 @@ async def test_setup_duplicate_resource_template(hass):
@respx.mock
async def test_setup_get(hass):
"""Test setup with valid configuration."""
respx.get("http://localhost", status_code=200, content="{}")
respx.get("http://localhost").respond(status_code=200, json={})
assert await async_setup_component(
hass,
"binary_sensor",
@ -174,7 +172,7 @@ async def test_setup_get(hass):
@respx.mock
async def test_setup_get_digest_auth(hass):
"""Test setup with valid configuration."""
respx.get("http://localhost", status_code=200, content="{}")
respx.get("http://localhost").respond(status_code=200, json={})
assert await async_setup_component(
hass,
"binary_sensor",
@ -202,7 +200,7 @@ async def test_setup_get_digest_auth(hass):
@respx.mock
async def test_setup_post(hass):
"""Test setup with valid configuration."""
respx.post("http://localhost", status_code=200, content="{}")
respx.post("http://localhost").respond(status_code=200, json={})
assert await async_setup_component(
hass,
"binary_sensor",
@ -230,11 +228,10 @@ async def test_setup_post(hass):
@respx.mock
async def test_setup_get_off(hass):
"""Test setup with valid off configuration."""
respx.get(
"http://localhost",
respx.get("http://localhost").respond(
status_code=200,
headers={"content-type": "text/json"},
content='{"dog": false}',
json={"dog": False},
)
assert await async_setup_component(
hass,
@ -261,11 +258,10 @@ async def test_setup_get_off(hass):
@respx.mock
async def test_setup_get_on(hass):
"""Test setup with valid on configuration."""
respx.get(
"http://localhost",
respx.get("http://localhost").respond(
status_code=200,
headers={"content-type": "text/json"},
content='{"dog": true}',
json={"dog": True},
)
assert await async_setup_component(
hass,
@ -292,7 +288,7 @@ async def test_setup_get_on(hass):
@respx.mock
async def test_setup_with_exception(hass):
"""Test setup with exception."""
respx.get("http://localhost", status_code=200, content="{}")
respx.get("http://localhost").respond(status_code=200, json={})
assert await async_setup_component(
hass,
"binary_sensor",
@ -318,9 +314,7 @@ async def test_setup_with_exception(hass):
await hass.async_block_till_done()
respx.clear()
respx.get(
"http://localhost", content=httpx.RequestError(message="any", request=Mock())
)
respx.get("http://localhost").mock(side_effect=httpx.RequestError)
await hass.services.async_call(
"homeassistant",
"update_entity",
@ -337,7 +331,7 @@ async def test_setup_with_exception(hass):
async def test_reload(hass):
"""Verify we can reload reset sensors."""
respx.get("http://localhost", status_code=200)
respx.get("http://localhost") % 200
await async_setup_component(
hass,
@ -380,10 +374,7 @@ async def test_reload(hass):
@respx.mock
async def test_setup_query_params(hass):
"""Test setup with query params."""
respx.get(
"http://localhost?search=something",
status_code=200,
)
respx.get("http://localhost", params={"search": "something"}) % 200
assert await async_setup_component(
hass,
binary_sensor.DOMAIN,

View file

@ -16,7 +16,7 @@ from homeassistant.const import (
)
from homeassistant.setup import async_setup_component
from tests.async_mock import Mock, patch
from tests.async_mock import patch
async def test_setup_missing_config(hass):
@ -42,9 +42,7 @@ async def test_setup_missing_schema(hass):
@respx.mock
async def test_setup_failed_connect(hass):
"""Test setup when connection error occurs."""
respx.get(
"http://localhost", content=httpx.RequestError(message="any", request=Mock())
)
respx.get("http://localhost").mock(side_effect=httpx.RequestError)
assert await async_setup_component(
hass,
sensor.DOMAIN,
@ -63,7 +61,7 @@ async def test_setup_failed_connect(hass):
@respx.mock
async def test_setup_timeout(hass):
"""Test setup when connection timeout occurs."""
respx.get("http://localhost", content=asyncio.TimeoutError())
respx.get("http://localhost").mock(side_effect=asyncio.TimeoutError())
assert await async_setup_component(
hass,
sensor.DOMAIN,
@ -76,7 +74,7 @@ async def test_setup_timeout(hass):
@respx.mock
async def test_setup_minimum(hass):
"""Test setup with minimum configuration."""
respx.get("http://localhost", status_code=200)
respx.get("http://localhost") % 200
assert await async_setup_component(
hass,
sensor.DOMAIN,
@ -95,7 +93,7 @@ async def test_setup_minimum(hass):
@respx.mock
async def test_setup_minimum_resource_template(hass):
"""Test setup with minimum configuration (resource_template)."""
respx.get("http://localhost", status_code=200)
respx.get("http://localhost") % 200
assert await async_setup_component(
hass,
sensor.DOMAIN,
@ -113,7 +111,7 @@ async def test_setup_minimum_resource_template(hass):
@respx.mock
async def test_setup_duplicate_resource_template(hass):
"""Test setup with duplicate resources."""
respx.get("http://localhost", status_code=200)
respx.get("http://localhost") % 200
assert await async_setup_component(
hass,
sensor.DOMAIN,
@ -132,7 +130,7 @@ async def test_setup_duplicate_resource_template(hass):
@respx.mock
async def test_setup_get(hass):
"""Test setup with valid configuration."""
respx.get("http://localhost", status_code=200, content="{}")
respx.get("http://localhost").respond(status_code=200, json={})
assert await async_setup_component(
hass,
"sensor",
@ -161,7 +159,7 @@ async def test_setup_get(hass):
@respx.mock
async def test_setup_get_digest_auth(hass):
"""Test setup with valid configuration."""
respx.get("http://localhost", status_code=200, content="{}")
respx.get("http://localhost").respond(status_code=200, json={})
assert await async_setup_component(
hass,
"sensor",
@ -190,7 +188,7 @@ async def test_setup_get_digest_auth(hass):
@respx.mock
async def test_setup_post(hass):
"""Test setup with valid configuration."""
respx.post("http://localhost", status_code=200, content="{}")
respx.post("http://localhost").respond(status_code=200, json={})
assert await async_setup_component(
hass,
"sensor",
@ -219,8 +217,7 @@ async def test_setup_post(hass):
@respx.mock
async def test_setup_get_xml(hass):
"""Test setup with valid xml configuration."""
respx.get(
"http://localhost",
respx.get("http://localhost").respond(
status_code=200,
headers={"content-type": "text/xml"},
content="<dog>abc</dog>",
@ -252,10 +249,7 @@ async def test_setup_get_xml(hass):
@respx.mock
async def test_setup_query_params(hass):
"""Test setup with query params."""
respx.get(
"http://localhost?search=something",
status_code=200,
)
respx.get("http://localhost", params={"search": "something"}) % 200
assert await async_setup_component(
hass,
sensor.DOMAIN,
@ -276,11 +270,9 @@ async def test_setup_query_params(hass):
async def test_update_with_json_attrs(hass):
"""Test attributes get extracted from a JSON result."""
respx.get(
"http://localhost",
respx.get("http://localhost").respond(
status_code=200,
headers={"content-type": CONTENT_TYPE_JSON},
content='{ "key": "some_json_value" }',
json={"key": "some_json_value"},
)
assert await async_setup_component(
hass,
@ -311,11 +303,9 @@ async def test_update_with_json_attrs(hass):
async def test_update_with_no_template(hass):
"""Test update when there is no value template."""
respx.get(
"http://localhost",
respx.get("http://localhost").respond(
status_code=200,
headers={"content-type": CONTENT_TYPE_JSON},
content='{ "key": "some_json_value" }',
json={"key": "some_json_value"},
)
assert await async_setup_component(
hass,
@ -338,15 +328,14 @@ async def test_update_with_no_template(hass):
assert len(hass.states.async_all()) == 1
state = hass.states.get("sensor.foo")
assert state.state == '{ "key": "some_json_value" }'
assert state.state == '{"key": "some_json_value"}'
@respx.mock
async def test_update_with_json_attrs_no_data(hass, caplog):
"""Test attributes when no JSON result fetched."""
respx.get(
"http://localhost",
respx.get("http://localhost").respond(
status_code=200,
headers={"content-type": CONTENT_TYPE_JSON},
content="",
@ -382,11 +371,9 @@ async def test_update_with_json_attrs_no_data(hass, caplog):
async def test_update_with_json_attrs_not_dict(hass, caplog):
"""Test attributes get extracted from a JSON result."""
respx.get(
"http://localhost",
respx.get("http://localhost").respond(
status_code=200,
headers={"content-type": CONTENT_TYPE_JSON},
content='["list", "of", "things"]',
json=["list", "of", "things"],
)
assert await async_setup_component(
hass,
@ -419,8 +406,7 @@ async def test_update_with_json_attrs_not_dict(hass, caplog):
async def test_update_with_json_attrs_bad_JSON(hass, caplog):
"""Test attributes get extracted from a JSON result."""
respx.get(
"http://localhost",
respx.get("http://localhost").respond(
status_code=200,
headers={"content-type": CONTENT_TYPE_JSON},
content="This is text rather than JSON data.",
@ -456,11 +442,17 @@ async def test_update_with_json_attrs_bad_JSON(hass, caplog):
async def test_update_with_json_attrs_with_json_attrs_path(hass):
"""Test attributes get extracted from a JSON result with a template for the attributes."""
respx.get(
"http://localhost",
respx.get("http://localhost").respond(
status_code=200,
headers={"content-type": CONTENT_TYPE_JSON},
content='{ "toplevel": {"master_value": "master", "second_level": {"some_json_key": "some_json_value", "some_json_key2": "some_json_value2" } } }',
json={
"toplevel": {
"master_value": "master",
"second_level": {
"some_json_key": "some_json_value",
"some_json_key2": "some_json_value2",
},
},
},
)
assert await async_setup_component(
hass,
@ -494,8 +486,7 @@ async def test_update_with_json_attrs_with_json_attrs_path(hass):
async def test_update_with_xml_convert_json_attrs_with_json_attrs_path(hass):
"""Test attributes get extracted from a JSON result that was converted from XML with a template for the attributes."""
respx.get(
"http://localhost",
respx.get("http://localhost").respond(
status_code=200,
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>",
@ -531,8 +522,7 @@ async def test_update_with_xml_convert_json_attrs_with_json_attrs_path(hass):
async def test_update_with_xml_convert_json_attrs_with_jsonattr_template(hass):
"""Test attributes get extracted from a JSON result that was converted from XML."""
respx.get(
"http://localhost",
respx.get("http://localhost").respond(
status_code=200,
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>',
@ -573,8 +563,7 @@ async def test_update_with_application_xml_convert_json_attrs_with_jsonattr_temp
):
"""Test attributes get extracted from a JSON result that was converted from XML with application/xml mime type."""
respx.get(
"http://localhost",
respx.get("http://localhost").respond(
status_code=200,
headers={"content-type": "application/xml"},
content="<main><dog>1</dog><cat>3</cat></main>",
@ -610,8 +599,7 @@ async def test_update_with_application_xml_convert_json_attrs_with_jsonattr_temp
async def test_update_with_xml_convert_bad_xml(hass, caplog):
"""Test attributes get extracted from a XML result with bad xml."""
respx.get(
"http://localhost",
respx.get("http://localhost").respond(
status_code=200,
headers={"content-type": "text/xml"},
content="",
@ -646,8 +634,7 @@ async def test_update_with_xml_convert_bad_xml(hass, caplog):
async def test_update_with_failed_get(hass, caplog):
"""Test attributes get extracted from a XML result with bad xml."""
respx.get(
"http://localhost",
respx.get("http://localhost").respond(
status_code=200,
headers={"content-type": "text/xml"},
content="",
@ -682,7 +669,7 @@ async def test_update_with_failed_get(hass, caplog):
async def test_reload(hass):
"""Verify we can reload reset sensors."""
respx.get("http://localhost", status_code=200)
respx.get("http://localhost") % 200
await async_setup_component(
hass,