Skip to content

Commit

Permalink
Improve test coverage for onedrive (home-assistant#138410)
Browse files Browse the repository at this point in the history
* Improve test coverage for onedrive

* set done in quality scale
  • Loading branch information
zweckj authored Feb 12, 2025
1 parent 03b3097 commit 641b487
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
3 changes: 2 additions & 1 deletion homeassistant/components/onedrive/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
AgentBackup,
BackupAgent,
BackupAgentError,
BackupNotFound,
suggested_filename,
)
from homeassistant.core import HomeAssistant, callback
Expand Down Expand Up @@ -137,7 +138,7 @@ async def async_download_backup(
"""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
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/onedrive/quality_scale.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ rules:
log-when-unavailable: done
parallel-updates: done
reauthentication-flow: done
test-coverage: todo
test-coverage: done

# Gold
devices: done
Expand Down
39 changes: 37 additions & 2 deletions tests/components/onedrive/test_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
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
Expand Down Expand Up @@ -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"),
[
Expand Down Expand Up @@ -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

0 comments on commit 641b487

Please sign in to comment.