Skip to content

Commit 462a2ec

Browse files
committed
update tests
1 parent 34cffb7 commit 462a2ec

File tree

5 files changed

+445
-46
lines changed

5 files changed

+445
-46
lines changed

tests/components/weatherflow_cloud/conftest.py

+75-25
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
"""Common fixtures for the WeatherflowCloud tests."""
22

33
from collections.abc import Generator
4-
from unittest.mock import AsyncMock, Mock, patch
4+
from unittest.mock import AsyncMock, MagicMock, Mock, patch
55

66
from aiohttp import ClientResponseError
77
import pytest
8+
from weatherflow4py.api import WeatherFlowRestAPI
89
from weatherflow4py.models.rest.forecast import WeatherDataForecastREST
910
from weatherflow4py.models.rest.observation import ObservationStationREST
1011
from weatherflow4py.models.rest.stations import StationsResponseREST
1112
from weatherflow4py.models.rest.unified import WeatherFlowDataREST
13+
from weatherflow4py.ws import WeatherFlowWebsocketAPI
1214

1315
from homeassistant.components.weatherflow_cloud.const import DOMAIN
1416
from homeassistant.const import CONF_API_TOKEN
@@ -81,35 +83,83 @@ async def mock_config_entry() -> MockConfigEntry:
8183

8284

8385
@pytest.fixture
84-
def mock_api():
85-
"""Fixture for Mock WeatherFlowRestAPI."""
86-
get_stations_response_data = StationsResponseREST.from_json(
87-
load_fixture("stations.json", DOMAIN)
88-
)
89-
get_forecast_response_data = WeatherDataForecastREST.from_json(
90-
load_fixture("forecast.json", DOMAIN)
91-
)
92-
get_observation_response_data = ObservationStationREST.from_json(
93-
load_fixture("station_observation.json", DOMAIN)
94-
)
86+
def mock_rest_api():
87+
"""Mock rest api."""
88+
fixtures = {
89+
"stations": StationsResponseREST.from_json(
90+
load_fixture("stations.json", DOMAIN)
91+
),
92+
"forecast": WeatherDataForecastREST.from_json(
93+
load_fixture("forecast.json", DOMAIN)
94+
),
95+
"observation": ObservationStationREST.from_json(
96+
load_fixture("station_observation.json", DOMAIN)
97+
),
98+
}
99+
100+
# Create device_station_map
101+
device_station_map = {
102+
device.device_id: station.station_id
103+
for station in fixtures["stations"].stations
104+
for device in station.devices
105+
}
95106

107+
# Prepare mock data
96108
data = {
97109
24432: WeatherFlowDataREST(
98-
weather=get_forecast_response_data,
99-
observation=get_observation_response_data,
100-
station=get_stations_response_data.stations[0],
110+
weather=fixtures["forecast"],
111+
observation=fixtures["observation"],
112+
station=fixtures["stations"].stations[0],
101113
device_observations=None,
102114
)
103115
}
104116

105-
with patch(
106-
"homeassistant.components.weatherflow_cloud.coordinator.WeatherFlowRestAPI",
107-
autospec=True,
108-
) as mock_api_class:
109-
# Create an instance of AsyncMock for the API
110-
mock_api = AsyncMock()
111-
mock_api.get_all_data.return_value = data
112-
# Patch the class to return our mock_api instance
113-
mock_api_class.return_value = mock_api
114-
117+
mock_api = AsyncMock(spec=WeatherFlowRestAPI)
118+
mock_api.get_all_data.return_value = data
119+
mock_api.async_get_stations.return_value = fixtures["stations"]
120+
mock_api.device_station_map = device_station_map
121+
mock_api.api_token = MOCK_API_TOKEN
122+
123+
# Apply patches
124+
with (
125+
patch(
126+
"homeassistant.components.weatherflow_cloud.WeatherFlowRestAPI",
127+
return_value=mock_api,
128+
) as _,
129+
patch(
130+
"homeassistant.components.weatherflow_cloud.coordinator.WeatherFlowRestAPI",
131+
return_value=mock_api,
132+
) as _,
133+
):
115134
yield mock_api
135+
136+
137+
@pytest.fixture
138+
async def mock_websocket_api():
139+
"""Mock WeatherFlowWebsocketAPI."""
140+
mock_websocket = AsyncMock()
141+
mock_websocket.send = AsyncMock()
142+
mock_websocket.recv = AsyncMock()
143+
144+
mock_ws_instance = AsyncMock(spec=WeatherFlowWebsocketAPI)
145+
mock_ws_instance.connect = AsyncMock()
146+
mock_ws_instance.send_message = AsyncMock()
147+
mock_ws_instance.register_observation_callback = MagicMock()
148+
mock_ws_instance.register_wind_callback = MagicMock()
149+
mock_ws_instance.websocket = mock_websocket
150+
151+
with (
152+
patch(
153+
"homeassistant.components.weatherflow_cloud.coordinator.WeatherFlowWebsocketAPI",
154+
return_value=mock_ws_instance,
155+
),
156+
patch(
157+
"homeassistant.components.weatherflow_cloud.WeatherFlowWebsocketAPI",
158+
return_value=mock_ws_instance,
159+
),
160+
patch(
161+
"weatherflow4py.ws.WeatherFlowWebsocketAPI", return_value=mock_ws_instance
162+
),
163+
):
164+
# mock_connect.return_value = mock_websocket
165+
yield mock_ws_instance

0 commit comments

Comments
 (0)