feat: Daily Report two-zone picker with pre-add quantity entry
Split the Daily Report form into a top position picker (system filter, live search) and a bottom added-items list grouped by system, with quantity edit and remove. Switching systems no longer drops the added list; A1->B1->A2 keeps all three. Quantity can be entered in the picker before pressing Добавить and is carried into the selected list; removing a position returns it to the picker with the typed quantity intact. Draft quantities of never-added positions stay out of POST via a submit guard, so they never become Daily Report facts. Server rows are always rendered client-filtered; POST contract and apply_quantities unchanged.
This commit is contained in:
@@ -0,0 +1,294 @@
|
||||
"""DOC-C11: quantity can be typed in the picker before pressing Добавить.
|
||||
|
||||
Contract:
|
||||
- picker quantity input is active before the row is added;
|
||||
- pressing Добавить carries the typed quantity into the selected list;
|
||||
- removing a position returns it to the picker together with its quantity;
|
||||
- quantities typed but never added never reach POST / DailyReportItem;
|
||||
- search and system switching do not reset typed (not yet added) values.
|
||||
"""
|
||||
import re
|
||||
from decimal import Decimal
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.pool import StaticPool
|
||||
|
||||
from app.database.database import Base
|
||||
from app.main import app
|
||||
from app.models.project import Project
|
||||
from app.models.boq import BoqDocument, System, Section, WorkItem
|
||||
from app.models.daily_report import DailyReport, DailyReportItem
|
||||
from app.routers import daily_reports
|
||||
|
||||
|
||||
def _seed(session):
|
||||
project = Project(name="C11 qty")
|
||||
session.add(project)
|
||||
session.flush()
|
||||
document = BoqDocument(project_id=project.id, name="BOQ")
|
||||
session.add(document)
|
||||
session.flush()
|
||||
systems = {}
|
||||
items = {}
|
||||
for code, names in (("A", ["A1", "A2"]), ("B", ["B1", "B2"])):
|
||||
system = System(project_id=project.id, code=code, name=f"Система {code}")
|
||||
session.add(system)
|
||||
session.flush()
|
||||
section = Section(system_id=system.id, name=f"Раздел {code}")
|
||||
session.add(section)
|
||||
session.flush()
|
||||
systems[code] = system
|
||||
items[code] = []
|
||||
for number, name in enumerate(names, start=1):
|
||||
item = WorkItem(
|
||||
project_id=project.id, boq_document_id=document.id,
|
||||
system_id=system.id, section_id=section.id,
|
||||
source_number=f"{code}{number}", name=name, unit="шт",
|
||||
quantity=Decimal("10"), unit_price=Decimal("10"), amount=Decimal("100"),
|
||||
)
|
||||
items[code].append(item)
|
||||
session.add(item)
|
||||
session.flush()
|
||||
return project.id, systems, items
|
||||
|
||||
|
||||
def _client(session_factory):
|
||||
def override_db():
|
||||
session = session_factory()
|
||||
try:
|
||||
yield session
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
app.dependency_overrides[daily_reports.get_db] = override_db
|
||||
return TestClient(app)
|
||||
|
||||
|
||||
def _cleanup():
|
||||
app.dependency_overrides.pop(daily_reports.get_db, None)
|
||||
|
||||
|
||||
def _setup():
|
||||
engine = create_engine(
|
||||
"sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool
|
||||
)
|
||||
Base.metadata.create_all(engine)
|
||||
Session = sessionmaker(bind=engine, autoflush=False, autocommit=False)
|
||||
with Session() as session:
|
||||
project_id, systems, items = _seed(session)
|
||||
session.commit()
|
||||
ids = {code: [i.id for i in lst] for code, lst in items.items()}
|
||||
system_ids = {code: s.id for code, s in systems.items()}
|
||||
return engine, Session, project_id, system_ids, ids
|
||||
|
||||
|
||||
def _picker_row(html, name):
|
||||
"""First <tr> with data-name=name inside the picker body."""
|
||||
pick = html.split('id="picker-body"')[1]
|
||||
m = re.search(rf'<tr[^>]*data-number="{re.escape(name)}"[^>]*>.*?</tr>', pick, re.S)
|
||||
assert m, f"picker row {name} not found"
|
||||
return m.group(0)
|
||||
|
||||
|
||||
def _selected_row(html, name):
|
||||
sel = html.split('id="selected-body"')[1]
|
||||
m = re.search(rf'<tr[^>]*data-number="{re.escape(name)}"[^>]*>.*?</tr>', sel, re.S)
|
||||
assert m, f"selected row {name} not found"
|
||||
return m.group(0)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 1-2. picker quantity field is active before adding
|
||||
# ---------------------------------------------------------------------------
|
||||
def test_picker_quantity_input_is_active():
|
||||
engine, Session, pid, systems, items = _setup()
|
||||
try:
|
||||
client = _client(Session)
|
||||
r = client.get(f"/projects/{pid}/daily-reports/new?system_id={systems['A']}")
|
||||
assert r.status_code == 200
|
||||
row = _picker_row(r.text, "A1")
|
||||
m = re.search(r'<input[^>]*name="quantity_\d+"[^>]*>', row)
|
||||
assert m is not None
|
||||
assert "disabled" not in m.group(0)
|
||||
finally:
|
||||
_cleanup()
|
||||
engine.dispose()
|
||||
|
||||
|
||||
def test_picker_quantity_input_carries_item_id():
|
||||
engine, Session, pid, systems, items = _setup()
|
||||
try:
|
||||
client = _client(Session)
|
||||
r = client.get(f"/projects/{pid}/daily-reports/new?system_id={systems['A']}")
|
||||
row = _picker_row(r.text, "A1")
|
||||
assert f'data-item-id="{items["A"][0]}"' in row
|
||||
assert f'name="quantity_{items["A"][0]}"' in row
|
||||
finally:
|
||||
_cleanup()
|
||||
engine.dispose()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 3. Add carries the typed quantity into the selected list
|
||||
# (UI step; server-side equivalent is POST of that quantity)
|
||||
# ---------------------------------------------------------------------------
|
||||
def test_add_carries_quantity_to_selected():
|
||||
engine, Session, pid, systems, items = _setup()
|
||||
try:
|
||||
client = _client(Session)
|
||||
created = client.post(f"/projects/{pid}/daily-reports", data={
|
||||
"report_date": "2026-03-01",
|
||||
"system_id": str(systems["A"]),
|
||||
f"quantity_{items['A'][0]}": "2",
|
||||
}, follow_redirects=False)
|
||||
assert created.status_code == 303
|
||||
report_id = int(created.headers["location"].rsplit("/", 1)[-1])
|
||||
edit = client.get(f"/projects/{pid}/daily-reports/{report_id}/edit?system_id={systems['A']}")
|
||||
row = _selected_row(edit.text, "A1")
|
||||
assert re.search(rf'name="quantity_{items["A"][0]}" value="2', row)
|
||||
finally:
|
||||
_cleanup()
|
||||
engine.dispose()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 4. Not-added picker quantity must not become a fact (server contract:
|
||||
# only posted quantities create items; Playwright checks the JS guard).
|
||||
# ---------------------------------------------------------------------------
|
||||
def test_only_posted_quantities_create_items():
|
||||
engine, Session, pid, systems, items = _setup()
|
||||
try:
|
||||
client = _client(Session)
|
||||
a1, a2 = items["A"]
|
||||
# user typed qty for A1 in the picker but clicked Добавить only on A2
|
||||
created = client.post(f"/projects/{pid}/daily-reports", data={
|
||||
"report_date": "2026-03-01",
|
||||
"system_id": str(systems["A"]),
|
||||
f"quantity_{a2}": "3",
|
||||
}, follow_redirects=False)
|
||||
assert created.status_code == 303
|
||||
with Session() as session:
|
||||
report = session.query(DailyReport).one()
|
||||
lines = {line.work_item_id: line.completed_quantity for line in report.items}
|
||||
assert lines == {a2: Decimal("3")}
|
||||
assert a1 not in lines
|
||||
finally:
|
||||
_cleanup()
|
||||
engine.dispose()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 5-6. remove / re-add keeps the quantity
|
||||
# ---------------------------------------------------------------------------
|
||||
def test_quantity_survives_remove_and_readd():
|
||||
engine, Session, pid, systems, items = _setup()
|
||||
try:
|
||||
client = _client(Session)
|
||||
a1, a2 = items["A"]
|
||||
created = client.post(f"/projects/{pid}/daily-reports", data={
|
||||
"report_date": "2026-03-01",
|
||||
"system_id": str(systems["A"]),
|
||||
f"quantity_{a1}": "2",
|
||||
f"quantity_{a2}": "3",
|
||||
}, follow_redirects=False)
|
||||
report_id = int(created.headers["location"].rsplit("/", 1)[-1])
|
||||
# remove A1 (quantity omitted)
|
||||
edited = client.post(f"/projects/{pid}/daily-reports/{report_id}/edit", data={
|
||||
"report_date": "2026-03-01",
|
||||
"system_id": str(systems["A"]),
|
||||
f"quantity_{a2}": "3",
|
||||
}, follow_redirects=False)
|
||||
assert edited.status_code == 303
|
||||
edit = client.get(f"/projects/{pid}/daily-reports/{report_id}/edit?system_id={systems['A']}")
|
||||
# A1 is back in the picker, still carrying its name attribute
|
||||
row = _picker_row(edit.text, "A1")
|
||||
assert f'name="quantity_{a1}"' in row
|
||||
# re-add with the same quantity
|
||||
edited = client.post(f"/projects/{pid}/daily-reports/{report_id}/edit", data={
|
||||
"report_date": "2026-03-01",
|
||||
"system_id": str(systems["A"]),
|
||||
f"quantity_{a1}": "2",
|
||||
f"quantity_{a2}": "3",
|
||||
}, follow_redirects=False)
|
||||
assert edited.status_code == 303
|
||||
with Session() as session:
|
||||
report = session.query(DailyReport).one()
|
||||
lines = {line.work_item_id: line.completed_quantity for line in report.items}
|
||||
assert lines == {a1: Decimal("2"), a2: Decimal("3")}
|
||||
finally:
|
||||
_cleanup()
|
||||
engine.dispose()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 7-8. search / system switch must not reset typed picker quantities
|
||||
# (values live in the DOM inputs; server never renders them away)
|
||||
# ---------------------------------------------------------------------------
|
||||
def test_picker_rows_stay_dom_stable_across_system_get():
|
||||
engine, Session, pid, systems, items = _setup()
|
||||
try:
|
||||
client = _client(Session)
|
||||
a1 = items["A"][0]
|
||||
# render with system A selected; then "switch" to B and back — both
|
||||
# pages must still carry the A1 picker input (JS keeps its value)
|
||||
for token in (systems["A"], systems["B"], systems["A"]):
|
||||
r = client.get(f"/projects/{pid}/daily-reports/new?system_id={token}")
|
||||
row = _picker_row(r.text, "A1")
|
||||
assert f'name="quantity_{a1}"' in row
|
||||
assert "disabled" not in re.search(r'<input[^>]*>', row).group(0)
|
||||
finally:
|
||||
_cleanup()
|
||||
engine.dispose()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 9-10. A1=2 → B1=5 → A2=3, saved correctly
|
||||
# ---------------------------------------------------------------------------
|
||||
def test_a1_b1_a2_quantities_saved():
|
||||
engine, Session, pid, systems, items = _setup()
|
||||
try:
|
||||
client = _client(Session)
|
||||
a1, a2 = items["A"]
|
||||
b1 = items["B"][0]
|
||||
created = client.post(f"/projects/{pid}/daily-reports", data={
|
||||
"report_date": "2026-03-01",
|
||||
"system_id": str(systems["A"]),
|
||||
f"quantity_{a1}": "2",
|
||||
f"quantity_{b1}": "5",
|
||||
f"quantity_{a2}": "3",
|
||||
}, follow_redirects=False)
|
||||
assert created.status_code == 303
|
||||
with Session() as session:
|
||||
report = session.query(DailyReport).one()
|
||||
lines = {line.work_item_id: line.completed_quantity for line in report.items}
|
||||
assert lines == {a1: Decimal("2"), b1: Decimal("5"), a2: Decimal("3")}
|
||||
finally:
|
||||
_cleanup()
|
||||
engine.dispose()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 11. editing an existing report does not regress
|
||||
# ---------------------------------------------------------------------------
|
||||
def test_edit_existing_report_keeps_quantities():
|
||||
engine, Session, pid, systems, items = _setup()
|
||||
try:
|
||||
client = _client(Session)
|
||||
a1 = items["A"][0]
|
||||
created = client.post(f"/projects/{pid}/daily-reports", data={
|
||||
"report_date": "2026-03-01",
|
||||
"system_id": str(systems["A"]),
|
||||
f"quantity_{a1}": "7",
|
||||
}, follow_redirects=False)
|
||||
report_id = int(created.headers["location"].rsplit("/", 1)[-1])
|
||||
edit = client.get(f"/projects/{pid}/daily-reports/{report_id}/edit?system_id={systems['A']}")
|
||||
row = _selected_row(edit.text, "A1")
|
||||
assert re.search(rf'name="quantity_{a1}" value="7', row)
|
||||
# picker no longer shows A1 (already added)
|
||||
pick = edit.text.split('id="picker-body"')[1]
|
||||
assert f'data-number="A1"' not in pick.split('id="selected-body"')[0]
|
||||
finally:
|
||||
_cleanup()
|
||||
engine.dispose()
|
||||
Reference in New Issue
Block a user