|
| 1 | +"""Tests for get_horizon_mines.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from unittest.mock import MagicMock, patch |
| 5 | + |
| 6 | +import pandas as pd |
| 7 | +import pytest |
| 8 | +import requests |
| 9 | + |
| 10 | +import solarpy |
| 11 | + |
| 12 | + |
| 13 | +def _make_response(elevations: list[float]) -> MagicMock: |
| 14 | + """Build a mock requests.Response with a CSV body matching the API format.""" |
| 15 | + # 27 header rows (content doesn't matter, just need the right count) |
| 16 | + header = "\n".join(f"# header line {i}" for i in range(27)) |
| 17 | + rows = "\n".join(f"{az};{el}" for az, el in enumerate(elevations)) |
| 18 | + mock_res = MagicMock() |
| 19 | + mock_res.text = header + "\n" + rows |
| 20 | + mock_res.raise_for_status = MagicMock() |
| 21 | + return mock_res |
| 22 | + |
| 23 | + |
| 24 | +_N = 360 |
| 25 | +_ELEVATIONS = [float(i % 10) for i in range(_N)] # deterministic test data |
| 26 | + |
| 27 | + |
| 28 | +# Tests |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture |
| 32 | +def mock_get(): |
| 33 | + with patch("solarpy.horizon.horizon_mines.requests.get") as mock: |
| 34 | + mock.return_value = _make_response(_ELEVATIONS) |
| 35 | + yield mock |
| 36 | + |
| 37 | + |
| 38 | +def test_returns_series_and_dict(mock_get): |
| 39 | + horizon, meta = solarpy.horizon.get_horizon_mines(48.8566, 2.3522) |
| 40 | + assert isinstance(horizon, pd.Series) |
| 41 | + assert isinstance(meta, dict) |
| 42 | + |
| 43 | + |
| 44 | +def test_horizon_length(mock_get): |
| 45 | + horizon, _ = solarpy.horizon.get_horizon_mines(48.8566, 2.3522) |
| 46 | + assert len(horizon) == _N |
| 47 | + |
| 48 | + |
| 49 | +def test_horizon_values(mock_get): |
| 50 | + horizon, _ = solarpy.horizon.get_horizon_mines(48.8566, 2.3522) |
| 51 | + assert list(horizon) == _ELEVATIONS |
| 52 | + |
| 53 | + |
| 54 | +def test_meta_keys(mock_get): |
| 55 | + _, meta = solarpy.horizon.get_horizon_mines(48.8566, 2.3522) |
| 56 | + assert set(meta.keys()) == { |
| 57 | + "data_provider", "database", |
| 58 | + "latitude", "longitude", "altitude", "ground_offset", |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | +def test_meta_coordinates(mock_get): |
| 63 | + _, meta = solarpy.horizon.get_horizon_mines(48.8566, 2.3522) |
| 64 | + assert meta["latitude"] == 48.8566 |
| 65 | + assert meta["longitude"] == 2.3522 |
| 66 | + |
| 67 | + |
| 68 | +def test_altitude_none_uses_sentinel(mock_get): |
| 69 | + _, meta = solarpy.horizon.get_horizon_mines(48.8566, 2.3522, altitude=None) |
| 70 | + call_url = mock_get.call_args[0][0] |
| 71 | + assert "altitude=-999" in call_url |
| 72 | + |
| 73 | + |
| 74 | +def test_altitude_explicit(mock_get): |
| 75 | + _, meta = solarpy.horizon.get_horizon_mines(48.8566, 2.3522, altitude=100) |
| 76 | + call_url = mock_get.call_args[0][0] |
| 77 | + assert "altitude=100" in call_url |
| 78 | + assert meta["altitude"] == 100 |
| 79 | + |
| 80 | + |
| 81 | +def test_ground_offset_in_url(mock_get): |
| 82 | + solarpy.horizon.get_horizon_mines(48.8566, 2.3522, ground_offset=2.5) |
| 83 | + call_url = mock_get.call_args[0][0] |
| 84 | + assert "ground_offset=2.5" in call_url |
| 85 | + |
| 86 | + |
| 87 | +def test_raise_for_status_called(mock_get): |
| 88 | + solarpy.horizon.get_horizon_mines(48.8566, 2.3522) |
| 89 | + mock_get.return_value.raise_for_status.assert_called_once() |
| 90 | + |
| 91 | + |
| 92 | +def test_http_error_propagates(): |
| 93 | + mock_res = MagicMock() |
| 94 | + mock_res.raise_for_status.side_effect = requests.HTTPError("500 Server Error") |
| 95 | + with patch("solarpy.horizon.horizon_mines.requests.get", return_value=mock_res): |
| 96 | + with pytest.raises(requests.HTTPError): |
| 97 | + solarpy.horizon.get_horizon_mines(48.8566, 2.3522) |
| 98 | + |
| 99 | + |
| 100 | +def test_kwargs_forwarded(mock_get): |
| 101 | + solarpy.horizon.get_horizon_mines(48.8566, 2.3522, timeout=10) |
| 102 | + _, kwargs = mock_get.call_args |
| 103 | + assert kwargs.get("timeout") == 10 |
0 commit comments