Skip to content

Commit

Permalink
🦄 refactor: Refactor test fixtures and dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
AndPuQing committed Feb 16, 2024
1 parent 8edd7fa commit 03fb4d7
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 37 deletions.
73 changes: 47 additions & 26 deletions backend/app/app/conftest.py
Original file line number Diff line number Diff line change
@@ -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:
"""
Expand All @@ -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
3 changes: 3 additions & 0 deletions backend/app/app/db/init_db.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

from sqlmodel import Session, SQLModel, select

from app.core.config import settings
Expand All @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion backend/app/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 2 additions & 4 deletions backend/app/app/tests/test_bemore.py
Original file line number Diff line number Diff line change
@@ -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
5 changes: 2 additions & 3 deletions backend/app/app/tests/test_login.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions backend/app/app/tests/test_user.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 1 addition & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ services:
PGADMIN_CONFIG_SERVER_MODE: "False"
networks:
- default

queue:
container_name: bemore-queue
image: rabbitmq:3-management
Expand Down
3 changes: 2 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 03fb4d7

Please sign in to comment.