mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-06-08 11:17:27 +00:00
60 lines
2 KiB
Python
60 lines
2 KiB
Python
from typing import Any
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
import requests
|
|
|
|
from infrastructure.solar.google_solar_api_client import (
|
|
BuildingInsightsNotFoundError,
|
|
GoogleSolarApiClient,
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Slice 1: Successful response returns parsed JSON
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_get_building_insights_returns_parsed_json() -> None:
|
|
# Arrange
|
|
client = GoogleSolarApiClient(api_key="test-key")
|
|
payload: dict[str, Any] = {"solarPotential": {"maxArrayPanelsCount": 20}}
|
|
mock_response = MagicMock()
|
|
mock_response.json.return_value = payload
|
|
|
|
with patch("requests.get", return_value=mock_response) as mock_get:
|
|
mock_response.raise_for_status.return_value = None
|
|
|
|
# Act
|
|
result = client.get_building_insights(longitude=-0.1278, latitude=51.5074)
|
|
|
|
# Assert
|
|
assert result == payload
|
|
mock_get.assert_called_once()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Slice 2: Transient HTTP errors trigger retries
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_get_building_insights_retries_on_transient_error() -> None:
|
|
# Arrange
|
|
client = GoogleSolarApiClient(api_key="test-key")
|
|
payload: dict[str, Any] = {"solarPotential": {"maxArrayPanelsCount": 20}}
|
|
|
|
transient_error = requests.exceptions.ConnectionError("timeout")
|
|
transient_error.response = None # type: ignore[attr-defined]
|
|
|
|
success_response = MagicMock()
|
|
success_response.json.return_value = payload
|
|
success_response.raise_for_status.return_value = None
|
|
|
|
with patch("requests.get", side_effect=[transient_error, success_response]) as mock_get:
|
|
with patch("time.sleep"):
|
|
# Act
|
|
result = client.get_building_insights(longitude=-0.1278, latitude=51.5074)
|
|
|
|
# Assert
|
|
assert result == payload
|
|
assert mock_get.call_count == 2
|