"""已落库样本编码在协议编辑中不可修改。""" from types import SimpleNamespace import pytest from app.crud.collection_protocol import validate_persisted_sample_codes_immutable def _row(*, sid="ps-1", code="OLD-001", code_id="pool-1"): return SimpleNamespace(id=sid, sample_code=code, sample_code_id=code_id) def test_rejects_code_change_when_persisted_id_present(): existing = [_row()] with pytest.raises(ValueError, match="更正编码"): validate_persisted_sample_codes_immutable( existing, [{"id": "ps-1", "sampleCode": "NEW-001", "sampleCodeId": "pool-1"}], ) def test_rejects_code_id_change_when_persisted_id_present(): existing = [_row()] with pytest.raises(ValueError, match="更正编码"): validate_persisted_sample_codes_immutable( existing, [{"id": "ps-1", "sampleCode": "OLD-001", "sampleCodeId": "pool-other"}], ) def test_allows_same_code_for_persisted_sample(): existing = [_row()] validate_persisted_sample_codes_immutable( existing, [{"id": "ps-1", "sampleCode": "OLD-001", "sampleCodeId": "pool-1"}], ) def test_allows_new_sample_without_id(): existing = [_row()] validate_persisted_sample_codes_immutable( existing, [ {"id": "ps-1", "sampleCode": "OLD-001", "sampleCodeId": "pool-1"}, {"sampleCode": "NEW-002", "sampleCodeId": "pool-2"}, ], ) def test_ignores_unknown_id_as_new_sample(): existing = [_row()] validate_persisted_sample_codes_immutable( existing, [{"id": "ps-unknown", "sampleCode": "ANY", "sampleCodeId": "x"}], ) def test_trims_before_compare(): existing = [_row(code="OLD-001", code_id="pool-1")] validate_persisted_sample_codes_immutable( existing, [{"id": "ps-1", "sample_code": " OLD-001 ", "sample_code_id": " pool-1 "}], )