From 4d7fc228dfd617f6fdae6d75642bfc3924d58384 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 19 Jun 2026 16:53:31 +0200 Subject: [PATCH] Add tests for aftership services (#174280) --- tests/components/aftership/conftest.py | 26 ++++++ tests/components/aftership/test_services.py | 96 +++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 tests/components/aftership/test_services.py diff --git a/tests/components/aftership/conftest.py b/tests/components/aftership/conftest.py index d66ae267bfe2..ed88dedb220f 100644 --- a/tests/components/aftership/conftest.py +++ b/tests/components/aftership/conftest.py @@ -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"}, + ) diff --git a/tests/components/aftership/test_services.py b/tests/components/aftership/test_services.py new file mode 100644 index 000000000000..78daadfe621a --- /dev/null +++ b/tests/components/aftership/test_services.py @@ -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", + )