Files
Арсентьев Александр 9cfb341774 feat: add KS-3/payments, tech debt fixes, UI polish, and KS-2 Excel/Word export
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.
2026-09-27 13:38:45 +03:00

196 lines
9.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import datetime
from decimal import Decimal
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.project import Project
from app.models import boq
from app.models.daily_report import DailyReport, DailyReportItem
from app.models.ks2 import KS2, KS2Item, KS2ItemSource
from app.routers import projects as project_router, ks6a as ks6a_router
def test_ks6a_full_workflow():
"""Интеграционный тест КС-6а: все 11 требований."""
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:
project = Project(name="KS-6a проект")
db.add(project)
db.flush()
system_a = boq.System(project_id=project.id, name="Система A", sort_order=1)
system_b = boq.System(project_id=project.id, name="Система B", sort_order=2)
db.add_all([system_a, system_b])
db.flush()
section_a = boq.Section(system_id=system_a.id, name="Раздел A1")
db.add(section_a)
db.flush()
doc = boq.BoqDocument(project_id=project.id, name="Ведомость")
db.add(doc)
db.flush()
wi1 = boq.WorkItem(
project_id=project.id, boq_document_id=doc.id,
system_id=system_a.id, section_id=section_a.id,
source_number="1.1", name="Монтаж трубы", unit="м",
quantity=Decimal("100"), unit_price=Decimal("50"), amount=Decimal("5000"),
)
wi2 = boq.WorkItem(
project_id=project.id, boq_document_id=doc.id,
system_id=system_b.id,
source_number="2.1", name="Укладка кабеля", unit="м",
quantity=Decimal("200"), unit_price=Decimal("30"), amount=Decimal("6000"),
)
db.add_all([wi1, wi2])
db.flush()
# Daily Reports в разные даты
dr1 = DailyReport(project_id=project.id, report_date=datetime.date(2024, 3, 1))
dr2 = DailyReport(project_id=project.id, report_date=datetime.date(2024, 3, 5))
dr3 = DailyReport(project_id=project.id, report_date=datetime.date(2024, 3, 10))
db.add_all([dr1, dr2, dr3])
db.flush()
# 5 DailyReportItem: wi1 в 3 разных даты, wi2 в 2 даты
dri1 = DailyReportItem(daily_report_id=dr1.id, work_item_id=wi1.id, completed_quantity=Decimal("10"))
dri2 = DailyReportItem(daily_report_id=dr2.id, work_item_id=wi1.id, completed_quantity=Decimal("15"))
dri3 = DailyReportItem(daily_report_id=dr3.id, work_item_id=wi1.id, completed_quantity=Decimal("5"))
dri4 = DailyReportItem(daily_report_id=dr1.id, work_item_id=wi2.id, completed_quantity=Decimal("20"))
dri5 = DailyReportItem(daily_report_id=dr2.id, work_item_id=wi2.id, completed_quantity=Decimal("30"))
db.add_all([dri1, dri2, dri3, dri4, dri5])
db.commit()
pid = project.id
sa_id = system_a.id
sb_id = system_b.id
def override_db():
db = TestingSession()
try:
yield db
finally:
db.close()
for dep in (project_router.get_db, ks6a_router.get_db):
app.dependency_overrides[dep] = override_db
try:
with TestClient(app) as client:
# === 1. Страница КС-6а открывается ===
page = client.get(f"/projects/{pid}/ks6a")
assert page.status_code == 200
assert "КС-6а" in page.text
assert "Журнал выполненных работ" in page.text
# === 2. Отображается выбранный период ===
report = client.get(f"/projects/{pid}/ks6a?date_from=2024-03-01&date_to=2024-03-31")
assert report.status_code == 200
assert "2024-03-01" in report.text
assert "2024-03-31" in report.text
# === 3. Все DailyReportItem попадают в журнал ===
assert "Монтаж трубы" in report.text
assert "Укладка кабеля" in report.text
assert "Строк: 5" in report.text
# === 4. Каждая DailyReportItem — отдельная строка ===
# 3 строки wi1 + 2 строки wi2 = 5 строк
assert report.text.count("Монтаж трубы") == 3
assert report.text.count("Укладка кабеля") == 2
# === 5. Фильтр по системе работает ===
sys_a_report = client.get(
f"/projects/{pid}/ks6a?date_from=2024-03-01&date_to=2024-03-31&system_id={sa_id}"
)
assert sys_a_report.status_code == 200
assert "Монтаж трубы" in sys_a_report.text
assert "Укладка кабеля" not in sys_a_report.text
assert "Строк: 3" in sys_a_report.text
# === 6. Фильтр по периоду работает ===
short_period = client.get(
f"/projects/{pid}/ks6a?date_from=2024-03-01&date_to=2024-03-03"
)
assert short_period.status_code == 200
# Только dr1 (01.03)
assert "Строк: 2" in short_period.text # dri1 + dri4
# === 7. date_to < date_from отклоняется ===
bad_dates = client.get(
f"/projects/{pid}/ks6a?date_from=2024-03-10&date_to=2024-03-01"
)
assert bad_dates.status_code == 200
assert "не может быть раньше" in bad_dates.text
# === 8. KS-2 не влияет на наличие строки КС-6а ===
# Проведём KS-2 по wi1
with TestingSession() as db:
ks2_doc = KS2(project_id=pid, number="KS-001", status="draft",
date=datetime.date(2024, 3, 15),
reporting_period_from=datetime.date(2024, 3, 1),
reporting_period_to=datetime.date(2024, 3, 31))
db.add(ks2_doc)
db.flush()
ks2_item = KS2Item(ks2_id=ks2_doc.id, work_item_id=wi1.id,
quantity=Decimal("10"), unit_price=Decimal("50"),
amount=Decimal("500"))
db.add(ks2_item)
db.flush()
# Проведём вручную
ks2_doc.status = "issued"
db.add(KS2ItemSource(ks2_item_id=ks2_item.id,
daily_report_item_id=dri1.id,
quantity=Decimal("10")))
db.commit()
# КС-6а всё равно показывает все 5 строк
after_ks2 = client.get(
f"/projects/{pid}/ks6a?date_from=2024-03-01&date_to=2024-03-31"
)
assert after_ks2.status_code == 200
assert "Строк: 5" in after_ks2.text
assert "Монтаж трубы" in after_ks2.text
# === 9. Цены и суммы не попадают в КС-6а ===
# Нет денежных символов и формата "5 000"
assert "₽" not in after_ks2.text
assert "5 000" not in after_ks2.text
# Проверяем что в строках таблицы нет unit_price как отдельного значения
# Цена/Сумма отсутствуют как колонки
assert "Цена" not in after_ks2.text
assert "Сумма" not in after_ks2.text
# === 10. Несколько DailyReportItem одного WorkItem не агрегируются ===
# wi1 имеет 3 строки в разные даты — проверяем что все 3 отображаются
all_dates = client.get(
f"/projects/{pid}/ks6a?date_from=2024-03-01&date_to=2024-03-31&system_id={sa_id}"
)
assert all_dates.status_code == 200
assert "01.03.2024" in all_dates.text
assert "05.03.2024" in all_dates.text
assert "10.03.2024" in all_dates.text
assert "Строк: 3" in all_dates.text
# === 11. Количество отображается с двумя знаками ===
# Ищем формат "10.00", "15.00", "5.00", "20.00", "30.00"
assert "10.00" in after_ks2.text
assert "15.00" in after_ks2.text
assert "5.00" in after_ks2.text
# Кнопка КС-6а на странице проекта
project_page = client.get(f"/projects/{pid}")
assert project_page.status_code == 200
assert "КС-6а" in project_page.text
finally:
for dep in (project_router.get_db, ks6a_router.get_db):
app.dependency_overrides.pop(dep, None)
engine.dispose()