Files
work-volume-tracker/tests/test_task021_auto_numbering.py
Арсентьев Александр 80b7d4732e feat: auto-suggest KS-2/KS-3 document numbers
2026-09-27 17:39:22 +03:00

386 lines
15 KiB
Python

"""Regression tests for task 021: automatic KS-2 / KS-3 numbering."""
import datetime
import shutil
from decimal import Decimal
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from app.database.database import Base, DATABASE_PATH
from app.main import app
from app.models.project import Project
from app.models.boq import BoqDocument, WorkItem
from app.models.daily_report import DailyReport, DailyReportItem
from app.models.ks2 import KS2, KS2Item
from app.models.ks3 import KS3
from app.routers import projects, ks2 as ks2_router, ks3 as ks3_router
from app.services.document_numbering import next_document_number
def _make_session(tmp_path):
db_path = tmp_path / "t021-copy.db"
shutil.copy2(DATABASE_PATH, db_path)
engine = create_engine(f"sqlite:///{db_path.as_posix()}", connect_args={"check_same_thread": False})
Base.metadata.create_all(bind=engine)
return engine, sessionmaker(bind=engine, autoflush=False, autocommit=False)
def _override(Session):
def override_db():
s = Session()
try:
yield s
finally:
s.close()
for dep in (projects.get_db, ks2_router.get_db, ks3_router.get_db):
app.dependency_overrides[dep] = override_db
def _clear():
for dep in (projects.get_db, ks2_router.get_db, ks3_router.get_db):
app.dependency_overrides.pop(dep, None)
def _setup_project(Session):
"""Project with work item and daily report fact so KS-2 can be created."""
with Session() as s:
p = Project(name="Numbering test", customer="C", contract_number="D-1")
s.add(p)
s.flush()
boq = BoqDocument(project_id=p.id, name="BOQ")
s.add(boq)
s.flush()
wi = WorkItem(project_id=p.id, boq_document_id=boq.id, name="Работа",
source_number="1.1", unit="м", quantity=Decimal("100"),
unit_price=Decimal("10"), amount=Decimal("1000"))
s.add(wi)
s.flush()
dr = DailyReport(project_id=p.id, report_date=datetime.date(2026, 3, 1))
s.add(dr)
s.flush()
s.add(DailyReportItem(daily_report_id=dr.id, work_item_id=wi.id, completed_quantity=Decimal("10")))
s.commit()
return p.id, wi.id
def _add_ks2(Session, pid, wi_id, number, status="draft"):
with Session() as s:
doc = KS2(project_id=pid, number=str(number), date=datetime.date(2026, 3, 10),
status=status,
reporting_period_from=datetime.date(2026, 3, 1),
reporting_period_to=datetime.date(2026, 3, 31))
s.add(doc)
s.flush()
s.add(KS2Item(ks2_id=doc.id, work_item_id=wi_id,
quantity=Decimal("1"), unit_price=Decimal("10"), amount=Decimal("10")))
s.commit()
return doc.id
def _add_ks3(Session, pid, number, status="draft"):
with Session() as s:
doc = KS3(project_id=pid, number=str(number), date=datetime.date(2026, 3, 15),
status=status,
reporting_period_from=datetime.date(2026, 3, 1),
reporting_period_to=datetime.date(2026, 3, 31))
s.add(doc)
s.commit()
return doc.id
def test_next_ks2_number_when_empty(tmp_path):
engine, Session = _make_session(tmp_path)
try:
with Session() as s:
p = Project(name="P")
s.add(p)
s.commit()
assert next_document_number(s, KS2, p.id) == "1"
finally:
engine.dispose()
def test_next_ks2_number_after_existing(tmp_path):
engine, Session = _make_session(tmp_path)
try:
with Session() as s:
p = Project(name="P")
s.add(p)
s.flush()
s.add(KS2(project_id=p.id, number="1", date=datetime.date(2026, 1, 1), status="issued"))
s.add(KS2(project_id=p.id, number="2", date=datetime.date(2026, 1, 2), status="issued"))
s.commit()
assert next_document_number(s, KS2, p.id) == "3"
finally:
engine.dispose()
def test_ks3_sequence_independent_from_ks2(tmp_path):
engine, Session = _make_session(tmp_path)
try:
with Session() as s:
p = Project(name="P")
s.add(p)
s.flush()
s.add(KS3(project_id=p.id, number="1", date=datetime.date(2026, 1, 1), status="issued"))
s.commit()
assert next_document_number(s, KS2, p.id) == "1"
assert next_document_number(s, KS3, p.id) == "2"
finally:
engine.dispose()
def test_ks2_sequence_independent_from_ks3(tmp_path):
engine, Session = _make_session(tmp_path)
try:
with Session() as s:
p = Project(name="P")
s.add(p)
s.flush()
s.add(KS2(project_id=p.id, number="1", date=datetime.date(2026, 1, 1), status="issued"))
s.commit()
assert next_document_number(s, KS3, p.id) == "1"
finally:
engine.dispose()
def test_cancelled_ks2_keeps_number(tmp_path):
engine, Session = _make_session(tmp_path)
try:
with Session() as s:
p = Project(name="P")
s.add(p)
s.flush()
s.add(KS2(project_id=p.id, number="1", date=datetime.date(2026, 1, 1), status="issued"))
s.add(KS2(project_id=p.id, number="2", date=datetime.date(2026, 1, 2), status="cancelled"))
s.commit()
assert next_document_number(s, KS2, p.id) == "3"
finally:
engine.dispose()
def test_manual_number_saved(tmp_path):
engine, Session = _make_session(tmp_path)
try:
pid, wi_id = _setup_project(Session)
from app.services.ks2_service import save_draft
with Session() as s:
doc = save_draft(s, pid, "42", datetime.date(2026, 3, 10), "", [(wi_id, "1")],
reporting_period_from=datetime.date(2026, 3, 1),
reporting_period_to=datetime.date(2026, 3, 31))
assert doc.number == "42"
finally:
engine.dispose()
def test_duplicate_ks2_number_rejected(tmp_path):
engine, Session = _make_session(tmp_path)
try:
with Session() as s:
p = Project(name="P")
s.add(p)
s.flush()
boq = BoqDocument(project_id=p.id, name="BOQ")
s.add(boq)
s.flush()
wi = WorkItem(project_id=p.id, boq_document_id=boq.id, name="W", quantity=Decimal("10"),
unit_price=Decimal("1"), amount=Decimal("10"))
s.add(wi)
s.flush()
dr = DailyReport(project_id=p.id, report_date=datetime.date(2026, 3, 1))
s.add(dr)
s.flush()
s.add(DailyReportItem(daily_report_id=dr.id, work_item_id=wi.id, completed_quantity=Decimal("5")))
s.commit()
pid, wi_id = p.id, wi.id
from app.services.ks2_service import KS2ValidationError, save_draft
with Session() as s:
save_draft(s, pid, "5", datetime.date(2026, 3, 10), "", [(wi_id, "1")],
reporting_period_from=datetime.date(2026, 3, 1),
reporting_period_to=datetime.date(2026, 3, 31))
with Session() as s:
try:
save_draft(s, pid, "5", datetime.date(2026, 3, 11), "", [(wi_id, "1")],
reporting_period_from=datetime.date(2026, 3, 1),
reporting_period_to=datetime.date(2026, 3, 31))
raise AssertionError("duplicate number must be rejected")
except KS2ValidationError as exc:
assert "уже существует" in str(exc)
finally:
engine.dispose()
def test_duplicate_ks3_number_rejected(tmp_path):
engine, Session = _make_session(tmp_path)
try:
with Session() as s:
p = Project(name="P")
s.add(p)
s.flush()
boq = BoqDocument(project_id=p.id, name="BOQ")
s.add(boq)
s.flush()
wi = WorkItem(project_id=p.id, boq_document_id=boq.id, name="W", quantity=Decimal("10"),
unit_price=Decimal("1"), amount=Decimal("10"))
s.add(wi)
s.flush()
ks2 = KS2(project_id=p.id, number="1", date=datetime.date(2026, 3, 1), status="issued")
s.add(ks2)
s.flush()
s.add(KS2Item(ks2_id=ks2.id, work_item_id=wi.id,
quantity=Decimal("1"), unit_price=Decimal("1"), amount=Decimal("1")))
s.commit()
pid, ks2_id = p.id, ks2.id
from app.services.ks3_service import KS3ValidationError, save_draft
with Session() as s:
save_draft(s, pid, "7", datetime.date(2026, 3, 10), "", [ks2_id],
reporting_period_from=datetime.date(2026, 3, 1),
reporting_period_to=datetime.date(2026, 3, 31))
with Session() as s:
try:
save_draft(s, pid, "7", datetime.date(2026, 3, 11), "", [ks2_id],
reporting_period_from=datetime.date(2026, 3, 1),
reporting_period_to=datetime.date(2026, 3, 31))
raise AssertionError("duplicate number must be rejected")
except KS3ValidationError as exc:
assert "уже существует" in str(exc)
finally:
engine.dispose()
def test_same_number_allowed_for_ks2_and_ks3(tmp_path):
engine, Session = _make_session(tmp_path)
try:
with Session() as s:
p = Project(name="P")
s.add(p)
s.flush()
s.add(KS2(project_id=p.id, number="5", date=datetime.date(2026, 1, 1), status="issued"))
s.add(KS3(project_id=p.id, number="5", date=datetime.date(2026, 1, 1), status="issued"))
s.commit()
assert next_document_number(s, KS2, p.id) == "6"
assert next_document_number(s, KS3, p.id) == "6"
finally:
engine.dispose()
def test_form_data_preserved_on_number_conflict(tmp_path):
engine, Session = _make_session(tmp_path)
_override(Session)
try:
pid, wi_id = _setup_project(Session)
_add_ks2(Session, pid, wi_id, "9", status="draft")
with TestClient(app) as client:
resp = client.post(f"/projects/{pid}/ks2", data={
"number": "9",
"document_date": "2026-03-15",
"reporting_period_from": "2026-03-01",
"reporting_period_to": "2026-03-31",
"comment": "Мой комментарий",
})
assert resp.status_code == 400
assert "уже существует" in resp.text
assert "value=\"9\"" in resp.text or 'value="9"' in resp.text
assert "Мой комментарий" in resp.text
assert "2026-03-15" in resp.text
finally:
_clear()
engine.dispose()
def test_edit_does_not_change_number_automatically(tmp_path):
engine, Session = _make_session(tmp_path)
try:
with Session() as s:
p = Project(name="P")
s.add(p)
s.flush()
boq = BoqDocument(project_id=p.id, name="BOQ")
s.add(boq)
s.flush()
wi = WorkItem(project_id=p.id, boq_document_id=boq.id, name="W", quantity=Decimal("10"),
unit_price=Decimal("1"), amount=Decimal("10"))
s.add(wi)
s.flush()
dr = DailyReport(project_id=p.id, report_date=datetime.date(2026, 3, 1))
s.add(dr)
s.flush()
s.add(DailyReportItem(daily_report_id=dr.id, work_item_id=wi.id, completed_quantity=Decimal("5")))
s.commit()
pid, wi_id = p.id, wi.id
from app.services.ks2_service import save_draft
with Session() as s:
doc = save_draft(s, pid, "11", datetime.date(2026, 3, 10), "old", [(wi_id, "1")],
reporting_period_from=datetime.date(2026, 3, 1),
reporting_period_to=datetime.date(2026, 3, 31))
doc_id = doc.id
with Session() as s:
doc = save_draft(s, pid, "11", datetime.date(2026, 3, 12), "new", [(wi_id, "1")],
ks2_id=doc_id,
reporting_period_from=datetime.date(2026, 3, 1),
reporting_period_to=datetime.date(2026, 3, 31))
assert doc.number == "11"
assert doc.comment == "new"
finally:
engine.dispose()
def test_same_numbers_in_different_projects(tmp_path):
engine, Session = _make_session(tmp_path)
try:
with Session() as s:
p1 = Project(name="P1")
p2 = Project(name="P2")
s.add_all([p1, p2])
s.flush()
s.add(KS2(project_id=p1.id, number="1", date=datetime.date(2026, 1, 1), status="issued"))
s.add(KS2(project_id=p2.id, number="1", date=datetime.date(2026, 1, 1), status="issued"))
s.add(KS3(project_id=p1.id, number="1", date=datetime.date(2026, 1, 1), status="issued"))
s.add(KS3(project_id=p2.id, number="1", date=datetime.date(2026, 1, 1), status="issued"))
s.commit()
assert next_document_number(s, KS2, p1.id) == "2"
assert next_document_number(s, KS2, p2.id) == "2"
finally:
engine.dispose()
def test_new_form_prefills_suggested_number(tmp_path):
engine, Session = _make_session(tmp_path)
_override(Session)
try:
pid, wi_id = _setup_project(Session)
_add_ks2(Session, pid, wi_id, "1", status="issued")
_add_ks2(Session, pid, wi_id, "2", status="cancelled")
with TestClient(app) as client:
page = client.get(f"/projects/{pid}/ks2/new")
assert page.status_code == 200
assert 'value="3"' in page.text # cancelled №2 keeps its number
page3 = client.get(f"/projects/{pid}/ks3/new")
assert page3.status_code == 200
assert 'value="1"' in page3.text # independent sequence
finally:
_clear()
engine.dispose()
def test_next_number_ignores_non_numeric_and_uses_existing(tmp_path):
engine, Session = _make_session(tmp_path)
try:
with Session() as s:
p = Project(name="P")
s.add(p)
s.flush()
s.add(KS2(project_id=p.id, number="abc", date=datetime.date(2026, 1, 1), status="issued"))
s.add(KS2(project_id=p.id, number="10", date=datetime.date(2026, 1, 2), status="cancelled"))
s.commit()
assert next_document_number(s, KS2, p.id) == "11"
finally:
engine.dispose()