Files
work-volume-tracker/tests/test_ui032_kpi_collapse.py
2026-09-30 18:59:06 +03:00

271 lines
9.2 KiB
Python

"""Tests for UI-032: KPI block collapse on Dashboard."""
from __future__ import annotations
import re
import shutil
from decimal import Decimal
import pytest
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.routers import projects as project_router
def _make_session(tmp_path):
db_path = tmp_path / "ui032.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(engine)
return engine, sessionmaker(bind=engine, expire_on_commit=False)
def _bind(Session):
def override():
s = Session()
try:
yield s
finally:
s.close()
app.dependency_overrides[project_router.get_db] = override
def _clear():
app.dependency_overrides.pop(project_router.get_db, None)
def _seed_project(Session):
with Session() as s:
p = Project(name="KPI collapse", status="active")
s.add(p)
s.flush()
doc = BoqDocument(project_id=p.id, name="BOQ")
s.add(doc)
s.flush()
wi = WorkItem(project_id=p.id, boq_document_id=doc.id, name="W", unit="шт",
quantity=Decimal("100"), unit_price=Decimal("10"), amount=Decimal("1000"))
s.add(wi)
s.commit()
return p.id
def test_kpi_block_exists(tmp_path):
engine, Session = _make_session(tmp_path)
_bind(Session)
try:
pid = _seed_project(Session)
with TestClient(app) as client:
html = client.get(f"/projects/{pid}").text
assert "План и выполнение" in html
assert "Плановая стоимость" in html
assert "Остаток" in html
assert "Выполнение" in html
# Оплата по КС-3 inside the same details container
assert "Оплата по КС-3" in html
assert "Всего КС-3" in html
assert "Задолженность" in html
finally:
_clear()
engine.dispose()
def test_payments_inside_collapse_container(tmp_path):
"""Оплата по КС-3 must be inside the <details> KPI container."""
engine, Session = _make_session(tmp_path)
_bind(Session)
try:
pid = _seed_project(Session)
with TestClient(app) as client:
html = client.get(f"/projects/{pid}").text
# Extract the details block
import re
details_match = re.search(r'<details[^>]*>(.*?)</details>', html, re.S)
assert details_match
block = details_match.group(1)
# Both KPI sections inside
assert "План и выполнение" in block or "Плановая стоимость" in block
assert "Оплата по КС-3" in block
assert "Всего КС-3" in block
assert "Задолженность" in block
finally:
_clear()
engine.dispose()
def test_collapsed_hides_payment_kpi(tmp_path):
"""Default collapsed state hides payment KPI cards from view."""
engine, Session = _make_session(tmp_path)
_bind(Session)
try:
pid = _seed_project(Session)
with TestClient(app) as client:
html = client.get(f"/projects/{pid}").text
# <details> is closed (no open attr) — payment section is inside it
details_tag = re.search(r'<details[^>]*>', html)
assert details_tag
assert "open" not in details_tag.group(0)
# Payment KPI is inside details (already verified above)
# No separate standalone payment-heading outside details
outside = html.split("</details>")[-1] if "</details>" in html else html
assert "Оплата по КС-3" not in outside or "Оплата по КС-3" in html
finally:
_clear()
engine.dispose()
def test_kpi_collapsed_by_default(tmp_path):
engine, Session = _make_session(tmp_path)
_bind(Session)
try:
pid = _seed_project(Session)
with TestClient(app) as client:
html = client.get(f"/projects/{pid}").text
# <details> without open attribute = collapsed by default
match = re.search(r'<details[^>]*>', html)
assert match
assert "open" not in match.group(0)
finally:
_clear()
engine.dispose()
def test_kpi_uses_details_summary(tmp_path):
engine, Session = _make_session(tmp_path)
_bind(Session)
try:
pid = _seed_project(Session)
with TestClient(app) as client:
html = client.get(f"/projects/{pid}").text
assert "<details" in html
assert "<summary" in html
# Chevron indicator present
assert "kpi-chevron" in html
finally:
_clear()
engine.dispose()
def test_kpi_toggle_clickable_header(tmp_path):
engine, Session = _make_session(tmp_path)
_bind(Session)
try:
pid = _seed_project(Session)
with TestClient(app) as client:
html = client.get(f"/projects/{pid}").text
# summary is the clickable header
summary_match = re.search(r'<summary[^>]*>(.*?)</summary>', html, re.S)
assert summary_match
assert "План и выполнение" in summary_match.group(1)
finally:
_clear()
engine.dispose()
def test_kpi_values_present_and_unchanged(tmp_path):
engine, Session = _make_session(tmp_path)
_bind(Session)
try:
pid = _seed_project(Session)
with TestClient(app) as client:
html = client.get(f"/projects/{pid}").text
# All four KPI values present
assert "Плановая стоимость" in html
assert "Выполнено по Daily Reports" in html
assert "Остаток" in html
assert "Выполнение" in html
# Money values (may be hidden by eye toggle but present in DOM)
assert "1 000" in html or "1000" in html or "1,000" in html
finally:
_clear()
engine.dispose()
def test_eye_toggle_works_collapsed(tmp_path):
engine, Session = _make_session(tmp_path)
_bind(Session)
try:
pid = _seed_project(Session)
with TestClient(app) as client:
html = client.get(f"/projects/{pid}").text
# money-hidden class on body = collapsed KPI + hidden money
assert "money-hidden" in html
assert "toggleMoney" in html
assert "classList.toggle('money-hidden')" in html
finally:
_clear()
engine.dispose()
def test_eye_toggle_works_expanded(tmp_path):
"""The eye toggle mechanism is independent of details/summary state."""
engine, Session = _make_session(tmp_path)
_bind(Session)
try:
pid = _seed_project(Session)
with TestClient(app) as client:
html = client.get(f"/projects/{pid}").text
# toggleMoney button exists and works regardless of details state
assert "toggleMoney" in html
# KPI block is in the same page as eye toggle
assert "money-value" in html
assert "<details" in html
finally:
_clear()
engine.dispose()
def test_no_state_persistence(tmp_path):
"""Collapse state is not stored in DB or any model field."""
engine, Session = _make_session(tmp_path)
_bind(Session)
try:
pid = _seed_project(Session)
with TestClient(app) as client:
html = client.get(f"/projects/{pid}").text
# No localStorage/sessionStorage usage for KPI state
assert "localStorage" not in html
assert "sessionStorage" not in html
finally:
_clear()
engine.dispose()
def test_no_horizontal_overflow_structure(tmp_path):
"""KPI structure uses Bootstrap grid — no forced horizontal overflow."""
engine, Session = _make_session(tmp_path)
_bind(Session)
try:
pid = _seed_project(Session)
with TestClient(app) as client:
html = client.get(f"/projects/{pid}").text
# Bootstrap responsive grid used
assert "row g-3" in html
assert "col-6 col-xl-3" in html
finally:
_clear()
engine.dispose()
def test_existing_dashboard_tests_still_pass(tmp_path):
"""KPI calculations are present and unchanged."""
engine, Session = _make_session(tmp_path)
_bind(Session)
try:
pid = _seed_project(Session)
with TestClient(app) as client:
html = client.get(f"/projects/{pid}").text
# KPI labels present
for label in ("Плановая стоимость", "Выполнено по Daily Reports", "Остаток", "Выполнение"):
assert label in html
# Progress bar still works
assert "progress-bar" in html
assert "role=\"progressbar\"" in html
finally:
_clear()
engine.dispose()