Skip to content

Commit

Permalink
add pytest-xdist and tests in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
notarious2 committed Mar 3, 2024
1 parent 6b035c6 commit 7587aac
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 41 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,45 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: "3.11"
cache: "poetry"
- name: Install dependencies
run: |
poetry env use "3.11"
poetry export --only lint --output lint-requirements.txt
pip install -r lint-requirements.txt
- name: Run Ruff
run: ruff check --output-format=github .
test:
name: Run Tests
needs: lint
runs-on: ubuntu-latest
services:
postgres:
image: postgres:15-alpine
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
ports: ["5432:5432"]
redis:
image: redis:7.0.12-alpine
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 6379:6379
steps:
- uses: actions/checkout@v3
- run: pipx install poetry
- uses: actions/setup-python@v4
with:
python-version: 3.11
cache: "poetry"
- run: poetry install
- run: poetry run pytest -x -n auto --dist loadfile
env:
DB_HOST: "localhost"
REDIS_HOST: "localhost"
REDIS_PORT: "6379"
14 changes: 0 additions & 14 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
repos:
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 23.7.0
hooks:
- id: black
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
exclude: "src/tests/"
additional_dependencies: [Flake8-pyproject]
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.3.0
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ WORKDIR /opt/chat

COPY poetry.lock pyproject.toml ./
RUN pip install "poetry==$POETRY_VERSION"
RUN poetry export --with dev --output requirements.txt
RUN poetry export --with test,lint --output requirements.txt
RUN pip install --no-deps -r requirements.txt

COPY . .
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ down:
test:
docker exec -it chat-backend python -m pytest -svv $(target)

ftest:
docker exec -it chat-backend python -m pytest -x -n 2 --dist loadfile

test-integration:
docker exec -it chat-backend python -m pytest -m "integration" -svv

Expand Down
46 changes: 35 additions & 11 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pytest = "7.4.3"
pytest-asyncio = "0.23.5"
pytest-env = "1.1.3"
pytest-mock = "3.12.0"
pytest-xdist = "3.5.0"

[tool.poetry.group.lint.dependencies]
ruff = "0.3.0"
Expand Down
9 changes: 7 additions & 2 deletions src/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
from random import randint

import boto3
from pydantic_settings import BaseSettings, SettingsConfigDict
Expand Down Expand Up @@ -58,7 +59,7 @@ class GlobalSettings(BaseSettings):


class TestSettings(GlobalSettings):
DB_SCHEMA: str = "test"
DB_SCHEMA: str = f"test_{randint(1, 100)}"


class DevelopmentSettings(GlobalSettings):
Expand Down Expand Up @@ -125,6 +126,10 @@ def get_settings():
},
"loggers": {
"": {"handlers": ["default"], "level": settings.LOG_LEVEL, "propagate": False},
"uvicorn": {"handlers": ["default"], "level": logging.ERROR, "propagate": False},
"uvicorn": {
"handlers": ["default"],
"level": logging.ERROR,
"propagate": False,
},
},
}
50 changes: 37 additions & 13 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@
f"postgresql+asyncpg://"
f"{settings.DB_USER}:{settings.DB_PASSWORD}@{settings.DB_HOST}:{settings.DB_PORT}/{settings.DB_NAME}"
)
engine_test = create_async_engine(DATABASE_URL_TEST, connect_args={"server_settings": {"jit": "off"}})
engine_test = create_async_engine(
DATABASE_URL_TEST, connect_args={"server_settings": {"jit": "off"}}
)
autocommit_engine = engine_test.execution_options(isolation_level="AUTOCOMMIT")

async_session_maker = sessionmaker(autocommit_engine, class_=AsyncSession, expire_on_commit=False)
async_session_maker = sessionmaker(
autocommit_engine, class_=AsyncSession, expire_on_commit=False
)
metadata.bind = engine_test


Expand All @@ -36,7 +40,7 @@ async def override_get_async_session() -> AsyncGenerator[AsyncSession, None]:
app.dependency_overrides[get_async_session] = override_get_async_session


@pytest.fixture(scope="session")
@pytest.fixture()
async def db_session():
async with autocommit_engine.begin() as conn:
await conn.execute(text(f"CREATE SCHEMA IF NOT EXISTS {settings.DB_SCHEMA}"))
Expand Down Expand Up @@ -140,31 +144,43 @@ def authenticated_doug_client(async_client: AsyncClient, doug_user: User):


@pytest.fixture
async def bob_emily_chat(db_session: AsyncSession, bob_user: User, emily_user: User) -> Chat:
async def bob_emily_chat(
db_session: AsyncSession, bob_user: User, emily_user: User
) -> Chat:
chat = Chat(chat_type=ChatType.DIRECT)
chat.users.append(bob_user)
chat.users.append(emily_user)
db_session.add(chat)
await db_session.flush()
# make empty read statuses for both users last_read_message_id = 0
initiator_read_status = ReadStatus(chat_id=chat.id, user_id=bob_user.id, last_read_message_id=0)
recipient_read_status = ReadStatus(chat_id=chat.id, user_id=emily_user.id, last_read_message_id=0)
initiator_read_status = ReadStatus(
chat_id=chat.id, user_id=bob_user.id, last_read_message_id=0
)
recipient_read_status = ReadStatus(
chat_id=chat.id, user_id=emily_user.id, last_read_message_id=0
)
db_session.add_all([initiator_read_status, recipient_read_status])
await db_session.commit()

return chat


@pytest.fixture
async def bob_doug_chat(db_session: AsyncSession, doug_user: User, bob_user: User) -> Chat:
async def bob_doug_chat(
db_session: AsyncSession, doug_user: User, bob_user: User
) -> Chat:
chat = Chat(chat_type=ChatType.DIRECT)
chat.users.append(doug_user)
chat.users.append(bob_user)
db_session.add(chat)
await db_session.flush()
# make empty read statuses for both users last_read_message_id = 0
initiator_read_status = ReadStatus(chat_id=chat.id, user_id=bob_user.id, last_read_message_id=0)
recipient_read_status = ReadStatus(chat_id=chat.id, user_id=doug_user.id, last_read_message_id=0)
initiator_read_status = ReadStatus(
chat_id=chat.id, user_id=bob_user.id, last_read_message_id=0
)
recipient_read_status = ReadStatus(
chat_id=chat.id, user_id=doug_user.id, last_read_message_id=0
)
db_session.add_all([initiator_read_status, recipient_read_status])

await db_session.commit()
Expand Down Expand Up @@ -199,12 +215,16 @@ async def bob_emily_chat_messages_history(

@pytest.fixture
async def bob_read_status(
db_session: AsyncSession, bob_user: User, bob_emily_chat_messages_history: list[Message]
db_session: AsyncSession,
bob_user: User,
bob_emily_chat_messages_history: list[Message],
) -> ReadStatus:
# bob read 10 messages
last_read_message = bob_emily_chat_messages_history[9]
read_status = ReadStatus(
user_id=bob_user.id, chat_id=last_read_message.chat.id, last_read_message_id=last_read_message.id
user_id=bob_user.id,
chat_id=last_read_message.chat.id,
last_read_message_id=last_read_message.id,
)
db_session.add(read_status)
await db_session.commit()
Expand All @@ -214,12 +234,16 @@ async def bob_read_status(

@pytest.fixture
async def emily_read_status(
db_session: AsyncSession, emily_user: User, bob_emily_chat_messages_history: list[Message]
db_session: AsyncSession,
emily_user: User,
bob_emily_chat_messages_history: list[Message],
) -> ReadStatus:
# emily read 15 messages
last_read_message = bob_emily_chat_messages_history[14]
read_status = ReadStatus(
user_id=emily_user.id, chat_id=last_read_message.chat.id, last_read_message_id=last_read_message.id
user_id=emily_user.id,
chat_id=last_read_message.chat.id,
last_read_message_id=last_read_message.id,
)
db_session.add(read_status)
await db_session.commit()
Expand Down

0 comments on commit 7587aac

Please sign in to comment.