避免只改接收表编码后,生产/质检/库位等关联副本仍保留旧码。 Co-authored-by: Cursor <cursoragent@cursor.com>
461 lines
14 KiB
Python
461 lines
14 KiB
Python
"""编辑样本接收时同步实验室入库编码副本。"""
|
|
from datetime import datetime
|
|
|
|
import pytest
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from app.crud import sample_reception as reception_svc
|
|
from app.models.base import Base
|
|
from app.models.models import (
|
|
BizCellProduct,
|
|
BizLabSampleProcessingExtra,
|
|
BizLabSampleProcessSupplement,
|
|
BizOutboundReleaseCell,
|
|
BizProductionTask,
|
|
BizProductionTaskInput,
|
|
BizProductionTaskOutput,
|
|
BizProductionTaskStep,
|
|
BizProductionTaskStepOutput,
|
|
BizProductionWorkOrder,
|
|
BizQcWorkOrder,
|
|
BizQcWorkOrderInspectionBatch,
|
|
BizSampleReception,
|
|
BizSampleReceptionCheckResult,
|
|
BizStorageSlotOccupancy,
|
|
)
|
|
from app.services.lab_storage_code_rename import replace_lab_code_token
|
|
|
|
|
|
def _session():
|
|
engine = create_engine("sqlite:///:memory:")
|
|
Base.metadata.create_all(
|
|
engine,
|
|
tables=[
|
|
BizSampleReception.__table__,
|
|
BizSampleReceptionCheckResult.__table__,
|
|
BizProductionWorkOrder.__table__,
|
|
BizProductionTask.__table__,
|
|
BizProductionTaskInput.__table__,
|
|
BizProductionTaskStep.__table__,
|
|
BizProductionTaskStepOutput.__table__,
|
|
BizProductionTaskOutput.__table__,
|
|
BizCellProduct.__table__,
|
|
BizStorageSlotOccupancy.__table__,
|
|
BizOutboundReleaseCell.__table__,
|
|
BizQcWorkOrder.__table__,
|
|
BizQcWorkOrderInspectionBatch.__table__,
|
|
BizLabSampleProcessSupplement.__table__,
|
|
BizLabSampleProcessingExtra.__table__,
|
|
],
|
|
)
|
|
return sessionmaker(bind=engine)()
|
|
|
|
|
|
def _now():
|
|
return datetime(2026, 11, 20, 9, 33, 18)
|
|
|
|
|
|
def _seed(db):
|
|
now = _now()
|
|
db.add(
|
|
BizSampleReception(
|
|
id="recv-1",
|
|
protocol_sample_id="ps-1",
|
|
collection_protocol_id="cp-1",
|
|
reception_time=now,
|
|
accept_conclusion="js",
|
|
reception_source="cell_storage",
|
|
sample_code="ZKZX34000957",
|
|
lab_storage_code="B00001112",
|
|
tenant_no="tenant-a",
|
|
create_dept_id="",
|
|
create_time=now,
|
|
update_time=now,
|
|
del_flag=0,
|
|
)
|
|
)
|
|
db.add(
|
|
BizProductionWorkOrder(
|
|
id="pwo-1",
|
|
work_order_no="SC202611200001",
|
|
reception_id="recv-1",
|
|
sample_code="ZKZX34000957",
|
|
work_order_status="ywc",
|
|
tenant_no="tenant-a",
|
|
create_dept_id="",
|
|
create_time=now,
|
|
update_time=now,
|
|
del_flag=0,
|
|
)
|
|
)
|
|
db.add(
|
|
BizProductionTask(
|
|
id="task-1",
|
|
task_no="T-1",
|
|
work_order_id="pwo-1",
|
|
task_name="B00001112样本处理",
|
|
description="编码B00001112",
|
|
task_status="ywc",
|
|
tenant_no="tenant-a",
|
|
create_dept_id="",
|
|
create_time=now,
|
|
update_time=now,
|
|
del_flag=0,
|
|
)
|
|
)
|
|
db.add(
|
|
BizProductionTask(
|
|
id="task-other",
|
|
task_no="T-2",
|
|
work_order_id="pwo-other",
|
|
task_name="B00001112样本处理",
|
|
task_status="ywc",
|
|
tenant_no="tenant-a",
|
|
create_dept_id="",
|
|
create_time=now,
|
|
update_time=now,
|
|
del_flag=0,
|
|
)
|
|
)
|
|
db.add(
|
|
BizProductionTaskInput(
|
|
id="in-1",
|
|
task_id="task-1",
|
|
input_name="B0000112",
|
|
tenant_no="tenant-a",
|
|
create_dept_id="",
|
|
create_time=now,
|
|
update_time=now,
|
|
del_flag=0,
|
|
)
|
|
)
|
|
db.add(
|
|
BizProductionTaskStep(
|
|
id="step-1",
|
|
task_id="task-1",
|
|
sop_content="请处理 B00001112,不要改 B000011120",
|
|
tenant_no="tenant-a",
|
|
create_dept_id="",
|
|
create_time=now,
|
|
update_time=now,
|
|
del_flag=0,
|
|
)
|
|
)
|
|
db.add(
|
|
BizProductionTaskStepOutput(
|
|
id="so-1",
|
|
step_id="step-1",
|
|
output_name="脐血",
|
|
output_barcode="B0000112",
|
|
tenant_no="tenant-a",
|
|
create_dept_id="",
|
|
create_time=now,
|
|
update_time=now,
|
|
del_flag=0,
|
|
)
|
|
)
|
|
db.add(
|
|
BizProductionTaskOutput(
|
|
id="out-1",
|
|
task_id="task-1",
|
|
output_name="B00001112",
|
|
output_barcode="B00001112",
|
|
tenant_no="tenant-a",
|
|
create_dept_id="",
|
|
create_time=now,
|
|
update_time=now,
|
|
del_flag=0,
|
|
)
|
|
)
|
|
db.add(
|
|
BizCellProduct(
|
|
id="cp-1",
|
|
product_code="B00001112",
|
|
product_name="B00001112",
|
|
batch_no="B00001112",
|
|
production_task_id="task-1",
|
|
tenant_no="tenant-a",
|
|
create_dept_id="",
|
|
create_time=now,
|
|
update_time=now,
|
|
del_flag=0,
|
|
)
|
|
)
|
|
db.add(
|
|
BizCellProduct(
|
|
id="cp-long",
|
|
product_code="B000011120",
|
|
product_name="其他更长编码",
|
|
production_task_id="task-other",
|
|
tenant_no="tenant-a",
|
|
create_dept_id="",
|
|
create_time=now,
|
|
update_time=now,
|
|
del_flag=0,
|
|
)
|
|
)
|
|
db.add(
|
|
BizStorageSlotOccupancy(
|
|
id="occ-1",
|
|
slot_id="slot-1",
|
|
item_type="cell_product",
|
|
item_id="cp-1",
|
|
item_code="B00001112",
|
|
item_name="B00001112",
|
|
occupancy_status="stored",
|
|
tenant_no="tenant-a",
|
|
create_dept_id="",
|
|
create_time=now,
|
|
update_time=now,
|
|
del_flag=0,
|
|
)
|
|
)
|
|
db.add(
|
|
BizOutboundReleaseCell(
|
|
id="ob-1",
|
|
material_id="mat-1",
|
|
infusion_schedule_id="inf-1",
|
|
cell_product_id="cp-1",
|
|
product_code="B00001112",
|
|
product_name="B00001112",
|
|
batch_no="B00001112",
|
|
tenant_no="tenant-a",
|
|
create_dept_id="",
|
|
create_time=now,
|
|
update_time=now,
|
|
del_flag=0,
|
|
)
|
|
)
|
|
db.add(
|
|
BizQcWorkOrder(
|
|
id="qc-1",
|
|
work_order_no="ZJ202611200001",
|
|
reception_id="recv-1",
|
|
sample_code="ZKZX34000957",
|
|
work_order_status="ypz",
|
|
tenant_no="tenant-a",
|
|
create_dept_id="",
|
|
create_time=now,
|
|
update_time=now,
|
|
del_flag=0,
|
|
)
|
|
)
|
|
db.add(
|
|
BizQcWorkOrderInspectionBatch(
|
|
id="batch-1",
|
|
work_order_id="qc-1",
|
|
batch_no="B1",
|
|
batch_name="B00001112-脐血",
|
|
tenant_no="tenant-a",
|
|
create_dept_id="",
|
|
create_time=now,
|
|
update_time=now,
|
|
del_flag=0,
|
|
)
|
|
)
|
|
db.add(
|
|
BizQcWorkOrderInspectionBatch(
|
|
id="batch-2",
|
|
work_order_id="qc-1",
|
|
batch_no="B2",
|
|
batch_name="B00001112",
|
|
tenant_no="tenant-a",
|
|
create_dept_id="",
|
|
create_time=now,
|
|
update_time=now,
|
|
del_flag=0,
|
|
)
|
|
)
|
|
db.add(
|
|
BizLabSampleProcessSupplement(
|
|
id="sup-1",
|
|
lab_storage_code="B00001112",
|
|
payload="{}",
|
|
tenant_no="tenant-a",
|
|
create_dept_id="",
|
|
create_time=now,
|
|
update_time=now,
|
|
del_flag=0,
|
|
)
|
|
)
|
|
db.add(
|
|
BizLabSampleProcessingExtra(
|
|
id="extra-1",
|
|
dataset="hsc",
|
|
sample_code="B00001112",
|
|
tenant_no="tenant-a",
|
|
create_dept_id="",
|
|
create_time=now,
|
|
update_time=now,
|
|
del_flag=0,
|
|
)
|
|
)
|
|
db.commit()
|
|
|
|
|
|
def test_replace_keeps_longer_code_and_chinese_suffix():
|
|
assert replace_lab_code_token("B00001112样本处理", "B00001112", "B0000112") == "B0000112样本处理"
|
|
assert replace_lab_code_token("B00001112-脐血", "B00001112", "B0000112") == "B0000112-脐血"
|
|
assert replace_lab_code_token("B000011120", "B00001112", "B0000112") == "B000011120"
|
|
assert replace_lab_code_token("B00001112", "B0000112", "B0000999") == "B00001112"
|
|
|
|
|
|
def test_edit_renames_linked_copies_and_skips_unrelated_rows(monkeypatch):
|
|
db = _session()
|
|
_seed(db)
|
|
monkeypatch.setattr(reception_svc, "_can_edit_sample_reception", lambda *args, **kwargs: True)
|
|
monkeypatch.setattr(
|
|
"app.crud.sample_reception_check_result.save_reception_check_results",
|
|
lambda *args, **kwargs: None,
|
|
)
|
|
monkeypatch.setattr(
|
|
"app.services.protocol_reception_discard_sync.after_sample_reception_saved",
|
|
lambda *args, **kwargs: None,
|
|
)
|
|
|
|
reception_svc.update_sample_reception(
|
|
db,
|
|
reception_id="recv-1",
|
|
tenant_no="tenant-a",
|
|
user_id="user-1",
|
|
reception_time=_now(),
|
|
handler_user_id=None,
|
|
handler_name="质检主管",
|
|
receive_temperature="4",
|
|
checks=[],
|
|
accept_conclusion="js",
|
|
receive_attachments=None,
|
|
remark=None,
|
|
lab_storage_code="B0000112",
|
|
)
|
|
|
|
rec = db.query(BizSampleReception).filter_by(id="recv-1").one()
|
|
task = db.query(BizProductionTask).filter_by(id="task-1").one()
|
|
other = db.query(BizProductionTask).filter_by(id="task-other").one()
|
|
step = db.query(BizProductionTaskStep).filter_by(id="step-1").one()
|
|
step_out = db.query(BizProductionTaskStepOutput).filter_by(id="so-1").one()
|
|
output = db.query(BizProductionTaskOutput).filter_by(id="out-1").one()
|
|
product = db.query(BizCellProduct).filter_by(id="cp-1").one()
|
|
longer = db.query(BizCellProduct).filter_by(id="cp-long").one()
|
|
occ = db.query(BizStorageSlotOccupancy).filter_by(id="occ-1").one()
|
|
outbound = db.query(BizOutboundReleaseCell).filter_by(id="ob-1").one()
|
|
batches = {
|
|
row.id: row.batch_name
|
|
for row in db.query(BizQcWorkOrderInspectionBatch).all()
|
|
}
|
|
wo = db.query(BizProductionWorkOrder).filter_by(id="pwo-1").one()
|
|
qc = db.query(BizQcWorkOrder).filter_by(id="qc-1").one()
|
|
supplement = db.query(BizLabSampleProcessSupplement).filter_by(id="sup-1").one()
|
|
extra = db.query(BizLabSampleProcessingExtra).filter_by(id="extra-1").one()
|
|
kept_input = db.query(BizProductionTaskInput).filter_by(id="in-1").one()
|
|
|
|
assert rec.lab_storage_code == "B0000112"
|
|
assert rec.sample_code == "ZKZX34000957"
|
|
assert task.task_name == "B0000112样本处理"
|
|
assert task.description == "编码B0000112"
|
|
assert other.task_name == "B00001112样本处理"
|
|
assert step.sop_content == "请处理 B0000112,不要改 B000011120"
|
|
assert step_out.output_barcode == "B0000112"
|
|
assert kept_input.input_name == "B0000112"
|
|
assert output.output_name == "B0000112"
|
|
assert output.output_barcode == "B0000112"
|
|
assert product.product_code == "B0000112"
|
|
assert product.batch_no == "B0000112"
|
|
assert longer.product_code == "B000011120"
|
|
assert occ.item_code == "B0000112"
|
|
assert outbound.product_code == "B0000112"
|
|
assert batches["batch-1"] == "B0000112-脐血"
|
|
assert batches["batch-2"] == "B0000112"
|
|
assert wo.sample_code == "ZKZX34000957"
|
|
assert qc.sample_code == "ZKZX34000957"
|
|
assert supplement.lab_storage_code == "B0000112"
|
|
assert extra.sample_code == "B0000112"
|
|
|
|
|
|
def test_unchanged_code_does_not_rewrite_copies(monkeypatch):
|
|
db = _session()
|
|
_seed(db)
|
|
monkeypatch.setattr(reception_svc, "_can_edit_sample_reception", lambda *args, **kwargs: True)
|
|
monkeypatch.setattr(
|
|
"app.crud.sample_reception_check_result.save_reception_check_results",
|
|
lambda *args, **kwargs: None,
|
|
)
|
|
monkeypatch.setattr(
|
|
"app.services.protocol_reception_discard_sync.after_sample_reception_saved",
|
|
lambda *args, **kwargs: None,
|
|
)
|
|
before = db.query(BizProductionTask).filter_by(id="task-1").one().update_time
|
|
|
|
reception_svc.update_sample_reception(
|
|
db,
|
|
reception_id="recv-1",
|
|
tenant_no="tenant-a",
|
|
user_id="user-1",
|
|
reception_time=_now(),
|
|
handler_user_id=None,
|
|
handler_name="质检主管",
|
|
receive_temperature="4",
|
|
checks=[],
|
|
accept_conclusion="js",
|
|
receive_attachments=None,
|
|
remark=None,
|
|
lab_storage_code="B00001112",
|
|
)
|
|
|
|
task = db.query(BizProductionTask).filter_by(id="task-1").one()
|
|
assert task.task_name == "B00001112样本处理"
|
|
assert task.update_time == before
|
|
|
|
|
|
def test_duplicate_product_code_rejects_and_keeps_old_copies(monkeypatch):
|
|
db = _session()
|
|
_seed(db)
|
|
now = _now()
|
|
db.add(
|
|
BizCellProduct(
|
|
id="cp-taken",
|
|
product_code="B0000112",
|
|
product_name="已被占用",
|
|
production_task_id="task-other",
|
|
tenant_no="tenant-a",
|
|
create_dept_id="",
|
|
create_time=now,
|
|
update_time=now,
|
|
del_flag=0,
|
|
)
|
|
)
|
|
db.commit()
|
|
monkeypatch.setattr(reception_svc, "_can_edit_sample_reception", lambda *args, **kwargs: True)
|
|
monkeypatch.setattr(
|
|
"app.crud.sample_reception_check_result.save_reception_check_results",
|
|
lambda *args, **kwargs: None,
|
|
)
|
|
monkeypatch.setattr(
|
|
"app.services.protocol_reception_discard_sync.after_sample_reception_saved",
|
|
lambda *args, **kwargs: None,
|
|
)
|
|
|
|
with pytest.raises(ValueError, match="实验室入库编码已存在"):
|
|
reception_svc.update_sample_reception(
|
|
db,
|
|
reception_id="recv-1",
|
|
tenant_no="tenant-a",
|
|
user_id="user-1",
|
|
reception_time=_now(),
|
|
handler_user_id=None,
|
|
handler_name="质检主管",
|
|
receive_temperature="4",
|
|
checks=[],
|
|
accept_conclusion="js",
|
|
receive_attachments=None,
|
|
remark=None,
|
|
lab_storage_code="B0000112",
|
|
)
|
|
|
|
db.rollback()
|
|
rec = db.query(BizSampleReception).filter_by(id="recv-1").one()
|
|
task = db.query(BizProductionTask).filter_by(id="task-1").one()
|
|
assert rec.lab_storage_code == "B00001112"
|
|
assert task.task_name == "B00001112样本处理"
|