Move sleepiq tests to pytest style functions (#42018)

This commit is contained in:
dmonego 2020-10-19 17:31:33 -04:00 committed by GitHub
parent e129ea4db7
commit c8821d930e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 85 additions and 148 deletions

View File

@ -1,67 +1,44 @@
"""The tests for SleepIQ binary sensor platform."""
import unittest
import requests_mock
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.common import get_test_home_assistant
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):
"""Mock add devices."""
for device in devices:
self.DEVICES.append(device)
await async_setup_component(hass, "sleepiq", {"sleepiq": CONFIG})
def setUp(self):
"""Initialize values for this testcase class."""
self.hass = get_test_home_assistant()
self.username = "foo"
self.password = "bar"
self.config = {"username": self.username, "password": self.password}
self.DEVICES = []
self.addCleanup(self.tear_down_cleanup)
device_mock = MagicMock()
sleepiq.setup_platform(hass, CONFIG, device_mock, MagicMock())
devices = device_mock.call_args[0][0]
assert 2 == len(devices)
def tear_down_cleanup(self):
"""Stop everything that was started."""
self.hass.stop()
left_side = devices[1]
assert "SleepNumber ILE Test1 Is In Bed" == left_side.name
assert "on" == left_side.state
@requests_mock.Mocker()
def test_setup(self, mock):
"""Test for successfully setting up the SleepIQ platform."""
mock_responses(mock)
right_side = devices[0]
assert "SleepNumber ILE Test2 Is In Bed" == right_side.name
assert "off" == right_side.state
setup_component(self.hass, "sleepiq", {"sleepiq": self.config})
sleepiq.setup_platform(self.hass, self.config, self.add_entities, MagicMock())
assert 2 == len(self.DEVICES)
async def test_setup_single(hass, requests_mock):
"""Test for successfully setting up the SleepIQ platform."""
mock_responses(requests_mock, single=True)
left_side = self.DEVICES[1]
assert "SleepNumber ILE Test1 Is In Bed" == left_side.name
assert "on" == left_side.state
await async_setup_component(hass, "sleepiq", {"sleepiq": CONFIG})
right_side = self.DEVICES[0]
assert "SleepNumber ILE Test2 Is In Bed" == right_side.name
assert "off" == right_side.state
device_mock = MagicMock()
sleepiq.setup_platform(hass, CONFIG, device_mock, MagicMock())
devices = device_mock.call_args[0][0]
assert 1 == len(devices)
@requests_mock.Mocker()
def test_setup_single(self, mock):
"""Test for successfully setting up the SleepIQ platform."""
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
right_side = devices[0]
assert "SleepNumber ILE Test1 Is In Bed" == right_side.name
assert "on" == right_side.state

View File

@ -1,13 +1,11 @@
"""The tests for the SleepIQ component."""
import unittest
import requests_mock
from homeassistant import setup
import homeassistant.components.sleepiq as sleepiq
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):
@ -26,55 +24,40 @@ def mock_responses(mock, single=False):
)
class TestSleepIQ(unittest.TestCase):
"""Tests the SleepIQ component."""
async def test_setup(hass, requests_mock):
"""Test the setup."""
mock_responses(requests_mock)
def setUp(self):
"""Initialize values for this test case class."""
self.hass = get_test_home_assistant()
self.username = "foo"
self.password = "bar"
self.config = {
"sleepiq": {"username": self.username, "password": self.password}
}
self.addCleanup(self.tear_down_cleanup)
# We're mocking the load_platform discoveries or else the platforms
# will be setup during tear down when blocking till done, but the mocks
# are no longer active.
with patch("homeassistant.helpers.discovery.load_platform", MagicMock()):
assert sleepiq.setup(hass, CONFIG)
def tear_down_cleanup(self):
"""Stop everything that was started."""
self.hass.stop()
@requests_mock.Mocker()
def test_setup(self, mock):
"""Test the setup."""
mock_responses(mock)
async def test_setup_login_failed(hass, requests_mock):
"""Test the setup if a bad username or password is given."""
mock_responses(requests_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
# will be setup during tear down when blocking till done, but the mocks
# are no longer active.
with patch("homeassistant.helpers.discovery.load_platform", MagicMock()):
assert sleepiq.setup(self.hass, self.config)
response = sleepiq.setup(hass, CONFIG)
assert not response
@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)
assert not response
async def test_setup_component_no_login(hass):
"""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):
"""Test the setup when no password is configured."""
conf = self.config.copy()
del conf["sleepiq"]["password"]
async def test_setup_component_no_password(hass):
"""Test the setup when no password is configured."""
conf = CONFIG.copy()
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)

View File

@ -1,67 +1,44 @@
"""The tests for SleepIQ sensor platform."""
import unittest
import requests_mock
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.common import get_test_home_assistant
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):
"""Mock add devices."""
for device in devices:
self.DEVICES.append(device)
assert await async_setup_component(hass, "sleepiq", {"sleepiq": CONFIG})
def setUp(self):
"""Initialize values for this testcase class."""
self.hass = get_test_home_assistant()
self.username = "foo"
self.password = "bar"
self.config = {"username": self.username, "password": self.password}
self.DEVICES = []
self.addCleanup(self.tear_down_cleanup)
device_mock = MagicMock()
sleepiq.setup_platform(hass, CONFIG, device_mock, MagicMock())
devices = device_mock.call_args[0][0]
assert 2 == len(devices)
def tear_down_cleanup(self):
"""Stop everything that was started."""
self.hass.stop()
left_side = devices[1]
assert "SleepNumber ILE Test1 SleepNumber" == left_side.name
assert 40 == left_side.state
@requests_mock.Mocker()
def test_setup(self, mock):
"""Test for successfully setting up the SleepIQ platform."""
mock_responses(mock)
right_side = devices[0]
assert "SleepNumber ILE Test2 SleepNumber" == right_side.name
assert 80 == right_side.state
assert setup_component(self.hass, "sleepiq", {"sleepiq": self.config})
sleepiq.setup_platform(self.hass, self.config, self.add_entities, MagicMock())
assert 2 == len(self.DEVICES)
async def test_setup_sigle(hass, requests_mock):
"""Test for successfully setting up the SleepIQ platform."""
mock_responses(requests_mock, single=True)
left_side = self.DEVICES[1]
assert "SleepNumber ILE Test1 SleepNumber" == left_side.name
assert 40 == left_side.state
assert await async_setup_component(hass, "sleepiq", {"sleepiq": CONFIG})
right_side = self.DEVICES[0]
assert "SleepNumber ILE Test2 SleepNumber" == right_side.name
assert 80 == right_side.state
device_mock = MagicMock()
sleepiq.setup_platform(hass, CONFIG, device_mock, MagicMock())
devices = device_mock.call_args[0][0]
assert 1 == len(devices)
@requests_mock.Mocker()
def test_setup_sigle(self, mock):
"""Test for successfully setting up the SleepIQ platform."""
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
right_side = devices[0]
assert "SleepNumber ILE Test1 SleepNumber" == right_side.name
assert 40 == right_side.state