Skip to content

Commit f73c4ae

Browse files
committed
add pytest-xdist and tests in CI
1 parent 6b035c6 commit f73c4ae

File tree

9 files changed

+102
-34
lines changed

9 files changed

+102
-34
lines changed

.github/workflows/ci.yml

+41
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on: [push, pull_request]
44

55
jobs:
66
lint:
7+
name: Run Linter
78
runs-on: ubuntu-latest
89
steps:
910
- uses: actions/checkout@v4
@@ -19,3 +20,43 @@ jobs:
1920
pip install -r lint-requirements.txt
2021
- name: Run Ruff
2122
run: ruff check --output-format=github .
23+
test:
24+
name: Run Tests
25+
needs: lint
26+
runs-on: ubuntu-latest
27+
services:
28+
postgres:
29+
image: postgres:15-alpine
30+
env:
31+
POSTGRES_USER: postgres
32+
POSTGRES_PASSWORD: postgres
33+
POSTGRES_DB: postgres
34+
ports: ["5432:5432"]
35+
redis:
36+
image: redis:7.0.12-alpine
37+
options: >-
38+
--health-cmd "redis-cli ping"
39+
--health-interval 10s
40+
--health-timeout 5s
41+
--health-retries 5
42+
ports:
43+
- 6379:6379
44+
steps:
45+
- uses: actions/checkout@v4
46+
- run: pipx install poetry
47+
- name: Install Python
48+
uses: actions/setup-python@v5
49+
with:
50+
python-version: "3.11"
51+
52+
- uses: actions/setup-python@v4
53+
with:
54+
python-version: 3.11
55+
- run: |
56+
poetry env use "3.11"
57+
poetry install
58+
poetry run pytest -x -n auto --dist loadfile
59+
env:
60+
DB_HOST: "localhost"
61+
REDIS_HOST: "localhost"
62+
REDIS_PORT: "6379"

.pre-commit-config.yaml

-14
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,4 @@
11
repos:
2-
- repo: https://github.com/pycqa/isort
3-
rev: 5.12.0
4-
hooks:
5-
- id: isort
6-
- repo: https://github.com/psf/black
7-
rev: 23.7.0
8-
hooks:
9-
- id: black
10-
- repo: https://github.com/pycqa/flake8
11-
rev: 6.0.0
12-
hooks:
13-
- id: flake8
14-
exclude: "src/tests/"
15-
additional_dependencies: [Flake8-pyproject]
162
- repo: https://github.com/astral-sh/ruff-pre-commit
173
# Ruff version.
184
rev: v0.3.0

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ WORKDIR /opt/chat
2121

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

2727
COPY . .

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ down:
2222
test:
2323
docker exec -it chat-backend python -m pytest -svv $(target)
2424

25+
ftest:
26+
docker exec -it chat-backend python -m pytest -x -n 2 --dist loadfile
27+
2528
test-integration:
2629
docker exec -it chat-backend python -m pytest -m "integration" -svv
2730

poetry.lock

+35-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pytest = "7.4.3"
3838
pytest-asyncio = "0.23.5"
3939
pytest-env = "1.1.3"
4040
pytest-mock = "3.12.0"
41+
pytest-xdist = "3.5.0"
4142

4243
[tool.poetry.group.lint.dependencies]
4344
ruff = "0.3.0"

src/config.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22
import os
3+
from random import randint
34

45
import boto3
56
from pydantic_settings import BaseSettings, SettingsConfigDict
@@ -58,7 +59,7 @@ class GlobalSettings(BaseSettings):
5859

5960

6061
class TestSettings(GlobalSettings):
61-
DB_SCHEMA: str = "test"
62+
DB_SCHEMA: str = f"test_{randint(1, 100)}"
6263

6364

6465
class DevelopmentSettings(GlobalSettings):
@@ -125,6 +126,10 @@ def get_settings():
125126
},
126127
"loggers": {
127128
"": {"handlers": ["default"], "level": settings.LOG_LEVEL, "propagate": False},
128-
"uvicorn": {"handlers": ["default"], "level": logging.ERROR, "propagate": False},
129+
"uvicorn": {
130+
"handlers": ["default"],
131+
"level": logging.ERROR,
132+
"propagate": False,
133+
},
129134
},
130135
}

tests/__init__.py

Whitespace-only changes.

tests/conftest.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ async def override_get_async_session() -> AsyncGenerator[AsyncSession, None]:
3636
app.dependency_overrides[get_async_session] = override_get_async_session
3737

3838

39-
@pytest.fixture(scope="session")
39+
@pytest.fixture
4040
async def db_session():
4141
async with autocommit_engine.begin() as conn:
4242
await conn.execute(text(f"CREATE SCHEMA IF NOT EXISTS {settings.DB_SCHEMA}"))
@@ -60,7 +60,7 @@ async def clear_tables(db_session: AsyncSession):
6060
client = TestClient(app)
6161

6262

63-
@pytest.fixture(scope="session", autouse=True)
63+
@pytest.fixture(scope="session")
6464
def event_loop(request):
6565
"""Create an instance of the default event loop for each test case."""
6666
loop = asyncio.get_event_loop_policy().new_event_loop()
@@ -199,12 +199,16 @@ async def bob_emily_chat_messages_history(
199199

200200
@pytest.fixture
201201
async def bob_read_status(
202-
db_session: AsyncSession, bob_user: User, bob_emily_chat_messages_history: list[Message]
202+
db_session: AsyncSession,
203+
bob_user: User,
204+
bob_emily_chat_messages_history: list[Message],
203205
) -> ReadStatus:
204206
# bob read 10 messages
205207
last_read_message = bob_emily_chat_messages_history[9]
206208
read_status = ReadStatus(
207-
user_id=bob_user.id, chat_id=last_read_message.chat.id, last_read_message_id=last_read_message.id
209+
user_id=bob_user.id,
210+
chat_id=last_read_message.chat.id,
211+
last_read_message_id=last_read_message.id,
208212
)
209213
db_session.add(read_status)
210214
await db_session.commit()
@@ -214,12 +218,16 @@ async def bob_read_status(
214218

215219
@pytest.fixture
216220
async def emily_read_status(
217-
db_session: AsyncSession, emily_user: User, bob_emily_chat_messages_history: list[Message]
221+
db_session: AsyncSession,
222+
emily_user: User,
223+
bob_emily_chat_messages_history: list[Message],
218224
) -> ReadStatus:
219225
# emily read 15 messages
220226
last_read_message = bob_emily_chat_messages_history[14]
221227
read_status = ReadStatus(
222-
user_id=emily_user.id, chat_id=last_read_message.chat.id, last_read_message_id=last_read_message.id
228+
user_id=emily_user.id,
229+
chat_id=last_read_message.chat.id,
230+
last_read_message_id=last_read_message.id,
223231
)
224232
db_session.add(read_status)
225233
await db_session.commit()

0 commit comments

Comments
 (0)