450 lines
17 KiB
Python
450 lines
17 KiB
Python
"""Tests for AGR-02: Agreement cancellation (approved → cancelled)."""
|
|
from __future__ import annotations
|
|
|
|
from datetime import date
|
|
from decimal import Decimal
|
|
|
|
import pytest
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
from sqlalchemy.pool import StaticPool
|
|
|
|
from app.database.database import Base
|
|
from app.exporters.project_export import export_project
|
|
from app.exporters.project_import import import_project
|
|
from app.models import ( # noqa: F401
|
|
agreement, boq, daily_report, document_artifact,
|
|
ks2, ks3, payment, project, work_journal,
|
|
)
|
|
from app.models.agreement import Agreement, AgreementItem
|
|
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.services.agreement_items import (
|
|
agreement_cancel_block_reason,
|
|
add_existing_item,
|
|
add_new_item,
|
|
cancel_agreement,
|
|
delete_draft_agreement,
|
|
)
|
|
from app.services.database_reset import reset_database
|
|
from app.services.effective_plan import approve_agreement, build_effective_plan
|
|
from app.main import app
|
|
from app.services.ks2_service import issue_ks2, save_draft as save_ks2_draft
|
|
from app.routers import agreements as agreement_router, projects as project_router
|
|
|
|
|
|
@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()
|
|
|
|
|
|
@pytest.fixture
|
|
def project_id(db_session):
|
|
p = Project(name="AGR-02", status="active")
|
|
db_session.add(p)
|
|
db_session.flush()
|
|
doc = BoqDocument(project_id=p.id, name="BOQ")
|
|
db_session.add(doc)
|
|
db_session.flush()
|
|
system = System(project_id=p.id, name="S", sort_order=1)
|
|
db_session.add(system)
|
|
db_session.flush()
|
|
wi = WorkItem(
|
|
project_id=p.id, boq_document_id=doc.id, system_id=system.id,
|
|
name="Base item", unit="шт",
|
|
quantity=Decimal("100"), unit_price=Decimal("10"), amount=Decimal("1000"),
|
|
)
|
|
db_session.add(wi)
|
|
db_session.commit()
|
|
return p.id
|
|
|
|
|
|
def _make_draft_agreement(db, pid, number="A-1", boq_document_id=None):
|
|
a = Agreement(project_id=pid, number=number, date=date(2026, 1, 1),
|
|
title="T", status="draft", boq_document_id=boq_document_id)
|
|
db.add(a)
|
|
db.commit()
|
|
return a
|
|
|
|
|
|
def _add_dr(db, pid, wi_id, qty=Decimal("50")):
|
|
dr = DailyReport(project_id=pid, report_date=date(2026, 2, 1))
|
|
db.add(dr)
|
|
db.flush()
|
|
dri = DailyReportItem(daily_report_id=dr.id, work_item_id=wi_id, completed_quantity=qty)
|
|
db.add(dri)
|
|
db.commit()
|
|
return dri.id
|
|
|
|
|
|
def _ks2_close(db, pid, wi_id, qty=Decimal("10"), number="K2-1"):
|
|
draft = save_ks2_draft(db, pid, number, date(2026, 3, 1), None,
|
|
[(wi_id, qty)],
|
|
reporting_period_from=date(2026, 2, 1),
|
|
reporting_period_to=date(2026, 2, 28))
|
|
db.commit()
|
|
return issue_ks2(db, pid, draft.id)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Lifecycle
|
|
# ---------------------------------------------------------------------------
|
|
def test_approved_can_be_cancelled(db_session, project_id):
|
|
wi = db_session.query(WorkItem).filter_by(project_id=project_id).one()
|
|
a = _make_draft_agreement(db_session, project_id)
|
|
add_existing_item(db_session, a, wi.id, Decimal("10"))
|
|
db_session.commit()
|
|
approve_agreement(db_session, a.id)
|
|
|
|
result = cancel_agreement(db_session, a)
|
|
assert result.status == "cancelled"
|
|
|
|
|
|
def test_draft_cannot_be_cancelled(db_session, project_id):
|
|
a = _make_draft_agreement(db_session, project_id)
|
|
with pytest.raises(ValueError, match="Черновик"):
|
|
cancel_agreement(db_session, a)
|
|
assert db_session.get(Agreement, a.id).status == "draft"
|
|
|
|
|
|
def test_cancelled_cannot_be_cancelled_again(db_session, project_id):
|
|
wi = db_session.query(WorkItem).filter_by(project_id=project_id).one()
|
|
a = _make_draft_agreement(db_session, project_id)
|
|
add_existing_item(db_session, a, wi.id, Decimal("10"))
|
|
db_session.commit()
|
|
approve_agreement(db_session, a.id)
|
|
cancel_agreement(db_session, a)
|
|
|
|
with pytest.raises(ValueError, match="уже отменено"):
|
|
cancel_agreement(db_session, a)
|
|
|
|
|
|
def test_cancelled_cannot_be_approved(db_session, project_id):
|
|
wi = db_session.query(WorkItem).filter_by(project_id=project_id).one()
|
|
a = _make_draft_agreement(db_session, project_id)
|
|
add_existing_item(db_session, a, wi.id, Decimal("10"))
|
|
db_session.commit()
|
|
approve_agreement(db_session, a.id)
|
|
cancel_agreement(db_session, a)
|
|
|
|
with pytest.raises(ValueError, match="draft"):
|
|
approve_agreement(db_session, a.id)
|
|
|
|
|
|
def test_cancelled_cannot_be_deleted(db_session, project_id):
|
|
wi = db_session.query(WorkItem).filter_by(project_id=project_id).one()
|
|
a = _make_draft_agreement(db_session, project_id)
|
|
add_existing_item(db_session, a, wi.id, Decimal("10"))
|
|
db_session.commit()
|
|
approve_agreement(db_session, a.id)
|
|
cancel_agreement(db_session, a)
|
|
|
|
with pytest.raises(ValueError, match="draft"):
|
|
delete_draft_agreement(db_session, a)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Effective plan
|
|
# ---------------------------------------------------------------------------
|
|
def test_increase_disappears_after_cancel(db_session, project_id):
|
|
wi = db_session.query(WorkItem).filter_by(project_id=project_id).one()
|
|
a = _make_draft_agreement(db_session, project_id)
|
|
add_existing_item(db_session, a, wi.id, Decimal("50"))
|
|
db_session.commit()
|
|
approve_agreement(db_session, a.id)
|
|
|
|
plan = build_effective_plan(db_session, project_id)
|
|
row = next(r for r in plan["items"] if r.item.id == wi.id)
|
|
assert row.quantity == Decimal("150.0000") # 100 + 50
|
|
|
|
cancel_agreement(db_session, a)
|
|
plan = build_effective_plan(db_session, project_id)
|
|
row = next(r for r in plan["items"] if r.item.id == wi.id)
|
|
assert row.quantity == Decimal("100.0000")
|
|
|
|
|
|
def test_decrease_disappears_after_cancel(db_session, project_id):
|
|
wi = db_session.query(WorkItem).filter_by(project_id=project_id).one()
|
|
a = _make_draft_agreement(db_session, project_id)
|
|
add_existing_item(db_session, a, wi.id, Decimal("-20"))
|
|
db_session.commit()
|
|
approve_agreement(db_session, a.id)
|
|
|
|
plan = build_effective_plan(db_session, project_id)
|
|
row = next(r for r in plan["items"] if r.item.id == wi.id)
|
|
assert row.quantity == Decimal("80.0000") # 100 - 20
|
|
|
|
cancel_agreement(db_session, a)
|
|
plan = build_effective_plan(db_session, project_id)
|
|
row = next(r for r in plan["items"] if r.item.id == wi.id)
|
|
assert row.quantity == Decimal("100.0000")
|
|
|
|
|
|
def test_new_position_disappears_after_cancel(db_session, project_id):
|
|
doc = db_session.query(BoqDocument).filter_by(project_id=project_id).one()
|
|
system = db_session.query(System).filter_by(project_id=project_id).one()
|
|
a = _make_draft_agreement(db_session, project_id)
|
|
line = add_new_item(
|
|
db_session, a, name="New pos", model=None, manufacturer=None,
|
|
unit="шт", quantity_delta=Decimal("10"), unit_price=Decimal("5"),
|
|
system_id=system.id, source_boq_document_id=doc.id,
|
|
)
|
|
db_session.commit()
|
|
new_wi_id = line.work_item_id
|
|
approve_agreement(db_session, a.id)
|
|
|
|
plan = build_effective_plan(db_session, project_id)
|
|
assert new_wi_id in {r.item.id for r in plan["items"]}
|
|
|
|
cancel_agreement(db_session, a)
|
|
plan = build_effective_plan(db_session, project_id)
|
|
assert new_wi_id not in {r.item.id for r in plan["items"]}
|
|
|
|
|
|
def test_actual_not_changed_after_cancel(db_session, project_id):
|
|
wi = db_session.query(WorkItem).filter_by(project_id=project_id).one()
|
|
a = _make_draft_agreement(db_session, project_id)
|
|
add_existing_item(db_session, a, wi.id, Decimal("50"))
|
|
db_session.commit()
|
|
approve_agreement(db_session, a.id)
|
|
|
|
_add_dr(db_session, project_id, wi.id, Decimal("140"))
|
|
|
|
from app.services.project_dashboard import build_project_dashboard
|
|
before = build_project_dashboard(db_session, db_session.get(Project, project_id))
|
|
assert before["performed_cost"] > 0
|
|
|
|
cancel_agreement(db_session, a)
|
|
after = build_project_dashboard(db_session, db_session.get(Project, project_id))
|
|
assert after["performed_cost"] == before["performed_cost"]
|
|
# actual > plan warning appears
|
|
assert after["over_plan_count"] > 0
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Cancellation blocking (KS-2 closed volume)
|
|
# ---------------------------------------------------------------------------
|
|
def test_cancel_allowed_without_ks2(db_session, project_id):
|
|
wi = db_session.query(WorkItem).filter_by(project_id=project_id).one()
|
|
a = _make_draft_agreement(db_session, project_id)
|
|
add_existing_item(db_session, a, wi.id, Decimal("10"))
|
|
db_session.commit()
|
|
approve_agreement(db_session, a.id)
|
|
|
|
reason = agreement_cancel_block_reason(db_session, a)
|
|
assert reason is None
|
|
cancel_agreement(db_session, a)
|
|
assert db_session.get(Agreement, a.id).status == "cancelled"
|
|
|
|
|
|
def test_cancel_rejected_when_work_item_closed_in_ks2(db_session, project_id):
|
|
wi = db_session.query(WorkItem).filter_by(project_id=project_id).one()
|
|
a = _make_draft_agreement(db_session, project_id)
|
|
add_existing_item(db_session, a, wi.id, Decimal("10"))
|
|
db_session.commit()
|
|
approve_agreement(db_session, a.id)
|
|
|
|
# base=100, DS=+10. Close 105 → 5 of DS volume used → block
|
|
_add_dr(db_session, project_id, wi.id, Decimal("105"))
|
|
_ks2_close(db_session, project_id, wi.id, Decimal("105"))
|
|
|
|
reason = agreement_cancel_block_reason(db_session, a)
|
|
assert reason is not None
|
|
assert "КС-2" in reason
|
|
with pytest.raises(ValueError, match="КС-2"):
|
|
cancel_agreement(db_session, a)
|
|
assert db_session.get(Agreement, a.id).status == "approved"
|
|
|
|
|
|
def test_cancelled_ks2_does_not_block(db_session, project_id):
|
|
wi = db_session.query(WorkItem).filter_by(project_id=project_id).one()
|
|
a = _make_draft_agreement(db_session, project_id)
|
|
add_existing_item(db_session, a, wi.id, Decimal("10"))
|
|
db_session.commit()
|
|
approve_agreement(db_session, a.id)
|
|
|
|
_add_dr(db_session, project_id, wi.id, Decimal("50"))
|
|
ks2 = _ks2_close(db_session, project_id, wi.id, Decimal("10"))
|
|
# Cancel the KS-2
|
|
from app.services.ks2_service import cancel_ks2
|
|
cancel_ks2(db_session, project_id, ks2.id)
|
|
|
|
reason = agreement_cancel_block_reason(db_session, a)
|
|
assert reason is None
|
|
cancel_agreement(db_session, a)
|
|
assert db_session.get(Agreement, a.id).status == "cancelled"
|
|
|
|
|
|
def test_new_position_closed_blocks_cancel(db_session, project_id):
|
|
doc = db_session.query(BoqDocument).filter_by(project_id=project_id).one()
|
|
system = db_session.query(System).filter_by(project_id=project_id).one()
|
|
a = _make_draft_agreement(db_session, project_id)
|
|
line = add_new_item(
|
|
db_session, a, name="New pos", model=None, manufacturer=None,
|
|
unit="шт", quantity_delta=Decimal("10"), unit_price=Decimal("5"),
|
|
system_id=system.id, source_boq_document_id=doc.id,
|
|
)
|
|
db_session.commit()
|
|
new_wi_id = line.work_item_id
|
|
approve_agreement(db_session, a.id)
|
|
|
|
_add_dr(db_session, project_id, new_wi_id, Decimal("5"))
|
|
_ks2_close(db_session, project_id, new_wi_id, Decimal("5"), number="K2-NP")
|
|
|
|
reason = agreement_cancel_block_reason(db_session, a)
|
|
assert reason is not None
|
|
with pytest.raises(ValueError, match="КС-2"):
|
|
cancel_agreement(db_session, a)
|
|
|
|
|
|
def test_new_position_without_close_allows_cancel(db_session, project_id):
|
|
doc = db_session.query(BoqDocument).filter_by(project_id=project_id).one()
|
|
system = db_session.query(System).filter_by(project_id=project_id).one()
|
|
a = _make_draft_agreement(db_session, project_id)
|
|
line = add_new_item(
|
|
db_session, a, name="New pos", model=None, manufacturer=None,
|
|
unit="шт", quantity_delta=Decimal("10"), unit_price=Decimal("5"),
|
|
system_id=system.id, source_boq_document_id=doc.id,
|
|
)
|
|
db_session.commit()
|
|
approve_agreement(db_session, a.id)
|
|
|
|
reason = agreement_cancel_block_reason(db_session, a)
|
|
assert reason is None
|
|
cancel_agreement(db_session, a)
|
|
assert db_session.get(Agreement, a.id).status == "cancelled"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Rollback
|
|
# ---------------------------------------------------------------------------
|
|
def test_rollback_preserves_approved_status(db_session, project_id):
|
|
wi = db_session.query(WorkItem).filter_by(project_id=project_id).one()
|
|
a = _make_draft_agreement(db_session, project_id)
|
|
add_existing_item(db_session, a, wi.id, Decimal("10"))
|
|
db_session.commit()
|
|
approve_agreement(db_session, a.id)
|
|
|
|
# base=100, DS=+10. Close 105 → DS volume used → cancel must fail
|
|
_add_dr(db_session, project_id, wi.id, Decimal("105"))
|
|
_ks2_close(db_session, project_id, wi.id, Decimal("105"))
|
|
|
|
# cancel fails → rollback → still approved
|
|
with pytest.raises(ValueError):
|
|
cancel_agreement(db_session, a)
|
|
db_session.rollback()
|
|
assert db_session.get(Agreement, a.id).status == "approved"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Export / Import
|
|
# ---------------------------------------------------------------------------
|
|
def test_export_import_cancelled_agreement(db_session, project_id):
|
|
wi = db_session.query(WorkItem).filter_by(project_id=project_id).one()
|
|
a = _make_draft_agreement(db_session, project_id, number="A-CAN")
|
|
add_existing_item(db_session, a, wi.id, Decimal("10"))
|
|
db_session.commit()
|
|
approve_agreement(db_session, a.id)
|
|
cancel_agreement(db_session, a)
|
|
|
|
data, _ = export_project(db_session, project_id)
|
|
reset_database(db_session)
|
|
imported = import_project(db_session, data)
|
|
|
|
new_a = db_session.query(Agreement).filter_by(project_id=imported.id, number="A-CAN").one()
|
|
assert new_a.status == "cancelled"
|
|
# Items preserved
|
|
items = db_session.query(AgreementItem).filter_by(agreement_id=new_a.id).all()
|
|
assert len(items) == 1
|
|
assert items[0].change_type == "increase"
|
|
assert items[0].quantity_delta == Decimal("10.0000")
|
|
|
|
# Effective plan ignores cancelled
|
|
plan = build_effective_plan(db_session, imported.id)
|
|
new_wi = db_session.query(WorkItem).filter_by(project_id=imported.id).one()
|
|
row = next(r for r in plan["items"] if r.item.id == new_wi.id)
|
|
assert row.quantity == Decimal("100.0000") # base only, no +10
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# HTTP / UI
|
|
# ---------------------------------------------------------------------------
|
|
from fastapi.testclient import TestClient # noqa: E402
|
|
|
|
|
|
def test_ui_cancelled_shows_otpmeneno(db_session, project_id):
|
|
wi = db_session.query(WorkItem).filter_by(project_id=project_id).one()
|
|
a = _make_draft_agreement(db_session, project_id)
|
|
add_existing_item(db_session, a, wi.id, Decimal("10"))
|
|
db_session.commit()
|
|
approve_agreement(db_session, a.id)
|
|
cancel_agreement(db_session, a)
|
|
|
|
def override():
|
|
yield db_session
|
|
app.dependency_overrides[agreement_router.get_db] = override
|
|
app.dependency_overrides[project_router.get_db] = override
|
|
try:
|
|
client = TestClient(app)
|
|
page = client.get(f"/projects/{project_id}/agreements/{a.id}")
|
|
assert page.status_code == 200
|
|
assert "ОТМЕНЕНО" in page.text
|
|
assert 'data-bs-target="#cancelAgreementModal"' not in page.text
|
|
assert 'data-bs-target="#deleteAgreementModal"' not in page.text
|
|
finally:
|
|
app.dependency_overrides.clear()
|
|
|
|
|
|
def test_ui_approved_shows_cancel_button(db_session, project_id):
|
|
wi = db_session.query(WorkItem).filter_by(project_id=project_id).one()
|
|
a = _make_draft_agreement(db_session, project_id)
|
|
add_existing_item(db_session, a, wi.id, Decimal("10"))
|
|
db_session.commit()
|
|
approve_agreement(db_session, a.id)
|
|
|
|
def override():
|
|
yield db_session
|
|
app.dependency_overrides[agreement_router.get_db] = override
|
|
app.dependency_overrides[project_router.get_db] = override
|
|
try:
|
|
client = TestClient(app)
|
|
page = client.get(f"/projects/{project_id}/agreements/{a.id}")
|
|
assert page.status_code == 200
|
|
assert "Отменить ДС" in page.text
|
|
assert "cancelAgreementModal" in page.text
|
|
finally:
|
|
app.dependency_overrides.clear()
|
|
|
|
|
|
def test_http_cancel_redirects(db_session, project_id):
|
|
wi = db_session.query(WorkItem).filter_by(project_id=project_id).one()
|
|
a = _make_draft_agreement(db_session, project_id)
|
|
add_existing_item(db_session, a, wi.id, Decimal("10"))
|
|
db_session.commit()
|
|
approve_agreement(db_session, a.id)
|
|
|
|
def override():
|
|
yield db_session
|
|
app.dependency_overrides[agreement_router.get_db] = override
|
|
app.dependency_overrides[project_router.get_db] = override
|
|
try:
|
|
client = TestClient(app)
|
|
resp = client.post(
|
|
f"/projects/{project_id}/agreements/{a.id}/cancel",
|
|
follow_redirects=False,
|
|
)
|
|
assert resp.status_code == 303
|
|
assert db_session.get(Agreement, a.id).status == "cancelled"
|
|
finally:
|
|
app.dependency_overrides.clear()
|