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

rows = [{'type': 'earthquake', 'mag': str(mag)}
        for mag in [-0.5, 0, 1, 2, 2.5, 3, 4, 5, 6, '', 'nan']]
rows.append({'type': 'quarry blast', 'mag': '3'})
assert summarize(rows, -2)['counts'] == [1, 1, 1, 2, 1, 1, 2]
summary = summarize(rows, 2.5)
assert summary['counts'] == [0, 0, 0, 1, 1, 1, 2]
assert summary['selected'] == 5
assert summary['earthquakes'] == 11
assert summary['missing_magnitudes'] == 2
assert summarize([], 2.5)['selected'] == 0

client = TestClient(app)
response = client.get('/earthquakes?minimum=2.5')
assert response.status_code == 200, response.text
result = response.json()
assert sum(result['counts']) == result['selected']
assert result['source']['source_url'].startswith('https://earthquake.usgs.gov/')
assert client.get('/earthquakes?minimum=elephant').status_code == 422
assert client.get('/earthquakes?minimum=11').status_code == 422
assert client.get('/').status_code == 200
print('Earthquake checks passed.')
