Files
core/tests/components/paperless_ngx/conftest.py
Florian von Garrel c14d17f88c Add status sensors to paperless (#145591)
* Add first status sensor and coordinator

* New snapshot

* Add comment

* Add test for forbidden status endpoint

* Changed comment

* Fixed translation

* Minor changes and code optimization

* Add common translation; minor tweaks

* Moved translation from common to integration
2025-05-26 17:24:23 +02:00

94 lines
2.7 KiB
Python

"""Common fixtures for the Paperless-ngx tests."""
from collections.abc import Generator
import json
from unittest.mock import AsyncMock, MagicMock, patch
from pypaperless.models import Statistic, Status
import pytest
from homeassistant.components.paperless_ngx.const import DOMAIN
from homeassistant.core import HomeAssistant
from . import setup_integration
from .const import USER_INPUT_ONE
from tests.common import MockConfigEntry, load_fixture
@pytest.fixture
def mock_status_data() -> Generator[MagicMock]:
"""Return test status data."""
return json.loads(load_fixture("test_data_status.json", DOMAIN))
@pytest.fixture
def mock_statistic_data() -> Generator[MagicMock]:
"""Return test statistic data."""
return json.loads(load_fixture("test_data_statistic.json", DOMAIN))
@pytest.fixture
def mock_statistic_data_update() -> Generator[MagicMock]:
"""Return updated test statistic data."""
return json.loads(load_fixture("test_data_statistic_update.json", DOMAIN))
@pytest.fixture(autouse=True)
def mock_paperless(
mock_statistic_data: MagicMock, mock_status_data: MagicMock
) -> Generator[AsyncMock]:
"""Mock the pypaperless.Paperless client."""
with (
patch(
"homeassistant.components.paperless_ngx.coordinator.Paperless",
autospec=True,
) as paperless_mock,
patch(
"homeassistant.components.paperless_ngx.config_flow.Paperless",
new=paperless_mock,
),
patch(
"homeassistant.components.paperless_ngx.Paperless",
new=paperless_mock,
),
):
paperless = paperless_mock.return_value
paperless.base_url = "http://paperless.example.com/"
paperless.host_version = "2.3.0"
paperless.initialize.return_value = None
paperless.statistics = AsyncMock(
return_value=Statistic.create_with_data(
paperless, data=mock_statistic_data, fetched=True
)
)
paperless.status = AsyncMock(
return_value=Status.create_with_data(
paperless, data=mock_status_data, fetched=True
)
)
yield paperless
@pytest.fixture
def mock_config_entry() -> MockConfigEntry:
"""Return the default mocked config entry."""
return MockConfigEntry(
entry_id="0KLG00V55WEVTJ0CJHM0GADNGH",
title="Paperless-ngx",
domain=DOMAIN,
data=USER_INPUT_ONE,
)
@pytest.fixture
async def init_integration(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_paperless: MagicMock
) -> MockConfigEntry:
"""Set up the Paperless-ngx integration for testing."""
await setup_integration(hass, mock_config_entry)
return mock_config_entry