feat: Add date range tests for pressure and humidity search endpoints

This commit is contained in:
2026-05-11 14:09:54 +08:00
parent b530a35dd6
commit ce6734d906
3 changed files with 74 additions and 1 deletions
+4 -1
View File
@@ -3,5 +3,8 @@
"app"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
"python.testing.pytestEnabled": true,
"chat.tools.terminal.autoApprove": {
"pytest": true
}
}
+24
View File
@@ -0,0 +1,24 @@
import types
import pytest
from fastapi import HTTPException
from app.api import deps
from app.api import api_messages
class FakeSession:
async def scalar(self, _query):
return None
@pytest.mark.asyncio
async def test_get_current_user_raises_when_user_removed(monkeypatch):
# make verify_jwt_token return an object with .sub
monkeypatch.setattr(deps, "verify_jwt_token", lambda token: types.SimpleNamespace(sub="non-existent"))
with pytest.raises(HTTPException) as excinfo:
await deps.get_current_user(token="ignored", session=FakeSession())
assert excinfo.value.status_code == 401
assert excinfo.value.detail == api_messages.JWT_ERROR_USER_REMOVED
@@ -146,6 +146,29 @@ async def test_search_pressure_with_limit(
assert len(response.json()) <= 1
@pytest.mark.asyncio(loop_scope="session")
async def test_search_pressure_with_date_range(
client: AsyncClient,
session: AsyncSession,
) -> None:
now = datetime.utcnow()
start_date = (now - timedelta(days=1)).isoformat() + "Z"
end_date = (now + timedelta(days=1)).isoformat() + "Z"
# ensure at least one record exists in this range
pressure = Pressure(pressure="1002.0")
session.add(pressure)
await session.commit()
response = await client.get(
app.url_path_for("search_pressure"),
params={"start_date": start_date, "end_date": end_date},
)
assert response.status_code == status.HTTP_200_OK
assert isinstance(response.json(), list)
@pytest.mark.asyncio(loop_scope="session")
async def test_create_new_humidity(client: AsyncClient) -> None:
response = await client.post(
@@ -203,3 +226,26 @@ async def test_search_humidity_with_limit(
)
assert response.status_code == status.HTTP_200_OK
assert len(response.json()) <= 1
@pytest.mark.asyncio(loop_scope="session")
async def test_search_humidity_with_date_range(
client: AsyncClient,
session: AsyncSession,
) -> None:
now = datetime.utcnow()
start_date = (now - timedelta(days=1)).isoformat() + "Z"
end_date = (now + timedelta(days=1)).isoformat() + "Z"
# ensure at least one record exists in this range
humidity = Humidity(humidity="55.0")
session.add(humidity)
await session.commit()
response = await client.get(
app.url_path_for("search_humidity"),
params={"start_date": start_date, "end_date": end_date},
)
assert response.status_code == status.HTTP_200_OK
assert isinstance(response.json(), list)