"""Plant registry CRUD tests — covers the full create/read/update/delete cycle.""" import pytest def test_create_plant_minimal(client): resp = client.post("/api/v1/plants", json={"common_name": "Tomato"}) assert resp.status_code == 201 data = resp.json() assert data["common_name"] == "Tomato" assert data["id"] is not None assert data["variety"] is None def test_create_plant_with_all_fields(client): resp = client.post("/api/v1/plants", json={ "common_name": "Tomato", "latin_binomial": "Solanum lycopersicum", "variety": "Brandywine", "planting_date": "2026-05-01", "days_to_maturity": 85, "notes": "Heirloom, beefsteak type", }) assert resp.status_code == 201 data = resp.json() assert data["latin_binomial"] == "Solanum lycopersicum" assert data["variety"] == "Brandywine" assert data["days_to_maturity"] == 85 def test_list_plants_empty(client): resp = client.get("/api/v1/plants") assert resp.status_code == 200 assert resp.json() == [] def test_list_plants_returns_created(client): client.post("/api/v1/plants", json={"common_name": "Basil"}) client.post("/api/v1/plants", json={"common_name": "Rosemary"}) resp = client.get("/api/v1/plants") assert resp.status_code == 200 names = {p["common_name"] for p in resp.json()} assert "Basil" in names assert "Rosemary" in names def test_get_plant_by_id(client): create_resp = client.post("/api/v1/plants", json={"common_name": "Courgette"}) plant_id = create_resp.json()["id"] resp = client.get(f"/api/v1/plants/{plant_id}") assert resp.status_code == 200 assert resp.json()["common_name"] == "Courgette" def test_get_plant_not_found(client): resp = client.get("/api/v1/plants/9999") assert resp.status_code == 404 def test_update_plant_partial(client): create_resp = client.post("/api/v1/plants", json={"common_name": "Pepper"}) plant_id = create_resp.json()["id"] resp = client.patch(f"/api/v1/plants/{plant_id}", json={"variety": "Padron"}) assert resp.status_code == 200 data = resp.json() assert data["common_name"] == "Pepper" assert data["variety"] == "Padron" def test_delete_plant(client): create_resp = client.post("/api/v1/plants", json={"common_name": "Mint"}) plant_id = create_resp.json()["id"] resp = client.delete(f"/api/v1/plants/{plant_id}") assert resp.status_code == 204 assert client.get(f"/api/v1/plants/{plant_id}").status_code == 404 def test_plant_with_location(client): loc = client.post("/api/v1/locations", json={"name": "Raised Bed 1"}).json() resp = client.post("/api/v1/plants", json={ "common_name": "Carrot", "location_id": loc["id"], }) assert resp.status_code == 201 assert resp.json()["location_id"] == loc["id"] filtered = client.get(f"/api/v1/plants?location_id={loc['id']}").json() assert len(filtered) == 1 assert filtered[0]["common_name"] == "Carrot"