This commit is contained in:
Paulus Schoutsen committed 2019-07-31 12:25:30 -07:00
1 parent da05dfe708
commit 4de97abc3a
2676 files changed
+163166 -140084

No files matched your search

+81 -84
View File
@@ -6,45 +6,42 @@ import pytest
import mock
from py17track.package import Package
from homeassistant.components.seventeentrack.sensor \
import CONF_SHOW_ARCHIVED, CONF_SHOW_DELIVERED
from homeassistant.components.seventeentrack.sensor import (
CONF_SHOW_ARCHIVED,
CONF_SHOW_DELIVERED,
)
from homeassistant.const import CONF_USERNAME, CONF_PASSWORD
from homeassistant.setup import async_setup_component
from homeassistant.util import utcnow
from tests.common import MockDependency, async_fire_time_changed
VALID_CONFIG_MINIMAL = {
'sensor': {
'platform': 'seventeentrack',
CONF_USERNAME: 'test',
CONF_PASSWORD: 'test'
"sensor": {
"platform": "seventeentrack",
CONF_USERNAME: "test",
CONF_PASSWORD: "test",
}
}
INVALID_CONFIG = {
'sensor': {
'platform': 'seventeentrack',
'boom': 'test',
}
}
INVALID_CONFIG = {"sensor": {"platform": "seventeentrack", "boom": "test"}}
VALID_CONFIG_FULL = {
'sensor': {
'platform': 'seventeentrack',
CONF_USERNAME: 'test',
CONF_PASSWORD: 'test',
"sensor": {
"platform": "seventeentrack",
CONF_USERNAME: "test",
CONF_PASSWORD: "test",
CONF_SHOW_ARCHIVED: True,
CONF_SHOW_DELIVERED: True
CONF_SHOW_DELIVERED: True,
}
}
VALID_CONFIG_FULL_NO_DELIVERED = {
'sensor': {
'platform': 'seventeentrack',
CONF_USERNAME: 'test',
CONF_PASSWORD: 'test',
"sensor": {
"platform": "seventeentrack",
CONF_USERNAME: "test",
CONF_PASSWORD: "test",
CONF_SHOW_ARCHIVED: False,
CONF_SHOW_DELIVERED: False
CONF_SHOW_DELIVERED: False,
}
}
@@ -55,7 +52,7 @@ DEFAULT_SUMMARY = {
"Ready to be Picked Up": 0,
"Undelivered": 0,
"Delivered": 0,
"Returned": 0
"Returned": 0,
}
NEW_SUMMARY_DATA = {
@@ -65,7 +62,7 @@ NEW_SUMMARY_DATA = {
"Ready to be Picked Up": 1,
"Undelivered": 1,
"Delivered": 1,
"Returned": 1
"Returned": 1,
}
@@ -83,7 +80,7 @@ class ProfileMock:
package_list = []
login_result = True
summary_data = DEFAULT_SUMMARY
account_id = '123'
account_id = "123"
@classmethod
def reset(cls):
@@ -91,7 +88,7 @@ class ProfileMock:
cls.package_list = []
cls.login_result = True
cls.summary_data = DEFAULT_SUMMARY
cls.account_id = '123'
cls.account_id = "123"
def __init__(self) -> None:
"""Override Account id."""
@@ -101,8 +98,9 @@ class ProfileMock:
"""Login mock."""
return self.__class__.login_result
async def packages(self, package_state: Union[int, str] = '',
show_archived: bool = False) -> list:
async def packages(
self, package_state: Union[int, str] = "", show_archived: bool = False
) -> list:
"""Packages mock."""
return self.__class__.package_list[:]
@@ -114,14 +112,14 @@ class ProfileMock:
@pytest.fixture(autouse=True, name="mock_py17track")
def fixture_mock_py17track():
"""Mock py17track dependency."""
with MockDependency('py17track'):
with MockDependency("py17track"):
yield
@pytest.fixture(autouse=True, name="mock_client")
def fixture_mock_client(mock_py17track):
"""Mock py17track client."""
with mock.patch('py17track.Client', new=ClientMock):
with mock.patch("py17track.Client", new=ClientMock):
yield
ProfileMock.reset()
@@ -134,120 +132,118 @@ async def _setup_seventeentrack(hass, config=None, summary_data=None):
summary_data = {}
ProfileMock.summary_data = summary_data
assert await async_setup_component(hass, 'sensor', config)
assert await async_setup_component(hass, "sensor", config)
async def _goto_future(hass, future=None):
"""Move to future."""
if not future:
future = utcnow() + datetime.timedelta(minutes=10)
with mock.patch('homeassistant.util.utcnow', return_value=future):
with mock.patch("homeassistant.util.utcnow", return_value=future):
async_fire_time_changed(hass, future)
await hass.async_block_till_done()
async def test_full_valid_config(hass):
"""Ensure everything starts correctly."""
assert await async_setup_component(hass, 'sensor', VALID_CONFIG_FULL)
assert len(hass.states.async_entity_ids()) == len(
ProfileMock.summary_data.keys())
assert await async_setup_component(hass, "sensor", VALID_CONFIG_FULL)
assert len(hass.states.async_entity_ids()) == len(ProfileMock.summary_data.keys())
async def test_valid_config(hass):
"""Ensure everything starts correctly."""
assert await async_setup_component(hass, 'sensor', VALID_CONFIG_MINIMAL)
assert await async_setup_component(hass, "sensor", VALID_CONFIG_MINIMAL)
assert len(hass.states.async_entity_ids()) == len(
ProfileMock.summary_data.keys())
assert len(hass.states.async_entity_ids()) == len(ProfileMock.summary_data.keys())
async def test_invalid_config(hass):
"""Ensure nothing is created when config is wrong."""
assert await async_setup_component(hass, 'sensor', INVALID_CONFIG)
assert await async_setup_component(hass, "sensor", INVALID_CONFIG)
assert not hass.states.async_entity_ids()
async def test_add_package(hass):
"""Ensure package is added correctly when user add a new package."""
package = Package('456', 206, 'friendly name 1', 'info text 1',
'location 1', 206, 2)
package = Package(
"456", 206, "friendly name 1", "info text 1", "location 1", 206, 2
)
ProfileMock.package_list = [package]
await _setup_seventeentrack(hass)
assert hass.states.get(
'sensor.seventeentrack_package_456') is not None
assert hass.states.get("sensor.seventeentrack_package_456") is not None
assert len(hass.states.async_entity_ids()) == 1
package2 = Package('789', 206, 'friendly name 2', 'info text 2',
'location 2', 206, 2)
package2 = Package(
"789", 206, "friendly name 2", "info text 2", "location 2", 206, 2
)
ProfileMock.package_list = [package, package2]
await _goto_future(hass)
assert hass.states.get(
'sensor.seventeentrack_package_789') is not None
assert hass.states.get("sensor.seventeentrack_package_789") is not None
assert len(hass.states.async_entity_ids()) == 2
async def test_remove_package(hass):
"""Ensure entity is not there anymore if package is not there."""
package1 = Package('456', 206, 'friendly name 1', 'info text 1',
'location 1', 206, 2)
package2 = Package('789', 206, 'friendly name 2', 'info text 2',
'location 2', 206, 2)
package1 = Package(
"456", 206, "friendly name 1", "info text 1", "location 1", 206, 2
)
package2 = Package(
"789", 206, "friendly name 2", "info text 2", "location 2", 206, 2
)
ProfileMock.package_list = [package1, package2]
await _setup_seventeentrack(hass)
assert hass.states.get(
'sensor.seventeentrack_package_456') is not None
assert hass.states.get(
'sensor.seventeentrack_package_789') is not None
assert hass.states.get("sensor.seventeentrack_package_456") is not None
assert hass.states.get("sensor.seventeentrack_package_789") is not None
assert len(hass.states.async_entity_ids()) == 2
ProfileMock.package_list = [package2]
await _goto_future(hass)
assert hass.states.get(
'sensor.seventeentrack_package_456') is None
assert hass.states.get(
'sensor.seventeentrack_package_789') is not None
assert hass.states.get("sensor.seventeentrack_package_456") is None
assert hass.states.get("sensor.seventeentrack_package_789") is not None
assert len(hass.states.async_entity_ids()) == 1
async def test_friendly_name_changed(hass):
"""Test friendly name change."""
package = Package('456', 206, 'friendly name 1', 'info text 1',
'location 1', 206, 2)
package = Package(
"456", 206, "friendly name 1", "info text 1", "location 1", 206, 2
)
ProfileMock.package_list = [package]
await _setup_seventeentrack(hass)
assert hass.states.get(
'sensor.seventeentrack_package_456') is not None
assert hass.states.get("sensor.seventeentrack_package_456") is not None
assert len(hass.states.async_entity_ids()) == 1
package = Package('456', 206, 'friendly name 2', 'info text 1',
'location 1', 206, 2)
package = Package(
"456", 206, "friendly name 2", "info text 1", "location 1", 206, 2
)
ProfileMock.package_list = [package]
await _goto_future(hass)
assert hass.states.get(
'sensor.seventeentrack_package_456') is not None
entity = hass.data['entity_components']['sensor'].get_entity(
'sensor.seventeentrack_package_456')
assert entity.name == 'Seventeentrack Package: friendly name 2'
assert hass.states.get("sensor.seventeentrack_package_456") is not None
entity = hass.data["entity_components"]["sensor"].get_entity(
"sensor.seventeentrack_package_456"
)
assert entity.name == "Seventeentrack Package: friendly name 2"
assert len(hass.states.async_entity_ids()) == 1
async def test_delivered_not_shown(hass):
"""Ensure delivered packages are not shown."""
package = Package('456', 206, 'friendly name 1', 'info text 1',
'location 1', 206, 2, 40)
package = Package(
"456", 206, "friendly name 1", "info text 1", "location 1", 206, 2, 40
)
ProfileMock.package_list = [package]
hass.components.persistent_notification = mock.MagicMock()
@@ -258,33 +254,34 @@ async def test_delivered_not_shown(hass):
async def test_delivered_shown(hass):
"""Ensure delivered packages are show when user choose to show them."""
package = Package('456', 206, 'friendly name 1', 'info text 1',
'location 1', 206, 2, 40)
package = Package(
"456", 206, "friendly name 1", "info text 1", "location 1", 206, 2, 40
)
ProfileMock.package_list = [package]
hass.components.persistent_notification = mock.MagicMock()
await _setup_seventeentrack(hass, VALID_CONFIG_FULL)
assert hass.states.get(
'sensor.seventeentrack_package_456') is not None
assert hass.states.get("sensor.seventeentrack_package_456") is not None
assert len(hass.states.async_entity_ids()) == 1
hass.components.persistent_notification.create.assert_not_called()
async def test_becomes_delivered_not_shown_notification(hass):
"""Ensure notification is triggered when package becomes delivered."""
package = Package('456', 206, 'friendly name 1', 'info text 1',
'location 1', 206, 2)
package = Package(
"456", 206, "friendly name 1", "info text 1", "location 1", 206, 2
)
ProfileMock.package_list = [package]
await _setup_seventeentrack(hass, VALID_CONFIG_FULL_NO_DELIVERED)
assert hass.states.get(
'sensor.seventeentrack_package_456') is not None
assert hass.states.get("sensor.seventeentrack_package_456") is not None
assert len(hass.states.async_entity_ids()) == 1
package_delivered = Package('456', 206, 'friendly name 1',
'info text 1', 'location 1', 206, 2, 40)
package_delivered = Package(
"456", 206, "friendly name 1", "info text 1", "location 1", 206, 2, 40
)
ProfileMock.package_list = [package_delivered]
hass.components.persistent_notification = mock.MagicMock()
@@ -300,7 +297,7 @@ async def test_summary_correctly_updated(hass):
assert len(hass.states.async_entity_ids()) == 7
for state in hass.states.async_all():
assert state.state == '0'
assert state.state == "0"
ProfileMock.summary_data = NEW_SUMMARY_DATA
@@ -308,4 +305,4 @@ async def test_summary_correctly_updated(hass):
assert len(hass.states.async_entity_ids()) == 7
for state in hass.states.async_all():
assert state.state == '1'
assert state.state == "1"