diff --git a/backend/app/app/conftest.py b/backend/app/app/conftest.py index 5771bfd..0042802 100644 --- a/backend/app/app/conftest.py +++ b/backend/app/app/conftest.py @@ -1,13 +1,22 @@ -from collections.abc import AsyncGenerator -from typing import Any - import pytest -from fastapi import FastAPI from httpx import AsyncClient +from sqlmodel import Session, SQLModel, create_engine +from app.core.config import settings +from app.web.api.deps import get_db from app.web.application import get_app +@pytest.fixture(name="session") +def session_fixture(): + engine = create_engine( + f"postgresql+psycopg://{settings.POSTGRES_USER}:{settings.POSTGRES_PASSWORD}@bemore-db/{settings.POSTGRES_DB}", + ) + SQLModel.metadata.create_all(engine) + with Session(engine) as session: + yield session + + @pytest.fixture(scope="session") def anyio_backend() -> str: """ @@ -18,29 +27,41 @@ def anyio_backend() -> str: return "asyncio" -@pytest.fixture -def fastapi_app() -> FastAPI: - """ - Fixture for creating FastAPI app. +@pytest.fixture(name="client") +def client_fixture(session: Session): + def get_session_override(): + return session - :return: fastapi app with mocked dependencies. - """ - application = get_app() - return application # noqa: WPS331 + app = get_app() + app.dependency_overrides[get_db] = get_session_override + client = AsyncClient(app=app, base_url="http://localhost:8000") + yield client + app.dependency_overrides.clear() -@pytest.fixture -async def client( - fastapi_app: FastAPI, - anyio_backend: Any, -) -> AsyncGenerator[AsyncClient, None]: - """ - Fixture that creates client for requesting server. - :param fastapi_app: the application. - :yield: client for the app. - """ - async with AsyncClient( - app=fastapi_app, base_url="http://localhost:8000" - ) as ac: - yield ac +# @pytest.fixture() +# def fastapi_app() -> FastAPI: +# """ +# Fixture for creating FastAPI app. + +# :return: fastapi app with mocked dependencies. +# """ +# application = get_app() +# return application # noqa: WPS331 + + +# @pytest.fixture(name="client") +# async def client_fixture( +# fastapi_app: FastAPI, +# ) -> AsyncGenerator[AsyncClient, None]: +# """ +# Fixture that creates client for requesting server. + +# :param fastapi_app: the application. +# :yield: client for the app. +# """ +# async with AsyncClient( +# app=fastapi_app, base_url="http://localhost:8000" +# ) as ac: +# yield ac diff --git a/backend/app/app/db/init_db.py b/backend/app/app/db/init_db.py index 3177322..3882eef 100644 --- a/backend/app/app/db/init_db.py +++ b/backend/app/app/db/init_db.py @@ -1,3 +1,5 @@ +import logging + from sqlmodel import Session, SQLModel, select from app.core.config import settings @@ -16,6 +18,7 @@ def init_db(session: Session) -> None: is_superuser=True, ) user = User.create(session, user_in) + logging.debug(f"Superuser {settings.FIRST_SUPERUSER} created") async def init(): diff --git a/backend/app/app/main.py b/backend/app/app/main.py index dbba442..b60d8db 100644 --- a/backend/app/app/main.py +++ b/backend/app/app/main.py @@ -13,9 +13,9 @@ def main(): if settings.reload: uvicorn.run( "app.web.application:get_app", - workers=settings.workers_count, host=settings.host, port=settings.port, + workers=settings.workers_count, reload=settings.reload, log_level=settings.log_level.value.lower(), factory=True, diff --git a/backend/app/app/tests/test_bemore.py b/backend/app/app/tests/test_bemore.py index 1afa908..8fd9e03 100644 --- a/backend/app/app/tests/test_bemore.py +++ b/backend/app/app/tests/test_bemore.py @@ -1,17 +1,15 @@ import pytest -from fastapi import FastAPI from httpx import AsyncClient from starlette import status @pytest.mark.anyio -async def test_health(client: AsyncClient, fastapi_app: FastAPI) -> None: +async def test_health(client: AsyncClient) -> None: """ Checks the health endpoint. :param client: client for the app. :param fastapi_app: current FastAPI application. """ - url = fastapi_app.url_path_for("health_check") - response = await client.get(url) + response = await client.get("/api/utils/health_check") assert response.status_code == status.HTTP_200_OK diff --git a/backend/app/app/tests/test_login.py b/backend/app/app/tests/test_login.py index 79eb4f3..04691c6 100644 --- a/backend/app/app/tests/test_login.py +++ b/backend/app/app/tests/test_login.py @@ -1,11 +1,10 @@ import pytest -from fastapi import FastAPI from httpx import AsyncClient from starlette import status @pytest.mark.anyio -async def test_login(client: AsyncClient, fastapi_app: FastAPI) -> None: +async def test_login(client: AsyncClient) -> None: """ Checks the health endpoint. @@ -24,7 +23,7 @@ async def test_login(client: AsyncClient, fastapi_app: FastAPI) -> None: @pytest.mark.anyio -async def test_token(client: AsyncClient, fastapi_app: FastAPI) -> None: +async def test_token(client: AsyncClient) -> None: """ Checks the health endpoint. diff --git a/backend/app/app/tests/test_user.py b/backend/app/app/tests/test_user.py index 069376e..62e06c9 100644 --- a/backend/app/app/tests/test_user.py +++ b/backend/app/app/tests/test_user.py @@ -1,11 +1,10 @@ import pytest -from fastapi import FastAPI from httpx import AsyncClient from starlette import status @pytest.mark.anyio -async def test_create_user(client: AsyncClient, fastapi_app: FastAPI) -> None: +async def test_create_user(client: AsyncClient) -> None: """ Checks the health endpoint. diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 9a01dc2..560bdba 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -25,6 +25,7 @@ services: PGADMIN_CONFIG_SERVER_MODE: "False" networks: - default + queue: container_name: bemore-queue image: rabbitmq:3-management diff --git a/justfile b/justfile index 3a1ae1f..d93fde6 100644 --- a/justfile +++ b/justfile @@ -21,8 +21,9 @@ dev-logs: test: clean docker-compose -f docker-compose.ci.yml up -d --build - sleep 0.5 docker-compose exec -T backend poetry run pytest --cov=app --cov-report=xml clean: docker-compose -f docker-compose.dev.yml down --rmi local --volumes --remove-orphans + docker-compose -f docker-compose.ci.yml down --rmi local --volumes --remove-orphans + docker-compose down --rmi local --volumes --remove-orphans