from fastapi.testclient import TestClient
from weather_api import summarize
from main import app

rows = [{'high_c': str(high)}
        for high in [-3, 0, 4.9, 5, 12, 20, 24.9, 25, 31, '', 'nan']]
assert summarize(rows, -60)['counts'] == [1, 2, 1, 1, 0, 2, 1, 1]
summary = summarize(rows, 20)
assert summary['counts'] == [0, 0, 0, 0, 0, 2, 1, 1]
assert summary['selected'] == 4
assert summary['days'] == 11
assert summary['missing_highs'] == 2
assert summarize([], 20)['selected'] == 0

client = TestClient(app)
response = client.get('/weather?minimum=20')
assert response.status_code == 200, response.text
result = response.json()
assert sum(result['counts']) == result['selected']
assert result['source']['source_url'].startswith('https://archive-api.open-meteo.com/')
assert client.get('/weather?minimum=elephant').status_code == 422
assert client.get('/weather?minimum=61').status_code == 422
assert client.get('/').status_code == 200
print('Weather checks passed.')
