File size: 3,853 Bytes
2985cd5 68c479d 2985cd5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | import pytest
from fastapi.testclient import TestClient
from src.api.main import app
@pytest.fixture
def client():
"""Create a test client for the API."""
with TestClient(app) as test_client:
yield test_client
def test_get_tasks_contract(client):
"""Test the contract for GET /api/tasks endpoint."""
# This would normally require authentication
headers = {"Authorization": "Bearer fake-token"}
response = client.get("/api/tasks/", headers=headers)
# The response should follow the contract format
if response.status_code != 401: # If not unauthorized (due to missing auth)
# If authorized, should return the expected format
assert "success" in response.json()
data = response.json()
assert isinstance(data["success"], bool)
if data["success"]:
assert "data" in data
assert "tasks" in data["data"]
assert isinstance(data["data"]["tasks"], list)
def test_post_tasks_contract(client):
"""Test the contract for POST /api/tasks endpoint."""
headers = {"Authorization": "Bearer fake-token"}
task_data = {
"title": "Test Task",
"description": "Test Description",
"completed": False
}
response = client.post("/api/tasks/", json=task_data, headers=headers)
# The response should follow the contract format
if response.status_code not in [401, 422]: # If not unauthorized or validation error
assert "success" in response.json()
data = response.json()
assert isinstance(data["success"], bool)
if data["success"]:
assert "data" in data
assert "id" in data["data"] or "message" in data["data"]
def test_get_specific_task_contract(client):
"""Test the contract for GET /api/tasks/{task_id} endpoint."""
headers = {"Authorization": "Bearer fake-token"}
response = client.get("/api/tasks/1", headers=headers)
# The response should follow the contract format
if response.status_code not in [401, 404]: # If not unauthorized or not found
assert "success" in response.json()
data = response.json()
assert isinstance(data["success"], bool)
def test_put_task_contract(client):
"""Test the contract for PUT /api/tasks/{task_id} endpoint."""
headers = {"Authorization": "Bearer fake-token"}
update_data = {
"title": "Updated Task",
"description": "Updated Description",
"completed": True
}
response = client.put("/api/tasks/1", json=update_data, headers=headers)
# The response should follow the contract format
if response.status_code not in [401, 404, 422]: # If not unauthorized, not found, or validation error
assert "success" in response.json()
data = response.json()
assert isinstance(data["success"], bool)
def test_delete_task_contract(client):
"""Test the contract for DELETE /api/tasks/{task_id} endpoint."""
headers = {"Authorization": "Bearer fake-token"}
response = client.delete("/api/tasks/1", headers=headers)
# The response should follow the contract format
if response.status_code not in [401, 404]: # If not unauthorized or not found
assert "success" in response.json()
data = response.json()
assert isinstance(data["success"], bool)
def test_patch_toggle_task_contract(client):
"""Test the contract for PATCH /api/tasks/{task_id}/toggle endpoint."""
headers = {"Authorization": "Bearer fake-token"}
response = client.patch("/api/tasks/1/toggle", headers=headers)
# The response should follow the contract format
if response.status_code not in [401, 404]: # If not unauthorized or not found
assert "success" in response.json()
data = response.json()
assert isinstance(data["success"], bool) |