feat: enforce KS-2/KS-3 stornation integrity
This commit is contained in:
@@ -0,0 +1,484 @@
|
||||
"""Regression tests for task 022: KS-2 / KS-3 stornation integrity."""
|
||||
import datetime
|
||||
import io
|
||||
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, KS3KS2
|
||||
from app.models.payment import Payment, PaymentAllocation
|
||||
from app.routers import projects, ks2 as ks2_router, ks3 as ks3_router, payments as payments_router
|
||||
|
||||
|
||||
def _make_session(tmp_path):
|
||||
db_path = tmp_path / "t022-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, payments_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, payments_router.get_db):
|
||||
app.dependency_overrides.pop(dep, None)
|
||||
|
||||
|
||||
def _setup(Session):
|
||||
with Session() as s:
|
||||
p = Project(name="Storno 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()
|
||||
dri = DailyReportItem(daily_report_id=dr.id, work_item_id=wi.id, completed_quantity=Decimal("10"))
|
||||
s.add(dri)
|
||||
s.commit()
|
||||
return {"pid": p.id, "wi_id": wi.id, "dri_id": dri.id}
|
||||
|
||||
|
||||
def _add_ks2(Session, pid, wi_id, number="1", status="issued"):
|
||||
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, ks2_id=None, number="1", status="issued"):
|
||||
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.flush()
|
||||
if ks2_id is not None:
|
||||
s.add(KS3KS2(ks3_id=doc.id, ks2_id=ks2_id))
|
||||
s.commit()
|
||||
return doc.id
|
||||
|
||||
|
||||
def _add_payment_alloc(Session, pid, ks3_id, amount="5"):
|
||||
with Session() as s:
|
||||
payment = Payment(project_id=pid, date=datetime.date(2026, 3, 20),
|
||||
amount=Decimal(amount), currency="RUB", status="active")
|
||||
s.add(payment)
|
||||
s.flush()
|
||||
s.add(PaymentAllocation(payment_id=payment.id, ks3_id=ks3_id, amount=Decimal(amount)))
|
||||
s.commit()
|
||||
return payment.id
|
||||
|
||||
|
||||
# --- КС-2 ---
|
||||
|
||||
def test_ks2_cancel_allowed_without_ks3(tmp_path):
|
||||
engine, Session = _make_session(tmp_path)
|
||||
try:
|
||||
data = _setup(Session)
|
||||
ks2_id = _add_ks2(Session, data["pid"], data["wi_id"])
|
||||
from app.services.ks2_service import cancel_ks2
|
||||
with Session() as s:
|
||||
doc = cancel_ks2(s, data["pid"], ks2_id)
|
||||
assert doc.status == "cancelled"
|
||||
finally:
|
||||
engine.dispose()
|
||||
|
||||
|
||||
def test_ks2_cancel_blocked_by_active_ks3(tmp_path):
|
||||
engine, Session = _make_session(tmp_path)
|
||||
try:
|
||||
data = _setup(Session)
|
||||
ks2_id = _add_ks2(Session, data["pid"], data["wi_id"])
|
||||
_add_ks3(Session, data["pid"], ks2_id=ks2_id, status="issued")
|
||||
from app.services.ks2_service import KS2ValidationError, cancel_ks2
|
||||
with Session() as s:
|
||||
try:
|
||||
cancel_ks2(s, data["pid"], ks2_id)
|
||||
raise AssertionError("must be blocked")
|
||||
except KS2ValidationError as exc:
|
||||
assert "КС-3" in str(exc)
|
||||
with Session() as s:
|
||||
assert s.get(KS2, ks2_id).status == "issued"
|
||||
finally:
|
||||
engine.dispose()
|
||||
|
||||
|
||||
def test_ks2_cancel_allowed_if_only_cancelled_ks3(tmp_path):
|
||||
engine, Session = _make_session(tmp_path)
|
||||
try:
|
||||
data = _setup(Session)
|
||||
ks2_id = _add_ks2(Session, data["pid"], data["wi_id"])
|
||||
_add_ks3(Session, data["pid"], ks2_id=ks2_id, status="cancelled")
|
||||
from app.services.ks2_service import cancel_ks2
|
||||
with Session() as s:
|
||||
doc = cancel_ks2(s, data["pid"], ks2_id)
|
||||
assert doc.status == "cancelled"
|
||||
finally:
|
||||
engine.dispose()
|
||||
|
||||
|
||||
def test_cancelled_ks2_excluded_from_new_ks3_selection(tmp_path):
|
||||
engine, Session = _make_session(tmp_path)
|
||||
_override(Session)
|
||||
try:
|
||||
data = _setup(Session)
|
||||
ks2_id = _add_ks2(Session, data["pid"], data["wi_id"], number="1")
|
||||
_add_ks2(Session, data["pid"], data["wi_id"], number="2", status="cancelled")
|
||||
with TestClient(app) as client:
|
||||
page = client.get(f"/projects/{data['pid']}/ks3/new")
|
||||
assert page.status_code == 200
|
||||
assert 'value="%d"' % ks2_id in page.text or f'value="{ks2_id}"' in page.text
|
||||
from app.services.ks3_service import available_ks2
|
||||
with Session() as s:
|
||||
rows = available_ks2(s, data["pid"])
|
||||
ids = [r["id"] for r in rows]
|
||||
assert ks2_id in ids
|
||||
assert len(ids) == 1
|
||||
finally:
|
||||
_clear()
|
||||
engine.dispose()
|
||||
|
||||
|
||||
def test_cancelled_ks2_remains_visible_in_history(tmp_path):
|
||||
engine, Session = _make_session(tmp_path)
|
||||
_override(Session)
|
||||
try:
|
||||
data = _setup(Session)
|
||||
ks2_id = _add_ks2(Session, data["pid"], data["wi_id"], status="cancelled")
|
||||
with TestClient(app) as client:
|
||||
page = client.get(f"/projects/{data['pid']}/ks2/{ks2_id}")
|
||||
assert page.status_code == 200
|
||||
assert "Сторнирован" in page.text
|
||||
listing = client.get(f"/projects/{data['pid']}/ks2")
|
||||
assert listing.status_code == 200
|
||||
assert "1" in listing.text
|
||||
finally:
|
||||
_clear()
|
||||
engine.dispose()
|
||||
|
||||
|
||||
def test_cancel_ks2_does_not_change_daily_reports(tmp_path):
|
||||
engine, Session = _make_session(tmp_path)
|
||||
try:
|
||||
data = _setup(Session)
|
||||
ks2_id = _add_ks2(Session, data["pid"], data["wi_id"])
|
||||
from app.services.ks2_service import cancel_ks2
|
||||
with Session() as s:
|
||||
cancel_ks2(s, data["pid"], ks2_id)
|
||||
with Session() as s:
|
||||
dri = s.get(DailyReportItem, data["dri_id"])
|
||||
assert dri.completed_quantity == Decimal("10")
|
||||
finally:
|
||||
engine.dispose()
|
||||
|
||||
|
||||
def test_double_cancel_ks2_impossible(tmp_path):
|
||||
engine, Session = _make_session(tmp_path)
|
||||
try:
|
||||
data = _setup(Session)
|
||||
ks2_id = _add_ks2(Session, data["pid"], data["wi_id"], status="cancelled")
|
||||
from app.services.ks2_service import KS2ValidationError, cancel_ks2
|
||||
with Session() as s:
|
||||
try:
|
||||
cancel_ks2(s, data["pid"], ks2_id)
|
||||
raise AssertionError("must fail")
|
||||
except KS2ValidationError as exc:
|
||||
assert "уже сторнирован" in str(exc)
|
||||
finally:
|
||||
engine.dispose()
|
||||
|
||||
|
||||
# --- КС-3 ---
|
||||
|
||||
def test_ks3_cancel_allowed_without_payments(tmp_path):
|
||||
engine, Session = _make_session(tmp_path)
|
||||
try:
|
||||
data = _setup(Session)
|
||||
ks2_id = _add_ks2(Session, data["pid"], data["wi_id"])
|
||||
ks3_id = _add_ks3(Session, data["pid"], ks2_id=ks2_id)
|
||||
from app.services.ks3_service import cancel_ks3
|
||||
with Session() as s:
|
||||
doc = cancel_ks3(s, data["pid"], ks3_id)
|
||||
assert doc.status == "cancelled"
|
||||
finally:
|
||||
engine.dispose()
|
||||
|
||||
|
||||
def test_ks3_cancel_blocked_by_payment_allocation(tmp_path):
|
||||
engine, Session = _make_session(tmp_path)
|
||||
try:
|
||||
data = _setup(Session)
|
||||
ks2_id = _add_ks2(Session, data["pid"], data["wi_id"])
|
||||
ks3_id = _add_ks3(Session, data["pid"], ks2_id=ks2_id)
|
||||
_add_payment_alloc(Session, data["pid"], ks3_id)
|
||||
from app.services.ks3_service import KS3ValidationError, cancel_ks3
|
||||
with Session() as s:
|
||||
try:
|
||||
cancel_ks3(s, data["pid"], ks3_id)
|
||||
raise AssertionError("must be blocked")
|
||||
except KS3ValidationError as exc:
|
||||
assert "платеж" in str(exc).lower()
|
||||
with Session() as s:
|
||||
assert s.get(KS3, ks3_id).status == "issued"
|
||||
finally:
|
||||
engine.dispose()
|
||||
|
||||
|
||||
def test_cancelled_ks3_excluded_from_payment_selection(tmp_path):
|
||||
engine, Session = _make_session(tmp_path)
|
||||
try:
|
||||
data = _setup(Session)
|
||||
ks2_id = _add_ks2(Session, data["pid"], data["wi_id"])
|
||||
_add_ks3(Session, data["pid"], ks2_id=ks2_id, number="1", status="cancelled")
|
||||
_add_ks3(Session, data["pid"], ks2_id=ks2_id, number="2", status="issued")
|
||||
from app.services.payment_service import available_ks3_for_payment
|
||||
with Session() as s:
|
||||
rows = available_ks3_for_payment(s, data["pid"])
|
||||
numbers = [r["number"] for r in rows]
|
||||
assert "2" in numbers
|
||||
assert "1" not in numbers
|
||||
finally:
|
||||
engine.dispose()
|
||||
|
||||
|
||||
def test_double_cancel_ks3_impossible(tmp_path):
|
||||
engine, Session = _make_session(tmp_path)
|
||||
try:
|
||||
data = _setup(Session)
|
||||
ks3_id = _add_ks3(Session, data["pid"], status="cancelled")
|
||||
from app.services.ks3_service import KS3ValidationError, cancel_ks3
|
||||
with Session() as s:
|
||||
try:
|
||||
cancel_ks3(s, data["pid"], ks3_id)
|
||||
raise AssertionError("must fail")
|
||||
except KS3ValidationError as exc:
|
||||
assert "уже сторнирован" in str(exc)
|
||||
finally:
|
||||
engine.dispose()
|
||||
|
||||
|
||||
# --- Экспорт ---
|
||||
|
||||
def test_issued_ks2_export_without_storno_mark(tmp_path):
|
||||
engine, Session = _make_session(tmp_path)
|
||||
_override(Session)
|
||||
try:
|
||||
data = _setup(Session)
|
||||
ks2_id = _add_ks2(Session, data["pid"], data["wi_id"], status="issued")
|
||||
with TestClient(app) as client:
|
||||
resp = client.get(f"/projects/{data['pid']}/ks2/{ks2_id}/export/excel")
|
||||
from openpyxl import load_workbook
|
||||
text = "\n".join(
|
||||
str(c.value) for row in load_workbook(io.BytesIO(resp.content)).active.iter_rows()
|
||||
for c in row if c.value is not None
|
||||
)
|
||||
assert "СТОРНИРОВАН" not in text
|
||||
finally:
|
||||
_clear()
|
||||
engine.dispose()
|
||||
|
||||
|
||||
def test_cancelled_ks2_export_marked_in_excel(tmp_path):
|
||||
engine, Session = _make_session(tmp_path)
|
||||
_override(Session)
|
||||
try:
|
||||
data = _setup(Session)
|
||||
ks2_id = _add_ks2(Session, data["pid"], data["wi_id"], status="cancelled")
|
||||
with TestClient(app) as client:
|
||||
resp = client.get(f"/projects/{data['pid']}/ks2/{ks2_id}/export/excel")
|
||||
from openpyxl import load_workbook
|
||||
text = "\n".join(
|
||||
str(c.value) for row in load_workbook(io.BytesIO(resp.content)).active.iter_rows()
|
||||
for c in row if c.value is not None
|
||||
)
|
||||
assert "СТОРНИРОВАН" in text
|
||||
finally:
|
||||
_clear()
|
||||
engine.dispose()
|
||||
|
||||
|
||||
def test_cancelled_ks2_export_marked_in_word(tmp_path):
|
||||
engine, Session = _make_session(tmp_path)
|
||||
_override(Session)
|
||||
try:
|
||||
data = _setup(Session)
|
||||
ks2_id = _add_ks2(Session, data["pid"], data["wi_id"], status="cancelled")
|
||||
with TestClient(app) as client:
|
||||
resp = client.get(f"/projects/{data['pid']}/ks2/{ks2_id}/export/word")
|
||||
from docx import Document
|
||||
d = Document(io.BytesIO(resp.content))
|
||||
text = "\n".join(p.text for p in d.paragraphs)
|
||||
assert "СТОРНИРОВАН" in text
|
||||
finally:
|
||||
_clear()
|
||||
engine.dispose()
|
||||
|
||||
|
||||
def test_issued_ks3_export_without_storno_mark(tmp_path):
|
||||
engine, Session = _make_session(tmp_path)
|
||||
_override(Session)
|
||||
try:
|
||||
data = _setup(Session)
|
||||
ks2_id = _add_ks2(Session, data["pid"], data["wi_id"])
|
||||
ks3_id = _add_ks3(Session, data["pid"], ks2_id=ks2_id, status="issued")
|
||||
with TestClient(app) as client:
|
||||
resp = client.get(f"/projects/{data['pid']}/ks3/{ks3_id}/export/excel")
|
||||
from openpyxl import load_workbook
|
||||
text = "\n".join(
|
||||
str(c.value) for row in load_workbook(io.BytesIO(resp.content)).active.iter_rows()
|
||||
for c in row if c.value is not None
|
||||
)
|
||||
assert "СТОРНИРОВАН" not in text
|
||||
finally:
|
||||
_clear()
|
||||
engine.dispose()
|
||||
|
||||
|
||||
def test_cancelled_ks3_export_marked_in_excel_and_word(tmp_path):
|
||||
engine, Session = _make_session(tmp_path)
|
||||
_override(Session)
|
||||
try:
|
||||
data = _setup(Session)
|
||||
ks2_id = _add_ks2(Session, data["pid"], data["wi_id"])
|
||||
ks3_id = _add_ks3(Session, data["pid"], ks2_id=ks2_id, status="cancelled")
|
||||
with TestClient(app) as client:
|
||||
excel = client.get(f"/projects/{data['pid']}/ks3/{ks3_id}/export/excel")
|
||||
from openpyxl import load_workbook
|
||||
text = "\n".join(
|
||||
str(c.value) for row in load_workbook(io.BytesIO(excel.content)).active.iter_rows()
|
||||
for c in row if c.value is not None
|
||||
)
|
||||
assert "СТОРНИРОВАН" in text
|
||||
|
||||
word = client.get(f"/projects/{data['pid']}/ks3/{ks3_id}/export/word")
|
||||
from docx import Document
|
||||
d = Document(io.BytesIO(word.content))
|
||||
wtext = "\n".join(p.text for p in d.paragraphs)
|
||||
assert "СТОРНИРОВАН" in wtext
|
||||
finally:
|
||||
_clear()
|
||||
engine.dispose()
|
||||
|
||||
|
||||
# --- Агрегаты ---
|
||||
|
||||
def test_cancelled_ks2_excluded_from_active_financial_totals(tmp_path):
|
||||
engine, Session = _make_session(tmp_path)
|
||||
try:
|
||||
data = _setup(Session)
|
||||
from app.models.ks2 import KS2ItemSource
|
||||
with Session() as s:
|
||||
ks2 = KS2(project_id=data["pid"], number="1", date=datetime.date(2026, 3, 10),
|
||||
status="issued",
|
||||
reporting_period_from=datetime.date(2026, 3, 1),
|
||||
reporting_period_to=datetime.date(2026, 3, 31))
|
||||
s.add(ks2)
|
||||
s.flush()
|
||||
item = KS2Item(ks2_id=ks2.id, work_item_id=data["wi_id"],
|
||||
quantity=Decimal("2"), unit_price=Decimal("10"), amount=Decimal("20"))
|
||||
s.add(item)
|
||||
s.flush()
|
||||
s.add(KS2ItemSource(ks2_item_id=item.id, daily_report_item_id=data["dri_id"],
|
||||
quantity=Decimal("2")))
|
||||
cancelled = KS2(project_id=data["pid"], number="2", date=datetime.date(2026, 3, 11),
|
||||
status="cancelled",
|
||||
reporting_period_from=datetime.date(2026, 3, 1),
|
||||
reporting_period_to=datetime.date(2026, 3, 31))
|
||||
s.add(cancelled)
|
||||
s.flush()
|
||||
s.add(KS2Item(ks2_id=cancelled.id, work_item_id=data["wi_id"],
|
||||
quantity=Decimal("3"), unit_price=Decimal("10"), amount=Decimal("30")))
|
||||
s.commit()
|
||||
|
||||
from app.services.ks2_service import build_work_item_balances
|
||||
with Session() as s:
|
||||
balances = build_work_item_balances(s, data["pid"])
|
||||
# Only issued KS-2 source closes quantity; cancelled is ignored
|
||||
assert balances[0].closed_quantity == Decimal("2")
|
||||
finally:
|
||||
engine.dispose()
|
||||
|
||||
|
||||
def test_cancelled_ks3_excluded_from_payment_summary(tmp_path):
|
||||
engine, Session = _make_session(tmp_path)
|
||||
try:
|
||||
data = _setup(Session)
|
||||
ks2_id = _add_ks2(Session, data["pid"], data["wi_id"])
|
||||
_add_ks3(Session, data["pid"], ks2_id=ks2_id, number="1", status="issued")
|
||||
_add_ks3(Session, data["pid"], ks2_id=ks2_id, number="2", status="cancelled")
|
||||
from app.services.payment_service import ks3_payment_summary
|
||||
with Session() as s:
|
||||
summary = ks3_payment_summary(s, data["pid"])
|
||||
assert summary["ks3_count"] == 1
|
||||
finally:
|
||||
engine.dispose()
|
||||
|
||||
|
||||
def test_daily_report_fact_unchanged_after_storno(tmp_path):
|
||||
engine, Session = _make_session(tmp_path)
|
||||
try:
|
||||
data = _setup(Session)
|
||||
ks2_id = _add_ks2(Session, data["pid"], data["wi_id"])
|
||||
from app.services.ks2_service import cancel_ks2
|
||||
with Session() as s:
|
||||
cancel_ks2(s, data["pid"], ks2_id)
|
||||
with Session() as s:
|
||||
assert s.get(DailyReportItem, data["dri_id"]).completed_quantity == Decimal("10")
|
||||
from app.services.ks2_service import build_work_item_balances
|
||||
balances = build_work_item_balances(s, data["pid"])
|
||||
assert balances[0].actual_quantity == Decimal("10")
|
||||
assert balances[0].closed_quantity == Decimal("0")
|
||||
finally:
|
||||
engine.dispose()
|
||||
|
||||
|
||||
def test_ui_shows_block_reason_for_ks2(tmp_path):
|
||||
engine, Session = _make_session(tmp_path)
|
||||
_override(Session)
|
||||
try:
|
||||
data = _setup(Session)
|
||||
ks2_id = _add_ks2(Session, data["pid"], data["wi_id"])
|
||||
_add_ks3(Session, data["pid"], ks2_id=ks2_id, status="issued")
|
||||
with TestClient(app) as client:
|
||||
page = client.get(f"/projects/{data['pid']}/ks2/{ks2_id}")
|
||||
assert page.status_code == 200
|
||||
assert "Сторнирование недоступно" in page.text
|
||||
assert "СТОРНИРОВАТЬ" not in page.text.upper().replace("СТОРНИРОВАНИЕ", "")
|
||||
finally:
|
||||
_clear()
|
||||
engine.dispose()
|
||||
Reference in New Issue
Block a user