feat: 网银流水导入模板+现金流联动(财务数据通道P1)
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
"""现金流模块测试 — 收付款计划 + 资金缺口预测 + 看板
|
||||
"""现金流模块测试 — 收付款计划 + 资金缺口预测 + 看板 + 网银流水导入
|
||||
|
||||
覆盖 cash.py 核心端点:
|
||||
gap-forecast / balance GET+POST / plans CRUD / plans{id}/complete /
|
||||
upcoming / dashboard / check-alerts / alerts/status
|
||||
upcoming / dashboard / check-alerts / alerts/status / import/vouchers
|
||||
"""
|
||||
import io
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
import pandas as pd
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from sqlalchemy.orm import Session
|
||||
@@ -218,3 +220,98 @@ class TestAlerts:
|
||||
assert resp.status_code == 200
|
||||
data = resp.json()
|
||||
assert "entity_id" in data and "critical_line" in data
|
||||
|
||||
|
||||
class TestVoucherImport:
|
||||
"""网银流水导入 — 三校验规则(借贷平衡/期间合计/结转行)+ 入库 + 现金流联动"""
|
||||
|
||||
COLS = ["凭证日期", "凭证号", "科目编码", "科目名称", "借方金额", "贷方金额", "摘要"]
|
||||
|
||||
def _xlsx(self, rows: list, cols: list = None) -> io.BytesIO:
|
||||
df = pd.DataFrame(rows, columns=cols or self.COLS)
|
||||
buf = io.BytesIO()
|
||||
df.to_excel(buf, index=False)
|
||||
buf.seek(0)
|
||||
return buf
|
||||
|
||||
def _upload(self, client, token, buf, fname="test_vouchers.xlsx"):
|
||||
return client.post(
|
||||
f"{BASE}/import/vouchers", headers=auth_header(token),
|
||||
files={"file": (fname, buf,
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")},
|
||||
)
|
||||
|
||||
def test_import_ok_with_rules(self, client: TestClient, db: Session):
|
||||
"""正常导入:借贷平衡+结转行识别+期间合计,全部成功"""
|
||||
create_test_user(db)
|
||||
token = get_token_for_user(client)
|
||||
buf = self._xlsx([
|
||||
["2026-08-01", "记-001", "1002", "银行存款-工行", 50000, 0, "收到客户回款"],
|
||||
["2026-08-02", "记-001", "1001", "库存现金", 0, 50000, "提现备用"],
|
||||
["2026-08-31", "记-099", "4103", "本年利润", 2000, 0, "结转利润"],
|
||||
["2026-08-31", "记-099", "6001", "主营业务收入", 0, 2000, "结转收入"],
|
||||
])
|
||||
resp = self._upload(client, token, buf)
|
||||
assert resp.status_code == 200
|
||||
data = resp.json()
|
||||
assert data["success"] is True
|
||||
assert data["total"] == 4
|
||||
assert data["success_rows"] == 4
|
||||
assert data["failed_rows"] == 0
|
||||
assert data["balance_check"]["passed"] is True
|
||||
assert data["balance_check"]["debit_total"] == 52000
|
||||
assert data["carry_forward_count"] == 2 # 结转行识别(本年利润+结转摘要)
|
||||
assert "2026-08" in data["period_totals"]
|
||||
assert data["cash_balance"] == 0.0 # 货币资金联动(50000-50000=0万元)
|
||||
# 入库验证
|
||||
from app.models import VoucherDetail, ImportLog
|
||||
details = db.query(VoucherDetail).all()
|
||||
assert len(details) == 4
|
||||
assert all(d.entity_id == 1 for d in details)
|
||||
cf = [d for d in details if d.carry_forward == 1]
|
||||
assert len(cf) == 2 and all("结转" in (d.summary or "") for d in cf)
|
||||
log = db.query(ImportLog).order_by(ImportLog.id.desc()).first()
|
||||
assert log is not None and log.import_type == "vouchers"
|
||||
assert log.success_rows == 4 and log.total_rows == 4
|
||||
|
||||
def test_import_unbalanced(self, client: TestClient, db: Session):
|
||||
"""借贷不平衡:balance_check.passed=False + 差额报告,行仍入库"""
|
||||
create_test_user(db)
|
||||
token = get_token_for_user(client)
|
||||
buf = self._xlsx([
|
||||
["2026-08-01", "记-001", "1002", "银行存款-工行", 10000, 0, "回款"],
|
||||
["2026-08-01", "记-002", "1002", "银行存款-工行", 0, 3000, "付款"],
|
||||
])
|
||||
resp = self._upload(client, token, buf)
|
||||
assert resp.status_code == 200
|
||||
data = resp.json()
|
||||
assert data["balance_check"]["passed"] is False
|
||||
assert data["balance_check"]["diff"] == 7000
|
||||
|
||||
def test_import_partial_fail(self, client: TestClient, db: Session):
|
||||
"""部分失败模式:坏行进errors,好行入库"""
|
||||
create_test_user(db)
|
||||
token = get_token_for_user(client)
|
||||
buf = self._xlsx([
|
||||
["2026-08-01", "记-001", "1002", "银行存款-工行", 8000, 0, "回款"],
|
||||
["bad-date", "记-002", "1001", "库存现金", 0, 8000, "提现"],
|
||||
["2026-08-01", "", "1001", "库存现金", 100, 0, "缺凭证号"],
|
||||
])
|
||||
resp = self._upload(client, token, buf)
|
||||
assert resp.status_code == 200
|
||||
data = resp.json()
|
||||
assert data["success_rows"] == 1
|
||||
assert data["failed_rows"] == 2
|
||||
# 行级错误2条 + 借贷平衡全局错误1条(仅8000借无贷)
|
||||
assert len(data["errors"]) == 3
|
||||
assert any(e["field"] == "balance" for e in data["errors"])
|
||||
from app.models import VoucherDetail
|
||||
assert db.query(VoucherDetail).count() == 1
|
||||
|
||||
def test_import_missing_cols(self, client: TestClient, db: Session):
|
||||
"""缺必要列 → 400"""
|
||||
create_test_user(db)
|
||||
token = get_token_for_user(client)
|
||||
buf = self._xlsx([["2026-08-01", "记-001", 100, 0]], cols=["日期", "凭证号", "借方金额", "贷方金额"])
|
||||
resp = self._upload(client, token, buf)
|
||||
assert resp.status_code == 400
|
||||
|
||||
Reference in New Issue
Block a user