2
This commit is contained in:
commit
21d41acf50
732 files changed
+55672
-15251
No files matched your search
@@ -77,6 +77,9 @@ def test_aggregator_dual_missing_metrics():
|
||||
assert by_invoice["A"]["cells"]["po_order"] == "缺件"
|
||||
assert by_invoice["A"]["cells"]["sales_release"] == "未检核"
|
||||
assert by_invoice["B"]["cells"]["po_order"] == "有"
|
||||
assert stats["by_file_type"]["po_order"]["missing_file_count"] == 1
|
||||
assert stats["by_file_type"]["po_order"]["missing_invoice_count"] == 1
|
||||
assert stats["by_file_type"]["tax_invoice"]["missing_file_count"] == 0
|
||||
|
||||
|
||||
def test_po_and_oa_adapters_are_placeholders():
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
"""
|
||||
backend/tests/voucher_check/test_erp_settings.py
|
||||
|
||||
用途:立账凭证 ERP 凭据回退到通用 ERP_* / 合同付款同一套只读账号。
|
||||
依赖:voucher_check.config._erp_settings、Flask app。
|
||||
逻辑:清空 VOUCHER_CHECK_ERP_USERNAME 后仍能从 CONTRACT_PAYMENT_ERP_* 读到账号。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from app.services.voucher_check.config import _erp_settings
|
||||
|
||||
|
||||
def test_erp_settings_falls_back_to_contract_payment_credentials(app):
|
||||
"""未单独配 VOUCHER_CHECK_ERP_USERNAME 时,应复用合同付款已有的只读账号。"""
|
||||
with app.app_context():
|
||||
app.config["VOUCHER_CHECK_ERP_DSN"] = ""
|
||||
app.config["VOUCHER_CHECK_ERP_USERNAME"] = ""
|
||||
app.config["VOUCHER_CHECK_ERP_PASSWORD"] = ""
|
||||
app.config["ERP_DB_DSN"] = "192.168.8.24:1521/topprod"
|
||||
app.config["ERP_USERNAME"] = ""
|
||||
app.config["ERP_PASSWORD"] = ""
|
||||
app.config["CONTRACT_PAYMENT_ERP_USERNAME"] = "hxjm_ro"
|
||||
app.config["CONTRACT_PAYMENT_ERP_PASSWORD"] = "secret-from-contract-payment"
|
||||
dsn, username, password, timeout = _erp_settings()
|
||||
assert dsn == "192.168.8.24:1521/topprod"
|
||||
assert username == "hxjm_ro"
|
||||
assert password == "secret-from-contract-payment"
|
||||
assert timeout >= 1
|
||||
@@ -12,6 +12,8 @@ from tests.conftest import parse_api_data, parse_api_error
|
||||
|
||||
from app import db
|
||||
from app.models import ScheduleRun, ScheduleTask, User
|
||||
from app.core.errors import ValidationError
|
||||
from app.services.voucher_check.dms_client import SalesDmsClient
|
||||
from app.services.voucher_check.schedule_seed import (
|
||||
DEFAULT_VOUCHER_CHECK_PLATFORM_PATH,
|
||||
DEFAULT_VOUCHER_CHECK_SCHEDULE_NO,
|
||||
@@ -38,7 +40,41 @@ def _ensure_user(*, username: str, role: str) -> User:
|
||||
return user
|
||||
|
||||
|
||||
def test_workspace_force_recheck_enqueues_force_all(app, client, admin_headers):
|
||||
def _stub_force_run_preflight(monkeypatch) -> None:
|
||||
"""隔离配置预校验,只验证工作台入队和失败可见性契约。"""
|
||||
monkeypatch.setattr(
|
||||
"app.services.voucher_check.config.load_engine_config",
|
||||
lambda _params: type("Config", (), {"profile_code": "sales"})(),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"app.services.voucher_check.profile_loader.load_profile_definition",
|
||||
lambda _code: {"profile_code": "sales"},
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"app.services.voucher_check.profile_loader.ensure_published_profile",
|
||||
lambda _code: None,
|
||||
)
|
||||
monkeypatch.setattr("app.services.voucher_check.dms_client.SalesDmsClient.ensure_configured", lambda: None)
|
||||
|
||||
|
||||
def test_dms_preflight_reports_missing_keys_without_secret_values(app, monkeypatch):
|
||||
"""DMS 配置缺失时要指出变量名,且不在错误内容中泄露凭据值。"""
|
||||
with app.app_context():
|
||||
monkeypatch.setattr(SalesDmsClient, "base_url", classmethod(lambda _cls: "https://dms.example"))
|
||||
monkeypatch.setattr(SalesDmsClient, "library_id", classmethod(lambda _cls: "library-id"))
|
||||
monkeypatch.setattr(SalesDmsClient, "root_folder_id", classmethod(lambda _cls: "folder-id"))
|
||||
monkeypatch.setattr(SalesDmsClient, "username", classmethod(lambda _cls: ""))
|
||||
monkeypatch.setattr(SalesDmsClient, "password", classmethod(lambda _cls: ""))
|
||||
try:
|
||||
SalesDmsClient.ensure_configured()
|
||||
except ValidationError as exc:
|
||||
assert exc.code == "VOUCHER_CHECK.CONFIG_INVALID"
|
||||
assert exc.details["missing"] == ["VOUCHER_CHECK_DMS_USERNAME", "VOUCHER_CHECK_DMS_PASSWORD"]
|
||||
else:
|
||||
raise AssertionError("expected missing DMS configuration to fail")
|
||||
|
||||
|
||||
def test_workspace_force_recheck_enqueues_force_all(app, client, admin_headers, monkeypatch):
|
||||
"""工作台按钮入队后,ScheduleRun 必须带 force_all,供 runner 跳过增量。"""
|
||||
with app.app_context():
|
||||
_ensure_user(username="admin", role="admin")
|
||||
@@ -46,6 +82,7 @@ def test_workspace_force_recheck_enqueues_force_all(app, client, admin_headers):
|
||||
assert task is not None
|
||||
assert task.schedule_no == DEFAULT_VOUCHER_CHECK_SCHEDULE_NO
|
||||
assert task.platform_api_path == DEFAULT_VOUCHER_CHECK_PLATFORM_PATH
|
||||
_stub_force_run_preflight(monkeypatch)
|
||||
|
||||
first = client.post(
|
||||
"/api/workspace/accounting-voucher-check/runs",
|
||||
@@ -74,11 +111,12 @@ def test_workspace_force_recheck_enqueues_force_all(app, client, admin_headers):
|
||||
assert error["code"] == "VOUCHER_CHECK.RUN_IN_PROGRESS"
|
||||
|
||||
|
||||
def test_workspace_force_recheck_rejects_non_admin_and_invalid_mode(app, client, admin_headers, user_headers):
|
||||
def test_workspace_force_recheck_rejects_non_admin_and_invalid_mode(app, client, admin_headers, user_headers, monkeypatch):
|
||||
with app.app_context():
|
||||
_ensure_user(username="admin", role="admin")
|
||||
_ensure_user(username="user", role="user")
|
||||
ensure_default_voucher_check_schedule(commit=True)
|
||||
_stub_force_run_preflight(monkeypatch)
|
||||
|
||||
forbidden = client.post(
|
||||
"/api/workspace/accounting-voucher-check/runs",
|
||||
@@ -95,3 +133,37 @@ def test_workspace_force_recheck_rejects_non_admin_and_invalid_mode(app, client,
|
||||
assert invalid.status_code == 400
|
||||
error = parse_api_error(invalid.get_json())
|
||||
assert error["code"] == "VOUCHER_CHECK.CONFIG_INVALID"
|
||||
|
||||
|
||||
def test_workspace_lists_pre_engine_failure_for_selected_period(app, client, admin_headers):
|
||||
"""配置校验在建引擎批次前失败时,页面仍应展示技术失败与原因。"""
|
||||
with app.app_context():
|
||||
_ensure_user(username="admin", role="admin")
|
||||
task = ensure_default_voucher_check_schedule(commit=True)
|
||||
assert task is not None
|
||||
run = ScheduleRun(
|
||||
schedule_task_id=task.id,
|
||||
run_no="RUN-PREENGINE-FAILED",
|
||||
trigger_type="manual",
|
||||
status="failed",
|
||||
total_count=0,
|
||||
success_count=0,
|
||||
failed_count=1,
|
||||
error_message="未配置销售开票附件文档库根目录",
|
||||
response_summary_json={"voucher_check": {"period": "2026-08"}},
|
||||
)
|
||||
db.session.add(run)
|
||||
db.session.commit()
|
||||
|
||||
response = client.get(
|
||||
"/api/workspace/accounting-voucher-check/runs?period=2026-08&status=technical_failed",
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
payload = parse_api_data(response.get_json())
|
||||
assert payload["total"] == 1
|
||||
item = payload["items"][0]
|
||||
# 关键断言:前端列表读取的 run 数据保留失败原因,而非呈现为“暂无检核批次”。
|
||||
assert item["run_no"] == "RUN-PREENGINE-FAILED"
|
||||
assert item["status"] == "technical_failed"
|
||||
assert item["error_message"] == "未配置销售开票附件文档库根目录"
|
||||
@@ -0,0 +1,144 @@
|
||||
"""
|
||||
backend/tests/voucher_check/test_workspace_notifications.py
|
||||
|
||||
用途:工作台企微发送记录列表 API。
|
||||
依赖:pytest、Flask client;不发送真实企微。
|
||||
逻辑:GET /api/workspace/accounting-voucher-check/notifications 读 voucher_check_notifications,
|
||||
回填接收人姓名与 has_excel,并可按期间、发送状态筛选。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date, datetime
|
||||
|
||||
from tests.conftest import parse_api_data
|
||||
|
||||
from app import db
|
||||
from app.models import ScheduleRun, User
|
||||
from app.models.voucher_check import VoucherCheckNotification, VoucherCheckRun
|
||||
from app.services.voucher_check.schedule_seed import ensure_default_voucher_check_schedule
|
||||
|
||||
|
||||
def _ensure_user(*, username: str, role: str, real_name: str | None = None) -> User:
|
||||
"""测试用户幂等创建。"""
|
||||
user = db.session.query(User).filter_by(username=username).first()
|
||||
if user:
|
||||
user.role = role
|
||||
user.status = "active"
|
||||
if real_name:
|
||||
user.real_name = real_name
|
||||
db.session.commit()
|
||||
return user
|
||||
user = User(
|
||||
username=username,
|
||||
email=f"{username}@example.com",
|
||||
role=role,
|
||||
status="active",
|
||||
real_name=real_name,
|
||||
)
|
||||
user.set_password("test")
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
return user
|
||||
|
||||
|
||||
def test_workspace_lists_wecom_notifications(app, client, user_headers):
|
||||
"""企微发送记录列表应带出批次统计、接收人姓名,并按发送状态筛选。"""
|
||||
with app.app_context():
|
||||
_ensure_user(username="admin", role="admin")
|
||||
recipient = _ensure_user(username="user", role="user", real_name="Zhang San")
|
||||
task = ensure_default_voucher_check_schedule(commit=True)
|
||||
assert task is not None
|
||||
schedule_run = ScheduleRun(
|
||||
schedule_task_id=task.id,
|
||||
run_no="RUN-VC-NOTIFY-1",
|
||||
trigger_type="cron",
|
||||
status="success",
|
||||
excel_file_name="voucher-check-2026-08.xlsx",
|
||||
excel_file_size=12,
|
||||
excel_file_data=b"xlsx-bytes",
|
||||
)
|
||||
db.session.add(schedule_run)
|
||||
db.session.flush()
|
||||
engine_run = VoucherCheckRun(
|
||||
id=1,
|
||||
schedule_run_id=schedule_run.id,
|
||||
run_no="VC-202608-001",
|
||||
profile_code="sales",
|
||||
profile_version="1",
|
||||
profile_snapshot_json={"profile_code": "sales"},
|
||||
period_month=date(2026, 8, 1),
|
||||
mode="incremental",
|
||||
status="missing",
|
||||
subject_count=10,
|
||||
missing_file_count=4,
|
||||
missing_invoice_count=2,
|
||||
)
|
||||
db.session.add(engine_run)
|
||||
db.session.flush()
|
||||
db.session.add(
|
||||
VoucherCheckNotification(
|
||||
id=1,
|
||||
run_id=engine_run.id,
|
||||
excel_filename="voucher-check-2026-08.xlsx",
|
||||
recipient_user_ids_json=[recipient.id],
|
||||
send_status="sent",
|
||||
skip_reason=None,
|
||||
summary_json={"period": "2026-08", "subject_count": 10},
|
||||
sent_at=datetime(2026, 9, 1, 6, 10, 0),
|
||||
)
|
||||
)
|
||||
skipped_run = VoucherCheckRun(
|
||||
id=2,
|
||||
run_no="VC-202607-001",
|
||||
profile_code="sales",
|
||||
profile_version="1",
|
||||
profile_snapshot_json={"profile_code": "sales"},
|
||||
period_month=date(2026, 7, 1),
|
||||
mode="incremental",
|
||||
status="completed",
|
||||
subject_count=8,
|
||||
missing_file_count=0,
|
||||
missing_invoice_count=0,
|
||||
)
|
||||
db.session.add(skipped_run)
|
||||
db.session.flush()
|
||||
db.session.add(
|
||||
VoucherCheckNotification(
|
||||
id=2,
|
||||
run_id=skipped_run.id,
|
||||
send_status="skipped_no_missing",
|
||||
skip_reason="本批无缺件",
|
||||
summary_json={"period": "2026-07"},
|
||||
)
|
||||
)
|
||||
db.session.commit()
|
||||
|
||||
listed = client.get(
|
||||
"/api/workspace/accounting-voucher-check/notifications",
|
||||
headers=user_headers,
|
||||
)
|
||||
assert listed.status_code == 200
|
||||
payload = parse_api_data(listed.get_json())
|
||||
assert payload["total"] >= 2
|
||||
sent = next(item for item in payload["items"] if item["run_no"] == "VC-202608-001")
|
||||
# 关键断言:工作台汇总列表能看到发送状态、接收人姓名和 Excel 入口。
|
||||
assert sent["send_status"] == "sent"
|
||||
assert sent["missing_invoice_count"] == 2
|
||||
assert sent["missing_file_count"] == 4
|
||||
assert sent["recipient_names"] == ["Zhang San"]
|
||||
assert sent["has_excel"] is True
|
||||
assert sent["schedule_run_id"]
|
||||
|
||||
filtered = client.get(
|
||||
"/api/workspace/accounting-voucher-check/notifications?period=2026-07&send_status=skipped_no_missing",
|
||||
headers=user_headers,
|
||||
)
|
||||
assert filtered.status_code == 200
|
||||
filtered_payload = parse_api_data(filtered.get_json())
|
||||
assert filtered_payload["total"] == 1
|
||||
item = filtered_payload["items"][0]
|
||||
assert item["run_no"] == "VC-202607-001"
|
||||
assert item["send_status"] == "skipped_no_missing"
|
||||
assert item["skip_reason"] == "本批无缺件"
|
||||
assert item["has_excel"] is False
|
||||
Reference in new issue
Block a user