Skip to content

Commit 3490416

Browse files
ambvclaude
andcommitted
Address review feedback and fix missing commit in upload endpoint
Review fixes: - Use StaticPool for in-memory SQLite so all sessions share one connection, avoiding potential "no such table" errors. - Fix test_valid_bearer_token to actually test a protected endpoint with and without auth headers. - Assert ordering in trends test instead of building a dict. - Make health test explicit about the expected unhealthy DB status. - Add memray status assertion to test_upload_clears_memray_failure. Bug fix found by the new assertion: the upload endpoint's delete of memray failures was missing an await db.commit(), so the deletion was never persisted. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 78d2cce commit 3490416

File tree

6 files changed

+44
-16
lines changed

6 files changed

+44
-16
lines changed

backend/app/routers/upload.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ def clean_flag(flag):
329329
models.MemrayBuildFailure.environment_id == environment_id
330330
)
331331
)
332+
await db.commit()
332333

333334
return {
334335
"message": "Worker run uploaded successfully",

backend/tests/conftest.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import pytest_asyncio
99
from httpx import ASGITransport, AsyncClient
1010
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
11+
from sqlalchemy.pool import StaticPool
1112

1213
from app.database import get_database
1314
from app.factory import create_app
@@ -28,7 +29,12 @@ def test_settings():
2829

2930
@pytest_asyncio.fixture
3031
async def db_engine():
31-
engine = create_async_engine("sqlite+aiosqlite://", echo=False)
32+
engine = create_async_engine(
33+
"sqlite+aiosqlite://",
34+
echo=False,
35+
connect_args={"check_same_thread": False},
36+
poolclass=StaticPool,
37+
)
3238
async with engine.begin() as conn:
3339
await conn.run_sync(Base.metadata.create_all)
3440
yield engine

backend/tests/test_auth.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,22 @@
55

66
@pytest.mark.asyncio
77
async def test_valid_bearer_token(client, auth_headers, sample_binary, sample_environment):
8-
"""A valid Bearer token should authenticate successfully."""
9-
response = await client.get("/api/binaries")
8+
"""A valid Bearer token should authenticate successfully on a protected endpoint."""
9+
payload = {
10+
"commit_sha": "a" * 40,
11+
"commit_timestamp": "2025-06-16T10:00:00",
12+
"binary_id": "default",
13+
"environment_id": "linux-x86_64",
14+
"error_message": "test",
15+
}
16+
# Without auth → rejected
17+
response = await client.post("/api/report-memray-failure", json=payload)
18+
assert response.status_code in (401, 403)
19+
20+
# With auth → accepted
21+
response = await client.post(
22+
"/api/report-memray-failure", json=payload, headers=auth_headers
23+
)
1024
assert response.status_code == 200
1125

1226

backend/tests/test_health.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ async def test_health_check(client):
1313

1414

1515
@pytest.mark.asyncio
16-
async def test_health_check_returns_db_status(client):
17-
"""The health endpoint reports database status when db check is enabled."""
16+
async def test_health_check_reports_db_status(client):
17+
"""The health router uses a module-level settings object with
18+
enable_health_check_db=True (not overridable via test_settings).
19+
It attempts db.execute("SELECT 1") which fails on SQLAlchemy 2.x
20+
because raw strings need text(). This is a pre-existing app bug.
21+
We verify the endpoint still returns 200 and reports the DB as
22+
unhealthy rather than crashing."""
1823
response = await client.get("/health")
24+
assert response.status_code == 200
1925
data = response.json()
20-
# The module-level settings have enable_health_check_db=True,
21-
# but db.execute("SELECT 1") uses a raw string which fails on
22-
# SQLAlchemy 2.x (needs text()). This is a pre-existing issue
23-
# in the app code, not a test problem.
2426
assert "database" in data
27+
assert data["database"] == "unhealthy"
28+
assert data["status"] == "unhealthy"

backend/tests/test_production_data.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,11 @@ async def test_trends_returns_chronological_data(client, prod_data):
155155
data = response.json()
156156
assert len(data) == 2
157157

158-
# Both data points present with correct memory values
159-
hwm_values = {d["sha"][:8]: d["high_watermark_bytes"] for d in data}
160-
assert hwm_values["e05182f9"] == 1_557_777
161-
assert hwm_values["d3d94e0e"] == 1_721_155
158+
# Ordered by timestamp DESC: newer commit first
159+
assert data[0]["sha"] == COMMIT_CURR["sha"]
160+
assert data[0]["high_watermark_bytes"] == 1_721_155
161+
assert data[1]["sha"] == COMMIT_PREV["sha"]
162+
assert data[1]["high_watermark_bytes"] == 1_557_777
162163

163164

164165
@pytest.mark.asyncio

backend/tests/test_upload.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,12 @@ async def test_upload_clears_memray_failure(
216216
"/api/upload-run", json=UPLOAD_PAYLOAD, headers=auth_headers
217217
)
218218
assert resp.status_code == 200
219+
assert resp.json()["results_created"] == 1
219220

220-
# Verify the upload response confirms success
221-
data = resp.json()
222-
assert data["results_created"] == 1
221+
# Verify the failure was cleared
222+
status = await client.get("/api/memray-status")
223+
assert status.json()["has_failures"] is False
224+
assert status.json()["failure_count"] == 0
223225

224226

225227
@pytest.mark.asyncio

0 commit comments

Comments
 (0)