feat(tests): Add test configuration and helpers
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
21
tests/conftest.py
Normal file
21
tests/conftest.py
Normal file
@@ -0,0 +1,21 @@
|
||||
import pytest
|
||||
from httpx import AsyncClient, ASGITransport
|
||||
from app.main import app
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def anyio_backend():
|
||||
"""
|
||||
Это фикстура, необходимая для pytest-asyncio, чтобы он работал
|
||||
с httpx в асинхронном режиме. Просто стандартный шаблон.
|
||||
"""
|
||||
return "asyncio"
|
||||
|
||||
@pytest.fixture
|
||||
async def client() -> AsyncClient:
|
||||
"""
|
||||
Главная фикстура. Создает тестовый клиент, который мы будем
|
||||
использовать во всех наших тестах.
|
||||
"""
|
||||
transport = ASGITransport(app=app)
|
||||
async with AsyncClient(transport=transport, base_url="http://test") as async_client:
|
||||
yield async_client
|
||||
@@ -1,16 +1,8 @@
|
||||
import pytest
|
||||
from httpx import AsyncClient, ASGITransport
|
||||
|
||||
from app.main import app
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_counters_unauthorized():
|
||||
transport = ASGITransport(app=app)
|
||||
async with AsyncClient(transport=transport, base_url="http://test") as client:
|
||||
response = await client.get("/api/v1/counters/")
|
||||
async def test_get_counters_unauthorized(client): # <-- Получаем client как аргумент
|
||||
response = await client.get("/api/v1/counters/")
|
||||
|
||||
# Ожидаем 403, а не 401
|
||||
assert response.status_code == 403
|
||||
|
||||
# Тело ответа для этой ошибки от HTTPBearer
|
||||
assert response.json() == {"detail": "Not authenticated"}
|
||||
assert response.status_code == 403
|
||||
assert response.json() == {"detail": "Not authenticated"}
|
||||
@@ -1,25 +1,12 @@
|
||||
import pytest
|
||||
from httpx import AsyncClient, ASGITransport
|
||||
|
||||
# Импортируем наше главное FastAPI приложение
|
||||
from app.main import app
|
||||
|
||||
# Тесты теперь не знают про 'app', они знают только про 'client'
|
||||
@pytest.mark.asyncio
|
||||
async def test_health_check():
|
||||
"""
|
||||
Тестирует эндпоинт проверки работоспособности (health check).
|
||||
"""
|
||||
# Создаем "транспорт", который работает с нашим app,
|
||||
# и передаем его в AsyncClient.
|
||||
transport = ASGITransport(app=app)
|
||||
async with AsyncClient(transport=transport, base_url="http://test") as client:
|
||||
async def test_health_check(client): # <-- Получаем client как аргумент
|
||||
response = await client.get("/")
|
||||
|
||||
# Отправляем асинхронный GET-запрос на корневой URL ("/")
|
||||
response = await client.get("/")
|
||||
|
||||
# Проверки остаются без изменений
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {
|
||||
"status": "ok",
|
||||
"message": "Welcome to Marquiz Metrica Connector!"
|
||||
}
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {
|
||||
"status": "ok",
|
||||
"message": "Welcome to Marquiz Metrica Connector!"
|
||||
}
|
||||
Reference in New Issue
Block a user