import uuid from datetime import date import pytest from pydantic import ValidationError from app.schemas.drug_shipment import DrugShipmentCreate def shipment_payload(**overrides): payload = { "direction": "SEND", "center_id": uuid.uuid4(), "ship_date": date(2026, 6, 4), "receive_date": None, "quantity": 1, "batch_no": "DP-001", "carrier": "顺丰", "tracking_no": "SF100003", "status": "IN_TRANSIT", "remark": None, } payload.update(overrides) return payload def test_create_allows_pending_shipment_execution_fields_to_be_empty(): shipment = DrugShipmentCreate.model_validate( shipment_payload( status="PENDING", ship_date=None, quantity=None, batch_no=None, carrier=None, tracking_no=None, receive_date=None, remark=None, ) ) assert shipment.ship_date is None assert shipment.quantity is None assert shipment.batch_no is None assert shipment.carrier is None assert shipment.tracking_no is None @pytest.mark.parametrize("field_name", ["ship_date", "quantity", "batch_no", "carrier", "tracking_no"]) def test_create_requires_shipment_execution_fields_after_pending(field_name): with pytest.raises(ValidationError) as exc_info: DrugShipmentCreate.model_validate(shipment_payload(status="IN_TRANSIT", **{field_name: None})) assert "发运信息" in str(exc_info.value) def test_create_allows_pending_receipt_fields_to_be_empty(): shipment = DrugShipmentCreate.model_validate(shipment_payload()) assert shipment.receive_date is None assert shipment.remark is None def test_create_requires_receive_date_when_signed(): with pytest.raises(ValidationError) as exc_info: DrugShipmentCreate.model_validate(shipment_payload(status="SIGNED", receive_date=None)) assert "接收日期" in str(exc_info.value) def test_create_rejects_removed_returned_status(): with pytest.raises(ValidationError) as exc_info: DrugShipmentCreate.model_validate(shipment_payload(status="RETURNED", receive_date=date(2026, 6, 5))) assert "无效状态" in str(exc_info.value) def test_create_requires_remark_when_exceptional(): with pytest.raises(ValidationError) as exc_info: DrugShipmentCreate.model_validate(shipment_payload(status="EXCEPTION", remark="")) assert "备注" in str(exc_info.value)