Weather-Crop-Damage/tests/test_threea.py
2026-10-27 14:13:32 +01:00

39 lines
1.6 KiB
Python

from datetime import datetime
from damage_engine.sources import threea
class FakeClient:
def __init__(self, answer):
self.answer, self.calls = answer, []
def call(self, method, path, body=None):
self.calls.append((method, path, body))
return self.answer
def test_fetch_daily_asks_for_data_column_not_timestamp(monkeypatch):
answer = [
{"CodiceStazione": 3, "Data": "2026-10-20 00:00:00.000", "Pioggia": "4.2", "EvapoPot": 1.1},
{"CodiceStazione": 3, "Data": "2026-10-19 00:00:00.000", "Pioggia": 0, "EvapoPot": 1.3},
{"CodiceStazione": 3, "Data": "2026-10-18 00:00:00.000", "Pioggia": None, "EvapoPot": None},
]
fake = FakeClient(answer)
monkeypatch.setattr(threea, "client", lambda: fake)
df = threea.fetch_daily([3], datetime(2026, 10, 18), datetime(2026, 10, 20), ["Pioggia", "EvapoPot"])
method, path, body = fake.calls[0]
assert path == "/getWeatherDaily"
assert body["columns"] == ["CodiceStazione", "Data", "Pioggia", "EvapoPot"]
assert "Timestamp" not in body["columns"]
assert list(df["Data"].dt.day) == [19, 20] # empty row dropped, sorted by date
assert df["Pioggia"].tolist() == [0.0, 4.2]
def test_fetch_hourly_still_uses_timestamp(monkeypatch):
fake = FakeClient([{"CodiceStazione": 3, "Timestamp": "2026-10-20 06:00:00", "Pioggia": 1}])
monkeypatch.setattr(threea, "client", lambda: fake)
df = threea.fetch_hourly([3], datetime(2026, 10, 20), datetime(2026, 10, 20, 6), ["Pioggia"])
assert fake.calls[0][1] == "/getWeatherHourly"
assert fake.calls[0][2]["columns"][:2] == ["CodiceStazione", "Timestamp"]
assert len(df) == 1