from datetime import date, datetime from decimal import Decimal from app.schemas.pl.plan import ( PlanLoadRead, PlanProductRead, PlanStopProductRead, PlanWithProducts, ) from app.services.pl_plan_price_mask import mask_plan_with_products_prices def _minimal_plan(**kwargs): base = dict( id="p1", plan_no="P001", belongs_company="shanxia", create_date=date(2026, 9, 1), total_amount=Decimal("100.00"), freight_amount=Decimal("50.00"), tenant_no="t1", create_time=datetime(2026, 9, 1), update_time=datetime(2026, 9, 1), products=[], deliveries=[], ) base.update(kwargs) return PlanWithProducts.model_validate(base) def test_mask_clears_plan_and_product_prices(): product = PlanProductRead.model_validate( { "id": "pp1", "planId": "p1", "tenantNo": "t1", "createTime": "2026-09-01T00:00:00", "updateTime": "2026-09-01T00:00:00", "productId": "g1", "productNo": "N1", "productName": "尿素", "inboundPrice": "12.5", "inboundAmount": "125", "outboundPrice": "13", "outboundAmount": "130", "brandQtys": [ {"brandId": "b1", "brandName": "君正", "qty": "10", "unitPrice": "12.5"} ], } ) stop_product = PlanStopProductRead.model_validate( { "id": "sp1", "planId": "p1", "tenantNo": "t1", "createTime": "2026-09-01T00:00:00", "updateTime": "2026-09-01T00:00:00", "productId": "g1", "productNo": "N1", "productName": "尿素", "inboundPrice": "12.5", "inboundAmount": "125", "brandQtys": [{"brandId": "b1", "qty": "10", "unitPrice": "12.5"}], } ) plan = _minimal_plan( products=[product], load=PlanLoadRead.model_validate( { "id": "l1", "planId": "p1", "stopKind": "load", "products": [stop_product], "tenantNo": "t1", "createTime": "2026-09-01T00:00:00", "updateTime": "2026-09-01T00:00:00", } ), ) out = mask_plan_with_products_prices(plan) assert out.total_amount == Decimal("0") assert out.freight_amount == Decimal("0") assert out.products[0].inbound_price is None assert out.products[0].inbound_amount is None assert out.products[0].outbound_price is None assert out.products[0].outbound_amount is None assert out.products[0].brand_qtys[0].unit_price is None assert out.load.products[0].inbound_price is None assert out.load.products[0].brand_qtys[0].unit_price is None def test_mask_is_noop_when_disabled(): plan = _minimal_plan(total_amount=Decimal("9"), freight_amount=Decimal("8")) out = mask_plan_with_products_prices(plan, enabled=False) assert out.total_amount == Decimal("9") assert out.freight_amount == Decimal("8")