Split locative to a separate component (#19964)
* Split locative to a separate component * Switch tests to use constants for http codes * Fix tests
This commit is contained in:
1 parent
8755389c49
commit
d820efc4e3
4 files changed
+202
-153
No files matched your search
@@ -0,0 +1 @@
|
||||
"""Tests for the Locative component."""
|
||||
+68
-59
@@ -1,13 +1,13 @@
|
||||
"""The tests the for Locative device tracker platform."""
|
||||
import asyncio
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.device_tracker import \
|
||||
DOMAIN as DEVICE_TRACKER_DOMAIN
|
||||
from homeassistant.components.locative import URL, DOMAIN
|
||||
from homeassistant.const import HTTP_OK, HTTP_UNPROCESSABLE_ENTITY
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.components.device_tracker as device_tracker
|
||||
from homeassistant.const import CONF_PLATFORM
|
||||
from homeassistant.components.device_tracker.locative import URL
|
||||
|
||||
|
||||
def _url(data=None):
|
||||
@@ -18,22 +18,25 @@ def _url(data=None):
|
||||
return "{}?{}".format(URL, data)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_dev_track(mock_device_tracker_conf):
|
||||
"""Mock device tracker config loading."""
|
||||
pass
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def locative_client(loop, hass, hass_client):
|
||||
"""Locative mock client."""
|
||||
assert loop.run_until_complete(async_setup_component(
|
||||
hass, device_tracker.DOMAIN, {
|
||||
device_tracker.DOMAIN: {
|
||||
CONF_PLATFORM: 'locative'
|
||||
}
|
||||
hass, DOMAIN, {
|
||||
DOMAIN: {}
|
||||
}))
|
||||
|
||||
with patch('homeassistant.components.device_tracker.update_config'):
|
||||
yield loop.run_until_complete(hass_client())
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_missing_data(locative_client):
|
||||
async def test_missing_data(locative_client):
|
||||
"""Test missing data."""
|
||||
data = {
|
||||
'latitude': 1.0,
|
||||
@@ -44,55 +47,54 @@ def test_missing_data(locative_client):
|
||||
}
|
||||
|
||||
# No data
|
||||
req = yield from locative_client.get(_url({}))
|
||||
assert req.status == 422
|
||||
req = await locative_client.get(_url({}))
|
||||
assert req.status == HTTP_UNPROCESSABLE_ENTITY
|
||||
|
||||
# No latitude
|
||||
copy = data.copy()
|
||||
del copy['latitude']
|
||||
req = yield from locative_client.get(_url(copy))
|
||||
assert req.status == 422
|
||||
req = await locative_client.get(_url(copy))
|
||||
assert req.status == HTTP_UNPROCESSABLE_ENTITY
|
||||
|
||||
# No device
|
||||
copy = data.copy()
|
||||
del copy['device']
|
||||
req = yield from locative_client.get(_url(copy))
|
||||
assert req.status == 422
|
||||
req = await locative_client.get(_url(copy))
|
||||
assert req.status == HTTP_UNPROCESSABLE_ENTITY
|
||||
|
||||
# No location
|
||||
copy = data.copy()
|
||||
del copy['id']
|
||||
req = yield from locative_client.get(_url(copy))
|
||||
assert req.status == 422
|
||||
req = await locative_client.get(_url(copy))
|
||||
assert req.status == HTTP_UNPROCESSABLE_ENTITY
|
||||
|
||||
# No trigger
|
||||
copy = data.copy()
|
||||
del copy['trigger']
|
||||
req = yield from locative_client.get(_url(copy))
|
||||
assert req.status == 422
|
||||
req = await locative_client.get(_url(copy))
|
||||
assert req.status == HTTP_UNPROCESSABLE_ENTITY
|
||||
|
||||
# Test message
|
||||
copy = data.copy()
|
||||
copy['trigger'] = 'test'
|
||||
req = yield from locative_client.get(_url(copy))
|
||||
assert req.status == 200
|
||||
req = await locative_client.get(_url(copy))
|
||||
assert req.status == HTTP_OK
|
||||
|
||||
# Test message, no location
|
||||
copy = data.copy()
|
||||
copy['trigger'] = 'test'
|
||||
del copy['id']
|
||||
req = yield from locative_client.get(_url(copy))
|
||||
assert req.status == 200
|
||||
req = await locative_client.get(_url(copy))
|
||||
assert req.status == HTTP_OK
|
||||
|
||||
# Unknown trigger
|
||||
copy = data.copy()
|
||||
copy['trigger'] = 'foobar'
|
||||
req = yield from locative_client.get(_url(copy))
|
||||
assert req.status == 422
|
||||
req = await locative_client.get(_url(copy))
|
||||
assert req.status == HTTP_UNPROCESSABLE_ENTITY
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_enter_and_exit(hass, locative_client):
|
||||
async def test_enter_and_exit(hass, locative_client):
|
||||
"""Test when there is a known zone."""
|
||||
data = {
|
||||
'latitude': 40.7855,
|
||||
@@ -103,9 +105,10 @@ def test_enter_and_exit(hass, locative_client):
|
||||
}
|
||||
|
||||
# Enter the Home
|
||||
req = yield from locative_client.get(_url(data))
|
||||
assert req.status == 200
|
||||
state_name = hass.states.get('{}.{}'.format('device_tracker',
|
||||
req = await locative_client.get(_url(data))
|
||||
await hass.async_block_till_done()
|
||||
assert req.status == HTTP_OK
|
||||
state_name = hass.states.get('{}.{}'.format(DEVICE_TRACKER_DOMAIN,
|
||||
data['device'])).state
|
||||
assert 'home' == state_name
|
||||
|
||||
@@ -113,9 +116,10 @@ def test_enter_and_exit(hass, locative_client):
|
||||
data['trigger'] = 'exit'
|
||||
|
||||
# Exit Home
|
||||
req = yield from locative_client.get(_url(data))
|
||||
assert req.status == 200
|
||||
state_name = hass.states.get('{}.{}'.format('device_tracker',
|
||||
req = await locative_client.get(_url(data))
|
||||
await hass.async_block_till_done()
|
||||
assert req.status == HTTP_OK
|
||||
state_name = hass.states.get('{}.{}'.format(DEVICE_TRACKER_DOMAIN,
|
||||
data['device'])).state
|
||||
assert 'not_home' == state_name
|
||||
|
||||
@@ -123,18 +127,20 @@ def test_enter_and_exit(hass, locative_client):
|
||||
data['trigger'] = 'enter'
|
||||
|
||||
# Enter Home again
|
||||
req = yield from locative_client.get(_url(data))
|
||||
assert req.status == 200
|
||||
state_name = hass.states.get('{}.{}'.format('device_tracker',
|
||||
req = await locative_client.get(_url(data))
|
||||
await hass.async_block_till_done()
|
||||
assert req.status == HTTP_OK
|
||||
state_name = hass.states.get('{}.{}'.format(DEVICE_TRACKER_DOMAIN,
|
||||
data['device'])).state
|
||||
assert 'home' == state_name
|
||||
|
||||
data['trigger'] = 'exit'
|
||||
|
||||
# Exit Home
|
||||
req = yield from locative_client.get(_url(data))
|
||||
assert req.status == 200
|
||||
state_name = hass.states.get('{}.{}'.format('device_tracker',
|
||||
req = await locative_client.get(_url(data))
|
||||
await hass.async_block_till_done()
|
||||
assert req.status == HTTP_OK
|
||||
state_name = hass.states.get('{}.{}'.format(DEVICE_TRACKER_DOMAIN,
|
||||
data['device'])).state
|
||||
assert 'not_home' == state_name
|
||||
|
||||
@@ -142,15 +148,15 @@ def test_enter_and_exit(hass, locative_client):
|
||||
data['trigger'] = 'enter'
|
||||
|
||||
# Enter Work
|
||||
req = yield from locative_client.get(_url(data))
|
||||
assert req.status == 200
|
||||
state_name = hass.states.get('{}.{}'.format('device_tracker',
|
||||
req = await locative_client.get(_url(data))
|
||||
await hass.async_block_till_done()
|
||||
assert req.status == HTTP_OK
|
||||
state_name = hass.states.get('{}.{}'.format(DEVICE_TRACKER_DOMAIN,
|
||||
data['device'])).state
|
||||
assert 'work' == state_name
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_exit_after_enter(hass, locative_client):
|
||||
async def test_exit_after_enter(hass, locative_client):
|
||||
"""Test when an exit message comes after an enter message."""
|
||||
data = {
|
||||
'latitude': 40.7855,
|
||||
@@ -161,20 +167,22 @@ def test_exit_after_enter(hass, locative_client):
|
||||
}
|
||||
|
||||
# Enter Home
|
||||
req = yield from locative_client.get(_url(data))
|
||||
assert req.status == 200
|
||||
req = await locative_client.get(_url(data))
|
||||
await hass.async_block_till_done()
|
||||
assert req.status == HTTP_OK
|
||||
|
||||
state = hass.states.get('{}.{}'.format('device_tracker',
|
||||
state = hass.states.get('{}.{}'.format(DEVICE_TRACKER_DOMAIN,
|
||||
data['device']))
|
||||
assert state.state == 'home'
|
||||
|
||||
data['id'] = 'Work'
|
||||
|
||||
# Enter Work
|
||||
req = yield from locative_client.get(_url(data))
|
||||
assert req.status == 200
|
||||
req = await locative_client.get(_url(data))
|
||||
await hass.async_block_till_done()
|
||||
assert req.status == HTTP_OK
|
||||
|
||||
state = hass.states.get('{}.{}'.format('device_tracker',
|
||||
state = hass.states.get('{}.{}'.format(DEVICE_TRACKER_DOMAIN,
|
||||
data['device']))
|
||||
assert state.state == 'work'
|
||||
|
||||
@@ -182,16 +190,16 @@ def test_exit_after_enter(hass, locative_client):
|
||||
data['trigger'] = 'exit'
|
||||
|
||||
# Exit Home
|
||||
req = yield from locative_client.get(_url(data))
|
||||
assert req.status == 200
|
||||
req = await locative_client.get(_url(data))
|
||||
await hass.async_block_till_done()
|
||||
assert req.status == HTTP_OK
|
||||
|
||||
state = hass.states.get('{}.{}'.format('device_tracker',
|
||||
state = hass.states.get('{}.{}'.format(DEVICE_TRACKER_DOMAIN,
|
||||
data['device']))
|
||||
assert state.state == 'work'
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_exit_first(hass, locative_client):
|
||||
async def test_exit_first(hass, locative_client):
|
||||
"""Test when an exit message is sent first on a new device."""
|
||||
data = {
|
||||
'latitude': 40.7855,
|
||||
@@ -202,9 +210,10 @@ def test_exit_first(hass, locative_client):
|
||||
}
|
||||
|
||||
# Exit Home
|
||||
req = yield from locative_client.get(_url(data))
|
||||
assert req.status == 200
|
||||
req = await locative_client.get(_url(data))
|
||||
await hass.async_block_till_done()
|
||||
assert req.status == HTTP_OK
|
||||
|
||||
state = hass.states.get('{}.{}'.format('device_tracker',
|
||||
state = hass.states.get('{}.{}'.format(DEVICE_TRACKER_DOMAIN,
|
||||
data['device']))
|
||||
assert state.state == 'not_home'
|
||||
Reference in new issue
Block a user