mirror of
https://github.com/home-assistant/core.git
synced 2026-04-03 00:46:46 +01:00
Co-authored-by: mik-laj <12058428+mik-laj@users.noreply.github.com> Co-authored-by: Claude Haiku 4.5 <noreply@anthropic.com>
93 lines
2.8 KiB
Python
93 lines
2.8 KiB
Python
"""Common fixtures for the AWS S3 tests."""
|
|
|
|
from collections.abc import AsyncIterator, Generator
|
|
import json
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from homeassistant.components.aws_s3.backup import suggested_filenames
|
|
from homeassistant.components.aws_s3.const import DOMAIN
|
|
from homeassistant.components.backup import AgentBackup
|
|
|
|
from .const import CONFIG_ENTRY_DATA
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
|
@pytest.fixture
|
|
def backup_size() -> int:
|
|
"""Backup size, override in tests to change defaults."""
|
|
return 2**20
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_agent_backup(backup_size: int) -> AgentBackup:
|
|
"""Test backup fixture."""
|
|
return AgentBackup(
|
|
addons=[],
|
|
backup_id="23e64aec",
|
|
date="2024-11-22T11:48:48.727189+01:00",
|
|
database_included=True,
|
|
extra_metadata={},
|
|
folders=[],
|
|
homeassistant_included=True,
|
|
homeassistant_version="2024.12.0.dev0",
|
|
name="Core 2024.12.0.dev0",
|
|
protected=False,
|
|
size=backup_size,
|
|
)
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def mock_client(mock_agent_backup: AgentBackup) -> Generator[AsyncMock]:
|
|
"""Mock the S3 client."""
|
|
with patch(
|
|
"aiobotocore.session.AioSession.create_client",
|
|
autospec=True,
|
|
return_value=AsyncMock(),
|
|
) as create_client:
|
|
client = create_client.return_value
|
|
|
|
tar_file, metadata_file = suggested_filenames(mock_agent_backup)
|
|
|
|
# Mock the paginator for list_objects_v2
|
|
client.get_paginator = MagicMock()
|
|
client.get_paginator.return_value.paginate.return_value.__aiter__.return_value = [
|
|
{"Contents": [{"Key": tar_file}, {"Key": metadata_file}]}
|
|
]
|
|
|
|
client.create_multipart_upload.return_value = {"UploadId": "upload_id"}
|
|
client.upload_part.return_value = {"ETag": "etag"}
|
|
|
|
# to simplify this mock, we assume that backup is always "iterated" over, while metadata is always "read" as a whole
|
|
class MockStream:
|
|
async def iter_chunks(self) -> AsyncIterator[bytes]:
|
|
yield b"backup data"
|
|
|
|
async def read(self) -> bytes:
|
|
return json.dumps(mock_agent_backup.as_dict()).encode()
|
|
|
|
client.get_object.return_value = {"Body": MockStream()}
|
|
client.head_bucket.return_value = {}
|
|
|
|
create_client.return_value.__aenter__.return_value = client
|
|
yield client
|
|
|
|
|
|
@pytest.fixture
|
|
def config_entry_extra_data() -> dict:
|
|
"""Extra config entry data, override in tests to change defaults."""
|
|
return {}
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_config_entry(config_entry_extra_data: dict) -> MockConfigEntry:
|
|
"""Return the default mocked config entry."""
|
|
return MockConfigEntry(
|
|
entry_id="test",
|
|
title="test",
|
|
domain=DOMAIN,
|
|
data=CONFIG_ENTRY_DATA | config_entry_extra_data,
|
|
)
|