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 sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
|
||||||
from app.database import Base, get_db
|
from app.database import Base, get_db
|
||||||
from app.main import app
|
from app.main import app
|
||||||
from httpx import AsyncClient
|
from httpx import AsyncClient, ASGITransport
|
||||||
|
|
||||||
TEST_DATABASE_URL = "sqlite+aiosqlite:///:memory:"
|
TEST_DATABASE_URL = "sqlite+aiosqlite:///:memory:"
|
||||||
|
|
||||||
@@ -28,6 +28,6 @@ async def client(db):
|
|||||||
yield db
|
yield db
|
||||||
|
|
||||||
app.dependency_overrides[get_db] = override_get_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
|
yield ac
|
||||||
app.dependency_overrides.clear()
|
app.dependency_overrides.clear()
|
||||||
|
|||||||
+13
-13
@@ -1,10 +1,10 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from httpx import AsyncClient
|
from httpx import AsyncClient, ASGITransport
|
||||||
from app.main import app
|
from app.main import app
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_create_job():
|
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)
|
# Create a source first (job requires source_id)
|
||||||
source_resp = await ac.post("/api/sources/", json={
|
source_resp = await ac.post("/api/sources/", json={
|
||||||
"name": "Test Source",
|
"name": "Test Source",
|
||||||
@@ -31,7 +31,7 @@ async def test_create_job():
|
|||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_list_jobs():
|
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
|
# Create source and job
|
||||||
source_resp = await ac.post("/api/sources/", json={
|
source_resp = await ac.post("/api/sources/", json={
|
||||||
"name": "Test Source",
|
"name": "Test Source",
|
||||||
@@ -56,7 +56,7 @@ async def test_list_jobs():
|
|||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_get_job():
|
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={
|
source_resp = await ac.post("/api/sources/", json={
|
||||||
"name": "Test Source",
|
"name": "Test Source",
|
||||||
"type": "local",
|
"type": "local",
|
||||||
@@ -80,7 +80,7 @@ async def test_get_job():
|
|||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_delete_job():
|
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={
|
source_resp = await ac.post("/api/sources/", json={
|
||||||
"name": "Test Source",
|
"name": "Test Source",
|
||||||
"type": "local",
|
"type": "local",
|
||||||
@@ -102,13 +102,13 @@ async def test_delete_job():
|
|||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
# Verify deletion
|
# 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}")
|
get_resp = await ac.get(f"/api/jobs/{job_id}")
|
||||||
assert get_resp.status_code == 404
|
assert get_resp.status_code == 404
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_run_job():
|
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
|
# Create source
|
||||||
source_resp = await ac.post("/api/sources/", json={
|
source_resp = await ac.post("/api/sources/", json={
|
||||||
"name": "Test Source",
|
"name": "Test Source",
|
||||||
@@ -134,13 +134,13 @@ async def test_run_job():
|
|||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_run_job_not_found():
|
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")
|
response = await ac.post("/api/jobs/999/run")
|
||||||
assert response.status_code == 404
|
assert response.status_code == 404
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_update_job():
|
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
|
# Create source
|
||||||
source_resp = await ac.post("/api/sources/", json={
|
source_resp = await ac.post("/api/sources/", json={
|
||||||
"name": "Test Source",
|
"name": "Test Source",
|
||||||
@@ -169,7 +169,7 @@ async def test_update_job():
|
|||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_create_schedule():
|
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
|
# Create source
|
||||||
source_resp = await ac.post("/api/sources/", json={
|
source_resp = await ac.post("/api/sources/", json={
|
||||||
"name": "Test Source",
|
"name": "Test Source",
|
||||||
@@ -202,18 +202,18 @@ async def test_create_schedule():
|
|||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_get_job_not_found():
|
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")
|
response = await ac.get("/api/jobs/99999")
|
||||||
assert response.status_code == 404
|
assert response.status_code == 404
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_update_job_not_found():
|
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"})
|
response = await ac.put("/api/jobs/99999", json={"name": "Test"})
|
||||||
assert response.status_code == 404
|
assert response.status_code == 404
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_delete_job_not_found():
|
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")
|
response = await ac.delete("/api/jobs/99999")
|
||||||
assert response.status_code == 404
|
assert response.status_code == 404
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from httpx import AsyncClient
|
from httpx import AsyncClient, ASGITransport
|
||||||
from app.main import app
|
from app.main import app
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_create_source():
|
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={
|
response = await ac.post("/api/sources/", json={
|
||||||
"name": "Test Source",
|
"name": "Test Source",
|
||||||
"type": "local",
|
"type": "local",
|
||||||
@@ -18,7 +18,7 @@ async def test_create_source():
|
|||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_list_sources():
|
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
|
# Create source first
|
||||||
await ac.post("/api/sources/", json={
|
await ac.post("/api/sources/", json={
|
||||||
"name": "Test Source",
|
"name": "Test Source",
|
||||||
@@ -33,7 +33,7 @@ async def test_list_sources():
|
|||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_get_source():
|
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={
|
create_resp = await ac.post("/api/sources/", json={
|
||||||
"name": "Test Source",
|
"name": "Test Source",
|
||||||
"type": "local",
|
"type": "local",
|
||||||
@@ -47,7 +47,7 @@ async def test_get_source():
|
|||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_delete_source():
|
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={
|
create_resp = await ac.post("/api/sources/", json={
|
||||||
"name": "Delete Me",
|
"name": "Delete Me",
|
||||||
"type": "local",
|
"type": "local",
|
||||||
@@ -59,13 +59,13 @@ async def test_delete_source():
|
|||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
# Verify deletion
|
# 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}")
|
get_resp = await ac.get(f"/api/sources/{source_id}")
|
||||||
assert get_resp.status_code == 404
|
assert get_resp.status_code == 404
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_update_source():
|
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={
|
create_resp = await ac.post("/api/sources/", json={
|
||||||
"name": "Original Name",
|
"name": "Original Name",
|
||||||
"type": "local",
|
"type": "local",
|
||||||
@@ -82,18 +82,18 @@ async def test_update_source():
|
|||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_get_source_not_found():
|
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")
|
response = await ac.get("/api/sources/99999")
|
||||||
assert response.status_code == 404
|
assert response.status_code == 404
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_update_source_not_found():
|
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"})
|
response = await ac.put("/api/sources/99999", json={"name": "Test"})
|
||||||
assert response.status_code == 404
|
assert response.status_code == 404
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_delete_source_not_found():
|
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")
|
response = await ac.delete("/api/sources/99999")
|
||||||
assert response.status_code == 404
|
assert response.status_code == 404
|
||||||
|
|||||||
Reference in New Issue
Block a user