Skip to content

Commit 3e29459

Browse files
committed
git add pytest, conftest, test users
1 parent 565a384 commit 3e29459

7 files changed

Lines changed: 94 additions & 6 deletions

File tree

api_v1/tests/conftest.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,62 @@
1+
import sys
12
import asyncio
3+
import httpx
4+
import pytest_asyncio
5+
import pytest
6+
7+
from typing import Any, AsyncGenerator
8+
from contextlib import asynccontextmanager
9+
from asgi_lifespan import LifespanManager
10+
from fastapi import FastAPI
11+
from sqlalchemy.pool import NullPool
12+
13+
from config import test_connection, settings, BaseModel
14+
from config import db_connection
15+
from api_v1.routers import register_routers
16+
17+
18+
db_setup = test_connection(
19+
settings.test_db.url,
20+
poolclass=NullPool,
21+
)
22+
23+
24+
@pytest.fixture(scope='session', autouse=True)
25+
def event_loop(request):
26+
loop = asyncio.get_event_loop_policy().new_event_loop()
27+
yield loop
28+
loop.close()
29+
30+
31+
async def override_get_async_session():
32+
async with db_setup.session() as session:
33+
yield session
34+
35+
36+
@pytest_asyncio.fixture(scope='session', autouse=True)
37+
async def app():
38+
@asynccontextmanager
39+
async def lifespan(app: FastAPI):
40+
async with db_setup.engine.begin() as conn:
41+
await conn.run_sync(BaseModel.metadata.create_all)
42+
sys.stdout.write('alembic upgrade head')
43+
yield
44+
await conn.run_sync(BaseModel.metadata.drop_all)
45+
46+
app = FastAPI(docs_url=None, redoc_url=None)
47+
register_routers(app=app)
48+
app.dependency_overrides[db_connection.session_geter] = override_get_async_session
49+
50+
async with LifespanManager(app) as manager:
51+
yield manager.app
52+
53+
54+
@pytest_asyncio.fixture(scope='session')
55+
async def client(app: FastAPI) -> AsyncGenerator[httpx.AsyncClient, Any]:
56+
current_home = settings.CURRENT_ORIGIN
57+
current_api = settings.API_PREFIX
58+
async with httpx.AsyncClient(
59+
app=app,
60+
base_url=current_home + current_api,
61+
) as client:
62+
yield client

api_v1/tests/test_users.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import pytest
2+
3+
from httpx import AsyncClient
4+
5+
6+
@pytest.mark.asyncio
7+
async def test_get_user_error(client: AsyncClient):
8+
response = await client.get(
9+
'/users/get',
10+
)
11+
assert response.status_code == 400

config/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
from .config import settings
22
from .celery.connection import app as celery_app
33
from .database.db_helper import db_helper as db_connection
4+
from .database.db_helper import db_test as test_connection
45
from .models.base import Base as BaseModel
56

67

78
__all__ = ('settings',
89
'celery_app',
910
'db_connection',
11+
'test_connection',
1012
'BaseModel',
1113
)

config/config.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ class TestDBSettings(BaseModel):
1515
"""
1616
Настройки тестовой базы данных
1717
"""
18-
18+
_engine: str = config('TEST_DB_ENGINE')
19+
_owner: str = config('TEST_DB_USER')
20+
_password: str = config('TEST_DB_PASSWORD')
21+
_name: str = config('TEST_DB_HOST')
22+
_db_name: str = config('TEST_DB_NAME')
23+
url: str = f'{_engine}://{_owner}:{_password}@{_name}/{_db_name}'
1924

2025

2126
class DBSettings(BaseModel):
@@ -56,6 +61,7 @@ class Settings(BaseSettings):
5661
extra='ignore',
5762
)
5863
db: DBSettings = DBSettings()
64+
test_db: TestDBSettings = TestDBSettings()
5965
rabbit: RabbitSettings = RabbitSettings()
6066
debug: bool = bool(int(config('DEBUG')))
6167
API_PREFIX: str = '/api/v1'

docker-compose.yml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ services:
4949
command: /start-flower
5050
volumes:
5151
- .:/app
52-
ports:
53-
- 5555:5555
52+
expose:
53+
- 5555
5454
env_file:
5555
- .env
5656
depends_on:
@@ -124,9 +124,14 @@ services:
124124
- grafana-data:/var/lib/grafana
125125
- ./grafana/provisioning:/etc/grafana/provisioning
126126
environment:
127+
- DATABASE_USER=${DB_USER}
128+
- DATABASE_PASS=${DB_PASSWORD}
129+
- DATABASE_NAME=${DB_NAME}
130+
- DATABASE_HOST=${DB_HOST}
131+
- DATABASE_SSL_MODE=disable
127132
- GF_USERS_ALLOW_SIGN_UP=false
128-
env_file:
129-
- .env
133+
- GF_SECURITY_ADMIN_USER=${GF_SECURITY_ADMIN_USER}
134+
- GF_SECURITY_ADMIN_PASSWORD=${GF_SECURITY_ADMIN_PASSWORD}
130135
restart: unless-stopped
131136
expose:
132137
- 3000
@@ -142,6 +147,7 @@ services:
142147
- "3000:3000"
143148
- "9090:9090"
144149
- "9093:9093"
150+
- "5555:5555"
145151
volumes:
146152
- ./caddy:/etc/caddy
147153
environment:

docker/alertmanager/config.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ receivers:
77
- send_resolved: true
88
text: "{{ .CommonAnnotations.description }}"
99
username: 'Prometheus'
10-
channel: '#prometheus'
10+
channel: '#prometheus'
11+
api_url: https://hooks.slack.com/services/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

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ prometheus-fastapi-instrumentator = "^7.0.0"
2525
black = "^24.10.0"
2626
pytest-asyncio = "^0.24.0"
2727
asgi-lifespan = "^2.1.0"
28+
flake8 = "^7.1.1"
2829

2930
[build-system]
3031
requires = ["poetry-core"]

0 commit comments

Comments
 (0)