Spaces:
Runtime error
Runtime error
File size: 9,500 Bytes
9cf9a2a 7435134 9cf9a2a 9697198 9cf9a2a 9697198 9cf9a2a 9697198 9cf9a2a 9697198 9cf9a2a 9697198 9cf9a2a 9697198 9cf9a2a 9697198 9cf9a2a 9697198 9cf9a2a 9697198 9cf9a2a 9697198 9cf9a2a 9697198 9cf9a2a da2ad52 9cf9a2a 9697198 9cf9a2a 9697198 9cf9a2a da2ad52 9cf9a2a 9697198 9cf9a2a 9697198 9cf9a2a da2ad52 9cf9a2a 9697198 9cf9a2a 9697198 9cf9a2a da2ad52 9cf9a2a da2ad52 9697198 9cf9a2a da2ad52 9cf9a2a da2ad52 9697198 9cf9a2a 9697198 9cf9a2a da2ad52 9cf9a2a 9697198 9cf9a2a da2ad52 9cf9a2a da2ad52 9cf9a2a 9697198 9cf9a2a da2ad52 9cf9a2a 9697198 9cf9a2a 9697198 9cf9a2a da2ad52 9cf9a2a da2ad52 9697198 9cf9a2a 9697198 9cf9a2a da2ad52 9cf9a2a | 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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | """
Tests API pour les endpoints Laws.
Ces tests vérifient:
- GET /laws (liste avec filtres)
- GET /laws/{id} (détail)
- POST /admin/laws (création)
- PUT /admin/laws/{id} (mise à jour)
- DELETE /admin/laws/{id} (suppression)
Usage:
pytest backend/tests/test_api/test_laws_api.py -v
"""
from datetime import date
import pytest
from httpx import AsyncClient
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.law import Law
@pytest.fixture
async def sample_law(db_session: AsyncSession):
"""Fixture: Create a sample law for testing."""
law = Law(
reference="LOI-2024-001",
title="Loi sur les sociétés commerciales",
type="loi",
content="Article 1. La présente loi régit les sociétés commerciales au Cameroun.",
language="fr",
status="published",
publication_date=date(2024, 1, 1)
)
db_session.add(law)
await db_session.commit()
await db_session.refresh(law)
return law
# ==================== GET /laws TESTS ====================
class TestGetLaws:
"""Tests pour GET /api/v1/laws."""
@pytest.mark.asyncio
async def test_get_laws_success(self, client: AsyncClient, sample_law):
"""Test récupération liste des lois."""
response = await client.get("/api/v1/laws/")
assert response.status_code == 200
laws = response.json()
assert isinstance(laws, list)
assert len(laws) >= 1
@pytest.mark.asyncio
async def test_get_laws_filter_language(self, client: AsyncClient, sample_law):
"""Test filtre par langue."""
response = await client.get("/api/v1/laws/?language=fr")
assert response.status_code == 200
laws = response.json()
assert all(law["language"] == "fr" for law in laws)
@pytest.mark.asyncio
async def test_get_laws_filter_status(self, client: AsyncClient, sample_law):
"""Test filtre par statut."""
response = await client.get("/api/v1/laws/?law_status=published")
assert response.status_code == 200
laws = response.json()
assert all(law["status"] == "published" for law in laws)
@pytest.mark.asyncio
async def test_get_laws_pagination(self, client: AsyncClient, sample_law):
"""Test pagination."""
response = await client.get("/api/v1/laws/?skip=0&limit=10")
assert response.status_code == 200
laws = response.json()
assert len(laws) <= 10
@pytest.mark.asyncio
async def test_get_laws_empty(self, client: AsyncClient):
"""Test résultat vide."""
response = await client.get("/api/v1/laws/?language=xx")
assert response.status_code == 200
laws = response.json()
assert laws == []
# ==================== GET /laws/{id} TESTS ====================
class TestGetLaw:
"""Tests pour GET /api/v1/laws/{id}."""
@pytest.mark.asyncio
async def test_get_law_success(self, client: AsyncClient, sample_law):
"""Test récupération détail loi."""
response = await client.get(f"/api/v1/laws/{sample_law.id}")
assert response.status_code == 200
law = response.json()
assert law["id"] == sample_law.id
assert law["reference"] == sample_law.reference
assert law["title"] == sample_law.title
@pytest.mark.asyncio
async def test_get_law_not_found(self, client: AsyncClient):
"""Test loi non trouvée."""
response = await client.get("/api/v1/laws/99999")
assert response.status_code == 404
assert "not found" in response.json()["detail"].lower()
@pytest.mark.asyncio
async def test_get_law_with_articles(self, client: AsyncClient, sample_law):
"""Test loi avec articles."""
response = await client.get(f"/api/v1/laws/{sample_law.id}")
assert response.status_code == 200
law = response.json()
# LawResponse expose article_count, pas la liste des articles :
# la charger sur chaque loi d'une liste serait un N+1.
assert "article_count" in law
# ==================== POST /admin/laws TESTS ====================
class TestCreateLaw:
"""Tests pour POST /api/v1/laws/admin."""
@pytest.mark.asyncio
async def test_create_law_success(self, admin_client: AsyncClient):
"""Test création loi."""
new_law = {
"reference": "LOI-2024-002",
"title": "Loi test",
"type": "loi",
"content": "Contenu de la loi test.",
"language": "fr",
"status": "published"
}
response = await admin_client.post("/api/v1/laws/admin", json=new_law)
assert response.status_code == 201
law = response.json()
assert law["reference"] == new_law["reference"]
assert law["title"] == new_law["title"]
assert "id" in law
@pytest.mark.asyncio
async def test_create_law_duplicate_reference(self, admin_client: AsyncClient, sample_law):
"""Test création avec référence existante."""
duplicate_law = {
"reference": sample_law.reference,
"title": "Loi duplicate",
"type": "loi",
"content": "Contenu de la loi dupliquee.",
"language": "fr"
}
response = await admin_client.post("/api/v1/laws/admin", json=duplicate_law)
assert response.status_code == 409
assert "already exists" in response.json()["detail"].lower()
@pytest.mark.asyncio
async def test_create_law_validation_error(self, admin_client: AsyncClient):
"""Test création avec données invalides."""
invalid_law = {
"reference": "", # Invalid: empty
"title": "Test"
}
response = await admin_client.post("/api/v1/laws/admin", json=invalid_law)
assert response.status_code == 422
# ==================== PUT /admin/laws/{id} TESTS ====================
class TestUpdateLaw:
"""Tests pour PUT /api/v1/laws/admin/{id}."""
@pytest.mark.asyncio
async def test_update_law_success(self, admin_client: AsyncClient, sample_law):
"""Test mise à jour loi."""
update_data = {
"title": "Titre mis à jour",
"status": "archived"
}
response = await admin_client.put(
f"/api/v1/laws/admin/{sample_law.id}",
json=update_data
)
assert response.status_code == 200
law = response.json()
assert law["title"] == update_data["title"]
assert law["status"] == update_data["status"]
@pytest.mark.asyncio
async def test_update_law_not_found(self, admin_client: AsyncClient):
"""Test mise à jour loi non trouvée."""
response = await admin_client.put(
"/api/v1/laws/admin/99999",
json={"title": "Test"}
)
assert response.status_code == 404
# ==================== DELETE /admin/laws/{id} TESTS ====================
class TestDeleteLaw:
"""Tests pour DELETE /api/v1/laws/admin/{id}."""
@pytest.mark.asyncio
async def test_delete_law_success(self, admin_client: AsyncClient, sample_law):
"""Test suppression loi."""
response = await admin_client.delete(f"/api/v1/laws/admin/{sample_law.id}")
assert response.status_code == 204
# Vérifier que la loi est supprimée
get_response = await admin_client.get(f"/api/v1/laws/{sample_law.id}")
assert get_response.status_code == 404
@pytest.mark.asyncio
async def test_delete_law_not_found(self, admin_client: AsyncClient):
"""Test suppression loi non trouvée."""
response = await admin_client.delete("/api/v1/laws/admin/99999")
assert response.status_code == 404
# ==================== INTEGRATION TESTS ====================
class TestLawsIntegration:
"""Tests d'intégration."""
@pytest.mark.asyncio
async def test_full_crud_workflow(self, admin_client: AsyncClient):
"""Test workflow CRUD complet."""
# 1. Create
new_law = {
"reference": "LOI-2024-CRUD",
"title": "Loi CRUD Test",
"type": "loi",
"content": "Contenu test.",
"language": "fr",
"status": "published"
}
create_response = await admin_client.post("/api/v1/laws/admin", json=new_law)
assert create_response.status_code == 201
law_id = create_response.json()["id"]
# 2. Read
read_response = await admin_client.get(f"/api/v1/laws/{law_id}")
assert read_response.status_code == 200
assert read_response.json()["title"] == new_law["title"]
# 3. Update
update_response = await admin_client.put(
f"/api/v1/laws/admin/{law_id}",
json={"title": "Titre modifié"}
)
assert update_response.status_code == 200
assert update_response.json()["title"] == "Titre modifié"
# 4. Delete
delete_response = await admin_client.delete(f"/api/v1/laws/admin/{law_id}")
assert delete_response.status_code == 204
# 5. Verify deletion
verify_response = await admin_client.get(f"/api/v1/laws/{law_id}")
assert verify_response.status_code == 404
|