forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve 17track tests (home-assistant#112734)
* 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
Showing
4 changed files
with
316 additions
and
324 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
Oops, something went wrong.