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 f73c4ae
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 34 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on: [push, pull_request]

jobs:
lint:
name: Run Linter
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -19,3 +20,43 @@ jobs:
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@v4
- run: pipx install poetry
- name: Install Python
uses: actions/setup-python@v5
with:
python-version: "3.11"

- uses: actions/setup-python@v4
with:
python-version: 3.11
- run: |
poetry env use "3.11"
poetry install
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,
},
},
}
Empty file added tests/__init__.py
Empty file.
20 changes: 14 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,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 All @@ -60,7 +60,7 @@ async def clear_tables(db_session: AsyncSession):
client = TestClient(app)


@pytest.fixture(scope="session", autouse=True)
@pytest.fixture(scope="session")
def event_loop(request):
"""Create an instance of the default event loop for each test case."""
loop = asyncio.get_event_loop_policy().new_event_loop()
Expand Down Expand Up @@ -199,12 +199,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 +218,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 f73c4ae

Please sign in to comment.