"""公开归档字段经真实来源、条件核对进入补分配,不接受客户端数量覆盖。""" from test_pl_quota_usage import case from test_pl_quota_business import business def archive_payload(c, **changes): from app.schemas.pl.plan import PlanArchiveBody return PlanArchiveBody(operationId='supplement', quotaAllowSupplement=True, quotaConfiguration=c.payload.quota_configuration, quotaReason='磅单实际量超预占', quotaSourceNote='核对磅单', **changes) def test_public_archive_supplement_uses_saved_actual_and_original_item_ids(business): from app.services.pl_quota_business import apply_plan_quota, settle_plan_quota c = business apply_plan_quota(c.db, c.user, c.plan, 'reserve', c.payload) ids = {i.id for i in c.db.query(c.m.PlPlanCertUsageItem).all()} c.db.get(c.models.PlPlanLoadProduct, 'sp').inbound_qty = 35 payload = archive_payload(c) assert settle_plan_quota(c.db, c.user, c.plan, payload) assert c.db.get(c.m.PlPurchaseCert, 'p').used_qty == 35 assert c.db.get(c.m.PlTransportCert, 't').used_qty == 35 assert {i.id for i in c.db.query(c.m.PlPlanCertUsageItem).all()} == ids assert settle_plan_quota(c.db, c.user, c.plan, payload) assert c.db.get(c.m.PlPurchaseCert, 'p').used_qty == 35 def test_supplement_stale_condition_preserves_actual_and_original_reservation(business): from app.services.pl_quota_business import apply_plan_quota, settle_plan_quota c = business apply_plan_quota(c.db, c.user, c.plan, 'reserve', c.payload) c.db.get(c.models.PlPlanLoadProduct, 'sp').inbound_qty = 35 c.db.get(c.m.PlPurchaseCert, 'p').revision = 2 assert not settle_plan_quota(c.db, c.user, c.plan, archive_payload(c)) assert c.db.get(c.models.PlPlanLoadProduct, 'sp').inbound_qty == 35 assert c.db.get(c.m.PlPurchaseCert, 'p').reserved_qty == 29 assert c.db.get(c.m.PlPurchaseCert, 'p').used_qty == 0 def test_post_lock_condition_failure_rolls_back_supplement(business, monkeypatch): from app.services import pl_quota_business as service c = business service.apply_plan_quota(c.db, c.user, c.plan, 'reserve', c.payload) c.db.get(c.models.PlPlanLoadProduct, 'sp').inbound_qty = 35 original = service.execute_usage def changed_revision(*args): result = original(*args) c.db.get(c.m.PlPurchaseCert, 'p').revision = 2 return result monkeypatch.setattr(service, 'execute_usage', changed_revision) assert not service.settle_plan_quota(c.db, c.user, c.plan, archive_payload(c)) assert c.db.get(c.models.PlPlanLoadProduct, 'sp').inbound_qty == 35 assert c.db.get(c.m.PlPurchaseCert, 'p').reserved_qty == 29 assert c.db.get(c.m.PlPurchaseCert, 'p').used_qty == 0 def test_calculation_incomplete_preserves_real_quantity_as_pending_exception(business, monkeypatch): from app.services import pl_quota_business as service from app.services.pl_quota_allocation import AllocationIncomplete c = business service.apply_plan_quota(c.db, c.user, c.plan, 'reserve', c.payload) c.db.get(c.models.PlPlanLoadProduct, 'sp').inbound_qty = 35 def unfinished(*args): raise AllocationIncomplete('分配计算未完成:达到计算预算') monkeypatch.setattr(service, 'execute_usage', unfinished) assert not service.settle_plan_quota(c.db, c.user, c.plan, archive_payload(c)) c.db.commit() assert c.db.get(c.models.PlPlanLoadProduct, 'sp').inbound_qty == 35 assert c.db.get(c.m.PlPurchaseCert, 'p').reserved_qty == 29 assert all('计算未完成' in u.confirmation_snapshot['settlementError'] for u in c.db.query(c.m.PlPlanCertUsage).all())