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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
|
"""Test sunweg.api."""
from datetime import date, datetime
from os import path
import os
from unittest import TestCase
from unittest.mock import MagicMock, patch
import pytest
from requests import Response
from sunweg.api import (
APIHelper,
convert_situation_status,
SunWegApiError,
separate_value_metric,
)
from sunweg.device import Inverter, String
from sunweg.util import Status
from .common import INVERTER_MOCK, PLANT_MOCK
class Api_Test(TestCase):
"""APIHelper test case."""
responses: dict[str, Response] = {}
def setUp(self) -> None:
"""Set tests up."""
for file in os.listdir(path.join(path.dirname(__file__), "responses")):
filename = path.basename(file)
with open(path.join(path.dirname(__file__), "responses", file)) as f:
response = Response()
if filename.startswith("error"):
response.status_code = int(filename.split("_")[1])
response.reason = "".join(f.readlines())
else:
response.status_code = 200
response._content = "".join(f.readlines()).encode()
self.responses[filename] = response
def test_convert_situation_status(self) -> None:
"""Test the conversion from situation to status."""
status_ok: Status = convert_situation_status(1)
status_err: Status = convert_situation_status(0)
status_wrn: Status = convert_situation_status(2)
assert status_ok == Status.OK
assert status_err == Status.ERROR
assert status_wrn == Status.WARN
def test_separate_value_metric_comma(self) -> None:
"""Test the separation from value and metric of string with comma."""
(value, metric) = separate_value_metric("0,0")
assert value == 0
assert metric == ""
(value, metric) = separate_value_metric("1,0", "W")
assert value == 1.0
assert metric == "W"
(value, metric) = separate_value_metric("0,2 kW", "W")
assert value == 0.2
assert metric == "kW"
def test_separate_value_metric_dot(self) -> None:
"""Test the separation from value and metric of string with dot."""
(value, metric) = separate_value_metric("0.0")
assert value == 0
assert metric == ""
(value, metric) = separate_value_metric("1.0", "W")
assert value == 1.0
assert metric == "W"
(value, metric) = separate_value_metric("0.2 kW", "W")
assert value == 0.2
assert metric == "kW"
def test_separate_value_metric_none_int(self) -> None:
"""Test the separation from value and metric of string with dot."""
(value, metric) = separate_value_metric(None)
assert value == 0
assert metric == ""
(value, metric) = separate_value_metric("1", "W")
assert value == 1.0
assert metric == "W"
(value, metric) = separate_value_metric("2 kW", "W")
assert value == 2.0
assert metric == "kW"
def test_error500(self) -> None:
"""Test error 500."""
with patch(
"requests.Session.post",
return_value=self.responses["error_500_response.txt"],
):
api = APIHelper("user@acme.com", "password")
with pytest.raises(SunWegApiError) as e_info:
api.authenticate()
assert e_info.value.__str__() == "Request failed: <Response [500]>"
def test_initialize_token(self) -> None:
"""Test initialize token."""
api = APIHelper(token="token")
assert api._token == "token"
def test_set_token(self) -> None:
"""Test set token."""
api = APIHelper(token="token")
api.set_token("new_token")
assert api._token == "new_token"
def test_authenticate_success(self) -> None:
"""Test authentication success."""
with patch(
"requests.Session.post",
return_value=self.responses["auth_success_response.json"],
):
api = APIHelper("user@acme.com", "password")
assert api.authenticate()
def test_authenticate_fail_empty_credentials(self) -> None:
"""Test authentication failed."""
api = APIHelper(None, None)
assert not api.authenticate()
def test_authenticate_failed(self) -> None:
"""Test authentication failed."""
with patch(
"requests.Session.post",
return_value=self.responses["auth_fail_response.json"],
):
api = APIHelper("user@acme.com", "password")
assert not api.authenticate()
def test_list_plants_none_success(self) -> None:
"""Test list plants with empty plant list."""
with patch(
"requests.Session.get",
return_value=self.responses["list_plant_success_none_response.json"],
), patch("sunweg.api.APIHelper.plant", return_value=PLANT_MOCK):
api = APIHelper("user@acme.com", "password")
assert len(api.listPlants()) == 0
def test_list_plants_1_success(self) -> None:
"""Test list plants with one plant in the list."""
with patch(
"requests.Session.get",
return_value=self.responses["list_plant_success_1_response.json"],
), patch("sunweg.api.APIHelper.plant", return_value=PLANT_MOCK):
api = APIHelper("user@acme.com", "password")
assert len(api.listPlants()) == 1
def test_list_plants_2_success(self) -> None:
"""Test list plants with two plant in the list."""
with patch(
"requests.Session.get",
return_value=self.responses["list_plant_success_2_response.json"],
), patch("sunweg.api.APIHelper.plant", return_value=PLANT_MOCK):
api = APIHelper("user@acme.com", "password")
assert len(api.listPlants()) == 2
def test_list_plants_401(self) -> None:
"""Test list plants with expired token."""
with patch(
"requests.Session.post",
return_value=self.responses["auth_success_response.json"],
), patch(
"requests.Session.get",
return_value=self.responses["error_401_response.txt"],
):
api = APIHelper("user@acme.com", "password")
assert len(api.listPlants()) == 0
def test_plant_success(self) -> None:
"""Test plant success."""
with patch(
"requests.Session.get",
return_value=self.responses["plant_success_response.json"],
), patch("sunweg.api.APIHelper.inverter", return_value=INVERTER_MOCK):
api = APIHelper("user@acme.com", "password")
plant = api.plant(16925)
assert plant is not None
assert plant.id == 16925
assert plant.name == "Plant Name"
assert plant.total_power == 25.23
assert plant.last_update == datetime(2023, 2, 25, 8, 4, 22)
assert plant.kwh_per_kwp == 0.0
assert plant.performance_rate == 0.0
assert plant.saving == 12.78
assert plant.today_energy == 1.23
assert plant.today_energy_metric == "kWh"
assert plant.total_carbon_saving == 0.012296
assert plant.total_energy == 23.2
assert plant.__str__().startswith("<class 'sunweg.plant.Plant'>")
assert len(plant.inverters) == 1
for inverter in plant.inverters:
assert inverter.id == 21255
assert inverter.name == "Inverter Name"
assert inverter.frequency == 0
assert inverter.power == 0.0
assert inverter.power_metric == ""
assert inverter.power_factor == 0.0
assert inverter.sn == "1234ABC"
assert inverter.status == Status.ERROR
assert inverter.temperature == 80
assert inverter.today_energy == 0.0
assert inverter.today_energy_metric == ""
assert inverter.total_energy == 0.0
assert inverter.total_energy_metric == ""
assert not inverter.is_complete
def test_plant_success_alt(self) -> None:
"""Test plant success."""
with patch(
"requests.Session.get",
return_value=self.responses["plant_success_alt_response.json"],
), patch("sunweg.api.APIHelper.inverter", return_value=INVERTER_MOCK):
api = APIHelper("user@acme.com", "password")
plant = api.plant(16925)
assert plant is not None
assert plant.id == 16925
assert plant.name == "Plant Name"
assert plant.total_power == 25.23
assert plant.last_update is None
assert plant.kwh_per_kwp == 0.0
assert plant.performance_rate == 0.0
assert plant.saving == 12.78
assert plant.today_energy == 1.23
assert plant.today_energy_metric == "kWh"
assert plant.total_carbon_saving == 0.012296
assert plant.total_energy == 23.2
assert plant.__str__().startswith("<class 'sunweg.plant.Plant'>")
assert len(plant.inverters) == 1
for inverter in plant.inverters:
assert inverter.id == 21255
assert inverter.name == "Inverter Name"
assert inverter.frequency == 0
assert inverter.power == 0.0
assert inverter.power_metric == ""
assert inverter.power_factor == 0.0
assert inverter.sn == "1234ABC"
assert inverter.status == Status.ERROR
assert inverter.temperature == 80
assert inverter.today_energy == 0.0
assert inverter.today_energy_metric == ""
assert inverter.total_energy == 0.0
assert inverter.total_energy_metric == ""
assert not inverter.is_complete
def test_plant_401(self) -> None:
"""Test plant with expired token."""
with patch(
"requests.Session.post",
return_value=self.responses["auth_success_response.json"],
), patch(
"requests.Session.get",
return_value=self.responses["error_401_response.txt"],
):
api = APIHelper("user@acme.com", "password")
assert api.plant(16925) is None
def test_inverter_success(self) -> None:
"""Test inverter success."""
with patch(
"requests.Session.get",
return_value=self.responses["inverter_success_response.json"],
):
api = APIHelper("user@acme.com", "password")
inverter = api.inverter(21255)
assert inverter is not None
assert inverter.id == 21255
assert inverter.name == "Inverter Name"
assert inverter.frequency == 59.85
assert inverter.power == 0.0
assert inverter.power_metric == "kW"
assert inverter.power_factor == 0.0
assert inverter.sn == "1234ABC"
assert inverter.status == Status.OK
assert inverter.temperature == 80
assert inverter.today_energy == 0.0
assert inverter.today_energy_metric == "kWh"
assert inverter.total_energy == 23.2
assert inverter.today_energy_metric == "kWh"
strings: list[String] = []
for mppt in inverter.mppts:
assert mppt.__str__().startswith("<class 'sunweg.device.MPPT'>")
assert mppt.name != ""
strings.extend(mppt.strings)
assert len(strings) == 4
assert len(inverter.phases) == 3
assert inverter.__str__().startswith("<class 'sunweg.device.Inverter'>")
for string in strings:
assert string.name != ""
assert string.amperage != 0
assert string.voltage != 0
assert string.status == Status.OK
assert string.__str__().startswith("<class 'sunweg.device.String'>")
for phase in inverter.phases:
assert phase.name != ""
assert phase.amperage != 0
assert phase.voltage != 0
assert phase.status_amperage == Status.OK
assert phase.status_voltage == Status.ERROR
assert phase.__str__().startswith("<class 'sunweg.device.Phase'>")
def test_inverter_401(self) -> None:
"""Test inverter with expired token."""
with patch(
"requests.Session.post",
return_value=self.responses["auth_success_response.json"],
), patch(
"requests.Session.get",
return_value=self.responses["error_401_response.txt"],
):
api = APIHelper("user@acme.com", "password")
assert api.inverter(21255) is None
def test_complete_inverter_success(self) -> None:
"""Test complete inverter success."""
with patch(
"requests.Session.get",
return_value=self.responses["inverter_success_response.json"],
):
api = APIHelper("user@acme.com", "password")
inverter = Inverter(
id=12345,
name="Other inverter name",
sn="1234ABCD",
status=Status.ERROR,
temperature=70,
)
api.complete_inverter(inverter)
assert inverter is not None
assert inverter.id == 12345
assert inverter.name == "Other inverter name"
assert inverter.frequency == 59.85
assert inverter.power == 0.0
assert inverter.power_factor == 0.0
assert inverter.sn == "1234ABCD"
assert inverter.status == Status.ERROR
assert inverter.temperature == 70
assert inverter.today_energy == 0.0
assert inverter.total_energy == 23.2
strings: list[String] = []
for mppt in inverter.mppts:
assert mppt.__str__().startswith("<class 'sunweg.device.MPPT'>")
assert mppt.name != ""
strings.extend(mppt.strings)
assert len(strings) == 4
assert len(inverter.phases) == 3
assert inverter.__str__().startswith("<class 'sunweg.device.Inverter'>")
for string in strings:
assert string.name != ""
assert string.amperage != 0
assert string.voltage != 0
assert string.status == Status.OK
assert string.__str__().startswith("<class 'sunweg.device.String'>")
for phase in inverter.phases:
assert phase.name != ""
assert phase.amperage != 0
assert phase.voltage != 0
assert phase.status_amperage == Status.OK
assert phase.status_voltage == Status.ERROR
assert phase.__str__().startswith("<class 'sunweg.device.Phase'>")
def test_complete_inverter_401(self) -> None:
"""Test complete inverter with expired token."""
with patch(
"requests.Session.post",
return_value=self.responses["auth_success_response.json"],
), patch(
"requests.Session.get",
return_value=self.responses["error_401_response.txt"],
):
api = APIHelper("user@acme.com", "password")
inverter = Inverter(
id=12345,
name="Other inverter name",
sn="1234ABCD",
status=Status.ERROR,
temperature=70,
)
api.complete_inverter(inverter)
assert not inverter.is_complete
def test_setters(self) -> None:
"""Test API setters."""
api = APIHelper("user@acme.com", "password")
assert api._username == "user@acme.com"
assert api._password == "password"
api.username = "user1@acme.com"
api.password = "password1"
assert api._username == "user1@acme.com"
assert api._password == "password1"
def test_month_stats_fail(self) -> None:
"""Test month stats with error from server."""
with patch(
"requests.Session.get",
return_value=self.responses["month_stats_fail_response.json"],
):
api = APIHelper("user@acme.com", "password")
plant = MagicMock()
plant.id = 1
with pytest.raises(SunWegApiError) as e_info:
api.month_stats_production(2013, 12, plant)
assert e_info.value.__str__() == "Error message"
def test_month_stats_401(self) -> None:
"""Test month stats with data from server with expired token."""
with patch(
"requests.Session.post",
return_value=self.responses["auth_success_response.json"],
), patch(
"requests.Session.get",
return_value=self.responses["error_401_response.txt"],
):
api = APIHelper("user@acme.com", "password")
plant = MagicMock()
plant.id = 1
stats = api.month_stats_production(2023, 12, plant)
assert isinstance(stats, list)
assert len(stats) == 0
def test_month_stats_success(self) -> None:
"""Test month stats with data from server."""
with patch(
"requests.Session.get",
return_value=self.responses["month_stats_success_response.json"],
):
api = APIHelper("user@acme.com", "password")
plant = MagicMock()
plant.id = 1
stats = api.month_stats_production(2023, 12, plant)
assert len(stats) > 0
i: int = 1
for stat in stats:
assert stat.date == date(2024, 5, i)
assert isinstance(stat.production, float)
assert stat.prognostic == 111.03225806451613
assert stat.__str__().startswith(
"<class 'sunweg.util.ProductionStats'>"
)
i += 1
|