"""Regression tests for task 014: boq delete/archive, storno UI, row click.""" import shutil from decimal import Decimal from pathlib import Path 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 from app.models.payment import Payment from app.routers import projects, boq as boq_router, ks2 as ks2_router, ks3 as ks3_router, payments as payments_router def _make_session(tmp_path): db_path = tmp_path / "t014-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_all(Session): def override_db(): s = Session() try: yield s finally: s.close() for dep in (projects.get_db, boq_router.get_db, ks2_router.get_db, ks3_router.get_db, payments_router.get_db): app.dependency_overrides[dep] = override_db def _clear_overrides(): for dep in (projects.get_db, boq_router.get_db, ks2_router.get_db, ks3_router.get_db, payments_router.get_db): app.dependency_overrides.pop(dep, None) def test_boq_delete_without_history(tmp_path): engine, Session = _make_session(tmp_path) _override_all(Session) try: with Session() as s: p = Project(name="BOQ del test") s.add(p) s.flush() doc = BoqDocument(project_id=p.id, name="Test doc") s.add(doc) s.flush() wi = WorkItem(project_id=p.id, boq_document_id=doc.id, name="Item", quantity=Decimal("10")) s.add(wi) s.commit() pid, did = p.id, doc.id with TestClient(app) as client: resp = client.post(f"/boq/{pid}/documents/{did}/delete", follow_redirects=False) assert resp.status_code == 303 with Session() as s: assert s.get(BoqDocument, did) is None finally: _clear_overrides() engine.dispose() def test_boq_delete_blocked_with_history(tmp_path): engine, Session = _make_session(tmp_path) _override_all(Session) try: with Session() as s: p = Project(name="BOQ hist test") s.add(p) s.flush() doc = BoqDocument(project_id=p.id, name="Doc with history") s.add(doc) s.flush() wi = WorkItem(project_id=p.id, boq_document_id=doc.id, name="Item", quantity=Decimal("10")) s.add(wi) s.flush() dr = DailyReport(project_id=p.id, report_date=__import__("datetime").date(2026, 1, 1)) s.add(dr) s.flush() s.add(DailyReportItem(daily_report_id=dr.id, work_item_id=wi.id, completed_quantity=Decimal("5"))) s.commit() pid, did = p.id, doc.id with TestClient(app) as client: resp = client.post(f"/boq/{pid}/documents/{did}/delete", follow_redirects=False) assert resp.status_code == 400 with Session() as s: assert s.get(BoqDocument, did) is not None finally: _clear_overrides() engine.dispose() def test_boq_archive_with_history(tmp_path): engine, Session = _make_session(tmp_path) _override_all(Session) try: with Session() as s: p = Project(name="BOQ arch test") s.add(p) s.flush() doc = BoqDocument(project_id=p.id, name="Doc to archive") s.add(doc) s.flush() wi = WorkItem(project_id=p.id, boq_document_id=doc.id, name="Item", quantity=Decimal("10")) s.add(wi) s.flush() dr = DailyReport(project_id=p.id, report_date=__import__("datetime").date(2026, 1, 1)) s.add(dr) s.flush() s.add(DailyReportItem(daily_report_id=dr.id, work_item_id=wi.id, completed_quantity=Decimal("5"))) s.commit() pid, did = p.id, doc.id with TestClient(app) as client: resp = client.post(f"/boq/{pid}/documents/{did}/archive", follow_redirects=False) assert resp.status_code == 303 with Session() as s: assert s.get(BoqDocument, did).version == "archived" finally: _clear_overrides() engine.dispose() def test_ks2_cancel_button_visible_for_issued(tmp_path): engine, Session = _make_session(tmp_path) _override_all(Session) try: with Session() as s: p = Project(name="KS2 cancel test") s.add(p) s.flush() doc = BoqDocument(project_id=p.id, name="Doc") s.add(doc) s.flush() wi = WorkItem(project_id=p.id, boq_document_id=doc.id, name="Item", quantity=Decimal("10"), unit_price=Decimal("100")) s.add(wi) s.flush() dr = DailyReport(project_id=p.id, report_date=__import__("datetime").date(2026, 1, 1)) s.add(dr) s.flush() s.add(DailyReportItem(daily_report_id=dr.id, work_item_id=wi.id, completed_quantity=Decimal("10"))) s.flush() ks2 = KS2(project_id=p.id, number="K1", date=__import__("datetime").date(2026, 1, 2), status="issued") s.add(ks2) s.commit() pid, kid = p.id, ks2.id with TestClient(app) as client: resp = client.get(f"/projects/{pid}/ks2/{kid}") assert resp.status_code == 200 assert "Сторнировать" in resp.text assert "cancel" in resp.text finally: _clear_overrides() engine.dispose() def test_ks2_cancelled_shows_status(tmp_path): engine, Session = _make_session(tmp_path) _override_all(Session) try: with Session() as s: p = Project(name="KS2 cancelled test") s.add(p) s.flush() ks2 = KS2(project_id=p.id, number="K1", date=__import__("datetime").date(2026, 1, 2), status="cancelled") s.add(ks2) s.commit() pid, kid = p.id, ks2.id with TestClient(app) as client: resp = client.get(f"/projects/{pid}/ks2/{kid}") assert resp.status_code == 200 assert "Сторнирован" in resp.text assert "Сторнировать" not in resp.text # button hidden finally: _clear_overrides() engine.dispose() def test_payment_cancel_button_visible(tmp_path): engine, Session = _make_session(tmp_path) _override_all(Session) try: with Session() as s: p = Project(name="Pay cancel test") s.add(p) s.flush() pay = Payment(project_id=p.id, date=__import__("datetime").date(2026, 1, 1), amount=Decimal("100"), currency="RUB", status="active") s.add(pay) s.commit() pid, payid = p.id, pay.id with TestClient(app) as client: resp = client.get(f"/projects/{pid}/payments/{payid}") assert resp.status_code == 200 assert "Сторнировать" in resp.text finally: _clear_overrides() engine.dispose() def test_clickable_rows_in_lists(tmp_path): engine, Session = _make_session(tmp_path) _override_all(Session) try: with TestClient(app) as client: for path in ["/projects/2/ks2", "/projects/2/ks3", "/projects/2/payments"]: resp = client.get(path) assert resp.status_code == 200 assert "clickable-row" in resp.text assert "data-url" in resp.text finally: _clear_overrides() engine.dispose()