refactor: Improve code formatting and organization in test files and migration scripts

This commit is contained in:
2026-05-11 14:57:24 +08:00
parent 38e71f8b55
commit 63e946f0ad
6 changed files with 37 additions and 16 deletions
@@ -56,7 +56,9 @@ def upgrade() -> None:
server_default=sa.text("now()"),
nullable=False,
),
sa.ForeignKeyConstraint(["device_id"], ["iot_device.device_id"], ondelete="CASCADE"),
sa.ForeignKeyConstraint(
["device_id"], ["iot_device.device_id"], ondelete="CASCADE"
),
sa.PrimaryKeyConstraint("id"),
)
# ### end Alembic commands ###
+11 -3
View File
@@ -14,12 +14,20 @@ class FakeSession:
@pytest.mark.asyncio
async def test_get_current_user_raises_when_user_removed(monkeypatch: pytest.MonkeyPatch) -> None:
async def test_get_current_user_raises_when_user_removed(
monkeypatch: pytest.MonkeyPatch,
) -> None:
# make verify_jwt_token return an object with .sub
monkeypatch.setattr(deps, "verify_jwt_token", lambda token: types.SimpleNamespace(sub="non-existent"))
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=cast(AsyncSession, FakeSession()))
await deps.get_current_user(
token="ignored", session=cast(AsyncSession, FakeSession())
)
assert excinfo.value.status_code == status.HTTP_401_UNAUTHORIZED
assert excinfo.value.detail == api_messages.JWT_ERROR_USER_REMOVED
+21 -10
View File
@@ -9,10 +9,15 @@ from app.models import IoTReading
@pytest.mark.asyncio(loop_scope="session")
async def test_log_iot_readings_single_device(client: AsyncClient, session: AsyncSession) -> None:
async def test_log_iot_readings_single_device(
client: AsyncClient, session: AsyncSession
) -> None:
payload = {
"devices": [
{"device_id": "dev-1", "readings": [{"sensor_type": "temperature", "value": "25.5"}]}
{
"device_id": "dev-1",
"readings": [{"sensor_type": "temperature", "value": "25.5"}],
}
]
}
@@ -22,16 +27,22 @@ async def test_log_iot_readings_single_device(client: AsyncClient, session: Asyn
@pytest.mark.asyncio(loop_scope="session")
async def test_log_iot_readings_persists_multiple(client: AsyncClient, session: AsyncSession) -> None:
async def test_log_iot_readings_persists_multiple(
client: AsyncClient, session: AsyncSession
) -> None:
payload = {
"devices": [
{"device_id": "dev-2", "readings": [
{"sensor_type": "temperature", "value": "21.0"},
{"sensor_type": "humidity", "value": "60%"}
]},
{"device_id": "dev-3", "readings": [
{"sensor_type": "pressure", "value": "1001"}
]},
{
"device_id": "dev-2",
"readings": [
{"sensor_type": "temperature", "value": "21.0"},
{"sensor_type": "humidity", "value": "60%"},
],
},
{
"device_id": "dev-3",
"readings": [{"sensor_type": "pressure", "value": "1001"}],
},
]
}
@@ -168,7 +168,6 @@ async def test_search_pressure_with_date_range(
assert isinstance(response.json(), list)
@pytest.mark.asyncio(loop_scope="session")
async def test_create_new_humidity(client: AsyncClient) -> None:
response = await client.post(
@@ -248,4 +247,3 @@ async def test_search_humidity_with_date_range(
)
assert response.status_code == status.HTTP_200_OK
assert isinstance(response.json(), list)
+1
View File
@@ -29,6 +29,7 @@ dev = [
"pytest-cov>=7.0.0,<8",
"pytest-xdist>=3.8.0,<4",
"ruff>=0.14.14,<1",
"isort>=5.12.0,<6",
"uvicorn[standard]>=0.40.0,<1",
]
+1
View File
@@ -11,6 +11,7 @@ pytest-xdist
ruff
mypy
pre-commit
isort
# HTTP client for tests
httpx