9cfb341774
Tasks 008-016: KS-6a journal, KS-3 documents, payments with allocations, tech debt cleanup, financial privacy, document tree, storno mechanism, row-click UI, BOQ collapse/sticky header, and KS-2 export to Excel/Word via shared DTO architecture.
365 lines
13 KiB
Python
365 lines
13 KiB
Python
import datetime
|
|
from decimal import Decimal
|
|
|
|
import pytest
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from app.models.daily_report import DailyReport, DailyReportItem
|
|
from app.models.ks2 import KS2, KS2Item, KS2ItemSource
|
|
from app.models.ks3 import KS3, KS3KS2
|
|
from app.models.project import Project
|
|
from app.models.boq import BoqDocument, WorkItem
|
|
from app.services.ks2_service import save_draft as save_ks2_draft, issue_ks2
|
|
from app.services.ks3_service import (
|
|
KS3ValidationError, available_ks2, issue_ks3, project_ks3_totals, save_draft,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def db_session():
|
|
engine = create_engine("sqlite:///:memory:")
|
|
from app.database.database import Base
|
|
Base.metadata.create_all(engine)
|
|
Session = sessionmaker(bind=engine)
|
|
session = Session()
|
|
yield session
|
|
session.close()
|
|
|
|
|
|
@pytest.fixture
|
|
def project(db_session):
|
|
project = Project(name="KS-3 проект")
|
|
db_session.add(project)
|
|
db_session.commit()
|
|
db_session.refresh(project)
|
|
return project
|
|
|
|
|
|
@pytest.fixture
|
|
def work_item(db_session, project):
|
|
boq_doc = BoqDocument(project_id=project.id, name="Ведомость")
|
|
db_session.add(boq_doc)
|
|
db_session.flush()
|
|
wi = WorkItem(
|
|
project_id=project.id, boq_document_id=boq_doc.id,
|
|
name="Работа", unit="ед", quantity=Decimal("10"),
|
|
unit_price=Decimal("100"), amount=Decimal("1000"),
|
|
)
|
|
db_session.add(wi)
|
|
db_session.commit()
|
|
db_session.refresh(wi)
|
|
return wi
|
|
|
|
|
|
@pytest.fixture
|
|
def daily_report_item(db_session, project, work_item):
|
|
dr = DailyReport(project_id=project.id, report_date=datetime.date(2024, 1, 1))
|
|
db_session.add(dr)
|
|
db_session.flush()
|
|
dri = DailyReportItem(daily_report_id=dr.id, work_item_id=work_item.id,
|
|
completed_quantity=Decimal("10"))
|
|
db_session.add(dri)
|
|
db_session.commit()
|
|
db_session.refresh(dri)
|
|
return dri
|
|
|
|
|
|
@pytest.fixture
|
|
def issued_ks2(db_session, project, work_item, daily_report_item):
|
|
"""Create and issue a KS-2 with known amount."""
|
|
doc = save_ks2_draft(
|
|
db_session, project.id, "KS2-001", datetime.date(2024, 2, 1), "",
|
|
[(work_item.id, "5")],
|
|
reporting_period_from=datetime.date(2024, 1, 1),
|
|
reporting_period_to=datetime.date(2024, 1, 31),
|
|
)
|
|
issued = issue_ks2(db_session, project.id, doc.id)
|
|
return issued
|
|
|
|
|
|
@pytest.fixture
|
|
def issued_ks2_b(db_session, project, work_item, daily_report_item):
|
|
"""Create and issue a second KS-2."""
|
|
doc = save_ks2_draft(
|
|
db_session, project.id, "KS2-002", datetime.date(2024, 2, 15), "",
|
|
[(work_item.id, "3")],
|
|
reporting_period_from=datetime.date(2024, 1, 1),
|
|
reporting_period_to=datetime.date(2024, 1, 31),
|
|
)
|
|
issued = issue_ks2(db_session, project.id, doc.id)
|
|
return issued
|
|
|
|
|
|
DEFAULT_PERIOD_FROM = datetime.date(2024, 1, 1)
|
|
DEFAULT_PERIOD_TO = datetime.date(2024, 1, 31)
|
|
|
|
|
|
def test_create_draft(db_session, project, issued_ks2):
|
|
"""1. Создание draft КС-3."""
|
|
doc = save_draft(
|
|
db_session, project.id, "KS3-001", datetime.date(2024, 3, 1), "Тест",
|
|
[issued_ks2.id],
|
|
reporting_period_from=DEFAULT_PERIOD_FROM,
|
|
reporting_period_to=DEFAULT_PERIOD_TO,
|
|
)
|
|
assert doc.number == "KS3-001"
|
|
assert doc.status == "draft"
|
|
assert len(doc.links) == 1
|
|
assert doc.links[0].ks2_id == issued_ks2.id
|
|
|
|
|
|
def test_period_required(db_session, project, issued_ks2):
|
|
"""2. Обязательность периода."""
|
|
with pytest.raises(KS3ValidationError, match="дату начала периода"):
|
|
save_draft(
|
|
db_session, project.id, "KS3-001", datetime.date(2024, 3, 1), "",
|
|
[issued_ks2.id],
|
|
reporting_period_from=None,
|
|
reporting_period_to=DEFAULT_PERIOD_TO,
|
|
)
|
|
with pytest.raises(KS3ValidationError, match="дату окончания периода"):
|
|
save_draft(
|
|
db_session, project.id, "KS3-001", datetime.date(2024, 3, 1), "",
|
|
[issued_ks2.id],
|
|
reporting_period_from=DEFAULT_PERIOD_FROM,
|
|
reporting_period_to=None,
|
|
)
|
|
|
|
|
|
def test_period_validation(db_session, project, issued_ks2):
|
|
"""3. Проверка date_to >= date_from."""
|
|
with pytest.raises(KS3ValidationError, match="не может быть раньше"):
|
|
save_draft(
|
|
db_session, project.id, "KS3-001", datetime.date(2024, 3, 1), "",
|
|
[issued_ks2.id],
|
|
reporting_period_from=datetime.date(2024, 2, 1),
|
|
reporting_period_to=datetime.date(2024, 1, 1),
|
|
)
|
|
|
|
|
|
def test_select_single_ks2(db_session, project, issued_ks2):
|
|
"""4. Выбор одного КС-2."""
|
|
doc = save_draft(
|
|
db_session, project.id, "KS3-001", datetime.date(2024, 3, 1), "",
|
|
[issued_ks2.id],
|
|
reporting_period_from=DEFAULT_PERIOD_FROM,
|
|
reporting_period_to=DEFAULT_PERIOD_TO,
|
|
)
|
|
assert len(doc.links) == 1
|
|
assert doc.links[0].ks2_id == issued_ks2.id
|
|
|
|
|
|
def test_select_multiple_ks2(db_session, project, issued_ks2, issued_ks2_b):
|
|
"""5. Выбор нескольких КС-2."""
|
|
doc = save_draft(
|
|
db_session, project.id, "KS3-001", datetime.date(2024, 3, 1), "",
|
|
[issued_ks2.id, issued_ks2_b.id],
|
|
reporting_period_from=DEFAULT_PERIOD_FROM,
|
|
reporting_period_to=DEFAULT_PERIOD_TO,
|
|
)
|
|
assert len(doc.links) == 2
|
|
ids = {link.ks2_id for link in doc.links}
|
|
assert ids == {issued_ks2.id, issued_ks2_b.id}
|
|
|
|
|
|
def test_cannot_select_draft_ks2(db_session, project, work_item, daily_report_item):
|
|
"""6. Нельзя выбрать draft КС-2."""
|
|
draft_ks2 = save_ks2_draft(
|
|
db_session, project.id, "KS2-DRAFT", datetime.date(2024, 2, 1), "",
|
|
[(work_item.id, "5")],
|
|
reporting_period_from=DEFAULT_PERIOD_FROM,
|
|
reporting_period_to=DEFAULT_PERIOD_TO,
|
|
)
|
|
with pytest.raises(KS3ValidationError, match="не проведён"):
|
|
save_draft(
|
|
db_session, project.id, "KS3-001", datetime.date(2024, 3, 1), "",
|
|
[draft_ks2.id],
|
|
reporting_period_from=DEFAULT_PERIOD_FROM,
|
|
reporting_period_to=DEFAULT_PERIOD_TO,
|
|
)
|
|
|
|
|
|
def test_cannot_select_other_project_ks2(db_session, project, issued_ks2):
|
|
"""7. Нельзя выбрать КС-2 другого проекта."""
|
|
project2 = Project(name="Другой проект")
|
|
db_session.add(project2)
|
|
db_session.commit()
|
|
db_session.refresh(project2)
|
|
|
|
with pytest.raises(KS3ValidationError, match="не принадлежит"):
|
|
save_draft(
|
|
db_session, project2.id, "KS3-001", datetime.date(2024, 3, 1), "",
|
|
[issued_ks2.id],
|
|
reporting_period_from=DEFAULT_PERIOD_FROM,
|
|
reporting_period_to=DEFAULT_PERIOD_TO,
|
|
)
|
|
|
|
|
|
def test_ks3_amount_equals_sum_of_issued_ks2(db_session, project, issued_ks2, issued_ks2_b):
|
|
"""8. Сумма КС-3 = сумме выбранных issued КС-2."""
|
|
doc = save_draft(
|
|
db_session, project.id, "KS3-001", datetime.date(2024, 3, 1), "",
|
|
[issued_ks2.id, issued_ks2_b.id],
|
|
reporting_period_from=DEFAULT_PERIOD_FROM,
|
|
reporting_period_to=DEFAULT_PERIOD_TO,
|
|
)
|
|
totals = project_ks3_totals(db_session, project.id)
|
|
assert len(totals) == 1
|
|
# ks2 amount: 5*100=500, ks2_b amount: 3*100=300, total=800
|
|
assert totals[0]["amount"] == Decimal("800.000000000000")
|
|
|
|
|
|
def test_ks3_does_not_modify_ks2(db_session, project, issued_ks2, issued_ks2_b):
|
|
"""9. КС-3 не изменяет КС-2."""
|
|
doc = save_draft(
|
|
db_session, project.id, "KS3-001", datetime.date(2024, 3, 1), "",
|
|
[issued_ks2.id, issued_ks2_b.id],
|
|
reporting_period_from=DEFAULT_PERIOD_FROM,
|
|
reporting_period_to=DEFAULT_PERIOD_TO,
|
|
)
|
|
ks2a = db_session.query(KS2).filter(KS2.id == issued_ks2.id).first()
|
|
ks2b = db_session.query(KS2).filter(KS2.id == issued_ks2_b.id).first()
|
|
assert ks2a.status == "issued"
|
|
assert ks2b.status == "issued"
|
|
assert len(ks2a.items) == 1
|
|
assert ks2a.items[0].quantity == Decimal("5")
|
|
|
|
|
|
def test_issue_ks3(db_session, project, issued_ks2):
|
|
"""10. Выпуск КС-3."""
|
|
doc = save_draft(
|
|
db_session, project.id, "KS3-001", datetime.date(2024, 3, 1), "",
|
|
[issued_ks2.id],
|
|
reporting_period_from=DEFAULT_PERIOD_FROM,
|
|
reporting_period_to=DEFAULT_PERIOD_TO,
|
|
)
|
|
issued = issue_ks3(db_session, project.id, doc.id)
|
|
assert issued.status == "issued"
|
|
|
|
|
|
def test_issued_ks3_cannot_be_edited(db_session, project, issued_ks2):
|
|
"""11. Issued КС-3 нельзя редактировать."""
|
|
doc = save_draft(
|
|
db_session, project.id, "KS3-001", datetime.date(2024, 3, 1), "",
|
|
[issued_ks2.id],
|
|
reporting_period_from=DEFAULT_PERIOD_FROM,
|
|
reporting_period_to=DEFAULT_PERIOD_TO,
|
|
)
|
|
issue_ks3(db_session, project.id, doc.id)
|
|
|
|
with pytest.raises(KS3ValidationError, match="Проведённый КС-3 нельзя редактировать"):
|
|
save_draft(
|
|
db_session, project.id, "KS3-001", datetime.date(2024, 3, 2), "edit",
|
|
[issued_ks2.id],
|
|
reporting_period_from=DEFAULT_PERIOD_FROM,
|
|
reporting_period_to=DEFAULT_PERIOD_TO,
|
|
ks3_id=doc.id,
|
|
)
|
|
|
|
|
|
def test_duplicate_ks2_in_same_ks3_rejected(db_session, project, issued_ks2):
|
|
"""12. Повторный выбор одного КС-2 внутри одного КС-3 запрещён."""
|
|
with pytest.raises(KS3ValidationError, match="дважды"):
|
|
save_draft(
|
|
db_session, project.id, "KS3-001", datetime.date(2024, 3, 1), "",
|
|
[issued_ks2.id, issued_ks2.id],
|
|
reporting_period_from=DEFAULT_PERIOD_FROM,
|
|
reporting_period_to=DEFAULT_PERIOD_TO,
|
|
)
|
|
|
|
|
|
def test_issue_atomicity(db_session, project, work_item, daily_report_item):
|
|
"""13. Выпуск атомарен при изменении состояния КС-2."""
|
|
ks2_doc = save_ks2_draft(
|
|
db_session, project.id, "KS2-X", datetime.date(2024, 2, 1), "",
|
|
[(work_item.id, "2")],
|
|
reporting_period_from=DEFAULT_PERIOD_FROM,
|
|
reporting_period_to=DEFAULT_PERIOD_TO,
|
|
)
|
|
ks2_issued = issue_ks2(db_session, project.id, ks2_doc.id)
|
|
|
|
ks3_doc = save_draft(
|
|
db_session, project.id, "KS3-001", datetime.date(2024, 3, 1), "",
|
|
[ks2_issued.id],
|
|
reporting_period_from=DEFAULT_PERIOD_FROM,
|
|
reporting_period_to=DEFAULT_PERIOD_TO,
|
|
)
|
|
|
|
# Change KS-2 back to draft to simulate invalid state at issue time
|
|
ks2_issued.status = "draft"
|
|
db_session.commit()
|
|
|
|
with pytest.raises(KS3ValidationError, match="не проведён"):
|
|
issue_ks3(db_session, project.id, ks3_doc.id)
|
|
|
|
# KS-3 remains draft
|
|
ks3 = db_session.query(KS3).filter(KS3.id == ks3_doc.id).first()
|
|
assert ks3.status == "draft"
|
|
|
|
|
|
def test_ks3_count_on_project_page():
|
|
"""Счётчик КС-3 отображается на странице проекта."""
|
|
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.routers import projects as project_router, ks3 as ks3_router
|
|
|
|
engine = create_engine("sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool)
|
|
Base.metadata.create_all(engine)
|
|
TestingSession = sessionmaker(bind=engine, expire_on_commit=False)
|
|
|
|
with TestingSession() as db:
|
|
proj = Project(name="Count test")
|
|
db.add(proj)
|
|
db.commit()
|
|
pid = proj.id
|
|
|
|
def override_db():
|
|
db = TestingSession()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
for dep in (project_router.get_db, ks3_router.get_db):
|
|
app.dependency_overrides[dep] = override_db
|
|
try:
|
|
with TestClient(app) as client:
|
|
# No KS-3 yet — no count shown
|
|
page = client.get(f"/projects/{pid}")
|
|
assert page.status_code == 200
|
|
assert "КС-3" in page.text
|
|
|
|
# Create a KS-3 directly in DB
|
|
with TestingSession() as db:
|
|
doc = KS3(project_id=pid, number="C-001", status="draft",
|
|
date=datetime.date(2024, 1, 1),
|
|
reporting_period_from=datetime.date(2024, 1, 1),
|
|
reporting_period_to=datetime.date(2024, 1, 31))
|
|
db.add(doc)
|
|
db.commit()
|
|
|
|
page2 = client.get(f"/projects/{pid}")
|
|
assert page2.status_code == 200
|
|
assert "КС-3 (1)" in page2.text
|
|
|
|
# Add a second one
|
|
with TestingSession() as db:
|
|
doc2 = KS3(project_id=pid, number="C-002", status="draft",
|
|
date=datetime.date(2024, 2, 1),
|
|
reporting_period_from=datetime.date(2024, 2, 1),
|
|
reporting_period_to=datetime.date(2024, 2, 29))
|
|
db.add(doc2)
|
|
db.commit()
|
|
|
|
page3 = client.get(f"/projects/{pid}")
|
|
assert page3.status_code == 200
|
|
assert "КС-3 (2)" in page3.text
|
|
finally:
|
|
for dep in (project_router.get_db, ks3_router.get_db):
|
|
app.dependency_overrides.pop(dep, None)
|
|
engine.dispose() |