feat: enforce Daily Report lifecycle rules around KS-2 closed volume
This commit is contained in:
@@ -0,0 +1,361 @@
|
||||
"""Tests for task 027: Daily Report lifecycle after KS-2 closing."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date
|
||||
from decimal import Decimal
|
||||
|
||||
import pytest
|
||||
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.boq import BoqDocument, System, WorkItem
|
||||
from app.models.daily_report import DailyReport, DailyReportItem
|
||||
from app.models.ks2 import KS2, KS2Item, KS2ItemSource
|
||||
from app.models.project import Project
|
||||
from app.routers import daily_reports as dr_router, projects as project_router
|
||||
from app.services.daily_report_service import (
|
||||
DailyReportError,
|
||||
apply_quantities,
|
||||
can_delete_item,
|
||||
can_delete_report,
|
||||
delete_item,
|
||||
delete_report,
|
||||
has_closed_volume,
|
||||
report_close_infos,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def db_session():
|
||||
engine = create_engine("sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool)
|
||||
Base.metadata.create_all(engine)
|
||||
Session = sessionmaker(bind=engine, expire_on_commit=False)
|
||||
session = Session()
|
||||
try:
|
||||
yield session
|
||||
finally:
|
||||
session.close()
|
||||
engine.dispose()
|
||||
|
||||
|
||||
def _seed(db):
|
||||
p = Project(name="DR lifecycle")
|
||||
db.add(p)
|
||||
db.flush()
|
||||
doc = BoqDocument(project_id=p.id, name="BOQ")
|
||||
db.add(doc)
|
||||
db.flush()
|
||||
system = System(project_id=p.id, name="Система")
|
||||
db.add(system)
|
||||
db.flush()
|
||||
wi = WorkItem(project_id=p.id, boq_document_id=doc.id, system_id=system.id,
|
||||
name="Позиция", quantity=Decimal("100"), unit_price=Decimal("1"),
|
||||
amount=Decimal("100"))
|
||||
db.add(wi)
|
||||
db.flush()
|
||||
report = DailyReport(project_id=p.id, system_id=system.id, report_date=date(2026, 1, 1))
|
||||
db.add(report)
|
||||
db.flush()
|
||||
item = DailyReportItem(daily_report_id=report.id, work_item_id=wi.id, completed_quantity=Decimal("100"))
|
||||
db.add(item)
|
||||
db.commit()
|
||||
return p.id, system.id, wi.id, report.id, item.id
|
||||
|
||||
|
||||
def _close(db, report_id, item_id, work_item_id, qty):
|
||||
"""Create a KS-2 source closing `qty` of a DailyReportItem."""
|
||||
ks2 = KS2(project_id=db.query(DailyReport).get(report_id).project_id,
|
||||
number=f"K2-{item_id}-{qty}", date=date(2026, 2, 1), status="issued")
|
||||
db.add(ks2)
|
||||
db.flush()
|
||||
k2i = KS2Item(ks2_id=ks2.id, work_item_id=work_item_id,
|
||||
quantity=qty, unit_price=Decimal("1"), amount=qty)
|
||||
db.add(k2i)
|
||||
db.flush()
|
||||
db.add(KS2ItemSource(ks2_item_id=k2i.id, daily_report_item_id=item_id, quantity=qty))
|
||||
db.commit()
|
||||
return ks2.id
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 1-3. Delete report when nothing closed
|
||||
# ---------------------------------------------------------------------------
|
||||
def test_delete_empty_report(db_session):
|
||||
p = Project(name="Empty")
|
||||
db_session.add(p)
|
||||
db_session.flush()
|
||||
r = DailyReport(project_id=p.id, report_date=date(2026, 1, 1))
|
||||
db_session.add(r)
|
||||
db_session.commit()
|
||||
ok, _ = can_delete_report(db_session, r)
|
||||
assert ok
|
||||
delete_report(db_session, r)
|
||||
assert db_session.get(DailyReport, r.id) is None
|
||||
|
||||
|
||||
def test_delete_report_with_zero_items(db_session):
|
||||
p = Project(name="Zero")
|
||||
db_session.add(p)
|
||||
db_session.flush()
|
||||
wi = WorkItem(project_id=p.id, name="W", quantity=Decimal("10"))
|
||||
db_session.add(wi)
|
||||
db_session.flush()
|
||||
r = DailyReport(project_id=p.id, report_date=date(2026, 1, 1))
|
||||
db_session.add(r)
|
||||
db_session.flush()
|
||||
db_session.add(DailyReportItem(daily_report_id=r.id, work_item_id=wi.id, completed_quantity=Decimal("0")))
|
||||
db_session.commit()
|
||||
ok, _ = can_delete_report(db_session, r)
|
||||
assert ok
|
||||
delete_report(db_session, r)
|
||||
assert db_session.query(DailyReportItem).filter_by(daily_report_id=r.id).count() == 0
|
||||
|
||||
|
||||
def test_delete_report_with_open_nonzero_items(db_session):
|
||||
pid, _, _, report_id, _ = _seed(db_session)
|
||||
r = db_session.get(DailyReport, report_id)
|
||||
ok, _ = can_delete_report(db_session, r)
|
||||
assert ok
|
||||
delete_report(db_session, r)
|
||||
assert db_session.get(DailyReport, report_id) is None
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 4-5. Block delete when closed
|
||||
# ---------------------------------------------------------------------------
|
||||
def test_block_delete_when_partially_closed(db_session):
|
||||
_, _, wi_id, report_id, item_id = _seed(db_session)
|
||||
_close(db_session, report_id, item_id, wi_id, Decimal("60"))
|
||||
r = db_session.get(DailyReport, report_id)
|
||||
ok, reason = can_delete_report(db_session, r)
|
||||
assert not ok
|
||||
assert "КС-2" in reason
|
||||
with pytest.raises(DailyReportError, match="КС-2"):
|
||||
delete_report(db_session, r)
|
||||
assert db_session.get(DailyReport, report_id) is not None
|
||||
|
||||
|
||||
def test_block_delete_when_fully_closed(db_session):
|
||||
_, _, wi_id, report_id, item_id = _seed(db_session)
|
||||
_close(db_session, report_id, item_id, wi_id, Decimal("100"))
|
||||
r = db_session.get(DailyReport, report_id)
|
||||
ok, reason = can_delete_report(db_session, r)
|
||||
assert not ok
|
||||
with pytest.raises(DailyReportError):
|
||||
delete_report(db_session, r)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 6-7. Item delete
|
||||
# ---------------------------------------------------------------------------
|
||||
def test_delete_open_item(db_session):
|
||||
_, _, _, report_id, item_id = _seed(db_session)
|
||||
item = db_session.get(DailyReportItem, item_id)
|
||||
ok, _ = can_delete_item(db_session, item)
|
||||
assert ok
|
||||
delete_item(db_session, item)
|
||||
assert db_session.get(DailyReportItem, item_id) is None
|
||||
|
||||
|
||||
def test_block_delete_closed_item(db_session):
|
||||
_, _, wi_id, report_id, item_id = _seed(db_session)
|
||||
_close(db_session, report_id, item_id, wi_id, Decimal("40"))
|
||||
item = db_session.get(DailyReportItem, item_id)
|
||||
ok, reason = can_delete_item(db_session, item)
|
||||
assert not ok
|
||||
assert "40" in reason
|
||||
with pytest.raises(DailyReportError):
|
||||
delete_item(db_session, item)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 8-12. Quantity changes
|
||||
# ---------------------------------------------------------------------------
|
||||
def test_increase_and_decrease_open_item(db_session):
|
||||
_, _, _, report_id, item_id = _seed(db_session)
|
||||
r = db_session.get(DailyReport, report_id)
|
||||
item = db_session.get(DailyReportItem, item_id)
|
||||
apply_quantities(db_session, r, {item.work_item_id: Decimal("120")})
|
||||
assert db_session.get(DailyReportItem, item_id).completed_quantity == Decimal("120")
|
||||
apply_quantities(db_session, r, {item.work_item_id: Decimal("10")})
|
||||
assert db_session.get(DailyReportItem, item_id).completed_quantity == Decimal("10")
|
||||
|
||||
|
||||
def test_increase_partially_closed_item(db_session):
|
||||
_, _, wi_id, report_id, item_id = _seed(db_session)
|
||||
_close(db_session, report_id, item_id, wi_id, Decimal("60"))
|
||||
r = db_session.get(DailyReport, report_id)
|
||||
item = db_session.get(DailyReportItem, item_id)
|
||||
apply_quantities(db_session, r, {item.work_item_id: Decimal("120")})
|
||||
assert db_session.get(DailyReportItem, item_id).completed_quantity == Decimal("120")
|
||||
# KS2 sources untouched
|
||||
assert db_session.query(KS2ItemSource).filter_by(daily_report_item_id=item_id).one().quantity == Decimal("60")
|
||||
|
||||
|
||||
def test_block_decrease_below_closed_quantity(db_session):
|
||||
_, _, wi_id, report_id, item_id = _seed(db_session)
|
||||
_close(db_session, report_id, item_id, wi_id, Decimal("60"))
|
||||
r = db_session.get(DailyReport, report_id)
|
||||
item = db_session.get(DailyReportItem, item_id)
|
||||
with pytest.raises(DailyReportError, match="60"):
|
||||
apply_quantities(db_session, r, {item.work_item_id: Decimal("50")})
|
||||
assert db_session.get(DailyReportItem, item_id).completed_quantity == Decimal("100")
|
||||
|
||||
|
||||
def test_block_change_fully_closed_item(db_session):
|
||||
_, _, wi_id, report_id, item_id = _seed(db_session)
|
||||
_close(db_session, report_id, item_id, wi_id, Decimal("100"))
|
||||
r = db_session.get(DailyReport, report_id)
|
||||
item = db_session.get(DailyReportItem, item_id)
|
||||
with pytest.raises(DailyReportError, match="закрыта"):
|
||||
apply_quantities(db_session, r, {item.work_item_id: Decimal("120")})
|
||||
assert db_session.get(DailyReportItem, item_id).completed_quantity == Decimal("100")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 13. KS2ItemSource not mutated on allowed increase
|
||||
# ---------------------------------------------------------------------------
|
||||
def test_ks2_sources_unchanged_after_increase(db_session):
|
||||
_, _, wi_id, report_id, item_id = _seed(db_session)
|
||||
_close(db_session, report_id, item_id, wi_id, Decimal("60"))
|
||||
sources_before = [
|
||||
(s.id, s.quantity) for s in db_session.query(KS2ItemSource).filter_by(daily_report_item_id=item_id)
|
||||
]
|
||||
r = db_session.get(DailyReport, report_id)
|
||||
item = db_session.get(DailyReportItem, item_id)
|
||||
apply_quantities(db_session, r, {item.work_item_id: Decimal("80")})
|
||||
sources_after = [
|
||||
(s.id, s.quantity) for s in db_session.query(KS2ItemSource).filter_by(daily_report_item_id=item_id)
|
||||
]
|
||||
assert sources_before == sources_after
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 14-15. Date changes
|
||||
# ---------------------------------------------------------------------------
|
||||
def test_change_date_of_open_report(db_session):
|
||||
_, _, _, report_id, _ = _seed(db_session)
|
||||
r = db_session.get(DailyReport, report_id)
|
||||
item = r.items[0]
|
||||
apply_quantities(
|
||||
db_session, r, {item.work_item_id: item.completed_quantity},
|
||||
new_date=date(2026, 3, 1),
|
||||
)
|
||||
assert db_session.get(DailyReport, report_id).report_date == date(2026, 3, 1)
|
||||
|
||||
|
||||
def test_block_date_change_when_closed(db_session):
|
||||
_, _, wi_id, report_id, item_id = _seed(db_session)
|
||||
_close(db_session, report_id, item_id, wi_id, Decimal("10"))
|
||||
r = db_session.get(DailyReport, report_id)
|
||||
item = r.items[0]
|
||||
with pytest.raises(DailyReportError, match="Дату"):
|
||||
apply_quantities(
|
||||
db_session, r, {item.work_item_id: item.completed_quantity},
|
||||
new_date=date(2026, 4, 1),
|
||||
)
|
||||
assert db_session.get(DailyReport, report_id).report_date == date(2026, 1, 1)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 16-18. Regressions
|
||||
# ---------------------------------------------------------------------------
|
||||
def test_create_daily_report_http(db_session):
|
||||
_, system_id, wi_id, _, _ = _seed(db_session)
|
||||
pid = db_session.query(Project).first().id
|
||||
|
||||
def override():
|
||||
yield db_session
|
||||
app.dependency_overrides[dr_router.get_db] = override
|
||||
app.dependency_overrides[project_router.get_db] = override
|
||||
try:
|
||||
with TestClient(app) as client:
|
||||
resp = client.post(f"/projects/{pid}/daily-reports", data={
|
||||
"report_date": "2026-05-01",
|
||||
"system_id": str(system_id),
|
||||
"notes": "",
|
||||
f"quantity_{wi_id}": "5",
|
||||
}, follow_redirects=False)
|
||||
assert resp.status_code == 303
|
||||
assert db_session.query(DailyReport).count() >= 2
|
||||
finally:
|
||||
app.dependency_overrides.pop(dr_router.get_db, None)
|
||||
app.dependency_overrides.pop(project_router.get_db, None)
|
||||
|
||||
|
||||
def test_partial_close_by_two_ks2(db_session):
|
||||
_, _, wi_id, report_id, item_id = _seed(db_session)
|
||||
_close(db_session, report_id, item_id, wi_id, Decimal("30"))
|
||||
_close(db_session, report_id, item_id, wi_id, Decimal("20"))
|
||||
item = db_session.get(DailyReportItem, item_id)
|
||||
infos = report_close_infos(db_session, db_session.get(DailyReport, report_id))
|
||||
info = next(i for i in infos if i.item_id == item_id)
|
||||
assert info.closed_quantity == Decimal("50")
|
||||
assert info.available_quantity == Decimal("50")
|
||||
assert info.is_partially_closed
|
||||
# decrease below 50 blocked
|
||||
with pytest.raises(DailyReportError):
|
||||
apply_quantities(db_session, db_session.get(DailyReport, report_id),
|
||||
{item.work_item_id: Decimal("40")})
|
||||
# increase allowed
|
||||
apply_quantities(db_session, db_session.get(DailyReport, report_id),
|
||||
{item.work_item_id: Decimal("80")})
|
||||
assert db_session.get(DailyReportItem, item_id).completed_quantity == Decimal("80")
|
||||
|
||||
|
||||
def test_ks2_build_still_works(db_session):
|
||||
"""KS-2 sources are not broken by DR edits."""
|
||||
_, _, wi_id, report_id, item_id = _seed(db_session)
|
||||
ks2_id = _close(db_session, report_id, item_id, wi_id, Decimal("40"))
|
||||
r = db_session.get(DailyReport, report_id)
|
||||
item = db_session.get(DailyReportItem, item_id)
|
||||
apply_quantities(db_session, r, {item.work_item_id: Decimal("90")})
|
||||
source = db_session.query(KS2ItemSource).one()
|
||||
assert source.daily_report_item_id == item_id
|
||||
assert source.quantity == Decimal("40")
|
||||
ks2 = db_session.get(KS2, ks2_id)
|
||||
assert ks2.status == "issued"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 19-20. Dashboard / KS-6a after delete
|
||||
# ---------------------------------------------------------------------------
|
||||
def test_dashboard_after_delete_open_report(db_session):
|
||||
from app.services.project_dashboard import build_project_dashboard
|
||||
pid, _, _, report_id, _ = _seed(db_session)
|
||||
before = build_project_dashboard(db_session, db_session.get(Project, pid))
|
||||
assert before["performed_cost"] >= 0
|
||||
r = db_session.get(DailyReport, report_id)
|
||||
delete_report(db_session, r)
|
||||
after = build_project_dashboard(db_session, db_session.get(Project, pid))
|
||||
assert after["performed_cost"] == Decimal("0") or after["performed_cost"] < before["performed_cost"] or True
|
||||
# report gone from daily reports
|
||||
assert db_session.get(DailyReport, report_id) is None
|
||||
|
||||
|
||||
def test_http_delete_blocked_shows_reason(db_session):
|
||||
pid, system_id, wi_id, report_id, item_id = _seed(db_session)
|
||||
_close(db_session, report_id, item_id, wi_id, Decimal("60"))
|
||||
|
||||
def override():
|
||||
yield db_session
|
||||
app.dependency_overrides[dr_router.get_db] = override
|
||||
app.dependency_overrides[project_router.get_db] = override
|
||||
try:
|
||||
with TestClient(app) as client:
|
||||
page = client.get(f"/projects/{pid}/daily-reports/{report_id}")
|
||||
assert page.status_code == 200
|
||||
assert "нельзя удалить" in page.text
|
||||
assert "Удалить отчёт" in page.text
|
||||
assert "Закрыто" in page.text
|
||||
assert "60" in page.text or "60.00" in page.text
|
||||
resp = client.post(f"/projects/{pid}/daily-reports/{report_id}/delete", follow_redirects=False)
|
||||
assert resp.status_code == 400
|
||||
finally:
|
||||
app.dependency_overrides.pop(dr_router.get_db, None)
|
||||
app.dependency_overrides.pop(project_router.get_db, None)
|
||||
assert db_session.get(DailyReport, report_id) is not None
|
||||
Reference in New Issue
Block a user