fix: use ASGITransport for httpx AsyncClient in tests
- httpx.AsyncClient requires ASGITransport for FastAPI app testing - Updated all test files to use correct API
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -3,7 +3,7 @@ import pytest_asyncio
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
|
||||
from app.database import Base, get_db
|
||||
from app.main import app
|
||||
from httpx import AsyncClient
|
||||
from httpx import AsyncClient, ASGITransport
|
||||
|
||||
TEST_DATABASE_URL = "sqlite+aiosqlite:///:memory:"
|
||||
|
||||
@@ -28,6 +28,6 @@ async def client(db):
|
||||
yield db
|
||||
|
||||
app.dependency_overrides[get_db] = override_get_db
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
|
||||
yield ac
|
||||
app.dependency_overrides.clear()
|
||||
|
||||
+13
-13
@@ -1,10 +1,10 @@
|
||||
import pytest
|
||||
from httpx import AsyncClient
|
||||
from httpx import AsyncClient, ASGITransport
|
||||
from app.main import app
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_job():
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
|
||||
# Create a source first (job requires source_id)
|
||||
source_resp = await ac.post("/api/sources/", json={
|
||||
"name": "Test Source",
|
||||
@@ -31,7 +31,7 @@ async def test_create_job():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_jobs():
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
|
||||
# Create source and job
|
||||
source_resp = await ac.post("/api/sources/", json={
|
||||
"name": "Test Source",
|
||||
@@ -56,7 +56,7 @@ async def test_list_jobs():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_job():
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
|
||||
source_resp = await ac.post("/api/sources/", json={
|
||||
"name": "Test Source",
|
||||
"type": "local",
|
||||
@@ -80,7 +80,7 @@ async def test_get_job():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_job():
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
|
||||
source_resp = await ac.post("/api/sources/", json={
|
||||
"name": "Test Source",
|
||||
"type": "local",
|
||||
@@ -102,13 +102,13 @@ async def test_delete_job():
|
||||
assert response.status_code == 200
|
||||
|
||||
# Verify deletion
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
|
||||
get_resp = await ac.get(f"/api/jobs/{job_id}")
|
||||
assert get_resp.status_code == 404
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_job():
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
|
||||
# Create source
|
||||
source_resp = await ac.post("/api/sources/", json={
|
||||
"name": "Test Source",
|
||||
@@ -134,13 +134,13 @@ async def test_run_job():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_job_not_found():
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
|
||||
response = await ac.post("/api/jobs/999/run")
|
||||
assert response.status_code == 404
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_job():
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
|
||||
# Create source
|
||||
source_resp = await ac.post("/api/sources/", json={
|
||||
"name": "Test Source",
|
||||
@@ -169,7 +169,7 @@ async def test_update_job():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_schedule():
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
|
||||
# Create source
|
||||
source_resp = await ac.post("/api/sources/", json={
|
||||
"name": "Test Source",
|
||||
@@ -202,18 +202,18 @@ async def test_create_schedule():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_job_not_found():
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
|
||||
response = await ac.get("/api/jobs/99999")
|
||||
assert response.status_code == 404
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_job_not_found():
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
|
||||
response = await ac.put("/api/jobs/99999", json={"name": "Test"})
|
||||
assert response.status_code == 404
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_job_not_found():
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
|
||||
response = await ac.delete("/api/jobs/99999")
|
||||
assert response.status_code == 404
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import pytest
|
||||
from httpx import AsyncClient
|
||||
from httpx import AsyncClient, ASGITransport
|
||||
from app.main import app
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_source():
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
|
||||
response = await ac.post("/api/sources/", json={
|
||||
"name": "Test Source",
|
||||
"type": "local",
|
||||
@@ -18,7 +18,7 @@ async def test_create_source():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_sources():
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
|
||||
# Create source first
|
||||
await ac.post("/api/sources/", json={
|
||||
"name": "Test Source",
|
||||
@@ -33,7 +33,7 @@ async def test_list_sources():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_source():
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
|
||||
create_resp = await ac.post("/api/sources/", json={
|
||||
"name": "Test Source",
|
||||
"type": "local",
|
||||
@@ -47,7 +47,7 @@ async def test_get_source():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_source():
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
|
||||
create_resp = await ac.post("/api/sources/", json={
|
||||
"name": "Delete Me",
|
||||
"type": "local",
|
||||
@@ -59,13 +59,13 @@ async def test_delete_source():
|
||||
assert response.status_code == 200
|
||||
|
||||
# Verify deletion
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
|
||||
get_resp = await ac.get(f"/api/sources/{source_id}")
|
||||
assert get_resp.status_code == 404
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_source():
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
|
||||
create_resp = await ac.post("/api/sources/", json={
|
||||
"name": "Original Name",
|
||||
"type": "local",
|
||||
@@ -82,18 +82,18 @@ async def test_update_source():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_source_not_found():
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
|
||||
response = await ac.get("/api/sources/99999")
|
||||
assert response.status_code == 404
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_source_not_found():
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
|
||||
response = await ac.put("/api/sources/99999", json={"name": "Test"})
|
||||
assert response.status_code == 404
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_source_not_found():
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
|
||||
response = await ac.delete("/api/sources/99999")
|
||||
assert response.status_code == 404
|
||||
|
||||
Reference in New Issue
Block a user