mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
Improve test coverage for onedrive (#138410)
* Improve test coverage for onedrive * set done in quality scale
This commit is contained in:
parent
03b3097c34
commit
641b487196
@ -25,6 +25,7 @@ from homeassistant.components.backup import (
|
||||
AgentBackup,
|
||||
BackupAgent,
|
||||
BackupAgentError,
|
||||
BackupNotFound,
|
||||
suggested_filename,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
@ -137,7 +138,7 @@ class OneDriveBackupAgent(BackupAgent):
|
||||
"""Download a backup file."""
|
||||
backups = await self._list_cached_backups()
|
||||
if backup_id not in backups:
|
||||
raise BackupAgentError("Backup not found")
|
||||
raise BackupNotFound("Backup not found")
|
||||
|
||||
stream = await self._client.download_drive_item(
|
||||
backups[backup_id].backup_file_id, timeout=TIMEOUT
|
||||
|
@ -40,7 +40,7 @@ rules:
|
||||
log-when-unavailable: done
|
||||
parallel-updates: done
|
||||
reauthentication-flow: done
|
||||
test-coverage: todo
|
||||
test-coverage: done
|
||||
|
||||
# Gold
|
||||
devices: done
|
||||
|
@ -14,13 +14,16 @@ from onedrive_personal_sdk.exceptions import (
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.backup import DOMAIN as BACKUP_DOMAIN, AgentBackup
|
||||
from homeassistant.components.onedrive.const import DOMAIN
|
||||
from homeassistant.components.onedrive.backup import (
|
||||
async_register_backup_agents_listener,
|
||||
)
|
||||
from homeassistant.components.onedrive.const import DATA_BACKUP_AGENT_LISTENERS, DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_REAUTH
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from . import setup_integration
|
||||
from .const import BACKUP_METADATA
|
||||
from .const import BACKUP_METADATA, MOCK_BACKUP_FILE, MOCK_METADATA_FILE
|
||||
|
||||
from tests.common import AsyncMock, MockConfigEntry
|
||||
from tests.typing import ClientSessionGenerator, MagicMock, WebSocketGenerator
|
||||
@ -241,6 +244,26 @@ async def test_agents_download(
|
||||
assert await resp.content.read() == b"backup data"
|
||||
|
||||
|
||||
async def test_error_on_agents_download(
|
||||
hass_client: ClientSessionGenerator,
|
||||
mock_onedrive_client: MagicMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test we get not found on an not existing backup on download."""
|
||||
client = await hass_client()
|
||||
backup_id = BACKUP_METADATA["backup_id"]
|
||||
mock_onedrive_client.list_drive_items.side_effect = [
|
||||
[MOCK_BACKUP_FILE, MOCK_METADATA_FILE],
|
||||
[],
|
||||
]
|
||||
|
||||
with patch("homeassistant.components.onedrive.backup.CACHE_TTL", -1):
|
||||
resp = await client.get(
|
||||
f"/api/backup/download/{backup_id}?agent_id={DOMAIN}.{mock_config_entry.unique_id}"
|
||||
)
|
||||
assert resp.status == 404
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("side_effect", "error"),
|
||||
[
|
||||
@ -349,3 +372,15 @@ async def test_reauth_on_403(
|
||||
assert "context" in flow
|
||||
assert flow["context"]["source"] == SOURCE_REAUTH
|
||||
assert flow["context"]["entry_id"] == mock_config_entry.entry_id
|
||||
|
||||
|
||||
async def test_listeners_get_cleaned_up(hass: HomeAssistant) -> None:
|
||||
"""Test listener gets cleaned up."""
|
||||
listener = MagicMock()
|
||||
remove_listener = async_register_backup_agents_listener(hass, listener=listener)
|
||||
|
||||
# make sure it's the last listener
|
||||
hass.data[DATA_BACKUP_AGENT_LISTENERS] = [listener]
|
||||
remove_listener()
|
||||
|
||||
assert hass.data.get(DATA_BACKUP_AGENT_LISTENERS) is None
|
||||
|
Loading…
x
Reference in New Issue
Block a user