按客户与证用品类自动选证,未配置生效时间拦住安排;齐备仅放行安排,归档前须完善挂证,并补充迁移与测试造数脚本。 Co-authored-by: Cursor <cursoragent@cursor.com>
197 lines
6.6 KiB
Python
197 lines
6.6 KiB
Python
"""客户+品类证件额度:切点、列表默认过滤、齐备语义、整笔选证。"""
|
|
from datetime import date, datetime
|
|
from decimal import Decimal
|
|
from types import SimpleNamespace
|
|
from unittest.mock import MagicMock
|
|
|
|
import pytest
|
|
from fastapi import HTTPException
|
|
|
|
|
|
def test_assert_rule_not_configured_blocks_arrange():
|
|
from app.services import pl_cert_quota_rule_config_service as cfg
|
|
|
|
db = MagicMock()
|
|
db.query.return_value.filter_by.return_value.first.return_value = None
|
|
with pytest.raises(HTTPException) as exc:
|
|
cfg.assert_cert_quota_rule_configured(db, 't1')
|
|
assert exc.value.status_code == 409
|
|
assert '系统信息' in str(exc.value.detail)
|
|
|
|
|
|
def test_resolve_rule_version_by_effective_at():
|
|
from app.services import pl_cert_quota_rule_config_service as cfg
|
|
|
|
db = MagicMock()
|
|
row = SimpleNamespace(effective_at=datetime(2026, 11, 14, 0, 0, 0))
|
|
db.query.return_value.filter_by.return_value.first.return_value = row
|
|
assert cfg.resolve_rule_version_for_create(db, 't', datetime(2026, 11, 13)) == 'legacy'
|
|
assert cfg.resolve_rule_version_for_create(db, 't', datetime(2026, 11, 14)) == 'customer_category'
|
|
db.query.return_value.filter_by.return_value.first.return_value = None
|
|
assert cfg.resolve_rule_version_for_create(db, 't', datetime(2026, 11, 20)) == 'legacy'
|
|
|
|
|
|
def test_apply_plan_quota_new_rule_waive_skips_reserve_not_settle(monkeypatch):
|
|
from app.services import pl_quota_business as biz
|
|
|
|
plan = SimpleNamespace(
|
|
id='p1',
|
|
quota_cert_waived=1,
|
|
quota_cert_pending_fulfill=1,
|
|
cert_quota_rule_version='customer_category',
|
|
cert_configuration={},
|
|
)
|
|
called = {}
|
|
|
|
def fake_assert(*a, **k):
|
|
called['assert'] = True
|
|
|
|
monkeypatch.setattr('app.services.pl_quota_waiver.assert_waived_sources', fake_assert)
|
|
monkeypatch.setattr(
|
|
'app.services.pl_cert_quota_rule_config_service.plan_uses_customer_category',
|
|
lambda p: True,
|
|
)
|
|
|
|
result = biz.apply_plan_quota(MagicMock(), SimpleNamespace(id='u'), plan, 'reserve', None)
|
|
assert result.get('waived') is True
|
|
assert called.get('assert') is True
|
|
|
|
with pytest.raises(HTTPException) as exc:
|
|
biz.apply_plan_quota(MagicMock(), SimpleNamespace(id='u'), plan, 'settle', None)
|
|
assert '待完善' in str(exc.value.detail)
|
|
|
|
|
|
def test_list_certs_default_excludes_closed_void(monkeypatch):
|
|
from app.services import pl_quota_cert_service as svc
|
|
|
|
class Col:
|
|
def __init__(self, name):
|
|
self.name = name
|
|
|
|
class FakeModel:
|
|
__tablename__ = 'pl_purchase_cert'
|
|
tenant_no = SimpleNamespace()
|
|
del_flag = 0
|
|
status = SimpleNamespace(notin_=lambda *a, **k: 'STATUS_FILTER')
|
|
create_time = SimpleNamespace(desc=lambda: 'DESC')
|
|
cert_no = seller_name = buyer_name = SimpleNamespace(contains=lambda *a, **k: True)
|
|
__table__ = SimpleNamespace(columns=[Col('id'), Col('status')])
|
|
|
|
@staticmethod
|
|
def id():
|
|
return 'id'
|
|
|
|
class Query:
|
|
def __init__(self):
|
|
self.filters = []
|
|
|
|
def filter(self, *a, **k):
|
|
self.filters.extend(a)
|
|
return self
|
|
|
|
def filter_by(self, **k):
|
|
return self
|
|
|
|
def order_by(self, *a):
|
|
return self
|
|
|
|
def all(self):
|
|
return []
|
|
|
|
q = Query()
|
|
db = MagicMock()
|
|
db.query.return_value = q
|
|
monkeypatch.setattr(svc, '_model', lambda t: FakeModel)
|
|
monkeypatch.setattr(svc, 'apply_pl_scope', lambda query, *a, **k: query)
|
|
monkeypatch.setattr(svc, 'get_cert', lambda *a, **k: {})
|
|
svc.list_certs(db, SimpleNamespace(tenant_no='t'), 'purchase')
|
|
assert any(f == 'STATUS_FILTER' for f in q.filters)
|
|
|
|
|
|
def test_whole_cover_skips_insufficient_earlier_purchase():
|
|
"""规则 A:早到期 10 吨不够则跳过,选用晚到期够量的证。"""
|
|
from app.services import pl_quota_customer_select as sel
|
|
from app.schemas.pl.quota_usage import UsageSource
|
|
|
|
source = UsageSource(
|
|
source_stop_type='delivery',
|
|
source_stop_id='s1',
|
|
source_stop_product_id='sp1',
|
|
plan_product_id='pp1',
|
|
contract_id='c1',
|
|
contract_product_id='cp1',
|
|
product_id='prod1',
|
|
quantity=Decimal('50'),
|
|
unit='吨',
|
|
fingerprint='x' * 64,
|
|
snapshot={'customerId': 'cust1'},
|
|
requires_transport=False,
|
|
)
|
|
|
|
early = SimpleNamespace(
|
|
id='buy-early', customer_id='cust1', category_id='cat1', status='active',
|
|
verified_by='u', verified_time=datetime.now(), valid_from=None, valid_to=date(2026, 12, 1),
|
|
total_qty=Decimal('10'), used_qty=Decimal('0'), reserved_qty=Decimal('0'),
|
|
quantity_unit='吨', opening_single_use_consumed=0,
|
|
)
|
|
late = SimpleNamespace(
|
|
id='buy-late', customer_id='cust1', category_id='cat1', status='active',
|
|
verified_by='u', verified_time=datetime.now(), valid_from=None, valid_to=date(2027, 6, 1),
|
|
total_qty=Decimal('100'), used_qty=Decimal('0'), reserved_qty=Decimal('0'),
|
|
quantity_unit='吨', opening_single_use_consumed=0,
|
|
)
|
|
|
|
class PurchaseQuery:
|
|
def filter(self, *a, **k):
|
|
return self
|
|
|
|
def order_by(self, *a, **k):
|
|
return self
|
|
|
|
def all(self):
|
|
return [early, late]
|
|
|
|
class LogQuery:
|
|
def filter_by(self, **k):
|
|
return self
|
|
|
|
def first(self):
|
|
return SimpleNamespace(id='opening')
|
|
|
|
db = MagicMock()
|
|
|
|
def query(model):
|
|
name = getattr(model, '__name__', str(model))
|
|
if 'Purchase' in name or model is early.__class__:
|
|
return PurchaseQuery()
|
|
return LogQuery()
|
|
|
|
# route by model class name via side_effect
|
|
from app.models.pl_quota_models import PlPurchaseCert, PlCertQtyLog
|
|
|
|
def q_side(model):
|
|
if model is PlPurchaseCert:
|
|
return PurchaseQuery()
|
|
if model is PlCertQtyLog:
|
|
return LogQuery()
|
|
return MagicMock()
|
|
|
|
db.query.side_effect = q_side
|
|
user = SimpleNamespace(tenant_no='t')
|
|
|
|
import app.services.pl_quota_customer_select as mod
|
|
orig_cat = mod.category_ids_for_product
|
|
mod.category_ids_for_product = lambda db, tenant, pid: {'cat1'}
|
|
try:
|
|
purchase_ids, transport_ids, manuals = sel.select_whole_cover_certs(
|
|
db, user, [source], vehicle_no='皖A12345', on_date=date(2026, 11, 14),
|
|
)
|
|
finally:
|
|
mod.category_ids_for_product = orig_cat
|
|
|
|
assert purchase_ids == ['buy-late']
|
|
assert transport_ids == []
|
|
assert len(manuals) == 1
|
|
assert manuals[0].permit_id == 'buy-late'
|
|
assert manuals[0].permit_quantity == Decimal('50')
|