1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
"""Test the models."""
from datetime import datetime
import pytest
from aresponses import ResponsesMockServer
from syrupy.assertion import SnapshotAssertion
from forecast_solar import AccountType, Estimate, ForecastSolar, Plane
from . import load_fixtures
@pytest.mark.freeze_time("2024-04-26T12:00:00+02:00")
async def test_estimated_forecast(
aresponses: ResponsesMockServer,
snapshot: SnapshotAssertion,
forecast_client: ForecastSolar,
) -> None:
"""Test estimated forecast."""
aresponses.add(
"api.forecast.solar",
"/estimate/52.16/4.47/20/10/2.16",
"GET",
aresponses.Response(
status=200,
headers={
"Content-Type": "application/json",
"X-Ratelimit-Limit": "10",
"X-Ratelimit-Period": "1",
},
text=load_fixtures("forecast.json"),
),
)
forecast: Estimate = await forecast_client.estimate()
assert forecast == snapshot
assert forecast.timezone == "Europe/Amsterdam"
assert forecast.account_type == AccountType.PUBLIC
assert forecast.energy_production_today == 6660
assert forecast.energy_production_tomorrow == 5338
assert forecast.power_production_now == 773
assert forecast.energy_production_today_remaining == 4144
assert forecast.energy_current_hour == 821
assert forecast.power_highest_peak_time_today == datetime.fromisoformat(
"2024-04-26T11:00:00+02:00"
)
assert forecast.power_highest_peak_time_tomorrow == datetime.fromisoformat(
"2024-04-27T12:00:00+02:00"
)
assert forecast.sum_energy_production(1) == 742
assert forecast.sum_energy_production(6) == 3093
assert forecast.sum_energy_production(12) == 3323
assert forecast.sum_energy_production(24) == 5633
@pytest.mark.freeze_time("2024-04-27T07:00:00+02:00")
async def test_estimated_forecast_with_subscription(
aresponses: ResponsesMockServer,
snapshot: SnapshotAssertion,
forecast_key_client: ForecastSolar,
) -> None:
"""Test estimated forecast."""
aresponses.add(
"api.forecast.solar",
"/myapikey/estimate/52.16/4.47/20/10/2.16",
"GET",
aresponses.Response(
status=200,
headers={
"Content-Type": "application/json",
"X-Ratelimit-Limit": "60",
"X-Ratelimit-Period": "3600",
},
text=load_fixtures("forecast_personal.json"),
),
)
forecast: Estimate = await forecast_key_client.estimate()
assert forecast == snapshot
assert forecast.timezone == "Europe/Amsterdam"
assert forecast.account_type == AccountType.PERSONAL
assert forecast.energy_production_today == 5788
assert forecast.energy_production_tomorrow == 7507
assert forecast.power_production_now == 92
assert forecast.energy_production_today_remaining == 5783
assert forecast.energy_current_hour == 96
assert forecast.power_highest_peak_time_today == datetime.fromisoformat(
"2024-04-27T13:30:00+02:00"
)
assert forecast.power_highest_peak_time_tomorrow == datetime.fromisoformat(
"2024-04-28T14:30:00+02:00"
)
assert forecast.sum_energy_production(1) == 216
assert forecast.sum_energy_production(6) == 2802
assert forecast.sum_energy_production(12) == 5582
assert forecast.sum_energy_production(24) == 5784
@pytest.mark.freeze_time("2024-04-27T07:00:00+02:00")
async def test_estimated_forecast_with_subscription_and_actual_value(
aresponses: ResponsesMockServer,
snapshot: SnapshotAssertion,
forecast_key_client: ForecastSolar,
) -> None:
"""Test estimated forecast."""
aresponses.add(
"api.forecast.solar",
"/myapikey/estimate/52.16/4.47/20/10/2.16",
"GET",
aresponses.Response(
status=200,
headers={
"Content-Type": "application/json",
"X-Ratelimit-Limit": "60",
"X-Ratelimit-Period": "3600",
},
text=load_fixtures("forecast_personal.json"),
),
)
forecast: Estimate = await forecast_key_client.estimate(actual=2.300)
assert forecast == snapshot
assert forecast.timezone == "Europe/Amsterdam"
assert forecast.account_type == AccountType.PERSONAL
assert forecast.energy_production_today == 5788
assert forecast.energy_production_tomorrow == 7507
assert forecast.sum_energy_production(1) == 216
assert forecast.sum_energy_production(6) == 2802
assert forecast.sum_energy_production(12) == 5582
assert forecast.sum_energy_production(24) == 5784
async def test_api_key_validation(
aresponses: ResponsesMockServer,
forecast_key_client: ForecastSolar,
) -> None:
"""Test API key validation."""
aresponses.add(
"api.forecast.solar",
"/myapikey/info",
"GET",
aresponses.Response(
status=200,
headers={
"Content-Type": "application/json",
"X-Ratelimit-Limit": "10",
"X-Ratelimit-Period": "1",
},
text=load_fixtures("validate_key.json"),
),
)
assert await forecast_key_client.validate_api_key() is True
async def test_plane_validation(
aresponses: ResponsesMockServer,
forecast_client: ForecastSolar,
) -> None:
"""Test plane validation."""
aresponses.add(
"api.forecast.solar",
"/check/52.16/4.47/20/10/2.16",
"GET",
aresponses.Response(
status=200,
headers={
"Content-Type": "application/json",
"X-Ratelimit-Limit": "10",
"X-Ratelimit-Period": "1",
},
text=load_fixtures("validate_plane.json"),
),
)
assert await forecast_client.validate_plane() is True
@pytest.mark.freeze_time("2024-04-26T12:00:00+02:00")
async def test_estimated_forecast_multi_plane(
aresponses: ResponsesMockServer,
snapshot: SnapshotAssertion,
forecast_multi_plane_client: ForecastSolar,
) -> None:
"""Test estimated forecast with multiple planes."""
aresponses.add(
"api.forecast.solar",
"/myapikey/estimate/52.16/4.47/20/10/2.16/30/-90/1.5",
"GET",
aresponses.Response(
status=200,
headers={
"Content-Type": "application/json",
"X-Ratelimit-Limit": "60",
"X-Ratelimit-Period": "3600",
},
text=load_fixtures("forecast_personal.json"),
),
)
forecast: Estimate = await forecast_multi_plane_client.estimate()
assert forecast == snapshot
assert forecast.timezone == "Europe/Amsterdam"
assert forecast.account_type == AccountType.PERSONAL
async def test_multi_plane_validation(
aresponses: ResponsesMockServer,
forecast_multi_plane_client: ForecastSolar,
) -> None:
"""Test plane validation with multiple planes."""
aresponses.add(
"api.forecast.solar",
"/check/52.16/4.47/20/10/2.16/30/-90/1.5",
"GET",
aresponses.Response(
status=200,
headers={
"Content-Type": "application/json",
"X-Ratelimit-Limit": "10",
"X-Ratelimit-Period": "1",
},
text=load_fixtures("validate_plane.json"),
),
)
assert await forecast_multi_plane_client.validate_plane() is True
async def test_planes_ignored_without_api_key(
aresponses: ResponsesMockServer,
) -> None:
"""Test that planes are silently ignored when no API key is provided."""
# When no API key is provided, planes should be ignored
# and only the primary plane used
aresponses.add(
"api.forecast.solar",
"/estimate/52.16/4.47/20/10/2.16", # Only primary plane, no additional planes
"GET",
aresponses.Response(
status=200,
headers={
"Content-Type": "application/json",
"X-Ratelimit-Limit": "10",
"X-Ratelimit-Period": "1",
},
text=load_fixtures("forecast.json"),
),
)
async with ForecastSolar(
latitude=52.16,
longitude=4.47,
declination=20,
azimuth=10,
kwp=2.160,
planes=[
Plane(declination=30, azimuth=-90, kwp=1.5),
],
) as forecast:
estimate = await forecast.estimate()
assert estimate is not None
|