mirror of
https://github.com/home-assistant/core.git
synced 2025-04-26 10:17:51 +00:00
Move sleepiq tests to pytest style functions (#42018)
This commit is contained in:
parent
e129ea4db7
commit
c8821d930e
@ -1,67 +1,44 @@
|
|||||||
"""The tests for SleepIQ binary sensor platform."""
|
"""The tests for SleepIQ binary sensor platform."""
|
||||||
import unittest
|
|
||||||
|
|
||||||
import requests_mock
|
|
||||||
|
|
||||||
from homeassistant.components.sleepiq import binary_sensor as sleepiq
|
from homeassistant.components.sleepiq import binary_sensor as sleepiq
|
||||||
from homeassistant.setup import setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
from tests.async_mock import MagicMock
|
from tests.async_mock import MagicMock
|
||||||
from tests.common import get_test_home_assistant
|
|
||||||
from tests.components.sleepiq.test_init import mock_responses
|
from tests.components.sleepiq.test_init import mock_responses
|
||||||
|
|
||||||
|
CONFIG = {"username": "foo", "password": "bar"}
|
||||||
|
|
||||||
class TestSleepIQBinarySensorSetup(unittest.TestCase):
|
|
||||||
"""Tests the SleepIQ Binary Sensor platform."""
|
|
||||||
|
|
||||||
DEVICES = []
|
async def test_sensor_setup(hass, requests_mock):
|
||||||
|
"""Test for successfully setting up the SleepIQ platform."""
|
||||||
|
mock_responses(requests_mock)
|
||||||
|
|
||||||
def add_entities(self, devices):
|
await async_setup_component(hass, "sleepiq", {"sleepiq": CONFIG})
|
||||||
"""Mock add devices."""
|
|
||||||
for device in devices:
|
|
||||||
self.DEVICES.append(device)
|
|
||||||
|
|
||||||
def setUp(self):
|
device_mock = MagicMock()
|
||||||
"""Initialize values for this testcase class."""
|
sleepiq.setup_platform(hass, CONFIG, device_mock, MagicMock())
|
||||||
self.hass = get_test_home_assistant()
|
devices = device_mock.call_args[0][0]
|
||||||
self.username = "foo"
|
assert 2 == len(devices)
|
||||||
self.password = "bar"
|
|
||||||
self.config = {"username": self.username, "password": self.password}
|
|
||||||
self.DEVICES = []
|
|
||||||
self.addCleanup(self.tear_down_cleanup)
|
|
||||||
|
|
||||||
def tear_down_cleanup(self):
|
left_side = devices[1]
|
||||||
"""Stop everything that was started."""
|
assert "SleepNumber ILE Test1 Is In Bed" == left_side.name
|
||||||
self.hass.stop()
|
assert "on" == left_side.state
|
||||||
|
|
||||||
@requests_mock.Mocker()
|
right_side = devices[0]
|
||||||
def test_setup(self, mock):
|
assert "SleepNumber ILE Test2 Is In Bed" == right_side.name
|
||||||
"""Test for successfully setting up the SleepIQ platform."""
|
assert "off" == right_side.state
|
||||||
mock_responses(mock)
|
|
||||||
|
|
||||||
setup_component(self.hass, "sleepiq", {"sleepiq": self.config})
|
|
||||||
|
|
||||||
sleepiq.setup_platform(self.hass, self.config, self.add_entities, MagicMock())
|
async def test_setup_single(hass, requests_mock):
|
||||||
assert 2 == len(self.DEVICES)
|
"""Test for successfully setting up the SleepIQ platform."""
|
||||||
|
mock_responses(requests_mock, single=True)
|
||||||
|
|
||||||
left_side = self.DEVICES[1]
|
await async_setup_component(hass, "sleepiq", {"sleepiq": CONFIG})
|
||||||
assert "SleepNumber ILE Test1 Is In Bed" == left_side.name
|
|
||||||
assert "on" == left_side.state
|
|
||||||
|
|
||||||
right_side = self.DEVICES[0]
|
device_mock = MagicMock()
|
||||||
assert "SleepNumber ILE Test2 Is In Bed" == right_side.name
|
sleepiq.setup_platform(hass, CONFIG, device_mock, MagicMock())
|
||||||
assert "off" == right_side.state
|
devices = device_mock.call_args[0][0]
|
||||||
|
assert 1 == len(devices)
|
||||||
|
|
||||||
@requests_mock.Mocker()
|
right_side = devices[0]
|
||||||
def test_setup_single(self, mock):
|
assert "SleepNumber ILE Test1 Is In Bed" == right_side.name
|
||||||
"""Test for successfully setting up the SleepIQ platform."""
|
assert "on" == right_side.state
|
||||||
mock_responses(mock, single=True)
|
|
||||||
|
|
||||||
setup_component(self.hass, "sleepiq", {"sleepiq": self.config})
|
|
||||||
|
|
||||||
sleepiq.setup_platform(self.hass, self.config, self.add_entities, MagicMock())
|
|
||||||
assert 1 == len(self.DEVICES)
|
|
||||||
|
|
||||||
right_side = self.DEVICES[0]
|
|
||||||
assert "SleepNumber ILE Test1 Is In Bed" == right_side.name
|
|
||||||
assert "on" == right_side.state
|
|
||||||
|
@ -1,13 +1,11 @@
|
|||||||
"""The tests for the SleepIQ component."""
|
"""The tests for the SleepIQ component."""
|
||||||
import unittest
|
|
||||||
|
|
||||||
import requests_mock
|
|
||||||
|
|
||||||
from homeassistant import setup
|
from homeassistant import setup
|
||||||
import homeassistant.components.sleepiq as sleepiq
|
import homeassistant.components.sleepiq as sleepiq
|
||||||
|
|
||||||
from tests.async_mock import MagicMock, patch
|
from tests.async_mock import MagicMock, patch
|
||||||
from tests.common import get_test_home_assistant, load_fixture
|
from tests.common import load_fixture
|
||||||
|
|
||||||
|
CONFIG = {"sleepiq": {"username": "foo", "password": "bar"}}
|
||||||
|
|
||||||
|
|
||||||
def mock_responses(mock, single=False):
|
def mock_responses(mock, single=False):
|
||||||
@ -26,55 +24,40 @@ def mock_responses(mock, single=False):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class TestSleepIQ(unittest.TestCase):
|
async def test_setup(hass, requests_mock):
|
||||||
"""Tests the SleepIQ component."""
|
"""Test the setup."""
|
||||||
|
mock_responses(requests_mock)
|
||||||
|
|
||||||
def setUp(self):
|
# We're mocking the load_platform discoveries or else the platforms
|
||||||
"""Initialize values for this test case class."""
|
# will be setup during tear down when blocking till done, but the mocks
|
||||||
self.hass = get_test_home_assistant()
|
# are no longer active.
|
||||||
self.username = "foo"
|
with patch("homeassistant.helpers.discovery.load_platform", MagicMock()):
|
||||||
self.password = "bar"
|
assert sleepiq.setup(hass, CONFIG)
|
||||||
self.config = {
|
|
||||||
"sleepiq": {"username": self.username, "password": self.password}
|
|
||||||
}
|
|
||||||
self.addCleanup(self.tear_down_cleanup)
|
|
||||||
|
|
||||||
def tear_down_cleanup(self):
|
|
||||||
"""Stop everything that was started."""
|
|
||||||
self.hass.stop()
|
|
||||||
|
|
||||||
@requests_mock.Mocker()
|
async def test_setup_login_failed(hass, requests_mock):
|
||||||
def test_setup(self, mock):
|
"""Test the setup if a bad username or password is given."""
|
||||||
"""Test the setup."""
|
mock_responses(requests_mock)
|
||||||
mock_responses(mock)
|
requests_mock.put(
|
||||||
|
"https://prod-api.sleepiq.sleepnumber.com/rest/login",
|
||||||
|
status_code=401,
|
||||||
|
json=load_fixture("sleepiq-login-failed.json"),
|
||||||
|
)
|
||||||
|
|
||||||
# We're mocking the load_platform discoveries or else the platforms
|
response = sleepiq.setup(hass, CONFIG)
|
||||||
# will be setup during tear down when blocking till done, but the mocks
|
assert not response
|
||||||
# are no longer active.
|
|
||||||
with patch("homeassistant.helpers.discovery.load_platform", MagicMock()):
|
|
||||||
assert sleepiq.setup(self.hass, self.config)
|
|
||||||
|
|
||||||
@requests_mock.Mocker()
|
|
||||||
def test_setup_login_failed(self, mock):
|
|
||||||
"""Test the setup if a bad username or password is given."""
|
|
||||||
mock.put(
|
|
||||||
"https://prod-api.sleepiq.sleepnumber.com/rest/login",
|
|
||||||
status_code=401,
|
|
||||||
json=load_fixture("sleepiq-login-failed.json"),
|
|
||||||
)
|
|
||||||
|
|
||||||
response = sleepiq.setup(self.hass, self.config)
|
async def test_setup_component_no_login(hass):
|
||||||
assert not response
|
"""Test the setup when no login is configured."""
|
||||||
|
conf = CONFIG.copy()
|
||||||
|
del conf["sleepiq"]["username"]
|
||||||
|
assert not await setup.async_setup_component(hass, sleepiq.DOMAIN, conf)
|
||||||
|
|
||||||
def test_setup_component_no_login(self):
|
|
||||||
"""Test the setup when no login is configured."""
|
|
||||||
conf = self.config.copy()
|
|
||||||
del conf["sleepiq"]["username"]
|
|
||||||
assert not setup.setup_component(self.hass, sleepiq.DOMAIN, conf)
|
|
||||||
|
|
||||||
def test_setup_component_no_password(self):
|
async def test_setup_component_no_password(hass):
|
||||||
"""Test the setup when no password is configured."""
|
"""Test the setup when no password is configured."""
|
||||||
conf = self.config.copy()
|
conf = CONFIG.copy()
|
||||||
del conf["sleepiq"]["password"]
|
del conf["sleepiq"]["password"]
|
||||||
|
|
||||||
assert not setup.setup_component(self.hass, sleepiq.DOMAIN, conf)
|
assert not await setup.async_setup_component(hass, sleepiq.DOMAIN, conf)
|
||||||
|
@ -1,67 +1,44 @@
|
|||||||
"""The tests for SleepIQ sensor platform."""
|
"""The tests for SleepIQ sensor platform."""
|
||||||
import unittest
|
|
||||||
|
|
||||||
import requests_mock
|
|
||||||
|
|
||||||
import homeassistant.components.sleepiq.sensor as sleepiq
|
import homeassistant.components.sleepiq.sensor as sleepiq
|
||||||
from homeassistant.setup import setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
from tests.async_mock import MagicMock
|
from tests.async_mock import MagicMock
|
||||||
from tests.common import get_test_home_assistant
|
|
||||||
from tests.components.sleepiq.test_init import mock_responses
|
from tests.components.sleepiq.test_init import mock_responses
|
||||||
|
|
||||||
|
CONFIG = {"username": "foo", "password": "bar"}
|
||||||
|
|
||||||
class TestSleepIQSensorSetup(unittest.TestCase):
|
|
||||||
"""Tests the SleepIQ Sensor platform."""
|
|
||||||
|
|
||||||
DEVICES = []
|
async def test_setup(hass, requests_mock):
|
||||||
|
"""Test for successfully setting up the SleepIQ platform."""
|
||||||
|
mock_responses(requests_mock)
|
||||||
|
|
||||||
def add_entities(self, devices):
|
assert await async_setup_component(hass, "sleepiq", {"sleepiq": CONFIG})
|
||||||
"""Mock add devices."""
|
|
||||||
for device in devices:
|
|
||||||
self.DEVICES.append(device)
|
|
||||||
|
|
||||||
def setUp(self):
|
device_mock = MagicMock()
|
||||||
"""Initialize values for this testcase class."""
|
sleepiq.setup_platform(hass, CONFIG, device_mock, MagicMock())
|
||||||
self.hass = get_test_home_assistant()
|
devices = device_mock.call_args[0][0]
|
||||||
self.username = "foo"
|
assert 2 == len(devices)
|
||||||
self.password = "bar"
|
|
||||||
self.config = {"username": self.username, "password": self.password}
|
|
||||||
self.DEVICES = []
|
|
||||||
self.addCleanup(self.tear_down_cleanup)
|
|
||||||
|
|
||||||
def tear_down_cleanup(self):
|
left_side = devices[1]
|
||||||
"""Stop everything that was started."""
|
assert "SleepNumber ILE Test1 SleepNumber" == left_side.name
|
||||||
self.hass.stop()
|
assert 40 == left_side.state
|
||||||
|
|
||||||
@requests_mock.Mocker()
|
right_side = devices[0]
|
||||||
def test_setup(self, mock):
|
assert "SleepNumber ILE Test2 SleepNumber" == right_side.name
|
||||||
"""Test for successfully setting up the SleepIQ platform."""
|
assert 80 == right_side.state
|
||||||
mock_responses(mock)
|
|
||||||
|
|
||||||
assert setup_component(self.hass, "sleepiq", {"sleepiq": self.config})
|
|
||||||
|
|
||||||
sleepiq.setup_platform(self.hass, self.config, self.add_entities, MagicMock())
|
async def test_setup_sigle(hass, requests_mock):
|
||||||
assert 2 == len(self.DEVICES)
|
"""Test for successfully setting up the SleepIQ platform."""
|
||||||
|
mock_responses(requests_mock, single=True)
|
||||||
|
|
||||||
left_side = self.DEVICES[1]
|
assert await async_setup_component(hass, "sleepiq", {"sleepiq": CONFIG})
|
||||||
assert "SleepNumber ILE Test1 SleepNumber" == left_side.name
|
|
||||||
assert 40 == left_side.state
|
|
||||||
|
|
||||||
right_side = self.DEVICES[0]
|
device_mock = MagicMock()
|
||||||
assert "SleepNumber ILE Test2 SleepNumber" == right_side.name
|
sleepiq.setup_platform(hass, CONFIG, device_mock, MagicMock())
|
||||||
assert 80 == right_side.state
|
devices = device_mock.call_args[0][0]
|
||||||
|
assert 1 == len(devices)
|
||||||
|
|
||||||
@requests_mock.Mocker()
|
right_side = devices[0]
|
||||||
def test_setup_sigle(self, mock):
|
assert "SleepNumber ILE Test1 SleepNumber" == right_side.name
|
||||||
"""Test for successfully setting up the SleepIQ platform."""
|
assert 40 == right_side.state
|
||||||
mock_responses(mock, single=True)
|
|
||||||
|
|
||||||
assert setup_component(self.hass, "sleepiq", {"sleepiq": self.config})
|
|
||||||
|
|
||||||
sleepiq.setup_platform(self.hass, self.config, self.add_entities, MagicMock())
|
|
||||||
assert 1 == len(self.DEVICES)
|
|
||||||
|
|
||||||
right_side = self.DEVICES[0]
|
|
||||||
assert "SleepNumber ILE Test1 SleepNumber" == right_side.name
|
|
||||||
assert 40 == right_side.state
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user