fix: jobs router session handling, schedule creation, and tests
- Use new session in background task for thread safety - Fix schedule creation to associate with job - Fix schedule endpoint return type - Update tests to use AsyncClient - Add missing tests for list, get, delete, schedule - Add updated_at field to Schedule model
This commit is contained in:
+195
-73
@@ -1,31 +1,27 @@
|
||||
import pytest
|
||||
from httpx import AsyncClient
|
||||
from fastapi.testclient import TestClient
|
||||
from app.main import app
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
with TestClient(app) as c:
|
||||
yield c
|
||||
|
||||
def test_create_job(client):
|
||||
# Create a source first (job requires source_id)
|
||||
source_resp = 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 = client.post("/api/jobs/", json={
|
||||
"name": "Test Job",
|
||||
"source_id": source_id,
|
||||
"strategy": "full",
|
||||
"destination_path": "/tmp/backups",
|
||||
"exclude_patterns": [],
|
||||
"enabled": True
|
||||
})
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_job():
|
||||
async with AsyncClient(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",
|
||||
"type": "local",
|
||||
"config": {"path": "/tmp/test"}
|
||||
})
|
||||
assert source_resp.status_code == 200
|
||||
source_id = source_resp.json()["id"]
|
||||
|
||||
response = await ac.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"
|
||||
@@ -33,65 +29,191 @@ def test_create_job(client):
|
||||
assert data["strategy"] == "full"
|
||||
assert "id" in data
|
||||
|
||||
def test_run_job(client):
|
||||
# Create source
|
||||
source_resp = client.post("/api/sources/", json={
|
||||
"name": "Test Source",
|
||||
"type": "local",
|
||||
"config": {"path": "/tmp/test"}
|
||||
})
|
||||
source_id = source_resp.json()["id"]
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_jobs():
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
# Create source and job
|
||||
source_resp = await ac.post("/api/sources/", json={
|
||||
"name": "Test Source",
|
||||
"type": "local",
|
||||
"config": {"path": "/tmp/test"}
|
||||
})
|
||||
source_id = source_resp.json()["id"]
|
||||
|
||||
await ac.post("/api/jobs/", json={
|
||||
"name": "Test Job",
|
||||
"source_id": source_id,
|
||||
"strategy": "full",
|
||||
"destination_path": "/tmp/backups",
|
||||
"exclude_patterns": [],
|
||||
"enabled": True
|
||||
})
|
||||
|
||||
response = await ac.get("/api/jobs/")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert len(data) >= 1
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_job():
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
source_resp = await ac.post("/api/sources/", json={
|
||||
"name": "Test Source",
|
||||
"type": "local",
|
||||
"config": {"path": "/tmp/test"}
|
||||
})
|
||||
source_id = source_resp.json()["id"]
|
||||
|
||||
job_resp = await ac.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 ac.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():
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
source_resp = await ac.post("/api/sources/", json={
|
||||
"name": "Test Source",
|
||||
"type": "local",
|
||||
"config": {"path": "/tmp/test"}
|
||||
})
|
||||
source_id = source_resp.json()["id"]
|
||||
|
||||
job_resp = await ac.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 ac.delete(f"/api/jobs/{job_id}")
|
||||
assert response.status_code == 200
|
||||
|
||||
# Create job
|
||||
job_resp = 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 = client.post(f"/api/jobs/{job_id}/run")
|
||||
# Verify deletion
|
||||
async with AsyncClient(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:
|
||||
# Create source
|
||||
source_resp = await ac.post("/api/sources/", json={
|
||||
"name": "Test Source",
|
||||
"type": "local",
|
||||
"config": {"path": "/tmp/test"}
|
||||
})
|
||||
source_id = source_resp.json()["id"]
|
||||
|
||||
# Create job
|
||||
job_resp = await ac.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 ac.post(f"/api/jobs/{job_id}/run")
|
||||
assert response.status_code == 200
|
||||
assert response.json()["message"] == "Job execution started"
|
||||
|
||||
def test_update_job(client):
|
||||
# Create source
|
||||
source_resp = client.post("/api/sources/", json={
|
||||
"name": "Test Source",
|
||||
"type": "local",
|
||||
"config": {"path": "/tmp/test"}
|
||||
})
|
||||
source_id = source_resp.json()["id"]
|
||||
|
||||
# Create job
|
||||
job_resp = 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 = client.put(f"/api/jobs/{job_id}", json={
|
||||
"name": "Updated Name"
|
||||
})
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_job_not_found():
|
||||
async with AsyncClient(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:
|
||||
# Create source
|
||||
source_resp = await ac.post("/api/sources/", json={
|
||||
"name": "Test Source",
|
||||
"type": "local",
|
||||
"config": {"path": "/tmp/test"}
|
||||
})
|
||||
source_id = source_resp.json()["id"]
|
||||
|
||||
# Create job
|
||||
job_resp = await ac.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 ac.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
|
||||
|
||||
def test_get_job_not_found(client):
|
||||
response = client.get("/api/jobs/99999")
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_schedule():
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
# Create source
|
||||
source_resp = await ac.post("/api/sources/", json={
|
||||
"name": "Test Source",
|
||||
"type": "local",
|
||||
"config": {"path": "/tmp/test"}
|
||||
})
|
||||
source_id = source_resp.json()["id"]
|
||||
|
||||
# Create job
|
||||
job_resp = await ac.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 ac.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():
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
response = await ac.get("/api/jobs/99999")
|
||||
assert response.status_code == 404
|
||||
|
||||
def test_update_job_not_found(client):
|
||||
response = client.put("/api/jobs/99999", json={"name": "Test"})
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_job_not_found():
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
response = await ac.put("/api/jobs/99999", json={"name": "Test"})
|
||||
assert response.status_code == 404
|
||||
|
||||
def test_delete_job_not_found(client):
|
||||
response = client.delete("/api/jobs/99999")
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_job_not_found():
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
response = await ac.delete("/api/jobs/99999")
|
||||
assert response.status_code == 404
|
||||
|
||||
Reference in New Issue
Block a user