Files
PRISM/backend/tests/test_api_factors.py
boris.waaubandClaude Opus 5.5 22bddcf202 Initialise le back-end Django de la Phase 1
- Modèles : utilisateur (e-mail comme identifiant, statut, n° INAMI),
  traces d'e-mails, facteurs, sources, classes de seuil, contenus
  validés avec historique et références numérotées ; rôles par groupes.
- Modules de facteur appelés en parallèle (httpx) : bruit et inondation
  Wallonie via WalOnMap, qualité de l'air via irCELine ; Bruxelles et
  radon signalés indisponibles en attendant leurs sources.
- Géocodage Nominatim (Best Address à raccorder), région déduite du
  code postal ; aucune adresse stockée ni journalisée.
- API REST : POST /api/v1/assessments/, GET /api/v1/factors/,
  documentation OpenAPI sur /api/docs/.
- Docker Compose (Python 3.12, PostgreSQL 17, Mailpit), uv, ruff,
  pytest (66 tests, HTTP mocké avec respx), CLAUDE.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 10:40:58 +01:00

78 lines
2.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""GET /api/v1/factors/"""
import pytest
from rest_framework.test import APIClient
from factors.models import Factor, ThresholdClass
URL = "/api/v1/factors/"
pytestmark = pytest.mark.django_db
def test_anonymous_is_rejected(seeded):
assert APIClient().get(URL).status_code == 403
def test_post_is_not_allowed(api, seeded):
assert api.post(URL, {}, format="json").status_code == 405
def test_empty_without_factors(api):
response = api.get(URL)
assert response.status_code == 200
assert response.json() == []
def test_lists_active_factors_in_order(api, seeded):
Factor.objects.filter(code="radon").update(active=False)
Factor.objects.filter(code="flood").update(order=0)
body = api.get(URL).json()
assert [(f["code"], f["label"]) for f in body] == [
("flood", "Risque d'inondation"),
("noise", "Pollution sonore"),
("air", "Qualité de l'air"),
]
def test_legends_per_region(api, seeded):
noise = Factor.objects.get(code="noise")
ThresholdClass.objects.create(
factor=noise, region="WAL", source_value="65-69", label="65–69 dB", level=3, color="orange"
)
ThresholdClass.objects.create(
factor=noise, region="WAL", source_value="aucune", level=1, color="green"
)
ThresholdClass.objects.create(
factor=noise, region="BXL", source_value="Classe 8", level=4, color="red"
)
legends = {f["code"]: f["legends"] for f in api.get(URL).json()}["noise"]
assert set(legends) == {"WAL", "BXL"}
assert legends["WAL"]["source"] == "WalOnMap"
assert legends["WAL"]["scale"] == [
{"level": 1, "color": "green", "label": "aucune"},
{"level": 3, "color": "orange", "label": "65–69 dB"},
]
assert legends["BXL"]["source"] == "Bruxelles Environnement"
assert legends["BXL"]["scale"] == [{"level": 4, "color": "red", "label": "Classe 8"}]
def test_national_classes_appear_in_both_regions(api, seeded):
ThresholdClass.objects.create(
factor=Factor.objects.get(code="air"), region="BE", source_value="0", level=1, color="green"
)
legends = {f["code"]: f["legends"] for f in api.get(URL).json()}["air"]
assert legends["WAL"]["source"] == legends["BXL"]["source"] == "irCELine"
assert (
legends["WAL"]["scale"]
== legends["BXL"]["scale"]
== [{"level": 1, "color": "green", "label": "0"}]
)
def test_does_not_call_external_services(api, seeded, mock_http):
api.get(URL)
assert not mock_http.calls