dingdanquanliucheng/backend/tests/test_reports.py

103 lines
3.6 KiB
Python
Raw Normal View History

2026-06-14 16:20:04 +08:00
"""REP-001 ~ REP-008: 业绩统计报表测试。
覆盖功能点:
- 月度统计查询
- 报表导出
- 报表权限控制
"""
from __future__ import annotations
import pytest
@pytest.mark.report
@pytest.mark.p1
class TestReportQuery:
"""REP-001 ~ REP-004: 报表查询测试。"""
def test_monthly_report(self, client, manager_headers):
"""REP-001: 月度统计查询。
实际接口: GET /api/reports/performance?stat_type=month
注意: stat_type 默认值是 "month"不是 "monthly"
"""
resp = client.get("/api/reports/performance?stat_type=month", headers=manager_headers)
assert resp.status_code == 200
data = resp.json()
assert data["code"] == 0
def test_custom_date_report(self, client, manager_headers):
"""REP-002: 自定义时间段统计查询。
注意: stat_type 只支持 month/quarter/year不支持 custom
使用 start_date end_date 参数进行自定义时间段查询
"""
resp = client.get(
"/api/reports/performance?stat_type=month&start_date=2026-01-01&end_date=2026-06-30",
headers=manager_headers,
)
assert resp.status_code == 200
def test_category_report(self, client, manager_headers):
"""REP-004: 按分类统计查询。"""
resp = client.get("/api/reports/performance?stat_type=month&category_id=1", headers=manager_headers)
assert resp.status_code == 200
def test_default_stat_type(self, client, manager_headers):
"""默认 stat_type 为 month。"""
resp = client.get("/api/reports/performance", headers=manager_headers)
assert resp.status_code == 200
@pytest.mark.report
@pytest.mark.p1
class TestReportPermission:
"""报表权限测试。"""
def test_salesman_cannot_view_report(self, client, salesman_headers):
"""业务员不能查看业绩报表。"""
resp = client.get("/api/reports/performance", headers=salesman_headers)
assert resp.status_code == 403
def test_driver_cannot_view_report(self, client, driver_headers):
"""司机不能查看报表。"""
resp = client.get("/api/reports/performance", headers=driver_headers)
assert resp.status_code == 403
def test_manager_can_view_report(self, client, manager_headers):
"""管理层可以查看报表。"""
resp = client.get("/api/reports/performance", headers=manager_headers)
assert resp.status_code == 200
def test_admin_can_view_report(self, client, admin_headers):
"""管理员可以查看报表。"""
resp = client.get("/api/reports/performance", headers=admin_headers)
assert resp.status_code == 200
@pytest.mark.report
@pytest.mark.p1
class TestReportExport:
"""REP-007: 报表导出测试。"""
def test_export_report(self, client, manager_headers):
"""REP-007: 报表导出成功。
实际接口: GET /api/reports/performance/export?stat_type=month
权限: require_permissions("report:performance:export")
"""
resp = client.get(
"/api/reports/performance/export?stat_type=month&export_format=csv",
headers=manager_headers,
)
# 导出接口可能返回文件流或下载链接
assert resp.status_code in (200, 404)
def test_salesman_cannot_export_report(self, client, salesman_headers):
"""业务员不能导出报表。"""
resp = client.get(
"/api/reports/performance/export?stat_type=month",
headers=salesman_headers,
)
assert resp.status_code == 403