Skip to content

Commit

Permalink
Improve 17track tests (home-assistant#112734)
Browse files Browse the repository at this point in the history
* 17Track tests

* add 17Track sensor to coverage

* extract repeated code

* 1. _goto_future - call tick only once
2. change test name to reflect test
3. remove ifs from test

* remove undersocre from _goto_future
  • Loading branch information
shaiu authored Mar 10, 2024
1 parent 049f0f5 commit eb81bf1
Show file tree
Hide file tree
Showing 4 changed files with 316 additions and 324 deletions.
1 change: 0 additions & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,6 @@ omit =
homeassistant/components/serial_pm/sensor.py
homeassistant/components/sesame/lock.py
homeassistant/components/seven_segments/image_processing.py
homeassistant/components/seventeentrack/sensor.py
homeassistant/components/shodan/sensor.py
homeassistant/components/sia/__init__.py
homeassistant/components/sia/alarm_control_panel.py
Expand Down
24 changes: 24 additions & 0 deletions tests/components/seventeentrack/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
"""Tests for the seventeentrack component."""

from datetime import timedelta

from freezegun.api import FrozenDateTimeFactory

from homeassistant.components.seventeentrack.sensor import DEFAULT_SCAN_INTERVAL
from homeassistant.core import HomeAssistant
from homeassistant.helpers.typing import ConfigType
from homeassistant.setup import async_setup_component

from tests.common import async_fire_time_changed


async def init_integration(hass: HomeAssistant, config: ConfigType):
"""Set up the seventeentrack integration in Home Assistant."""
assert await async_setup_component(hass, "sensor", config)
await hass.async_block_till_done()


async def goto_future(hass: HomeAssistant, freezer: FrozenDateTimeFactory):
"""Move to future."""
freezer.tick(DEFAULT_SCAN_INTERVAL + timedelta(minutes=1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
108 changes: 108 additions & 0 deletions tests/components/seventeentrack/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
"""Configuration for 17Track tests."""

from typing import Optional
from unittest.mock import AsyncMock, patch

from py17track.package import Package
import pytest

from homeassistant.components.seventeentrack.sensor import (
CONF_SHOW_ARCHIVED,
CONF_SHOW_DELIVERED,
)
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME

VALID_CONFIG_MINIMAL = {
"sensor": {
"platform": "seventeentrack",
CONF_USERNAME: "test",
CONF_PASSWORD: "test",
}
}

INVALID_CONFIG = {"sensor": {"platform": "seventeentrack", "boom": "test"}}

VALID_CONFIG_FULL = {
"sensor": {
"platform": "seventeentrack",
CONF_USERNAME: "test",
CONF_PASSWORD: "test",
CONF_SHOW_ARCHIVED: True,
CONF_SHOW_DELIVERED: True,
}
}

VALID_CONFIG_FULL_NO_DELIVERED = {
"sensor": {
"platform": "seventeentrack",
CONF_USERNAME: "test",
CONF_PASSWORD: "test",
CONF_SHOW_ARCHIVED: False,
CONF_SHOW_DELIVERED: False,
}
}

DEFAULT_SUMMARY = {
"Not Found": 0,
"In Transit": 0,
"Expired": 0,
"Ready to be Picked Up": 0,
"Undelivered": 0,
"Delivered": 0,
"Returned": 0,
}

NEW_SUMMARY_DATA = {
"Not Found": 1,
"In Transit": 1,
"Expired": 1,
"Ready to be Picked Up": 1,
"Undelivered": 1,
"Delivered": 1,
"Returned": 1,
}


@pytest.fixture
def mock_seventeentrack():
"""Build a fixture for the 17Track API."""
mock_seventeentrack_api = AsyncMock()
with (
patch(
"homeassistant.components.seventeentrack.sensor.SeventeenTrackClient",
return_value=mock_seventeentrack_api,
) as mock_seventeentrack_api,
):
mock_seventeentrack_api.return_value.profile.login.return_value = True
mock_seventeentrack_api.return_value.profile.packages.return_value = []
mock_seventeentrack_api.return_value.profile.summary.return_value = (
DEFAULT_SUMMARY
)
yield mock_seventeentrack_api


def get_package(
tracking_number: str = "456",
destination_country: int = 206,
friendly_name: Optional[str] = "friendly name 1",
info_text: str = "info text 1",
location: str = "location 1",
timestamp: str = "2020-08-10 10:32",
origin_country: int = 206,
package_type: int = 2,
status: int = 0,
tz: str = "UTC",
):
"""Build a Package of the 17Track API."""
return Package(
tracking_number=tracking_number,
destination_country=destination_country,
friendly_name=friendly_name,
info_text=info_text,
location=location,
timestamp=timestamp,
origin_country=origin_country,
package_type=package_type,
status=status,
tz=tz,
)
Loading

0 comments on commit eb81bf1

Please sign in to comment.