"""Debug test_dashboard - replicate exact test flow""" import pytest from fastapi.testclient import TestClient from sqlalchemy.orm import Session # Setup tables from app import database as db_module from app.database import Base from tests.conftest import TEST_ENGINE, TEST_SESSION_LOCAL Base.metadata.create_all(bind=TEST_ENGINE) # Create session session = TEST_SESSION_LOCAL() # Override from app.main import app app.dependency_overrides[db_module.get_db] = lambda: session from tests.conftest import create_test_user, get_token_for_user, auth_header, create_test_kpi from app.models import KPIAlert, KPIValue, KPIDefinition from sqlalchemy import func # === test_summary_empty === print("=== test_summary_empty ===") create_test_user(session) client = TestClient(app) token = get_token_for_user(client) resp = client.get("/api/cma/dashboard/summary", headers=auth_header(token)) print(f" status={resp.status_code}, data={resp.json()}") # === test_summary_with_data === print("=== test_summary_with_data ===") kpi = create_test_kpi(session, kpi_code="F_REVENUE", dimension="finance") kpi2 = create_test_kpi(session, kpi_code="C_SATISFACTION", kpi_name="客户满意度", dimension="customer") cnt = session.query(func.count(KPIDefinition.id)).filter(KPIDefinition.status == "active").scalar() print(f" Direct count after creating KPIs: {cnt}") alert = KPIAlert(kpi_id=kpi.id, alert_level="red", alert_message="营收预警", status="pending") session.add(alert) session.commit() resp = client.get("/api/cma/dashboard/summary", headers=auth_header(token)) print(f" status={resp.status_code}, data={resp.json()}") # === test_kpis_with_data === print("=== test_kpis_with_data ===") kpi_val = KPIValue(kpi_id=kpi.id, period="2026-06", actual_value=85.0) session.add(kpi_val) session.commit() resp = client.get("/api/cma/dashboard/kpis", headers=auth_header(token)) print(f" status={resp.status_code}") rd = resp.json() print(f" data len={len(rd['data'])}") if rd['data']: print(f" first item: kpi_name={rd['data'][0].get('kpi_name')}, actual_value={rd['data'][0].get('actual_value')}") app.dependency_overrides.clear() Base.metadata.drop_all(bind=TEST_ENGINE)