"""Tests for UI-035: cancelled agreement status in list view.""" from __future__ import annotations from datetime import date from decimal import Decimal import pytest 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 import ( # noqa: F401 agreement, boq, daily_report, document_artifact, ks2, ks3, payment, project, work_journal, ) from app.models.agreement import Agreement from app.models.boq import BoqDocument, System, WorkItem from app.models.project import Project from app.routers import agreements as agr_router, projects as project_router from app.services.agreement_items import add_existing_item, cancel_agreement from app.services.effective_plan import approve_agreement @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 ctx(db_session): p = Project(name="UI-035") 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, source_number="1", name="Позиция", model="М", manufacturer="П", unit="шт", quantity=Decimal("100"), unit_price=Decimal("10"), amount=Decimal("1000"), ) db_session.add(wi) db_session.flush() # Draft a_draft = Agreement( project_id=p.id, number="DS-D", date=date(2026, 1, 1), title="Черновик", status="draft", boq_document_id=doc.id, ) db_session.add(a_draft) db_session.flush() # Approved a_approved = Agreement( project_id=p.id, number="DS-A", date=date(2026, 1, 2), title="Утверждённый", status="draft", boq_document_id=doc.id, ) db_session.add(a_approved) db_session.flush() add_existing_item(db_session, a_approved, wi.id, Decimal("10")) db_session.commit() approve_agreement(db_session, a_approved.id) # Cancelled a_cancelled = Agreement( project_id=p.id, number="DS-C", date=date(2026, 1, 3), title="Отменённый", status="draft", boq_document_id=doc.id, ) db_session.add(a_cancelled) db_session.flush() add_existing_item(db_session, a_cancelled, wi.id, Decimal("5")) db_session.commit() approve_agreement(db_session, a_cancelled.id) cancel_agreement(db_session, a_cancelled) db_session.commit() return { "pid": p.id, "draft_id": a_draft.id, "approved_id": a_approved.id, "cancelled_id": a_cancelled.id, } def _override(db_session): def override(): yield db_session app.dependency_overrides[agr_router.get_db] = override app.dependency_overrides[project_router.get_db] = override def _clear(): app.dependency_overrides.clear() def test_list_shows_correct_status_badges(db_session, ctx): _override(db_session) try: client = TestClient(app) html = client.get(f"/projects/{ctx['pid']}/agreements").text # Draft → Черновик assert "Черновик" in html # Approved → Утверждено assert "Утверждено" in html # Cancelled → Отменено assert "Отменено" in html # All three badges present with correct styling assert "text-bg-secondary" in html # Черновик assert "text-bg-success" in html # Утверждено assert "text-bg-danger" in html # Отменено finally: _clear() def test_cancelled_row_has_no_draft_actions(db_session, ctx): _override(db_session) try: client = TestClient(app) html = client.get(f"/projects/{ctx['pid']}/agreements").text # List should NOT have delete/approve/cancel action buttons # (list only shows "Открыть" for all rows) assert "Удалить" not in html assert "Утвердить" not in html assert 'data-bs-target="#deleteAgreementModal"' not in html assert 'data-bs-target="#cancelAgreementModal"' not in html finally: _clear() def test_cancelled_accessible_for_viewing(db_session, ctx): _override(db_session) try: client = TestClient(app) # Cancelled DS detail page loads resp = client.get(f"/projects/{ctx['pid']}/agreements/{ctx['cancelled_id']}") assert resp.status_code == 200 assert "ОТМЕНЕНО" in resp.text # Cancelled DS list entry has "Открыть" link html = client.get(f"/projects/{ctx['pid']}/agreements").text assert f"/projects/{ctx['pid']}/agreements/{ctx['cancelled_id']}" in html finally: _clear() def test_draft_still_shows_chernovik(db_session, ctx): _override(db_session) try: client = TestClient(app) html = client.get(f"/projects/{ctx['pid']}/agreements").text # Draft badge present assert 'Черновик' in html finally: _clear() def test_approved_still_shows_utverzhdeno(db_session, ctx): _override(db_session) try: client = TestClient(app) html = client.get(f"/projects/{ctx['pid']}/agreements").text assert 'Утверждено' in html finally: _clear()