312 lines
11 KiB
Python
312 lines
11 KiB
Python
"""
|
|
backend/tests/test_contract_payment_acceptance_archive.py
|
|
|
|
用途:验证合同付款验收归档从 OA SQL 读取、按采购单号落库,以及事件先于节点时仍能回放日期。
|
|
依赖:acceptance_oa_service、sync_service、ContractPayment* 模型。
|
|
逻辑:SQL 与 OA 连接一律 mock,不访问真实 ecology 库;覆盖 JOIN/currentnodetype=3/cgdh、幂等、无合同跳过、节点后生成回放。
|
|
"""
|
|
|
|
from datetime import date, datetime
|
|
from decimal import Decimal
|
|
|
|
from app import db
|
|
from app.models import (
|
|
ContractPaymentContract,
|
|
ContractPaymentEvent,
|
|
ContractPaymentNode,
|
|
)
|
|
from app.services.contract_payment.acceptance_oa_service import (
|
|
build_archived_acceptance_sql,
|
|
fetch_archived_acceptances,
|
|
parse_oa_archive_at,
|
|
source_event_id,
|
|
)
|
|
from app.services.contract_payment.sync_service import (
|
|
ingest_acceptance_archives,
|
|
refresh_acceptance_nodes_from_events,
|
|
_persist_payment_plan,
|
|
sync_notifications,
|
|
)
|
|
|
|
|
|
class _FakeCursor:
|
|
"""记录 SQL 参数并返回预设行,模拟 pyodbc cursor。"""
|
|
|
|
def __init__(self, rows):
|
|
self.rows = rows
|
|
self.sql = ""
|
|
self.params = None
|
|
|
|
def execute(self, sql, params=None):
|
|
self.sql = sql
|
|
self.params = list(params or [])
|
|
|
|
def fetchall(self):
|
|
return self.rows
|
|
|
|
|
|
class _FakeConnection:
|
|
def __init__(self, cursor):
|
|
self._cursor = cursor
|
|
self.closed = False
|
|
|
|
def cursor(self):
|
|
return self._cursor
|
|
|
|
def close(self):
|
|
self.closed = True
|
|
|
|
|
|
def test_acceptance_sql_joins_form_and_archived_nodetype():
|
|
"""关键断言:查询 JOIN 验收主表与流程主表,并以 currentnodetype=3、非空 cgdh 过滤。"""
|
|
sql, params = build_archived_acceptance_sql(
|
|
table="formtable_main_324",
|
|
po_field="cgdh",
|
|
request_id_field="requestId",
|
|
node_type="3",
|
|
since=datetime(2026, 8, 1, 12, 0, 0),
|
|
purchase_order_nos=["PO-1"],
|
|
)
|
|
|
|
assert "[formtable_main_324]" in sql
|
|
assert "[workflow_requestbase]" in sql
|
|
assert "w.[requestid] = f.[requestId]" in sql
|
|
assert "w.[currentnodetype]" in sql
|
|
assert "w.[status]" not in sql
|
|
assert "f.[cgdh]" in sql
|
|
assert "IN (?)" in sql
|
|
assert params[0] == "3"
|
|
assert "2026-08-01" in params
|
|
assert "PO-1" in params
|
|
|
|
|
|
def test_parse_oa_archive_at_combines_ecology_date_time():
|
|
"""关键断言:归档日使用 lastoperatedate+lastoperatetime,兼容无分隔日期。"""
|
|
assert parse_oa_archive_at("2026-08-01", "16:32:01") == datetime(2026, 8, 1, 16, 32, 1)
|
|
assert parse_oa_archive_at("20260801", "163201") == datetime(2026, 8, 1, 16, 32, 1)
|
|
assert parse_oa_archive_at("2026/08/01", None) == datetime(2026, 8, 1, 0, 0, 0)
|
|
|
|
|
|
def test_fetch_archived_acceptances_maps_cgdh_and_request_id(app, monkeypatch):
|
|
"""关键断言:游标行映射为 request_id + 采购单号 + 归档时间,并关闭连接。"""
|
|
cursor = _FakeCursor([
|
|
(32401, " PO-ACC-1 ", "3", "2026-08-01", "10:00:00", "2026-07-01", "09:00:00"),
|
|
(None, "PO-SKIP", "3", "2026-08-02", "10:00:00", None, None),
|
|
])
|
|
conn = _FakeConnection(cursor)
|
|
|
|
with app.app_context():
|
|
monkeypatch.setattr(
|
|
"app.services.contract_payment.acceptance_oa_service.OASQLServerService._connect",
|
|
lambda *args, **kwargs: conn,
|
|
)
|
|
events, ok = fetch_archived_acceptances(since=None)
|
|
|
|
assert ok is True
|
|
assert conn.closed is True
|
|
assert len(events) == 1
|
|
assert events[0]["request_id"] == "32401"
|
|
assert events[0]["purchase_order_no"] == "PO-ACC-1"
|
|
assert events[0]["currentnodetype"] == "3"
|
|
assert events[0]["archive_at"] == "2026-08-01 10:00:00"
|
|
assert "formtable_main_324" in cursor.sql
|
|
assert "currentnodetype" in cursor.sql
|
|
assert cursor.params == ["3"]
|
|
|
|
|
|
def test_ingest_acceptance_matches_purchase_order_and_is_idempotent(app):
|
|
"""关键断言:按 cgdh=采购单号落库;无合同跳过;同一 requestId 重复同步不新增事件。"""
|
|
with app.app_context():
|
|
contract = ContractPaymentContract(
|
|
contract_no="HT-ACC-1",
|
|
purchase_order_no="PO-ACC-1",
|
|
)
|
|
db.session.add(contract)
|
|
db.session.flush()
|
|
|
|
items = [
|
|
{
|
|
"request_id": "32401",
|
|
"purchase_order_no": "PO-ACC-1",
|
|
"archive_at": "2026-08-01 10:00:00",
|
|
"currentnodetype": "3",
|
|
},
|
|
{
|
|
"request_id": "32402",
|
|
"purchase_order_no": "PO-MISSING",
|
|
"archive_at": "2026-08-02 10:00:00",
|
|
"currentnodetype": "3",
|
|
},
|
|
]
|
|
created, skipped = ingest_acceptance_archives(items)
|
|
created_again, skipped_again = ingest_acceptance_archives(items)
|
|
events = db.session.query(ContractPaymentEvent).filter_by(contract_id=contract.id).all()
|
|
|
|
assert created == 1
|
|
assert skipped == 1
|
|
assert created_again == 0
|
|
assert skipped_again == 1
|
|
assert len(events) == 1
|
|
assert events[0].event_type == "acceptance_archive"
|
|
assert events[0].event_date == date(2026, 8, 1)
|
|
assert events[0].source_event_id == source_event_id("32401", contract.id)
|
|
|
|
|
|
def test_persist_plan_replays_existing_acceptance_event(app):
|
|
"""关键断言:验收事件先入库、节点后解析时,验收款/质保款仍能用归档日计算。"""
|
|
with app.app_context():
|
|
contract = ContractPaymentContract(
|
|
contract_no="HT-ACC-REPLAY",
|
|
purchase_order_no="PO-ACC-REPLAY",
|
|
)
|
|
db.session.add(contract)
|
|
db.session.flush()
|
|
db.session.add(ContractPaymentEvent(
|
|
contract_id=contract.id,
|
|
event_type="acceptance_archive",
|
|
event_date=date(2026, 8, 1),
|
|
source_event_id=source_event_id("9001", contract.id),
|
|
payload_json={"purchase_order_no": "PO-ACC-REPLAY"},
|
|
))
|
|
db.session.flush()
|
|
|
|
_persist_payment_plan(contract, result={
|
|
"status": "parsed",
|
|
"fields": {"warranty_period_days": 365},
|
|
"nodes": [
|
|
{
|
|
"node_code": "acceptance",
|
|
"node_type": "acceptance",
|
|
"amount": "100.00",
|
|
"percentage": "20",
|
|
"anchor_type": "acceptance_archive",
|
|
"due_date": None,
|
|
"evidence": {},
|
|
},
|
|
{
|
|
"node_code": "warranty",
|
|
"node_type": "warranty",
|
|
"amount": "50.00",
|
|
"percentage": "10",
|
|
"anchor_type": "acceptance_archive",
|
|
"due_date": None,
|
|
"evidence": {},
|
|
},
|
|
],
|
|
"issues": [],
|
|
})
|
|
acceptance = db.session.query(ContractPaymentNode).filter_by(
|
|
contract_id=contract.id, node_type="acceptance"
|
|
).one()
|
|
warranty = db.session.query(ContractPaymentNode).filter_by(
|
|
contract_id=contract.id, node_type="warranty"
|
|
).one()
|
|
|
|
assert acceptance.anchor_date == date(2026, 8, 1)
|
|
assert acceptance.due_date == date(2026, 8, 1)
|
|
assert warranty.anchor_date == date(2026, 8, 1)
|
|
assert warranty.due_date == date(2027, 8, 1)
|
|
|
|
|
|
def test_refresh_keeps_earliest_archive_date(app):
|
|
"""关键断言:多张验收单取最早归档日,后到事件不得把锚点推后。"""
|
|
with app.app_context():
|
|
contract = ContractPaymentContract(
|
|
contract_no="HT-ACC-MIN",
|
|
purchase_order_no="PO-ACC-MIN",
|
|
warranty_period_days=30,
|
|
)
|
|
db.session.add(contract)
|
|
db.session.flush()
|
|
db.session.add(ContractPaymentNode(
|
|
contract_id=contract.id,
|
|
node_code="acceptance",
|
|
node_type="acceptance",
|
|
amount=Decimal("100.00"),
|
|
percentage=Decimal("20"),
|
|
anchor_date=date(2026, 7, 1),
|
|
due_date=date(2026, 7, 1),
|
|
status="active",
|
|
))
|
|
db.session.add(ContractPaymentEvent(
|
|
contract_id=contract.id,
|
|
event_type="acceptance_archive",
|
|
event_date=date(2026, 8, 15),
|
|
source_event_id="later",
|
|
))
|
|
db.session.add(ContractPaymentEvent(
|
|
contract_id=contract.id,
|
|
event_type="acceptance_archive",
|
|
event_date=date(2026, 7, 1),
|
|
source_event_id="earlier",
|
|
))
|
|
refresh_acceptance_nodes_from_events(contract)
|
|
node = db.session.query(ContractPaymentNode).filter_by(
|
|
contract_id=contract.id, node_type="acceptance"
|
|
).one()
|
|
|
|
assert node.anchor_date == date(2026, 7, 1)
|
|
|
|
|
|
def test_sync_notifications_ingests_sql_acceptance(app, monkeypatch):
|
|
"""关键断言:合同 HTTP 同步后按采购单号吞入 OA SQL 验收归档,并推进独立断点。"""
|
|
with app.app_context():
|
|
contract = ContractPaymentContract(
|
|
contract_no="HT-ACC-SYNC",
|
|
purchase_order_no="PO-ACC-SYNC",
|
|
)
|
|
db.session.add(contract)
|
|
db.session.flush()
|
|
db.session.add(ContractPaymentNode(
|
|
contract_id=contract.id,
|
|
node_code="acceptance",
|
|
node_type="acceptance",
|
|
amount=Decimal("10.00"),
|
|
percentage=Decimal("20"),
|
|
status="active",
|
|
))
|
|
db.session.commit()
|
|
|
|
class FakeClient:
|
|
def configuration_status(self):
|
|
return {
|
|
"configured": True,
|
|
"list_configured": True,
|
|
"missing": [],
|
|
"attachment_template_configured": False,
|
|
}
|
|
|
|
def fetch_notifications(self, *, since=None):
|
|
return [], False
|
|
|
|
monkeypatch.setattr(
|
|
"app.services.contract_payment.sync_service.fetch_archived_acceptances",
|
|
lambda **_kwargs: (
|
|
[{
|
|
"request_id": "551",
|
|
"purchase_order_no": "PO-ACC-SYNC",
|
|
"archive_at": "2026-08-01 08:00:00",
|
|
"currentnodetype": "3",
|
|
}],
|
|
True,
|
|
),
|
|
)
|
|
result = sync_notifications(client=FakeClient())
|
|
event = db.session.query(ContractPaymentEvent).filter_by(
|
|
event_type="acceptance_archive"
|
|
).one()
|
|
node = db.session.query(ContractPaymentNode).filter_by(
|
|
contract_id=contract.id, node_type="acceptance"
|
|
).one()
|
|
from app.models import ContractPaymentSyncState
|
|
acc_state = db.session.query(ContractPaymentSyncState).filter_by(
|
|
state_key="oa-acceptance-archive"
|
|
).one()
|
|
|
|
assert result["acceptance_found"] == 1
|
|
assert result["acceptance_saved"] == 1
|
|
assert result["acceptance_error"] is False
|
|
assert event.source_event_id == source_event_id("551", contract.id)
|
|
assert node.anchor_date == date(2026, 8, 1)
|
|
assert acc_state.last_successful_sync_at is not None
|