Bestehende Lagerorte lassen sich jetzt nachträglich einem anderen Elternort zuordnen oder auf die oberste Ebene holen. - Backend: LocationUpdate um parent_id (Name jetzt optional); update_location haengt um, mit Schutz gegen Ringe (weder auf sich selbst noch auf einen eigenen Unterort) und Existenzpruefung des Ziels. 6 Tests. - Web: Lagerorte-Seite bekommt je Ort Bearbeiten (Name + Elternort ueber den CategorySelect-Baum, eigene Unterorte ausgeschlossen); api.updateLocation. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
72 lines
2.2 KiB
Python
72 lines
2.2 KiB
Python
"""Lagerorte: Umbenennen und Umhängen (mit Schutz vor Ringen)."""
|
|
|
|
import pytest
|
|
from fastapi import HTTPException
|
|
|
|
from app.models import Location, Role, User
|
|
from app.routers.locations import update_location
|
|
from app.schemas import LocationUpdate
|
|
|
|
|
|
@pytest.fixture()
|
|
def admin(db):
|
|
person = User(username="admin", password_hash="x", role=Role.admin)
|
|
db.add(person)
|
|
db.commit()
|
|
db.refresh(person)
|
|
return person
|
|
|
|
|
|
def _orte(db):
|
|
"""Keller → Regal → Fach."""
|
|
keller = Location(name="Keller")
|
|
db.add(keller)
|
|
db.flush()
|
|
regal = Location(name="Regal", parent_id=keller.id)
|
|
db.add(regal)
|
|
db.flush()
|
|
fach = Location(name="Fach", parent_id=regal.id)
|
|
db.add(fach)
|
|
db.commit()
|
|
return keller, regal, fach
|
|
|
|
|
|
def test_umbenennen_ohne_parent_bleibt_hierarchie(db, admin):
|
|
keller, regal, _ = _orte(db)
|
|
out = update_location(regal.id, LocationUpdate(name="Regal links"), db=db, _=admin)
|
|
assert out.name == "Regal links"
|
|
assert out.parent_id == keller.id # Umhängen war nicht gemeint
|
|
|
|
|
|
def test_umhaengen_setzt_neuen_parent(db, admin):
|
|
keller, _, fach = _orte(db)
|
|
out = update_location(fach.id, LocationUpdate(parent_id=keller.id), db=db, _=admin)
|
|
assert out.parent_id == keller.id
|
|
|
|
|
|
def test_umhaengen_auf_oberste_ebene(db, admin):
|
|
_, regal, _ = _orte(db)
|
|
out = update_location(regal.id, LocationUpdate(parent_id=None), db=db, _=admin)
|
|
assert out.parent_id is None
|
|
|
|
|
|
def test_umhaengen_auf_sich_selbst_wird_abgelehnt(db, admin):
|
|
keller, _, _ = _orte(db)
|
|
with pytest.raises(HTTPException) as ex:
|
|
update_location(keller.id, LocationUpdate(parent_id=keller.id), db=db, _=admin)
|
|
assert ex.value.status_code == 400
|
|
|
|
|
|
def test_umhaengen_in_eigenen_unterort_wird_abgelehnt(db, admin):
|
|
keller, _, fach = _orte(db)
|
|
with pytest.raises(HTTPException) as ex:
|
|
update_location(keller.id, LocationUpdate(parent_id=fach.id), db=db, _=admin)
|
|
assert ex.value.status_code == 400
|
|
|
|
|
|
def test_umhaengen_auf_unbekannten_ort_ist_404(db, admin):
|
|
_, _, fach = _orte(db)
|
|
with pytest.raises(HTTPException) as ex:
|
|
update_location(fach.id, LocationUpdate(parent_id=99999), db=db, _=admin)
|
|
assert ex.value.status_code == 404
|