1
0
mirror of https://github.com/home-assistant/core.git synced 2026-07-08 15:24:58 +01:00

Add tests for aftership services (#174280)

This commit is contained in:
epenet
2026-06-19 16:53:31 +02:00
committed by GitHub
parent 71058c7a0a
commit 4d7fc228df
2 changed files with 122 additions and 0 deletions
+26
View File
@@ -5,6 +5,11 @@ from unittest.mock import AsyncMock, patch
import pytest
from homeassistant.components.aftership.const import DOMAIN
from homeassistant.const import CONF_API_KEY
from tests.common import MockConfigEntry
@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock]:
@@ -13,3 +18,24 @@ def mock_setup_entry() -> Generator[AsyncMock]:
"homeassistant.components.aftership.async_setup_entry", return_value=True
) as mock_setup_entry:
yield mock_setup_entry
@pytest.fixture
def mock_aftership() -> Generator[AsyncMock]:
"""Mock the AfterShip client."""
with patch(
"homeassistant.components.aftership.AfterShip", return_value=AsyncMock()
) as mock_client:
client = mock_client.return_value
client.trackings.list.return_value = {"trackings": []}
yield client
@pytest.fixture
def mock_config_entry() -> MockConfigEntry:
"""Return a mocked config entry."""
return MockConfigEntry(
domain=DOMAIN,
title="AfterShip",
data={CONF_API_KEY: "mock-api-key"},
)
@@ -0,0 +1,96 @@
"""Test AfterShip services."""
from unittest.mock import AsyncMock
from homeassistant.components.aftership.const import (
CONF_SLUG,
CONF_TITLE,
CONF_TRACKING_NUMBER,
DOMAIN,
SERVICE_ADD_TRACKING,
SERVICE_REMOVE_TRACKING,
)
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
async def setup_integration(
hass: HomeAssistant, mock_config_entry: MockConfigEntry
) -> None:
"""Set up the AfterShip integration for testing."""
mock_config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
async def test_add_tracking(
hass: HomeAssistant,
mock_aftership: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test the add_tracking service forwards all fields to the client."""
await setup_integration(hass, mock_config_entry)
await hass.services.async_call(
DOMAIN,
SERVICE_ADD_TRACKING,
{
CONF_TRACKING_NUMBER: "123456789",
CONF_TITLE: "Laptop",
CONF_SLUG: "usps",
},
blocking=True,
)
mock_aftership.trackings.add.assert_called_once_with(
tracking_number="123456789",
title="Laptop",
slug="usps",
)
async def test_add_tracking_only_required(
hass: HomeAssistant,
mock_aftership: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test the add_tracking service with only the required field."""
await setup_integration(hass, mock_config_entry)
await hass.services.async_call(
DOMAIN,
SERVICE_ADD_TRACKING,
{CONF_TRACKING_NUMBER: "123456789"},
blocking=True,
)
mock_aftership.trackings.add.assert_called_once_with(
tracking_number="123456789",
title=None,
slug=None,
)
async def test_remove_tracking(
hass: HomeAssistant,
mock_aftership: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test the remove_tracking service forwards all fields to the client."""
await setup_integration(hass, mock_config_entry)
await hass.services.async_call(
DOMAIN,
SERVICE_REMOVE_TRACKING,
{
CONF_TRACKING_NUMBER: "123456789",
CONF_SLUG: "usps",
},
blocking=True,
)
mock_aftership.trackings.remove.assert_called_once_with(
tracking_number="123456789",
slug="usps",
)