Skip to content

Commit

Permalink
chore(tests): add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
firstof9 committed Aug 17, 2024
1 parent 7155e88 commit a234bff
Show file tree
Hide file tree
Showing 6 changed files with 1,073 additions and 7 deletions.
79 changes: 79 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,88 @@
"""Fixtures for tests"""

import pytest
from aioresponses import aioresponses

from unittest.mock import patch

import os

pytest_plugins = "pytest_homeassistant_custom_component"

API_URL = "https://api.weather.gov"
COUNT_URL = "https://api.weather.gov/alerts/active/count"
ZONE_URL = "https://api.weather.gov/alerts/active?zone=AZZ540,AZC013"
POINT_URL = "https://api.weather.gov/alerts/active?point=123,-456"


# This fixture enables loading custom integrations in all tests.
# Remove to enable selective use of this fixture
@pytest.fixture(autouse=True)
def auto_enable_custom_integrations(enable_custom_integrations):
"""Enable custom integration tests."""
yield


# This fixture is used to prevent HomeAssistant from attempting to create and dismiss persistent
# notifications. These calls would fail without this fixture since the persistent_notification
# integration is never loaded during a test.
@pytest.fixture(name="skip_notifications", autouse=True)
def skip_notifications_fixture():
"""Skip notification calls."""
with patch("homeassistant.components.persistent_notification.async_create"), patch(
"homeassistant.components.persistent_notification.async_dismiss"
):
yield


@pytest.fixture
def aioclient_mock():
"""Fixture to mock aioclient calls."""
with aioresponses() as mock_aiohttp:
mock_headers = {"content-type": "content-type: application/geo+json"}
mock_aiohttp.get(
API_URL,
status=200,
headers=mock_headers,
body={},
)

yield mock_aiohttp


@pytest.fixture
def mock_aioclient():
"""Fixture to mock aioclient calls."""
with aioresponses() as m:
yield m


def load_fixture(filename):
"""Load a fixture."""
path = os.path.join(os.path.dirname(__file__), "fixtures", filename)
with open(path, encoding="utf-8") as fptr:
return fptr.read()


@pytest.fixture(name="mock_api")
def mock_api(mock_aioclient):
"""Load the charger data."""
mock_aioclient.get(
ZONE_URL,
status=200,
body=load_fixture("api.json"),
repeat=True,
)
mock_aioclient.get(
POINT_URL,
status=200,
body=load_fixture("api.json"),
repeat=True,
)
mock_aioclient.get(
COUNT_URL,
status=200,
body=load_fixture("count_reply.json"),
repeat=True,
)
yield mock_aioclient
158 changes: 158 additions & 0 deletions tests/fixtures/api.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
{
"@context": [
"https://geojson.org/geojson-ld/geojson-context.jsonld",
{
"@version": "1.1",
"wx": "https://api.weather.gov/ontology#",
"@vocab": "https://api.weather.gov/ontology#"
}
],
"type": "FeatureCollection",
"features": [
{
"id": "https://api.weather.gov/alerts/urn:oid:2.49.0.1.840.0.505a7220d91b00eb1d75a3fb4f339f825496a522.004.1",
"type": "Feature",
"geometry": null,
"properties": {
"@id": "https://api.weather.gov/alerts/urn:oid:2.49.0.1.840.0.505a7220d91b00eb1d75a3fb4f339f825496a522.004.1",
"@type": "wx:Alert",
"id": "urn:oid:2.49.0.1.840.0.505a7220d91b00eb1d75a3fb4f339f825496a522.004.1",
"areaDesc": "Northwest Valley; Buckeye/Avondale; Deer Valley; Central Phoenix; North Phoenix/Glendale; Scottsdale/Paradise Valley; East Valley; South Mountain/Ahwatukee; Southeast Valley/Queen Creek",
"geocode": {
"SAME": [
"004013"
],
"UGC": [
"AZZ537",
"AZZ540",
"AZZ542",
"AZZ543",
"AZZ544",
"AZZ546",
"AZZ548",
"AZZ550",
"AZZ551"
]
},
"affectedZones": [
"https://api.weather.gov/zones/forecast/AZZ537",
"https://api.weather.gov/zones/forecast/AZZ540",
"https://api.weather.gov/zones/forecast/AZZ542",
"https://api.weather.gov/zones/forecast/AZZ543",
"https://api.weather.gov/zones/forecast/AZZ544",
"https://api.weather.gov/zones/forecast/AZZ546",
"https://api.weather.gov/zones/forecast/AZZ548",
"https://api.weather.gov/zones/forecast/AZZ550",
"https://api.weather.gov/zones/forecast/AZZ551"
],
"references": [
{
"@id": "https://api.weather.gov/alerts/urn:oid:2.49.0.1.840.0.2852349b8f25500975c692db29cb2f047ff20a7c.004.2",
"identifier": "urn:oid:2.49.0.1.840.0.2852349b8f25500975c692db29cb2f047ff20a7c.004.2",
"sender": "[email protected]",
"sent": "2024-07-18T01:14:00-07:00"
}
],
"sent": "2024-07-18T12:47:00-07:00",
"effective": "2024-07-18T12:47:00-07:00",
"onset": "2024-07-19T10:00:00-07:00",
"expires": "2024-07-19T03:00:00-07:00",
"ends": "2024-07-20T20:00:00-07:00",
"status": "Actual",
"messageType": "Update",
"category": "Met",
"severity": "Severe",
"certainty": "Likely",
"urgency": "Expected",
"event": "Excessive Heat Warning",
"sender": "[email protected]",
"senderName": "NWS Phoenix AZ",
"headline": "Excessive Heat Warning issued July 18 at 12:47PM MST until July 20 at 8:00PM MST by NWS Phoenix AZ",
"description": "* WHAT...Dangerously hot conditions. Afternoon temperatures 112 to\n116 expected. Major Heat Risk. Overexposure can cause heat cramps\nand heat exhaustion to develop and, without intervention, can lead\nto heat stroke.\n\n* WHERE...The Northwest Valley of the Phoenix Metro Area, The East\nValley of the Phoenix Metro Area, Buckeye/Avondale, Deer Valley,\nCentral Phoenix, North Phoenix/Glendale, Scottsdale/Paradise\nValley, South Mountain/Ahwatukee, and Southeast Valley/Queen Creek.\n\n* WHEN...From 10 AM Friday to 8 PM MST Saturday.\n\n* IMPACTS...Heat related illnesses increase significantly during\nextreme heat events.\n\n* ADDITIONAL DETAILS...In Maricopa County, call 2-1-1 to find a free\ncooling center, transportation, water, and more.\nhttps://www.maricopa.gov/heat",
"instruction": "An Excessive Heat Warning means that a period of very hot\ntemperatures, even by local standards, will occur. Actions should be\ntaken to lessen the impact of the extreme heat.\n\nTake extra precautions if you work or spend time outside. When\npossible, reschedule strenuous activities to early morning or\nevening. Know the signs and symptoms of heat exhaustion and heat\nstroke. Wear lightweight and loose-fitting clothing when possible\nand drink plenty of water.\n\nTo reduce risk during outdoor work, the Occupational Safety and\nHealth Administration recommends scheduling frequent rest breaks in\nshaded or air conditioned environments. Anyone overcome by heat\nshould be moved to a cool and shaded location. Heat stroke is an\nemergency! Call 9 1 1.\n\nPublic cooling shelters are available in some areas. Consult county\nofficials for more details.",
"response": "Execute",
"parameters": {
"AWIPSidentifier": [
"NPWPSR"
],
"WMOidentifier": [
"WWUS75 KPSR 181947"
],
"NWSheadline": [
"EXCESSIVE HEAT WARNING REMAINS IN EFFECT FROM 10 AM FRIDAY TO 8 PM MST SATURDAY"
],
"BLOCKCHANNEL": [
"EAS",
"NWEM",
"CMAS"
],
"VTEC": [
"/O.CON.KPSR.EH.W.0006.240719T1700Z-240721T0300Z/"
],
"eventEndingTime": [
"2024-07-21T03:00:00+00:00"
]
}
}
},
{
"id": "https://api.weather.gov/alerts/urn:oid:2.49.0.1.840.0.32b8fe5b1e8094248fdbb7a32619581ec4e1df14.001.1",
"type": "Feature",
"geometry": null,
"properties": {
"@id": "https://api.weather.gov/alerts/urn:oid:2.49.0.1.840.0.32b8fe5b1e8094248fdbb7a32619581ec4e1df14.001.1",
"@type": "wx:Alert",
"id": "urn:oid:2.49.0.1.840.0.32b8fe5b1e8094248fdbb7a32619581ec4e1df14.001.1",
"areaDesc": "Maricopa, AZ",
"geocode": {
"SAME": [
"004013"
],
"UGC": [
"AZC013"
]
},
"affectedZones": [
"https://api.weather.gov/zones/county/AZC013"
],
"references": [],
"sent": "2024-07-18T08:13:00-07:00",
"effective": "2024-07-18T08:13:00-07:00",
"onset": "2024-07-18T08:13:00-07:00",
"expires": "2024-07-19T21:00:00-07:00",
"ends": null,
"status": "Actual",
"messageType": "Alert",
"category": "Met",
"severity": "Unknown",
"certainty": "Unknown",
"urgency": "Unknown",
"event": "Air Quality Alert",
"sender": "[email protected]",
"senderName": "NWS Phoenix AZ",
"headline": "Air Quality Alert issued July 18 at 8:13AM MST by NWS Phoenix AZ",
"description": "AQAPSR\n\nThe Arizona Department of Environmental Quality (ADEQ) has issued an\nOzone High Pollution Advisory for the Phoenix Metro Area through\nFriday.\n\nThis means that forecast weather conditions combined with existing\nozone levels are expected to result in local maximum 8-hour ozone\nconcentrations that pose a health risk. Adverse health effects\nincrease as air quality deteriorates.\n\nOzone is an air contaminant which can cause breathing difficulties\nfor children, older adults, as well as persons with respiratory\nproblems. A decrease in physical activity is recommended.\n\nYou are urged to car pool, telecommute or use mass transit.\nThe use of gasoline-powered equipment should be reduced or done late\nin the day.\n\nFor details on this High Pollution Advisory, visit the ADEQ internet\nsite at www.azdeq.gov/forecast/phoenix or call 602-771-2300.",
"instruction": null,
"response": "Monitor",
"parameters": {
"AWIPSidentifier": [
"AQAPSR"
],
"WMOidentifier": [
"AEUS75 KPSR 181513"
],
"NWSheadline": [
"OZONE HIGH POLLUTION ADVISORY FOR MARICOPA COUNTY INCLUDING THE PHOENIX METRO AREA THROUGH FRIDAY"
],
"BLOCKCHANNEL": [
"EAS",
"NWEM",
"CMAS"
]
}
}
}
],
"title": "Current watches, warnings, and advisories for 33.25 N, 112.30 W",
"updated": "2024-07-18T19:50:06+00:00"
}
Loading

0 comments on commit a234bff

Please sign in to comment.