argos/tests/test_api.py
Alexis Métaireau e2d8066746 Update API endpoints and test cases
- Adjusted test cases in test_api.py
  - Updated test_db fixture to delete the database between each test.
  - Consolidated test cases for tasks retrieval and results
2023-10-12 00:16:06 +02:00

41 lines
1.2 KiB
Python

import pytest
from fastapi.testclient import TestClient
from argos.schemas import AgentResult
from argos.server import app, models
@pytest.fixture()
def test_db():
models.Base.metadata.create_all(bind=app.state.engine)
yield app.state.db
models.Base.metadata.drop_all(bind=app.state.engine)
def test_read_tasks_requires_auth():
with TestClient(app) as client:
response = client.get("/tasks")
assert response.status_code == 403
def test_tasks_retrieval_and_results(test_db):
with TestClient(app) as client:
token = app.state.config.service.secrets[0]
client.headers = {"Authorization": f"Bearer {token}"}
response = client.get("/tasks")
assert response.status_code == 200
tasks = response.json()
assert len(tasks) == 2
results = []
for task in tasks:
results.append(
AgentResult(task_id=task["id"], status="success", context={})
)
data = [r.model_dump() for r in results]
response = client.post("/results", json=data)
assert response.status_code == 201
assert test_db.query(models.Result).count() == 2