Improve mqtt test (#42490)

This commit is contained in:
Paulus Schoutsen 2020-10-27 23:22:59 +01:00 committed by GitHub
parent f0b676607d
commit d4efa938dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 3 deletions

View file

@ -428,7 +428,7 @@ class Template:
# output; use the original render instead of the evaluated one. # output; use the original render instead of the evaluated one.
if not isinstance(result, str): if not isinstance(result, str):
return result return result
except (ValueError, SyntaxError, MemoryError): except (ValueError, TypeError, SyntaxError, MemoryError):
pass pass
return render_result return render_result

View file

@ -1502,6 +1502,7 @@ async def test_debug_info_qos_retain(hass, mqtt_mock):
async def test_publish_json_from_template(hass, mqtt_mock): async def test_publish_json_from_template(hass, mqtt_mock):
"""Test the publishing of call to services.""" """Test the publishing of call to services."""
test_str = "{'valid': 'python', 'invalid': 'json'}" test_str = "{'valid': 'python', 'invalid': 'json'}"
test_str_tpl = "{'valid': '{{ \"python\" }}', 'invalid': 'json'}"
await async_setup_component( await async_setup_component(
hass, hass,
@ -1511,13 +1512,16 @@ async def test_publish_json_from_template(hass, mqtt_mock):
"test_script_payload": { "test_script_payload": {
"sequence": { "sequence": {
"service": "mqtt.publish", "service": "mqtt.publish",
"data": {"topic": "test-topic", "payload": test_str}, "data": {"topic": "test-topic", "payload": test_str_tpl},
} }
}, },
"test_script_payload_template": { "test_script_payload_template": {
"sequence": { "sequence": {
"service": "mqtt.publish", "service": "mqtt.publish",
"data": {"topic": "test-topic", "payload_template": test_str}, "data": {
"topic": "test-topic",
"payload_template": test_str_tpl,
},
} }
}, },
} }

View file

@ -2698,3 +2698,13 @@ async def test_result_wrappers(hass):
assert result == native assert result == native
assert result.render_result == text assert result.render_result == text
schema(result) # should not raise schema(result) # should not raise
async def test_parse_result(hass):
"""Test parse result."""
for tpl, result in (
('{{ "{{}}" }}', "{{}}"),
("not-something", "not-something"),
("2a", "2a"),
):
assert template.Template(tpl, hass).async_render() == result