Rewrite sigfox unittest tests to pytest (#41302)

This commit is contained in:
Björn Olsson Jarl 2020-10-06 10:16:41 +02:00 committed by GitHub
parent e303064f75
commit 3abf30cf42
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,5 @@
"""Tests for the sigfox sensor.""" """Tests for the sigfox sensor."""
import re import re
import unittest
import requests_mock import requests_mock
@ -9,9 +8,7 @@ from homeassistant.components.sigfox.sensor import (
CONF_API_LOGIN, CONF_API_LOGIN,
CONF_API_PASSWORD, CONF_API_PASSWORD,
) )
from homeassistant.setup import setup_component from homeassistant.setup import async_setup_component
from tests.common import get_test_home_assistant
TEST_API_LOGIN = "foo" TEST_API_LOGIN = "foo"
TEST_API_PASSWORD = "ebcd1234" TEST_API_PASSWORD = "ebcd1234"
@ -33,39 +30,32 @@ VALID_MESSAGE = """
""" """
class TestSigfoxSensor(unittest.TestCase): async def test_invalid_credentials(hass):
"""Test the sigfox platform.""" """Test for invalid credentials."""
with requests_mock.Mocker() as mock_req:
url = re.compile(API_URL + "devicetypes")
mock_req.get(url, text="{}", status_code=401)
assert await async_setup_component(hass, "sensor", VALID_CONFIG)
await hass.async_block_till_done()
assert len(hass.states.async_entity_ids()) == 0
def setUp(self):
"""Initialize values for this testcase class."""
self.hass = get_test_home_assistant()
self.addCleanup(self.hass.stop)
def test_invalid_credentials(self): async def test_valid_credentials(hass):
"""Test for invalid credentials.""" """Test for valid credentials."""
with requests_mock.Mocker() as mock_req: with requests_mock.Mocker() as mock_req:
url = re.compile(API_URL + "devicetypes") url1 = re.compile(API_URL + "devicetypes")
mock_req.get(url, text="{}", status_code=401) mock_req.get(url1, text='{"data":[{"id":"fake_type"}]}', status_code=200)
assert setup_component(self.hass, "sensor", VALID_CONFIG)
self.hass.block_till_done()
assert len(self.hass.states.entity_ids()) == 0
def test_valid_credentials(self): url2 = re.compile(API_URL + "devicetypes/fake_type/devices")
"""Test for valid credentials.""" mock_req.get(url2, text='{"data":[{"id":"fake_id"}]}')
with requests_mock.Mocker() as mock_req:
url1 = re.compile(API_URL + "devicetypes")
mock_req.get(url1, text='{"data":[{"id":"fake_type"}]}', status_code=200)
url2 = re.compile(API_URL + "devicetypes/fake_type/devices") url3 = re.compile(API_URL + "devices/fake_id/messages*")
mock_req.get(url2, text='{"data":[{"id":"fake_id"}]}') mock_req.get(url3, text=VALID_MESSAGE)
url3 = re.compile(API_URL + "devices/fake_id/messages*") assert await async_setup_component(hass, "sensor", VALID_CONFIG)
mock_req.get(url3, text=VALID_MESSAGE) await hass.async_block_till_done()
assert setup_component(self.hass, "sensor", VALID_CONFIG) assert len(hass.states.async_entity_ids()) == 1
self.hass.block_till_done() state = hass.states.get("sensor.sigfox_fake_id")
assert state.state == "payload"
assert len(self.hass.states.entity_ids()) == 1 assert state.attributes.get("snr") == "50.0"
state = self.hass.states.get("sensor.sigfox_fake_id")
assert state.state == "payload"
assert state.attributes.get("snr") == "50.0"