"""Regression tests for task 013: privacy, project lifecycle, payments, tree.""" 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.payment import Payment from app.models.ks2 import KS2 from app.models.ks3 import KS3 from app.routers import projects def _make_session(tmp_path): db_path = tmp_path / "t013-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 test_privacy_default_hidden_and_toggle(tmp_path): engine, Session = _make_session(tmp_path) def override_db(): s = Session() try: yield s finally: s.close() app.dependency_overrides[projects.get_db] = override_db try: with TestClient(app) as client: resp = client.get("/projects/2") assert resp.status_code == 200 assert 'class="bg-light money-hidden"' in resp.text assert 'id="toggleMoney"' in resp.text finally: app.dependency_overrides.pop(projects.get_db, None) engine.dispose() def test_ks3_privacy_mechanism(tmp_path): engine, Session = _make_session(tmp_path) def override_db(): s = Session() try: yield s finally: s.close() app.dependency_overrides[projects.get_db] = override_db try: with TestClient(app) as client: resp = client.get("/projects/2/ks3") assert resp.status_code == 200 assert 'id="toggleMoney"' in resp.text assert 'money-hidden' in resp.text finally: app.dependency_overrides.pop(projects.get_db, None) engine.dispose() def test_project_delete_without_history(tmp_path): engine, Session = _make_session(tmp_path) def override_db(): s = Session() try: yield s finally: s.close() app.dependency_overrides[projects.get_db] = override_db try: with TestClient(app) as client: with Session() as s: p = Project(name="Delete me") s.add(p) s.commit() pid = p.id resp = client.post(f"/projects/{pid}/delete", follow_redirects=False) assert resp.status_code == 303 with Session() as s: assert s.get(Project, pid) is None finally: app.dependency_overrides.pop(projects.get_db, None) engine.dispose() def test_project_archive(tmp_path): engine, Session = _make_session(tmp_path) def override_db(): s = Session() try: yield s finally: s.close() app.dependency_overrides[projects.get_db] = override_db try: with TestClient(app) as client: with Session() as s: p = Project(name="Archive me") s.add(p) s.commit() pid = p.id resp = client.post(f"/projects/{pid}/archive", follow_redirects=False) assert resp.status_code == 303 with Session() as s: assert s.get(Project, pid).status == "archived" finally: app.dependency_overrides.pop(projects.get_db, None) engine.dispose() def test_payment_summary_received_vs_distributed(tmp_path): from app.services.payment_service import ks3_payment_summary engine, Session = _make_session(tmp_path) with Session() as s: from app.models.ks3 import KS3KS2 from app.models.ks2 import KS2Item from app.models.boq import WorkItem from app.models.daily_report import DailyReport, DailyReportItem from app.models.payment import PaymentAllocation p = Project(name="Pay split test") s.add(p) s.flush() wi = WorkItem(project_id=p.id, name="Test 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() dri = DailyReportItem(daily_report_id=dr.id, work_item_id=wi.id, completed_quantity=Decimal("10")) s.add(dri) s.flush() ks2 = KS2(project_id=p.id, number="K1", date=__import__("datetime").date(2026, 1, 2), status="issued") s.add(ks2) s.flush() ks2i = KS2Item(ks2_id=ks2.id, work_item_id=wi.id, quantity=Decimal("10"), unit_price=Decimal("100"), amount=Decimal("1000")) s.add(ks2i) s.flush() ks3 = KS3(project_id=p.id, number="KS3-1", date=__import__("datetime").date(2026, 1, 3), status="issued") s.add(ks3) s.flush() s.add(KS3KS2(ks3_id=ks3.id, ks2_id=ks2.id)) s.flush() pay = Payment(project_id=p.id, date=__import__("datetime").date(2026, 1, 4), amount=Decimal("600"), currency="RUB", status="active") s.add(pay) s.flush() s.add(PaymentAllocation(payment_id=pay.id, ks3_id=ks3.id, amount=Decimal("600"))) s.commit() summary = ks3_payment_summary(s, p.id) assert summary["received"] == Decimal("600") assert summary["distributed"] == Decimal("600") assert summary["unallocated"] == Decimal("0") assert summary["outstanding"] == Decimal("400") # 1000 - 600 engine.dispose() def test_payment_cancelled_excluded_from_aggregates(tmp_path): from app.services.payment_service import ks3_payment_summary engine, Session = _make_session(tmp_path) with Session() as s: from app.models.ks3 import KS3KS2 from app.models.ks2 import KS2Item from app.models.boq import WorkItem from app.models.daily_report import DailyReport, DailyReportItem from app.models.payment import PaymentAllocation p = Project(name="Cancel pay test") s.add(p) s.flush() wi = WorkItem(project_id=p.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() dri = DailyReportItem(daily_report_id=dr.id, work_item_id=wi.id, completed_quantity=Decimal("10")) s.add(dri) s.flush() ks2 = KS2(project_id=p.id, number="K1", date=__import__("datetime").date(2026, 1, 2), status="issued") s.add(ks2) s.flush() s.add(KS2Item(ks2_id=ks2.id, work_item_id=wi.id, quantity=Decimal("10"), unit_price=Decimal("100"), amount=Decimal("1000"))) s.flush() ks3 = KS3(project_id=p.id, number="KS3-1", date=__import__("datetime").date(2026, 1, 3), status="issued") s.add(ks3) s.flush() s.add(KS3KS2(ks3_id=ks3.id, ks2_id=ks2.id)) s.flush() pay = Payment(project_id=p.id, date=__import__("datetime").date(2026, 1, 4), amount=Decimal("500"), currency="RUB", status="cancelled") s.add(pay) s.flush() s.add(PaymentAllocation(payment_id=pay.id, ks3_id=ks3.id, amount=Decimal("500"))) s.commit() summary = ks3_payment_summary(s, p.id) assert summary["received"] == Decimal("0") assert summary["distributed"] == Decimal("0") assert summary["outstanding"] == Decimal("1000") engine.dispose() def test_project_counters_on_detail_page(tmp_path): engine, Session = _make_session(tmp_path) def override_db(): s = Session() try: yield s finally: s.close() app.dependency_overrides[projects.get_db] = override_db try: with TestClient(app) as client: resp = client.get("/projects/2") assert resp.status_code == 200 text = resp.text assert "Ведомости" in text assert "Дополнительные соглашения" in text assert "Daily Reports" in text assert "КС-2" in text assert "КС-3" in text assert "Платежи" in text finally: app.dependency_overrides.pop(projects.get_db, None) engine.dispose()