57 lines
1.6 KiB
Python
57 lines
1.6 KiB
Python
"""Tests for the App API parser."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from copy import deepcopy
|
|
|
|
import pytest
|
|
|
|
from custom_components.solaredge_optimizers.api import (
|
|
SolarEdgeOptimizerInvalidResponseError,
|
|
normalize_base_url,
|
|
parse_snapshot,
|
|
)
|
|
|
|
from .sample_data import SAMPLE_PAYLOAD
|
|
|
|
|
|
def test_parse_snapshot() -> None:
|
|
"""Parse a valid snapshot."""
|
|
snapshot = parse_snapshot(SAMPLE_PAYLOAD)
|
|
|
|
assert snapshot.site_id == "4886699"
|
|
assert snapshot.optimizer_count == 2
|
|
assert snapshot.optimizers[0].optimizer_id == "1.1.1"
|
|
assert snapshot.optimizers[0].daily_energy_wh == 1234.5
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("value", "expected"),
|
|
[
|
|
("http://app:8099/", "http://app:8099"),
|
|
("http://app:8099/api/optimizers", "http://app:8099"),
|
|
("https://example.test/prefix/", "https://example.test/prefix"),
|
|
],
|
|
)
|
|
def test_normalize_base_url(value: str, expected: str) -> None:
|
|
"""Normalize supported URLs."""
|
|
assert normalize_base_url(value) == expected
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"value", ["app:8099", "ftp://app/data", "http://user:password@app:8099"]
|
|
)
|
|
def test_reject_invalid_url(value: str) -> None:
|
|
"""Reject invalid or credential-bearing URLs."""
|
|
with pytest.raises(ValueError):
|
|
normalize_base_url(value)
|
|
|
|
|
|
def test_reject_duplicate_serial() -> None:
|
|
"""Reject ambiguous optimizer records."""
|
|
payload = deepcopy(SAMPLE_PAYLOAD)
|
|
payload["optimizers"][1]["serial"] = payload["optimizers"][0]["serial"]
|
|
|
|
with pytest.raises(SolarEdgeOptimizerInvalidResponseError):
|
|
parse_snapshot(payload)
|