Skip to content

Commit 807c6d6

Browse files
committed
MEGA TEST COVERAGE
1 parent c008050 commit 807c6d6

3 files changed

Lines changed: 152 additions & 1 deletion

File tree

tests/components/openaq/test_config_flow.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import pytest
2424

2525
from homeassistant import config_entries
26+
from homeassistant.components.openaq.config_flow import OpenAQLocationFlowData
2627
from homeassistant.components.openaq.const import (
2728
CONF_LIMIT,
2829
CONF_LOCATION_ID,
@@ -265,6 +266,38 @@ async def test_location_subentry_map_flow_sorts_by_sensor_count_before_distance(
265266
]
266267

267268

269+
async def test_location_subentry_map_flow_labels_unknown_distance(
270+
hass: HomeAssistant,
271+
mock_openaq_client: AsyncMock,
272+
mock_config_entry: MockConfigEntry,
273+
) -> None:
274+
"""Test map search labels locations with invalid distances."""
275+
mock_config_entry.add_to_hass(hass)
276+
mock_openaq_client.locations.list.return_value = make_response(
277+
[make_location(location_id=9999, distance=True)]
278+
)
279+
result = await hass.config_entries.subentries.async_init(
280+
(mock_config_entry.entry_id, "location"),
281+
context={"source": config_entries.SOURCE_USER},
282+
)
283+
284+
result = await hass.config_entries.subentries.async_configure(
285+
result["flow_id"],
286+
{
287+
ATTR_LOCATION: {ATTR_LATITUDE: 35.1, ATTR_LONGITUDE: -106.6},
288+
CONF_RADIUS: 5000,
289+
CONF_LIMIT: 10,
290+
},
291+
)
292+
293+
assert _get_select_options(result) == [
294+
SelectOptionDict(
295+
value="9999",
296+
label="Del Norte, Albuquerque - 1 sensor: PM2.5 - unknown distance",
297+
)
298+
]
299+
300+
268301
async def test_location_subentry_map_flow_limits_to_top_five_locations(
269302
hass: HomeAssistant,
270303
mock_openaq_client: AsyncMock,
@@ -465,6 +498,41 @@ async def test_location_subentry_invalid_map_location_id(
465498
assert result["errors"] == {"base": "no_locations_found"}
466499

467500

501+
async def test_location_subentry_invalid_map_sensors(
502+
hass: HomeAssistant,
503+
mock_openaq_client: AsyncMock,
504+
mock_config_entry: MockConfigEntry,
505+
) -> None:
506+
"""Test map search ignores locations without sensor lists."""
507+
mock_config_entry.add_to_hass(hass)
508+
mock_openaq_client.locations.list.return_value = make_response(
509+
[SimpleNamespace(id=9999, name="Bad", locality="Albuquerque", sensors=None)]
510+
)
511+
result = await hass.config_entries.subentries.async_init(
512+
(mock_config_entry.entry_id, "location"),
513+
context={"source": config_entries.SOURCE_USER},
514+
)
515+
result = await hass.config_entries.subentries.async_configure(
516+
result["flow_id"],
517+
{
518+
ATTR_LOCATION: {ATTR_LATITUDE: 35.1, ATTR_LONGITUDE: -106.6},
519+
CONF_RADIUS: 5000,
520+
CONF_LIMIT: 10,
521+
},
522+
)
523+
524+
assert result["type"] is FlowResultType.FORM
525+
assert result["step_id"] == "map"
526+
assert result["errors"] == {"base": "no_locations_found"}
527+
528+
529+
def test_location_select_label_without_supported_parameters() -> None:
530+
"""Test location select label with no supported parameters."""
531+
location = OpenAQLocationFlowData(location_id=9999, title="Del Norte")
532+
533+
assert location.select_label == "Del Norte"
534+
535+
468536
@pytest.mark.parametrize(
469537
("exception", "error"),
470538
[

tests/components/openaq/test_coordinator.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
"""Test OpenAQ data coordinator helpers."""
22

33
from types import MappingProxyType, SimpleNamespace
4+
from unittest.mock import AsyncMock, patch
45

56
import httpx
67

78
from homeassistant.components.openaq.coordinator import (
9+
HomeAssistantOpenAQTransport,
810
OpenAQMeasurement,
11+
async_create_openaq_client,
912
create_openaq_client,
1013
get_openaq_value,
1114
normalize_latest_measurements,
@@ -14,10 +17,40 @@
1417
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
1518
CONCENTRATION_MILLIGRAMS_PER_CUBIC_METER,
1619
)
20+
from homeassistant.core import HomeAssistant
1721

1822
from .conftest import make_latest, make_sensor
1923

2024

25+
async def test_transport_sends_request_with_shared_httpx_client() -> None:
26+
"""Test the transport sends requests with the shared httpx client."""
27+
response = httpx.Response(
28+
200,
29+
json={"results": []},
30+
request=httpx.Request("GET", "https://api.openaq.org/v3/locations"),
31+
)
32+
httpx_client = AsyncMock(spec=httpx.AsyncClient)
33+
httpx_client.request.return_value = response
34+
35+
transport = HomeAssistantOpenAQTransport(httpx_client)
36+
37+
assert (
38+
await transport.send_request(
39+
"GET",
40+
"https://api.openaq.org/v3/locations",
41+
params={"limit": 1},
42+
headers={"X-API-Key": "api-key"},
43+
)
44+
is response
45+
)
46+
httpx_client.request.assert_awaited_once_with(
47+
method="GET",
48+
url="https://api.openaq.org/v3/locations",
49+
params={"limit": 1},
50+
headers={"X-API-Key": "api-key"},
51+
)
52+
53+
2154
async def test_create_openaq_client_keeps_shared_httpx_client_open() -> None:
2255
"""Test closing the OpenAQ client does not close the shared httpx client."""
2356
httpx_client = httpx.AsyncClient()
@@ -31,6 +64,25 @@ async def test_create_openaq_client_keeps_shared_httpx_client_open() -> None:
3164
await httpx_client.aclose()
3265

3366

67+
async def test_async_create_openaq_client_uses_shared_httpx_client(
68+
hass: HomeAssistant,
69+
) -> None:
70+
"""Test creating an OpenAQ client from Home Assistant."""
71+
httpx_client = httpx.AsyncClient()
72+
73+
try:
74+
with patch(
75+
"homeassistant.components.openaq.coordinator.get_async_client",
76+
return_value=httpx_client,
77+
):
78+
client = await async_create_openaq_client(hass, "api-key")
79+
80+
assert client.transport.client is httpx_client
81+
await client.close()
82+
finally:
83+
await httpx_client.aclose()
84+
85+
3486
def test_get_openaq_value_dict() -> None:
3587
"""Test getting OpenAQ values from dict data."""
3688
data = {"id": 123}
@@ -45,6 +97,7 @@ def test_normalize_latest_measurements_ignores_invalid_data() -> None:
4597
[
4698
make_latest("1", 8.5),
4799
make_latest("unknown", 12.1),
100+
make_latest(999, 44.1),
48101
make_latest(2, True),
49102
make_latest(3, 33.2),
50103
],

tests/components/openaq/test_sensor.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@
2020
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
2121

2222
from . import setup_integration
23-
from .conftest import LOCATION_ID, make_latest, make_response, make_sensor
23+
from .conftest import (
24+
LOCATION_ID,
25+
make_latest,
26+
make_location,
27+
make_response,
28+
make_sensor,
29+
)
2430

2531
from tests.common import MockConfigEntry, snapshot_platform
2632

@@ -161,6 +167,30 @@ async def test_distance_from_home_sensor_uses_configured_unit_system(
161167
assert state.attributes["unit_of_measurement"] == UnitOfLength.MILES
162168

163169

170+
async def test_distance_from_home_sensor_unknown_without_coordinates(
171+
hass: HomeAssistant,
172+
entity_registry: er.EntityRegistry,
173+
mock_openaq_client: AsyncMock,
174+
mock_config_entry: MockConfigEntry,
175+
) -> None:
176+
"""Test distance from Home Assistant sensor without valid coordinates."""
177+
mock_openaq_client.locations.get.return_value = make_response(
178+
[make_location(coordinates=(True, -106.6))]
179+
)
180+
entity_registry.async_get_or_create(
181+
"sensor",
182+
"openaq",
183+
f"{LOCATION_ID}_distance_from_home",
184+
suggested_object_id="del_norte_distance_from_home_assistant",
185+
disabled_by=None,
186+
)
187+
188+
await setup_integration(hass, mock_config_entry)
189+
190+
assert (state := hass.states.get("sensor.del_norte_distance_from_home_assistant"))
191+
assert state.state == STATE_UNKNOWN
192+
193+
164194
async def test_missing_latest_values_are_not_created(
165195
hass: HomeAssistant,
166196
entity_registry: er.EntityRegistry,

0 commit comments

Comments
 (0)