|
1 | 1 | """Tests for ClimateVision API endpoints.""" |
2 | 2 |
|
| 3 | +from unittest.mock import patch |
| 4 | + |
3 | 5 | import pytest |
4 | 6 | from fastapi.testclient import TestClient |
5 | 7 |
|
@@ -47,3 +49,72 @@ def test_predict_json_accepts_dev_key( |
47 | 49 | ) |
48 | 50 | # Should pass auth; inference may fail due to missing models/GEE |
49 | 51 | assert response.status_code in (200, 500) |
| 52 | + |
| 53 | + |
| 54 | +def test_predict_valid_date_range_reaches_inference( |
| 55 | + client: TestClient, monkeypatch: pytest.MonkeyPatch |
| 56 | +) -> None: |
| 57 | + """POST /api/predict with valid date range should reach the inference layer.""" |
| 58 | + monkeypatch.setenv("CLIMATEVISION_ALLOW_DEV_KEY", "1") |
| 59 | + payload = { |
| 60 | + "bbox": [-60.0, -15.0, -45.0, -5.0], |
| 61 | + "start_date": "2023-01-01", |
| 62 | + "end_date": "2023-06-30", |
| 63 | + "analysis_type": "deforestation", |
| 64 | + } |
| 65 | + fake_result = { |
| 66 | + "region": {"bbox": payload["bbox"]}, |
| 67 | + "inference": {"forest_percentage": 72.3}, |
| 68 | + "analysis_type": "deforestation", |
| 69 | + } |
| 70 | + with patch( |
| 71 | + "climatevision.api.main.run_inference_from_gee", return_value=fake_result |
| 72 | + ) as mock_infer: |
| 73 | + response = client.post( |
| 74 | + "/api/predict", |
| 75 | + json=payload, |
| 76 | + headers={"X-API-Key": "cv_dev"}, |
| 77 | + ) |
| 78 | + assert response.status_code == 200 |
| 79 | + mock_infer.assert_called_once() |
| 80 | + |
| 81 | + |
| 82 | +def test_predict_reversed_date_range_returns_422( |
| 83 | + client: TestClient, monkeypatch: pytest.MonkeyPatch |
| 84 | +) -> None: |
| 85 | + """POST /api/predict with start_date > end_date should return 422.""" |
| 86 | + monkeypatch.setenv("CLIMATEVISION_ALLOW_DEV_KEY", "1") |
| 87 | + payload = { |
| 88 | + "bbox": [-60.0, -15.0, -45.0, -5.0], |
| 89 | + "start_date": "2026-06-01", |
| 90 | + "end_date": "2026-01-01", |
| 91 | + "analysis_type": "deforestation", |
| 92 | + } |
| 93 | + response = client.post( |
| 94 | + "/api/predict", |
| 95 | + json=payload, |
| 96 | + headers={"X-API-Key": "cv_dev"}, |
| 97 | + ) |
| 98 | + assert response.status_code == 422 |
| 99 | + body = response.json() |
| 100 | + error_messages = [e["msg"] for e in body["detail"]] |
| 101 | + assert any("start_date" in msg or "end_date" in msg for msg in error_messages) |
| 102 | + |
| 103 | + |
| 104 | +def test_predict_equal_dates_returns_422( |
| 105 | + client: TestClient, monkeypatch: pytest.MonkeyPatch |
| 106 | +) -> None: |
| 107 | + """POST /api/predict with start_date == end_date should return 422.""" |
| 108 | + monkeypatch.setenv("CLIMATEVISION_ALLOW_DEV_KEY", "1") |
| 109 | + payload = { |
| 110 | + "bbox": [-60.0, -15.0, -45.0, -5.0], |
| 111 | + "start_date": "2023-06-01", |
| 112 | + "end_date": "2023-06-01", |
| 113 | + "analysis_type": "deforestation", |
| 114 | + } |
| 115 | + response = client.post( |
| 116 | + "/api/predict", |
| 117 | + json=payload, |
| 118 | + headers={"X-API-Key": "cv_dev"}, |
| 119 | + ) |
| 120 | + assert response.status_code == 422 |
0 commit comments