feat(backend): CRUD jardins + tests
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
31
backend/tests/conftest.py
Normal file
31
backend/tests/conftest.py
Normal file
@@ -0,0 +1,31 @@
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from sqlmodel import SQLModel, create_engine, Session
|
||||
from sqlmodel.pool import StaticPool
|
||||
|
||||
import app.models # noqa — force l'enregistrement des modèles
|
||||
from app.main import app
|
||||
from app.database import get_session
|
||||
|
||||
|
||||
@pytest.fixture(name="session")
|
||||
def session_fixture():
|
||||
engine = create_engine(
|
||||
"sqlite://",
|
||||
connect_args={"check_same_thread": False},
|
||||
poolclass=StaticPool,
|
||||
)
|
||||
SQLModel.metadata.create_all(engine)
|
||||
with Session(engine) as session:
|
||||
yield session
|
||||
|
||||
|
||||
@pytest.fixture(name="client")
|
||||
def client_fixture(session: Session):
|
||||
def get_session_override():
|
||||
yield session
|
||||
|
||||
app.dependency_overrides[get_session] = get_session_override
|
||||
client = TestClient(app)
|
||||
yield client
|
||||
app.dependency_overrides.clear()
|
||||
54
backend/tests/test_gardens.py
Normal file
54
backend/tests/test_gardens.py
Normal file
@@ -0,0 +1,54 @@
|
||||
def test_health(client):
|
||||
r = client.get("/api/health")
|
||||
assert r.status_code == 200
|
||||
|
||||
|
||||
def test_create_garden(client):
|
||||
r = client.post("/api/gardens", json={"nom": "Mon potager", "type": "plein_air"})
|
||||
assert r.status_code == 201
|
||||
data = r.json()
|
||||
assert data["nom"] == "Mon potager"
|
||||
assert data["id"] is not None
|
||||
|
||||
|
||||
def test_list_gardens(client):
|
||||
client.post("/api/gardens", json={"nom": "Jardin 1", "type": "plein_air"})
|
||||
client.post("/api/gardens", json={"nom": "Jardin 2", "type": "serre"})
|
||||
r = client.get("/api/gardens")
|
||||
assert r.status_code == 200
|
||||
assert len(r.json()) == 2
|
||||
|
||||
|
||||
def test_get_garden(client):
|
||||
r = client.post("/api/gardens", json={"nom": "Potager", "type": "plein_air"})
|
||||
id = r.json()["id"]
|
||||
r2 = client.get(f"/api/gardens/{id}")
|
||||
assert r2.status_code == 200
|
||||
assert r2.json()["nom"] == "Potager"
|
||||
|
||||
|
||||
def test_update_garden(client):
|
||||
r = client.post("/api/gardens", json={"nom": "Vieux nom", "type": "plein_air"})
|
||||
id = r.json()["id"]
|
||||
r2 = client.put(f"/api/gardens/{id}", json={"nom": "Nouveau nom", "type": "serre"})
|
||||
assert r2.status_code == 200
|
||||
assert r2.json()["nom"] == "Nouveau nom"
|
||||
|
||||
|
||||
def test_delete_garden(client):
|
||||
r = client.post("/api/gardens", json={"nom": "À supprimer", "type": "plein_air"})
|
||||
id = r.json()["id"]
|
||||
r2 = client.delete(f"/api/gardens/{id}")
|
||||
assert r2.status_code == 204
|
||||
r3 = client.get(f"/api/gardens/{id}")
|
||||
assert r3.status_code == 404
|
||||
|
||||
|
||||
def test_create_measurement(client):
|
||||
r = client.post("/api/gardens", json={"nom": "Potager", "type": "plein_air"})
|
||||
id = r.json()["id"]
|
||||
r2 = client.post(
|
||||
f"/api/gardens/{id}/measurements",
|
||||
json={"temp_air": 22.5, "humidite_air": 60.0},
|
||||
)
|
||||
assert r2.status_code == 201
|
||||
Reference in New Issue
Block a user