diff --git a/.vscode/settings.json b/.vscode/settings.json index d171c99..23380ff 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,5 +3,8 @@ "app" ], "python.testing.unittestEnabled": false, - "python.testing.pytestEnabled": true + "python.testing.pytestEnabled": true, + "chat.tools.terminal.autoApprove": { + "pytest": true + } } \ No newline at end of file diff --git a/app/tests/test_api/test_deps.py b/app/tests/test_api/test_deps.py new file mode 100644 index 0000000..8d00084 --- /dev/null +++ b/app/tests/test_api/test_deps.py @@ -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 diff --git a/app/tests/test_environment/test_environment.py b/app/tests/test_environment/test_environment.py index 6a21a6e..2890b0a 100644 --- a/app/tests/test_environment/test_environment.py +++ b/app/tests/test_environment/test_environment.py @@ -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) +