Convert Bang & Olufsen testing logging patches to caplog (#124366)

* Convert logging patches to caplog

* Remove unnecessary caplog log level handling
pull/124294/head^2
Markus Jacobsen 2024-08-21 17:11:01 +02:00 committed by GitHub
parent 88b95c1236
commit d86b816491
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 42 additions and 41 deletions

View File

@ -1,7 +1,8 @@
"""Test the Bang & Olufsen media_player entity.""" """Test the Bang & Olufsen media_player entity."""
from contextlib import nullcontext as does_not_raise from contextlib import nullcontext as does_not_raise
from unittest.mock import ANY, patch import logging
from unittest.mock import patch
from mozart_api.models import PlaybackContentMetadata from mozart_api.models import PlaybackContentMetadata
import pytest import pytest
@ -71,21 +72,21 @@ from tests.typing import WebSocketGenerator
async def test_initialization( async def test_initialization(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_mozart_client hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
mock_config_entry: MockConfigEntry,
mock_mozart_client,
) -> None: ) -> None:
"""Test the integration is initialized properly in _initialize, async_added_to_hass and __init__.""" """Test the integration is initialized properly in _initialize, async_added_to_hass and __init__."""
caplog.set_level(logging.DEBUG)
# Setup entity # Setup entity
with patch( mock_config_entry.add_to_hass(hass)
"homeassistant.components.bang_olufsen.media_player._LOGGER.debug" await hass.config_entries.async_setup(mock_config_entry.entry_id)
) as mock_logger:
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
# Ensure that the logger has been called with the debug message # Ensure that the logger has been called with the debug message
mock_logger.assert_called_once_with( assert "Connected to: Beosound Balance 11111111 running SW 1.0.0" in caplog.text
"Connected to: %s %s running SW %s", "Beosound Balance", "11111111", "1.0.0"
)
# Check state (The initial state in this test does not contain all that much. # Check state (The initial state in this test does not contain all that much.
# States are tested using simulated WebSocket events.) # States are tested using simulated WebSocket events.)
@ -167,7 +168,10 @@ async def test_async_update_playback_metadata(
async def test_async_update_playback_error( async def test_async_update_playback_error(
hass: HomeAssistant, mock_mozart_client, mock_config_entry hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
mock_mozart_client,
mock_config_entry,
) -> None: ) -> None:
"""Test _async_update_playback_error.""" """Test _async_update_playback_error."""
@ -175,18 +179,15 @@ async def test_async_update_playback_error(
await hass.config_entries.async_setup(mock_config_entry.entry_id) await hass.config_entries.async_setup(mock_config_entry.entry_id)
# The async_dispatcher_send function seems to swallow exceptions, making pytest.raises unusable # The async_dispatcher_send function seems to swallow exceptions, making pytest.raises unusable
with patch("homeassistant.helpers.dispatcher._LOGGER.error") as mock_logger: async_dispatcher_send(
async_dispatcher_send( hass,
hass, f"{TEST_SERIAL_NUMBER}_{WebsocketNotification.PLAYBACK_ERROR}",
f"{TEST_SERIAL_NUMBER}_{WebsocketNotification.PLAYBACK_ERROR}", TEST_PLAYBACK_ERROR,
TEST_PLAYBACK_ERROR, )
)
# The traceback can't be tested, so it is replaced with "ANY" assert (
mock_logger.assert_called_once_with( "Exception in _async_update_playback_error when dispatching '11111111_playback_error': (PlaybackError(error='Test error', item=None),)"
"%s\n%s", in caplog.text
"Exception in _async_update_playback_error when dispatching '11111111_playback_error': (PlaybackError(error='Test error', item=None),)",
ANY,
) )
@ -731,32 +732,32 @@ async def test_async_play_media_overlay_absolute_volume_uri(
async def test_async_play_media_overlay_invalid_offset_volume_tts( async def test_async_play_media_overlay_invalid_offset_volume_tts(
hass: HomeAssistant, mock_mozart_client, mock_config_entry hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
mock_mozart_client,
mock_config_entry,
) -> None: ) -> None:
"""Test async_play_media with Home Assistant invalid offset volume and B&O tts.""" """Test async_play_media with Home Assistant invalid offset volume and B&O tts."""
mock_config_entry.add_to_hass(hass) mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id) await hass.config_entries.async_setup(mock_config_entry.entry_id)
with patch( await hass.services.async_call(
"homeassistant.components.bang_olufsen.media_player._LOGGER.warning" "media_player",
) as mock_logger: "play_media",
await hass.services.async_call( {
"media_player", ATTR_ENTITY_ID: TEST_MEDIA_PLAYER_ENTITY_ID,
"play_media", ATTR_MEDIA_CONTENT_ID: "Dette er en test",
{ ATTR_MEDIA_CONTENT_TYPE: "overlay_tts",
ATTR_ENTITY_ID: TEST_MEDIA_PLAYER_ENTITY_ID, ATTR_MEDIA_ANNOUNCE: True,
ATTR_MEDIA_CONTENT_ID: "Dette er en test", ATTR_MEDIA_EXTRA: {
ATTR_MEDIA_CONTENT_TYPE: "overlay_tts", "overlay_offset_volume": 20,
ATTR_MEDIA_ANNOUNCE: True, "overlay_tts_language": "da-dk",
ATTR_MEDIA_EXTRA: {
"overlay_offset_volume": 20,
"overlay_tts_language": "da-dk",
},
}, },
blocking=True, },
) blocking=True,
mock_logger.assert_called_once_with("Error setting volume") )
assert "Error setting volume" in caplog.text
mock_mozart_client.post_overlay_play.assert_called_once_with( mock_mozart_client.post_overlay_play.assert_called_once_with(
TEST_OVERLAY_INVALID_OFFSET_VOLUME_TTS TEST_OVERLAY_INVALID_OFFSET_VOLUME_TTS