-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_endpoints.py
More file actions
237 lines (193 loc) · 11 KB
/
test_endpoints.py
File metadata and controls
237 lines (193 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
"""
End-to-end integration tests for the backend API endpoints.
Requires the full Docker Compose stack to be running:
docker compose up -d --build
Run with:
pytest test_endpoints.py -v
Test order matters — later tests depend on state created by earlier ones
(e.g. a job must be submitted before its status can be polled). The
pytest-ordering plugin or explicit state sharing via module-level variables
is used to enforce this.
Endpoint coverage:
1. GET /apis/stations → test_get_stations
2. POST /apis/run → test_submit_job
3. POST /apis/run (validation errors) → test_submit_job_*
4. GET /apis/status?job_id=<id> → test_poll_job_status
5. GET /apis/jobs/<job_id>/frames/<index> → test_fetch_frames
"""
import time
import pytest
import requests
from datetime import datetime, timedelta, timezone
BASE_URL = "http://localhost:8001"
POLL_INTERVAL_SECONDS = 5
MAX_POLLS = 120
# ── shared state across ordered tests ────────────────────────────────────────
_state = {
"station_id": None,
"job_id": None,
"num_frames": 0,
}
def _fmt_utc(dt: datetime) -> str:
"""Format a datetime as the ISO 8601 string the API expects."""
return dt.strftime("%Y-%m-%dT%H:%M:%SZ")
# ─────────────────────────────────────────────────────────────────────────────
# 1. GET /apis/stations
# ─────────────────────────────────────────────────────────────────────────────
class TestStationsEndpoint:
"""Tests for GET /apis/stations."""
def test_get_stations_returns_200(self):
resp = requests.get(f"{BASE_URL}/apis/stations")
assert resp.status_code == 200
def test_get_stations_returns_geojson(self):
resp = requests.get(f"{BASE_URL}/apis/stations")
body = resp.json()
assert "features" in body, "response should be a GeoJSON FeatureCollection"
assert isinstance(body["features"], list)
assert len(body["features"]) > 0, "station list should not be empty"
def test_station_feature_has_expected_properties(self):
resp = requests.get(f"{BASE_URL}/apis/stations")
feature = resp.json()["features"][0]
assert "properties" in feature
assert "station_id" in feature["properties"]
assert "geometry" in feature
# stash a station for use in later tests
_state["station_id"] = feature["properties"]["station_id"]
# ─────────────────────────────────────────────────────────────────────────────
# 2. POST /apis/run — validation error cases
# ─────────────────────────────────────────────────────────────────────────────
class TestRunEndpointValidation:
"""Tests for POST /apis/run input validation (no job should be created)."""
def test_missing_station_id_returns_400(self):
now = datetime.now(timezone.utc)
payload = {
"startUtc": _fmt_utc(now - timedelta(minutes=45)),
"endUtc": _fmt_utc(now - timedelta(minutes=25)),
}
resp = requests.post(f"{BASE_URL}/apis/run", json=payload)
assert resp.status_code == 400
assert "stationId" in resp.json().get("error", "").lower() or "stationid" in resp.json().get("error", "").lower()
def test_invalid_station_id_returns_400(self):
now = datetime.now(timezone.utc)
payload = {
"stationId": "ZZZZ_NOT_REAL",
"startUtc": _fmt_utc(now - timedelta(minutes=45)),
"endUtc": _fmt_utc(now - timedelta(minutes=25)),
}
resp = requests.post(f"{BASE_URL}/apis/run", json=payload)
assert resp.status_code == 400
def test_end_before_start_returns_400(self):
now = datetime.now(timezone.utc)
payload = {
"stationId": _state["station_id"] or "KABX",
"startUtc": _fmt_utc(now - timedelta(minutes=25)),
"endUtc": _fmt_utc(now - timedelta(minutes=45)),
}
resp = requests.post(f"{BASE_URL}/apis/run", json=payload)
assert resp.status_code == 400
def test_future_end_time_returns_400(self):
now = datetime.now(timezone.utc)
payload = {
"stationId": _state["station_id"] or "KABX",
"startUtc": _fmt_utc(now - timedelta(minutes=30)),
"endUtc": _fmt_utc(now + timedelta(hours=1)),
}
resp = requests.post(f"{BASE_URL}/apis/run", json=payload)
assert resp.status_code == 400
def test_end_time_too_recent_returns_400(self):
"""endUtc within 5 minutes of now triggers live-polling mode in the algorithm — must be rejected."""
now = datetime.now(timezone.utc)
payload = {
"stationId": _state["station_id"] or "KABX",
"startUtc": _fmt_utc(now - timedelta(minutes=30)),
"endUtc": _fmt_utc(now - timedelta(minutes=2)),
}
resp = requests.post(f"{BASE_URL}/apis/run", json=payload)
assert resp.status_code == 400
def test_duration_too_short_returns_400(self):
now = datetime.now(timezone.utc)
payload = {
"stationId": _state["station_id"] or "KABX",
"startUtc": _fmt_utc(now - timedelta(minutes=10)),
"endUtc": _fmt_utc(now - timedelta(minutes=8)),
}
resp = requests.post(f"{BASE_URL}/apis/run", json=payload)
assert resp.status_code == 400
# ─────────────────────────────────────────────────────────────────────────────
# 3. POST /apis/run — happy path (submit a real job)
# ─────────────────────────────────────────────────────────────────────────────
class TestRunEndpointHappyPath:
"""Submit a valid job and store the job_id for downstream tests."""
def test_submit_job_returns_202(self):
assert _state["station_id"], "station_id must be populated by TestStationsEndpoint"
now = datetime.now(timezone.utc)
payload = {
"stationId": _state["station_id"],
"startUtc": _fmt_utc(now - timedelta(minutes=45)),
"endUtc": _fmt_utc(now - timedelta(minutes=25)),
}
resp = requests.post(f"{BASE_URL}/apis/run", json=payload)
assert resp.status_code == 202
body = resp.json()
assert "job_id" in body
_state["job_id"] = body["job_id"]
# ─────────────────────────────────────────────────────────────────────────────
# 4. GET /apis/status
# ─────────────────────────────────────────────────────────────────────────────
class TestStatusEndpoint:
"""Tests for GET /apis/status."""
def test_missing_job_id_returns_400(self):
resp = requests.get(f"{BASE_URL}/apis/status")
assert resp.status_code == 400
def test_unknown_job_id_returns_404(self):
resp = requests.get(f"{BASE_URL}/apis/status", params={"job_id": "nonexistent-id"})
assert resp.status_code == 404
def test_valid_job_returns_status(self):
assert _state["job_id"], "job_id must be populated by TestRunEndpointHappyPath"
resp = requests.get(f"{BASE_URL}/apis/status", params={"job_id": _state["job_id"]})
assert resp.status_code == 200
body = resp.json()
assert "job_id" in body
assert "status" in body
assert body["status"] in {"PENDING", "PROCESSING", "COMPLETED", "FAILED"}
@pytest.mark.slow
def test_poll_until_terminal(self):
"""Poll the job until it reaches COMPLETED or FAILED (may take minutes)."""
assert _state["job_id"], "job_id must be populated by TestRunEndpointHappyPath"
terminal = {"COMPLETED", "FAILED"}
for _ in range(MAX_POLLS):
time.sleep(POLL_INTERVAL_SECONDS)
resp = requests.get(f"{BASE_URL}/apis/status", params={"job_id": _state["job_id"]})
body = resp.json()
if body.get("status") in terminal:
_state["num_frames"] = int(body.get("num_frames", 0))
return # success — reached terminal state
pytest.fail(f"Job {_state['job_id']} did not reach a terminal state after {MAX_POLLS} polls")
# ─────────────────────────────────────────────────────────────────────────────
# 5. GET /apis/jobs/<job_id>/frames/<index>
# ─────────────────────────────────────────────────────────────────────────────
class TestFramesEndpoint:
"""Tests for GET /apis/jobs/<job_id>/frames/<index>."""
def test_nonexistent_job_returns_404(self):
resp = requests.get(f"{BASE_URL}/apis/jobs/fake-job-id/frames/0")
assert resp.status_code == 404
@pytest.mark.slow
def test_fetch_all_frames(self):
"""After job completion, every advertised frame should be retrievable."""
if _state["num_frames"] == 0:
pytest.skip("No frames produced (job may have failed or not run yet)")
job_id = _state["job_id"]
for i in range(_state["num_frames"]):
resp = requests.get(f"{BASE_URL}/apis/jobs/{job_id}/frames/{i}")
assert resp.status_code == 200, f"frame {i} returned {resp.status_code}"
assert len(resp.content) > 0, f"frame {i} was empty"
assert resp.headers.get("Content-Type") == "image/tiff"
@pytest.mark.slow
def test_out_of_range_frame_returns_404(self):
"""Requesting a frame index beyond what the job produced should 404."""
if _state["num_frames"] == 0:
pytest.skip("No frames produced (job may have failed or not run yet)")
resp = requests.get(
f"{BASE_URL}/apis/jobs/{_state['job_id']}/frames/{_state['num_frames'] + 100}"
)
assert resp.status_code == 404