Use caplog fixture for log capturing (#46214)

This commit is contained in:
Anders Melchiorsen 2021-02-08 14:33:57 +01:00 committed by GitHub
parent 0780e52ca4
commit 48002f47f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 42 deletions

View file

@ -1,5 +1,6 @@
"""The tests for the automation component."""
import asyncio
import logging
from unittest.mock import Mock, patch
import pytest
@ -152,7 +153,7 @@ async def test_two_triggers(hass, calls):
assert len(calls) == 2
async def test_trigger_service_ignoring_condition(hass, calls):
async def test_trigger_service_ignoring_condition(hass, caplog, calls):
"""Test triggers."""
assert await async_setup_component(
hass,
@ -171,11 +172,15 @@ async def test_trigger_service_ignoring_condition(hass, calls):
},
)
with patch("homeassistant.components.automation.LOGGER.warning") as logwarn:
caplog.clear()
caplog.set_level(logging.WARNING)
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(calls) == 0
assert len(logwarn.mock_calls) == 1
assert len(caplog.record_tuples) == 1
assert caplog.record_tuples[0][1] == logging.WARNING
await hass.services.async_call(
"automation", "trigger", {"entity_id": "automation.test"}, blocking=True

View file

@ -1,5 +1,6 @@
"""The tests for numeric state automation."""
from datetime import timedelta
import logging
from unittest.mock import patch
import pytest
@ -572,7 +573,7 @@ async def test_if_not_fires_if_entity_not_match(hass, calls, below):
assert len(calls) == 0
async def test_if_not_fires_and_warns_if_below_entity_unknown(hass, calls):
async def test_if_not_fires_and_warns_if_below_entity_unknown(hass, caplog, calls):
"""Test if warns with unknown below entity."""
assert await async_setup_component(
hass,
@ -589,13 +590,15 @@ async def test_if_not_fires_and_warns_if_below_entity_unknown(hass, calls):
},
)
with patch(
"homeassistant.components.homeassistant.triggers.numeric_state._LOGGER.warning"
) as logwarn:
caplog.clear()
caplog.set_level(logging.WARNING)
hass.states.async_set("test.entity", 1)
await hass.async_block_till_done()
assert len(calls) == 0
assert len(logwarn.mock_calls) == 1
assert len(caplog.record_tuples) == 1
assert caplog.record_tuples[0][1] == logging.WARNING
@pytest.mark.parametrize("below", (10, "input_number.value_10"))
@ -1203,7 +1206,7 @@ async def test_wait_template_with_trigger(hass, calls, above):
hass.states.async_set("test.entity", "8")
await hass.async_block_till_done()
assert len(calls) == 1
assert "numeric_state - test.entity - 12" == calls[0].data["some"]
assert calls[0].data["some"] == "numeric_state - test.entity - 12"
@pytest.mark.parametrize(

View file

@ -1,5 +1,5 @@
"""Test the condition helper."""
from logging import ERROR
from logging import ERROR, WARNING
from unittest.mock import patch
import pytest
@ -338,23 +338,25 @@ async def test_time_using_input_datetime(hass):
assert not condition.time(hass, before="input_datetime.not_existing")
async def test_if_numeric_state_raises_on_unavailable(hass):
async def test_if_numeric_state_raises_on_unavailable(hass, caplog):
"""Test numeric_state raises on unavailable/unknown state."""
test = await condition.async_from_config(
hass,
{"condition": "numeric_state", "entity_id": "sensor.temperature", "below": 42},
)
with patch("homeassistant.helpers.condition._LOGGER.warning") as logwarn:
caplog.clear()
caplog.set_level(WARNING)
hass.states.async_set("sensor.temperature", "unavailable")
with pytest.raises(ConditionError):
test(hass)
assert len(logwarn.mock_calls) == 0
assert len(caplog.record_tuples) == 0
hass.states.async_set("sensor.temperature", "unknown")
with pytest.raises(ConditionError):
test(hass)
assert len(logwarn.mock_calls) == 0
assert len(caplog.record_tuples) == 0
async def test_state_multiple_entities(hass):

View file

@ -990,7 +990,7 @@ async def test_wait_for_trigger_generated_exception(hass, caplog):
assert "something bad" in caplog.text
async def test_condition_warning(hass):
async def test_condition_warning(hass, caplog):
"""Test warning on condition."""
event = "test_event"
events = async_capture_events(hass, event)
@ -1007,11 +1007,15 @@ async def test_condition_warning(hass):
)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
caplog.clear()
caplog.set_level(logging.WARNING)
hass.states.async_set("test.entity", "string")
with patch("homeassistant.helpers.script._LOGGER.warning") as logwarn:
await script_obj.async_run(context=Context())
await hass.async_block_till_done()
assert len(logwarn.mock_calls) == 1
assert len(caplog.record_tuples) == 1
assert caplog.record_tuples[0][1] == logging.WARNING
assert len(events) == 1
@ -1127,7 +1131,7 @@ async def test_repeat_count(hass):
@pytest.mark.parametrize("condition", ["while", "until"])
async def test_repeat_condition_warning(hass, condition):
async def test_repeat_condition_warning(hass, caplog, condition):
"""Test warning on repeat conditions."""
event = "test_event"
events = async_capture_events(hass, event)
@ -1156,10 +1160,14 @@ async def test_repeat_condition_warning(hass, condition):
# wait_started = async_watch_for_action(script_obj, "wait")
hass.states.async_set("sensor.test", "1")
with patch("homeassistant.helpers.script._LOGGER.warning") as logwarn:
caplog.clear()
caplog.set_level(logging.WARNING)
hass.async_create_task(script_obj.async_run(context=Context()))
await asyncio.wait_for(hass.async_block_till_done(), 1)
assert len(logwarn.mock_calls) == 1
assert len(caplog.record_tuples) == 1
assert caplog.record_tuples[0][1] == logging.WARNING
assert len(events) == count
@ -1369,7 +1377,7 @@ async def test_repeat_nested(hass, variables, first_last, inside_x):
}
async def test_choose_warning(hass):
async def test_choose_warning(hass, caplog):
"""Test warning on choose."""
event = "test_event"
events = async_capture_events(hass, event)
@ -1404,11 +1412,15 @@ async def test_choose_warning(hass):
hass.states.async_set("test.entity", "9")
await hass.async_block_till_done()
with patch("homeassistant.helpers.script._LOGGER.warning") as logwarn:
caplog.clear()
caplog.set_level(logging.WARNING)
await script_obj.async_run(context=Context())
await hass.async_block_till_done()
print(logwarn.mock_calls)
assert len(logwarn.mock_calls) == 2
assert len(caplog.record_tuples) == 2
assert caplog.record_tuples[0][1] == logging.WARNING
assert caplog.record_tuples[1][1] == logging.WARNING
assert len(events) == 1
assert events[0].data["choice"] == "default"