Refactor Airly tests (#44315)

This commit is contained in:
Maciej Bieniek 2020-12-17 16:44:46 +01:00 committed by GitHub
parent 7c63119ad2
commit 94e1f8e631
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 171 additions and 185 deletions

View file

@ -1,6 +1,5 @@
"""Test init of Airly integration."""
from datetime import timedelta
import json
from homeassistant.components.airly.const import DOMAIN
from homeassistant.config_entries import (
@ -10,14 +9,15 @@ from homeassistant.config_entries import (
)
from homeassistant.const import STATE_UNAVAILABLE
from tests.async_mock import patch
from . import API_POINT_URL
from tests.common import MockConfigEntry, load_fixture
from tests.components.airly import init_integration
async def test_async_setup_entry(hass):
async def test_async_setup_entry(hass, aioclient_mock):
"""Test a successful setup entry."""
await init_integration(hass)
await init_integration(hass, aioclient_mock)
state = hass.states.get("air_quality.home")
assert state is not None
@ -25,75 +25,69 @@ async def test_async_setup_entry(hass):
assert state.state == "14"
async def test_config_not_ready(hass):
async def test_config_not_ready(hass, aioclient_mock):
"""Test for setup failure if connection to Airly is missing."""
entry = MockConfigEntry(
domain=DOMAIN,
title="Home",
unique_id="55.55-122.12",
unique_id="123-456",
data={
"api_key": "foo",
"latitude": 55.55,
"longitude": 122.12,
"latitude": 123,
"longitude": 456,
"name": "Home",
},
)
with patch("airly._private._RequestsHandler.get", side_effect=ConnectionError()):
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
assert entry.state == ENTRY_STATE_SETUP_RETRY
aioclient_mock.get(API_POINT_URL, exc=ConnectionError())
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
assert entry.state == ENTRY_STATE_SETUP_RETRY
async def test_config_without_unique_id(hass):
async def test_config_without_unique_id(hass, aioclient_mock):
"""Test for setup entry without unique_id."""
entry = MockConfigEntry(
domain=DOMAIN,
title="Home",
data={
"api_key": "foo",
"latitude": 55.55,
"longitude": 122.12,
"latitude": 123,
"longitude": 456,
"name": "Home",
},
)
with patch(
"airly._private._RequestsHandler.get",
return_value=json.loads(load_fixture("airly_valid_station.json")),
):
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
assert entry.state == ENTRY_STATE_LOADED
assert entry.unique_id == "55.55-122.12"
aioclient_mock.get(API_POINT_URL, text=load_fixture("airly_valid_station.json"))
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
assert entry.state == ENTRY_STATE_LOADED
assert entry.unique_id == "123-456"
async def test_config_with_turned_off_station(hass):
async def test_config_with_turned_off_station(hass, aioclient_mock):
"""Test for setup entry for a turned off measuring station."""
entry = MockConfigEntry(
domain=DOMAIN,
title="Home",
unique_id="55.55-122.12",
unique_id="123-456",
data={
"api_key": "foo",
"latitude": 55.55,
"longitude": 122.12,
"latitude": 123,
"longitude": 456,
"name": "Home",
},
)
with patch(
"airly._private._RequestsHandler.get",
return_value=json.loads(load_fixture("airly_no_station.json")),
):
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
assert entry.state == ENTRY_STATE_SETUP_RETRY
aioclient_mock.get(API_POINT_URL, text=load_fixture("airly_no_station.json"))
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
assert entry.state == ENTRY_STATE_SETUP_RETRY
async def test_update_interval(hass):
async def test_update_interval(hass, aioclient_mock):
"""Test correct update interval when the number of configured instances changes."""
entry = await init_integration(hass)
entry = await init_integration(hass, aioclient_mock)
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
assert entry.state == ENTRY_STATE_LOADED
@ -112,13 +106,13 @@ async def test_update_interval(hass):
},
)
with patch(
"airly._private._RequestsHandler.get",
return_value=json.loads(load_fixture("airly_valid_station.json")),
):
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
aioclient_mock.get(
"https://airapi.airly.eu/v2/measurements/point?lat=66.660000&lng=111.110000",
text=load_fixture("airly_valid_station.json"),
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert len(hass.config_entries.async_entries(DOMAIN)) == 2
assert entry.state == ENTRY_STATE_LOADED
@ -126,9 +120,9 @@ async def test_update_interval(hass):
assert instance.update_interval == timedelta(minutes=30)
async def test_unload_entry(hass):
async def test_unload_entry(hass, aioclient_mock):
"""Test successful unload of entry."""
entry = await init_integration(hass)
entry = await init_integration(hass, aioclient_mock)
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
assert entry.state == ENTRY_STATE_LOADED