Skip to content

Commit 0dd256c

Browse files
style: auto-format code with ruff
1 parent 5088a0a commit 0dd256c

6 files changed

Lines changed: 120 additions & 93 deletions

File tree

tests/unit/api/middleware/test_audit_log_middleware.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# helpers
1818
# ---------------------------------------------------------------------------
1919

20+
2021
def _make_app_with_middleware():
2122
"""Return a minimal FastAPI app with AuditLogMiddleware attached."""
2223
app = FastAPI()
@@ -57,9 +58,9 @@ async def create_request():
5758
# dispatch matrix tests
5859
# ---------------------------------------------------------------------------
5960

61+
6062
@pytest.mark.unit
6163
class TestAuditLogMiddlewareDispatch:
62-
6364
def test_get_non_audit_prefix_not_audited(self, caplog):
6465
"""A plain GET not matching AUDIT_ALWAYS_PREFIXES is skipped."""
6566
app = _make_app_with_middleware()
@@ -133,9 +134,9 @@ def test_health_path_not_audited(self, caplog):
133134
# audit log fields
134135
# ---------------------------------------------------------------------------
135136

137+
136138
@pytest.mark.unit
137139
class TestAuditLogFields:
138-
139140
def test_latency_ms_present_in_log(self, caplog):
140141
app = _make_app_with_middleware()
141142
client = TestClient(app)
@@ -198,9 +199,9 @@ def test_method_and_path_recorded(self, caplog):
198199
# latency monotonic semantics
199200
# ---------------------------------------------------------------------------
200201

202+
201203
@pytest.mark.unit
202204
class TestAuditLogLatencySemantics:
203-
204205
def test_latency_uses_monotonic_clock(self, caplog):
205206
"""Latency must be measured with time.monotonic, not wall clock."""
206207
app = _make_app_with_middleware()

tests/unit/api/middleware/test_read_only_middleware.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# helpers
1414
# ---------------------------------------------------------------------------
1515

16+
1617
def _make_app(enabled: bool):
1718
"""Return a FastAPI app with ReadOnlyMiddleware configured."""
1819
app = FastAPI()
@@ -49,9 +50,9 @@ async def health_post():
4950
# Enabled + mutating → 403
5051
# ---------------------------------------------------------------------------
5152

53+
5254
@pytest.mark.unit
5355
class TestReadOnlyMiddlewareEnabled:
54-
5556
def test_post_is_blocked_when_enabled(self):
5657
client = TestClient(_make_app(enabled=True), raise_server_exceptions=True)
5758
resp = client.post("/api/v1/machines")
@@ -74,9 +75,9 @@ def test_post_to_request_blocked(self):
7475
# Enabled + safe verb → passes
7576
# ---------------------------------------------------------------------------
7677

78+
7779
@pytest.mark.unit
7880
class TestReadOnlyMiddlewareSafeVerbs:
79-
8081
def test_get_passes_when_enabled(self):
8182
client = TestClient(_make_app(enabled=True))
8283
resp = client.get("/api/v1/machines")
@@ -92,9 +93,9 @@ def test_get_returns_normal_body(self):
9293
# Enabled + allowed path → passes
9394
# ---------------------------------------------------------------------------
9495

96+
9597
@pytest.mark.unit
9698
class TestReadOnlyMiddlewareAllowedPaths:
97-
9899
def test_post_health_passes_when_enabled(self):
99100
client = TestClient(_make_app(enabled=True))
100101
resp = client.post("/health")
@@ -117,9 +118,9 @@ def test_post_reflex_upload_prefix_passes(self):
117118
# Disabled + mutating → passes
118119
# ---------------------------------------------------------------------------
119120

121+
120122
@pytest.mark.unit
121123
class TestReadOnlyMiddlewareDisabled:
122-
123124
def test_post_passes_when_disabled(self):
124125
client = TestClient(_make_app(enabled=False))
125126
resp = client.post("/api/v1/machines")
@@ -135,9 +136,9 @@ def test_get_passes_when_disabled(self):
135136
# Allowed path constants
136137
# ---------------------------------------------------------------------------
137138

139+
138140
@pytest.mark.unit
139141
class TestAllowedPathConstants:
140-
141142
def test_event_path_in_allowed_paths(self):
142143
assert "/_event" in _ALLOWED_PATHS
143144

tests/unit/application/services/orchestration/test_dashboard_summary.py

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
# helpers
2323
# ---------------------------------------------------------------------------
2424

25+
2526
def _make_uow(
2627
*,
2728
machine_by_status=None,
@@ -65,8 +66,9 @@ def _make_list_requests_orchestrator(requests=None):
6566
return orch
6667

6768

68-
def _make_orchestrator(*, machine_by_status=None, request_by_status=None,
69-
provider_api_counts=None, requests=None):
69+
def _make_orchestrator(
70+
*, machine_by_status=None, request_by_status=None, provider_api_counts=None, requests=None
71+
):
7072
uow = _make_uow(
7173
machine_by_status=machine_by_status,
7274
request_by_status=request_by_status,
@@ -86,6 +88,7 @@ def _make_orchestrator(*, machine_by_status=None, request_by_status=None,
8688
# _to_iso helper
8789
# ---------------------------------------------------------------------------
8890

91+
8992
@pytest.mark.unit
9093
class TestToIso:
9194
def test_none_returns_none(self):
@@ -116,10 +119,10 @@ def test_other_type_coerced_to_str(self):
116119
# DashboardSummaryOrchestrator.execute
117120
# ---------------------------------------------------------------------------
118121

122+
119123
@pytest.mark.unit
120124
@pytest.mark.application
121125
class TestDashboardSummaryOrchestrator:
122-
123126
@pytest.mark.asyncio
124127
async def test_happy_path_aggregates_all_sections(self):
125128
"""All three sections must be populated from the UoW repos."""
@@ -169,8 +172,14 @@ async def test_well_known_keys_present_even_when_count_is_zero(self):
169172
for key in ("running", "pending", "stopped", "terminated", "shutting-down"):
170173
assert key in result.machines["by_status"]
171174
for key in (
172-
"pending", "in_progress", "acquiring", "complete",
173-
"failed", "partial", "cancelled", "timeout",
175+
"pending",
176+
"in_progress",
177+
"acquiring",
178+
"complete",
179+
"failed",
180+
"partial",
181+
"cancelled",
182+
"timeout",
174183
):
175184
assert key in result.requests["by_status"]
176185
for key in ("aws", "EC2Fleet", "SpotFleet", "RunInstances", "ASG"):
@@ -211,10 +220,24 @@ async def test_recent_activity_capped_at_10(self):
211220
async def test_recent_activity_sorted_by_created_at_desc(self):
212221
"""Most recently created requests appear first."""
213222
requests = [
214-
{"request_id": "old", "created_at": "2024-01-01T00:00:00+00:00", "status": "complete",
215-
"request_type": "acquire", "template_id": "", "successful_count": 0, "requested_count": 0},
216-
{"request_id": "new", "created_at": "2024-06-01T00:00:00+00:00", "status": "complete",
217-
"request_type": "acquire", "template_id": "", "successful_count": 0, "requested_count": 0},
223+
{
224+
"request_id": "old",
225+
"created_at": "2024-01-01T00:00:00+00:00",
226+
"status": "complete",
227+
"request_type": "acquire",
228+
"template_id": "",
229+
"successful_count": 0,
230+
"requested_count": 0,
231+
},
232+
{
233+
"request_id": "new",
234+
"created_at": "2024-06-01T00:00:00+00:00",
235+
"status": "complete",
236+
"request_type": "acquire",
237+
"template_id": "",
238+
"successful_count": 0,
239+
"requested_count": 0,
240+
},
218241
]
219242
orchestrator = _make_orchestrator(requests=requests)
220243
result = await orchestrator.execute(DashboardSummaryInput())
@@ -225,12 +248,23 @@ async def test_recent_activity_sorted_by_created_at_desc(self):
225248
async def test_recent_activity_missing_created_at_lands_at_end(self):
226249
"""Rows without created_at must sort to the end (after dated rows)."""
227250
requests = [
228-
{"request_id": "has-date", "created_at": "2024-03-01T00:00:00+00:00",
229-
"status": "complete", "request_type": "acquire", "template_id": "",
230-
"successful_count": 0, "requested_count": 0},
231-
{"request_id": "no-date", "status": "pending",
232-
"request_type": "acquire", "template_id": "",
233-
"successful_count": 0, "requested_count": 0},
251+
{
252+
"request_id": "has-date",
253+
"created_at": "2024-03-01T00:00:00+00:00",
254+
"status": "complete",
255+
"request_type": "acquire",
256+
"template_id": "",
257+
"successful_count": 0,
258+
"requested_count": 0,
259+
},
260+
{
261+
"request_id": "no-date",
262+
"status": "pending",
263+
"request_type": "acquire",
264+
"template_id": "",
265+
"successful_count": 0,
266+
"requested_count": 0,
267+
},
234268
]
235269
orchestrator = _make_orchestrator(requests=requests)
236270
result = await orchestrator.execute(DashboardSummaryInput())
@@ -303,9 +337,7 @@ async def test_count_by_status_called_on_machines_and_requests_repos(self):
303337
@pytest.mark.asyncio
304338
async def test_count_by_provider_api_returns_real_values(self):
305339
"""count_by_provider_api values flow into templates section."""
306-
orchestrator = _make_orchestrator(
307-
provider_api_counts={"EC2Fleet": 7, "RunInstances": 3}
308-
)
340+
orchestrator = _make_orchestrator(provider_api_counts={"EC2Fleet": 7, "RunInstances": 3})
309341
result = await orchestrator.execute(DashboardSummaryInput())
310342
assert result.templates["by_provider_api"]["EC2Fleet"] == 7
311343
assert result.templates["by_provider_api"]["RunInstances"] == 3
@@ -315,8 +347,14 @@ async def test_count_by_provider_api_returns_real_values(self):
315347
async def test_request_id_fallback_to_id_field(self):
316348
"""If 'request_id' absent, 'id' is used instead."""
317349
requests = [
318-
{"id": "fallback-id", "status": "pending", "request_type": "acquire",
319-
"template_id": "", "successful_count": 0, "requested_count": 0}
350+
{
351+
"id": "fallback-id",
352+
"status": "pending",
353+
"request_type": "acquire",
354+
"template_id": "",
355+
"successful_count": 0,
356+
"requested_count": 0,
357+
}
320358
]
321359
orchestrator = _make_orchestrator(requests=requests)
322360
result = await orchestrator.execute(DashboardSummaryInput())

tests/unit/infrastructure/storage/test_safe_deserialize_iter.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# Concrete subclass for testing
1414
# ---------------------------------------------------------------------------
1515

16+
1617
class _ConcreteRepo(StorageRepositoryMixin):
1718
"""Minimal concrete repo that uses _safe_deserialize_iter."""
1819

@@ -34,9 +35,9 @@ def _get_storage(self):
3435
# _safe_deserialize_iter tests
3536
# ---------------------------------------------------------------------------
3637

38+
3739
@pytest.mark.unit
3840
class TestSafeDeserializeIter:
39-
4041
def test_all_valid_rows_returned(self):
4142
rows = [
4243
{"machine_id": "m1", "status": "running"},
@@ -70,6 +71,7 @@ def flaky(data):
7071

7172
def test_error_log_emitted_with_entity_id(self):
7273
"""logger.error must be called with the entity id of the bad row."""
74+
7375
def broken(_data):
7476
raise ValueError("schema changed")
7577

@@ -135,6 +137,7 @@ def always_fail(_data):
135137

136138
def test_unknown_entity_type_counted_under_unknown(self):
137139
"""Rows with no known id field go into 'unknown' bucket."""
140+
138141
def broken(_data):
139142
raise ValueError("bad")
140143

@@ -147,6 +150,7 @@ def broken(_data):
147150

148151
def test_skip_counter_accumulates_across_calls(self):
149152
"""Multiple calls to _safe_deserialize_iter must accumulate skip counts."""
153+
150154
def broken(_data):
151155
raise ValueError("bad")
152156

@@ -158,6 +162,7 @@ def broken(_data):
158162

159163
def test_error_logged_at_error_level(self):
160164
"""Skipped rows must be logged at ERROR level (not WARNING/INFO)."""
165+
161166
def broken(_data):
162167
raise ValueError("validation")
163168

0 commit comments

Comments
 (0)