19987d68a0
Replace all AsyncClient instantiations in test_sources.py and test_jobs.py with the shared fixture from conftest.py. This ensures proper test database isolation via dependency overrides. - Remove imports of httpx AsyncClient, ASGITransport, and app - Add client fixture parameter to all test functions - Remove async with blocks around HTTP requests - test_engine.py already uses db fixture correctly, no changes needed
206 lines
6.0 KiB
Python
206 lines
6.0 KiB
Python
import pytest
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_job(client):
|
|
# Create a source first (job requires source_id)
|
|
source_resp = await client.post("/api/sources/", json={
|
|
"name": "Test Source",
|
|
"type": "local",
|
|
"config": {"path": "/tmp/test"}
|
|
})
|
|
assert source_resp.status_code == 200
|
|
source_id = source_resp.json()["id"]
|
|
|
|
response = await client.post("/api/jobs/", json={
|
|
"name": "Test Job",
|
|
"source_id": source_id,
|
|
"strategy": "full",
|
|
"destination_path": "/tmp/backups",
|
|
"exclude_patterns": [],
|
|
"enabled": True
|
|
})
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["name"] == "Test Job"
|
|
assert data["source_id"] == source_id
|
|
assert data["strategy"] == "full"
|
|
assert "id" in data
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_jobs(client):
|
|
# Create source and job
|
|
source_resp = await client.post("/api/sources/", json={
|
|
"name": "Test Source",
|
|
"type": "local",
|
|
"config": {"path": "/tmp/test"}
|
|
})
|
|
source_id = source_resp.json()["id"]
|
|
|
|
await client.post("/api/jobs/", json={
|
|
"name": "Test Job",
|
|
"source_id": source_id,
|
|
"strategy": "full",
|
|
"destination_path": "/tmp/backups",
|
|
"exclude_patterns": [],
|
|
"enabled": True
|
|
})
|
|
|
|
response = await client.get("/api/jobs/")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert len(data) >= 1
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_job(client):
|
|
source_resp = await client.post("/api/sources/", json={
|
|
"name": "Test Source",
|
|
"type": "local",
|
|
"config": {"path": "/tmp/test"}
|
|
})
|
|
source_id = source_resp.json()["id"]
|
|
|
|
job_resp = await client.post("/api/jobs/", json={
|
|
"name": "Test Job",
|
|
"source_id": source_id,
|
|
"strategy": "full",
|
|
"destination_path": "/tmp/backups",
|
|
"exclude_patterns": [],
|
|
"enabled": True
|
|
})
|
|
job_id = job_resp.json()["id"]
|
|
|
|
response = await client.get(f"/api/jobs/{job_id}")
|
|
assert response.status_code == 200
|
|
assert response.json()["id"] == job_id
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_delete_job(client):
|
|
source_resp = await client.post("/api/sources/", json={
|
|
"name": "Test Source",
|
|
"type": "local",
|
|
"config": {"path": "/tmp/test"}
|
|
})
|
|
source_id = source_resp.json()["id"]
|
|
|
|
job_resp = await client.post("/api/jobs/", json={
|
|
"name": "Delete Me",
|
|
"source_id": source_id,
|
|
"strategy": "full",
|
|
"destination_path": "/tmp/backups",
|
|
"exclude_patterns": [],
|
|
"enabled": True
|
|
})
|
|
job_id = job_resp.json()["id"]
|
|
|
|
response = await client.delete(f"/api/jobs/{job_id}")
|
|
assert response.status_code == 200
|
|
|
|
# Verify deletion
|
|
get_resp = await client.get(f"/api/jobs/{job_id}")
|
|
assert get_resp.status_code == 404
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_run_job(client):
|
|
# Create source
|
|
source_resp = await client.post("/api/sources/", json={
|
|
"name": "Test Source",
|
|
"type": "local",
|
|
"config": {"path": "/tmp/test"}
|
|
})
|
|
source_id = source_resp.json()["id"]
|
|
|
|
# Create job
|
|
job_resp = await client.post("/api/jobs/", json={
|
|
"name": "Test Job",
|
|
"source_id": source_id,
|
|
"strategy": "full",
|
|
"destination_path": "/tmp/backups",
|
|
"exclude_patterns": [],
|
|
"enabled": True
|
|
})
|
|
job_id = job_resp.json()["id"]
|
|
|
|
response = await client.post(f"/api/jobs/{job_id}/run")
|
|
assert response.status_code == 200
|
|
assert response.json()["message"] == "Job execution started"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_run_job_not_found(client):
|
|
response = await client.post("/api/jobs/999/run")
|
|
assert response.status_code == 404
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_update_job(client):
|
|
# Create source
|
|
source_resp = await client.post("/api/sources/", json={
|
|
"name": "Test Source",
|
|
"type": "local",
|
|
"config": {"path": "/tmp/test"}
|
|
})
|
|
source_id = source_resp.json()["id"]
|
|
|
|
# Create job
|
|
job_resp = await client.post("/api/jobs/", json={
|
|
"name": "Original Name",
|
|
"source_id": source_id,
|
|
"strategy": "full",
|
|
"destination_path": "/tmp/backups",
|
|
"exclude_patterns": [],
|
|
"enabled": True
|
|
})
|
|
job_id = job_resp.json()["id"]
|
|
|
|
response = await client.put(f"/api/jobs/{job_id}", json={
|
|
"name": "Updated Name"
|
|
})
|
|
assert response.status_code == 200
|
|
assert response.json()["name"] == "Updated Name"
|
|
assert response.json()["strategy"] == "full" # Unchanged
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_schedule(client):
|
|
# Create source
|
|
source_resp = await client.post("/api/sources/", json={
|
|
"name": "Test Source",
|
|
"type": "local",
|
|
"config": {"path": "/tmp/test"}
|
|
})
|
|
source_id = source_resp.json()["id"]
|
|
|
|
# Create job
|
|
job_resp = await client.post("/api/jobs/", json={
|
|
"name": "Test Job",
|
|
"source_id": source_id,
|
|
"strategy": "full",
|
|
"destination_path": "/tmp/backups",
|
|
"exclude_patterns": [],
|
|
"enabled": True
|
|
})
|
|
job_id = job_resp.json()["id"]
|
|
|
|
response = await client.post(f"/api/jobs/{job_id}/schedule", json={
|
|
"job_id": job_id,
|
|
"cron_expression": "0 0 * * *",
|
|
"enabled": True
|
|
})
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["job_id"] == job_id
|
|
assert data["cron_expression"] == "0 0 * * *"
|
|
assert "id" in data
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_job_not_found(client):
|
|
response = await client.get("/api/jobs/99999")
|
|
assert response.status_code == 404
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_update_job_not_found(client):
|
|
response = await client.put("/api/jobs/99999", json={"name": "Test"})
|
|
assert response.status_code == 404
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_delete_job_not_found(client):
|
|
response = await client.delete("/api/jobs/99999")
|
|
assert response.status_code == 404
|