| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683 |
- from datetime import datetime, timedelta
- import pytest
- from fastapi import HTTPException
- from pydantic import ValidationError
- from sqlalchemy import create_engine, event
- from sqlalchemy.orm import sessionmaker
- from app.models.base import Base
- from app.models.models import (
- BizAttachment,
- BizCellSampleType,
- BizCollectionProtocol,
- BizCollectionRegionStaff,
- BizProtocolCollectionInfo,
- BizProtocolFullmoonTodo,
- BizProtocolPartyCustomer,
- BizProtocolSample,
- BizProtocolStorageCert,
- BizQcWorkOrder,
- BizSampleReception,
- BizStorageReleaseReview,
- SysPost,
- SysPostRole,
- SysRole,
- SysRoleDataScopeDetail,
- SysUser,
- SysUserPost,
- SysUserRole,
- )
- from app.models.research_donation import (
- BizResearchDonation,
- BizResearchDonationCollection,
- BizResearchDonationParty,
- BizResearchDonationSample,
- )
- TABLES = [
- BizAttachment.__table__,
- BizCollectionProtocol.__table__,
- BizProtocolCollectionInfo.__table__,
- BizProtocolPartyCustomer.__table__,
- BizProtocolSample.__table__,
- BizResearchDonation.__table__,
- BizResearchDonationCollection.__table__,
- BizResearchDonationParty.__table__,
- BizResearchDonationSample.__table__,
- BizCellSampleType.__table__,
- BizSampleReception.__table__,
- BizQcWorkOrder.__table__,
- BizStorageReleaseReview.__table__,
- BizProtocolStorageCert.__table__,
- BizProtocolFullmoonTodo.__table__,
- BizCollectionRegionStaff.__table__,
- SysUser.__table__,
- SysRole.__table__,
- SysRoleDataScopeDetail.__table__,
- SysUserRole.__table__,
- SysPost.__table__,
- SysPostRole.__table__,
- SysUserPost.__table__,
- ]
- def _session():
- engine = create_engine('sqlite:///:memory:')
- Base.metadata.create_all(engine, tables=TABLES)
- db = sessionmaker(bind=engine)()
- for index, name in enumerate(
- (
- 'a.jpg',
- 'b.jpg',
- 'cert-new.jpg',
- 'cert-old.jpg',
- 'i.jpg',
- 'insurance-new.jpg',
- 'insurance-old.jpg',
- 'keep.jpg',
- 'keep-insurance.jpg',
- 'old.jpg',
- )
- ):
- db.add(
- BizAttachment(
- id=f'att-sample-{index}',
- business_type='market:specialist:sample:staging',
- original_name=name,
- file_ext='jpg',
- file_url=name,
- file_size=10,
- file_type='image/jpeg',
- tenant_no='tenant-a',
- create_by='u1',
- create_dept_id='',
- del_flag=0,
- )
- )
- db.commit()
- return db
- def _ctx(kind='specialist', user_id='u1'):
- from app.services.market_field_scope import MarketFieldContext
- return MarketFieldContext('tenant-a', user_id, kind)
- def _user(user_id='u1'):
- return {
- 'user_id': user_id,
- 'tenant_no': 'tenant-a',
- 'role': 'super_admin',
- 'region_id': 'dept-a',
- }
- def _assign_region_staff(db, user_id: str, key: str, region_id: str = 'r1'):
- db.add(
- BizCollectionRegionStaff(
- id=f'staff-{key}',
- region_id=region_id,
- user_id=user_id,
- is_leader=0,
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- )
- )
- def test_sample_handle_rejects_external_attachment_reference():
- from app.services.market_sample import update_storage_cert
- db = _session()
- _, _, sample = _cell_sample(db, 'attachment-reject')
- with pytest.raises(ValueError, match='附件'):
- update_storage_cert(
- db,
- sample_id=f"cell_storage:{sample.sample_code}",
- fields={'attachments': 'https://evil.test/a.jpg'},
- ctx=_ctx(),
- user_info=_user(),
- )
- def _cell_sample(
- db,
- key,
- *,
- sample_code=None,
- sales='u1',
- manager='m1',
- region='r1',
- tenant='tenant-a',
- deleted=0,
- created=None,
- row_id=None,
- ):
- created = created or datetime(2026, 10, 1, 8, 0)
- code = f'C-{key}' if sample_code is None else sample_code
- protocol = BizCollectionProtocol(
- id=f'cp-{key}',
- protocol_no=f'CP-{key}',
- protocol_status='sxz',
- tenant_no=tenant,
- create_dept_id='dept-a',
- create_time=created,
- del_flag=deleted,
- )
- collection = BizProtocolCollectionInfo(
- id=f'cc-{key}',
- collection_protocol_id=protocol.id,
- region_id=region,
- sales_user_id=sales,
- region_manager_user_id=manager,
- sample_collect_time=created,
- tenant_no=tenant,
- create_dept_id='dept-a',
- del_flag=deleted,
- )
- party = BizProtocolPartyCustomer(
- id=f'pc-{key}',
- collection_protocol_id=protocol.id,
- mother_name=f'客户-{key}',
- tenant_no=tenant,
- create_dept_id='dept-a',
- del_flag=deleted,
- )
- sample = BizProtocolSample(
- id=row_id or f'cs-{key}',
- collection_protocol_id=protocol.id,
- sample_code=code,
- sample_status='ycj',
- tenant_no=tenant,
- create_dept_id='dept-a',
- create_time=created,
- del_flag=deleted,
- )
- db.add_all([protocol, collection, party, sample])
- db.commit()
- return protocol, collection, sample
- def _research_sample(
- db,
- key,
- *,
- sample_code=None,
- sales='u1',
- manager='m1',
- region='r1',
- tenant='tenant-a',
- deleted=0,
- created=None,
- ):
- created = created or datetime(2026, 10, 1, 8, 0)
- code = f'R-{key}' if sample_code is None else sample_code
- donation = BizResearchDonation(
- id=f'rd-{key}',
- donation_no=f'RD-{key}',
- donation_status='sxz',
- tenant_no=tenant,
- create_dept_id='dept-a',
- create_time=created,
- del_flag=deleted,
- )
- collection = BizResearchDonationCollection(
- id=f'rc-{key}',
- research_donation_id=donation.id,
- region_id=region,
- sales_user_id=sales,
- region_manager_user_id=manager,
- sample_collect_time=created,
- tenant_no=tenant,
- create_dept_id='dept-a',
- del_flag=deleted,
- )
- party = BizResearchDonationParty(
- id=f'rp-{key}',
- research_donation_id=donation.id,
- mother_name=f'科研客户-{key}',
- tenant_no=tenant,
- create_dept_id='dept-a',
- del_flag=deleted,
- )
- sample = BizResearchDonationSample(
- id=f'rs-{key}',
- research_donation_id=donation.id,
- sample_code=code,
- sample_status='jcz',
- tenant_no=tenant,
- create_dept_id='dept-a',
- create_time=created,
- del_flag=deleted,
- )
- db.add_all([donation, collection, party, sample])
- db.commit()
- return donation, collection, sample
- def _downstream(db, key, code, source, *, tenant='tenant-a', when=None, conclusion='tg'):
- when = when or datetime(2026, 10, 2, 8, 0)
- db.add_all(
- [
- BizSampleReception(
- id=f'rec-{key}',
- protocol_sample_id='stale-row-id',
- collection_protocol_id='stale-parent-id',
- reception_time=when,
- accept_conclusion='js',
- reception_source=source,
- sample_code=code,
- tenant_no=tenant,
- create_dept_id='dept-a',
- create_time=when,
- del_flag=0,
- ),
- BizQcWorkOrder(
- id=f'qc-{key}',
- work_order_no=f'QC-{key}',
- sample_code=code,
- reception_source=source,
- trigger_source='reception',
- work_order_status='ywc',
- conclusion=conclusion,
- tenant_no=tenant,
- create_dept_id='dept-a',
- create_time=when,
- update_time=when,
- del_flag=0,
- ),
- BizStorageReleaseReview(
- id=f'st-{key}',
- sample_code=code,
- reception_source=source,
- storage_status='yrk',
- storage_time=when,
- tenant_no=tenant,
- create_dept_id='dept-a',
- create_time=when,
- del_flag=0,
- ),
- ]
- )
- db.commit()
- def test_sample_list_resolves_parent_scope_and_uses_global_stable_paging():
- from app.services.market_sample import list_samples
- db = _session()
- base = datetime(2026, 10, 10, 8, 0)
- _cell_sample(db, 'c-old', created=base)
- _research_sample(db, 'r-new', created=base + timedelta(hours=3))
- _cell_sample(db, 'c-mid', created=base + timedelta(hours=2))
- _research_sample(db, 'r-mid', created=base + timedelta(hours=1))
- _cell_sample(db, 'other-user', sales='u2', created=base + timedelta(hours=4))
- _cell_sample(db, 'cross-tenant', tenant='tenant-b', created=base + timedelta(hours=5))
- _research_sample(db, 'deleted', deleted=1, created=base + timedelta(hours=6))
- page1, total = list_samples(db, ctx=_ctx(), user_info=_user(), page=1, page_size=2)
- page2, total2 = list_samples(db, ctx=_ctx(), user_info=_user(), page=2, page_size=2)
- assert total == total2 == 4
- assert [item['sampleCode'] for item in page1] == ['R-r-new', 'C-c-mid']
- assert [item['sampleCode'] for item in page2] == ['R-r-mid', 'C-c-old']
- assert page1[0]['id'] == 'research_donation:R-r-new'
- assert page1[1]['id'] == 'cell_storage:C-c-mid'
- assert all('statusTone' in item and 'tagType' in item for item in page1)
- def test_manager_and_dual_role_use_the_fixed_route_field_scope():
- from app.services.market_sample import list_samples
- db = _session()
- db.add(
- BizCollectionRegionStaff(
- id='staff-m1',
- region_id='r-managed',
- user_id='m1',
- is_leader=1,
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- )
- )
- db.commit()
- _cell_sample(db, 'managed', sales='u2', manager='other', region='r-managed')
- _cell_sample(db, 'assigned', sales='u2', manager='m1', region='r-other')
- _cell_sample(db, 'hidden', sales='m1', manager='other', region='r-other')
- manager_items, _ = list_samples(db, ctx=_ctx('manager', 'm1'), user_info=_user('m1'))
- specialist_items, _ = list_samples(
- db, ctx=_ctx('specialist', 'm1'), user_info=_user('m1')
- )
- assert {item['sampleCode'] for item in manager_items} == {'C-managed', 'C-assigned'}
- assert {item['sampleCode'] for item in specialist_items} == {'C-hidden'}
- def test_detail_matches_downstream_only_by_tenant_code_and_source_and_latest():
- from app.services.market_sample import get_sample_detail
- db = _session()
- _cell_sample(db, 'same', sample_code='SAME', row_id='rebuilt-row-id')
- _research_sample(db, 'same', sample_code='SAME')
- old = datetime(2026, 10, 1, 8, 0)
- new = datetime(2026, 10, 2, 8, 0)
- _downstream(db, 'cell-old', 'SAME', 'cell_storage', when=old, conclusion='btg')
- _downstream(db, 'cell-new', 'SAME', 'cell_storage', when=new, conclusion='tg')
- _downstream(db, 'research', 'SAME', 'research_donation', when=new, conclusion='btg')
- _downstream(db, 'cross', 'SAME', 'cell_storage', tenant='tenant-b', when=new + timedelta(days=1), conclusion='btg')
- cell = get_sample_detail(
- db, sample_id='cell_storage:SAME', ctx=_ctx(), user_info=_user()
- )
- research = get_sample_detail(
- db, sample_id='research_donation:SAME', ctx=_ctx(), user_info=_user()
- )
- assert cell['card']['sampleRowId'] == 'rebuilt-row-id'
- assert cell['progress']['qc']['code'] == 'tg'
- assert research['progress']['qc']['code'] == 'btg'
- assert cell['progress']['reception']['recordId'] == 'rec-cell-new'
- assert cell['progress']['storage']['recordId'] == 'st-cell-new'
- assert cell['sections']
- def test_detail_idor_and_malformed_ids_are_not_exposed():
- from app.services.market_sample import get_sample_detail
- db = _session()
- _cell_sample(db, 'other', sales='u2')
- _cell_sample(db, 'cross', tenant='tenant-b')
- for sample_id in ('cell_storage:C-other', 'cell_storage:C-cross', 'bad', 'evil:C-other'):
- with pytest.raises(HTTPException) as exc:
- get_sample_detail(db, sample_id=sample_id, ctx=_ctx(), user_info=_user())
- assert exc.value.status_code == 404
- def test_empty_sample_code_has_basic_read_only_detail_without_downstream():
- from app.services.market_sample import get_sample_detail, list_samples
- db = _session()
- _cell_sample(db, 'blank', sample_code='')
- _downstream(db, 'unrelated', 'C-other', 'cell_storage')
- items, total = list_samples(db, ctx=_ctx(), user_info=_user())
- assert total == 1
- assert items[0]['id'] == 'cell_storage:@id:cs-blank'
- detail = get_sample_detail(db, sample_id=items[0]['id'], ctx=_ctx(), user_info=_user())
- assert detail['card']['sampleCode'] == ''
- assert detail['actions'] == []
- assert detail['progress']['reception']['code'] == 'none'
- def test_cell_lightweight_storage_cert_and_insurance_writes_validate_handler():
- from app.services.market_sample import update_insurance, update_storage_cert
- db = _session()
- protocol, _, _ = _cell_sample(db, 'write')
- db.add_all(
- [
- SysUser(
- id='handler',
- user_no='handler',
- nick_name='办理人',
- email='',
- phone_no='',
- status='1',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- ),
- BizCollectionRegionStaff(
- id='staff-handler-write',
- region_id='r1',
- user_id='handler',
- is_leader=0,
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- ),
- SysUser(
- id='disabled',
- user_no='disabled',
- nick_name='停用人',
- email='',
- phone_no='',
- status='0',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- ),
- ]
- )
- db.commit()
- when = '2026-10-11 09:30:00'
- cert = update_storage_cert(
- db,
- sample_id='cell_storage:C-write',
- fields={
- 'handleTime': when,
- 'handlerUserId': 'handler',
- 'attachments': 'a.jpg,b.jpg',
- 'remark': '证书备注',
- },
- ctx=_ctx(),
- user_info=_user(),
- )
- insurance = update_insurance(
- db,
- sample_id='cell_storage:C-write',
- fields={
- 'handleTime': when,
- 'handlerUserId': 'handler',
- 'attachments': 'i.jpg',
- 'remark': '保险备注',
- },
- ctx=_ctx(),
- user_info=_user(),
- )
- cert_row = db.query(BizProtocolStorageCert).filter_by(collection_protocol_id=protocol.id).one()
- todo_row = db.query(BizProtocolFullmoonTodo).filter_by(collection_protocol_id=protocol.id).one()
- assert cert['handlerName'] == insurance['handlerName'] == '办理人'
- assert cert_row.cert_image_urls == 'a.jpg,b.jpg'
- assert cert_row.remark == '证书备注'
- assert todo_row.insurance_attachments == 'i.jpg'
- assert todo_row.handle_remark == '保险备注'
- with pytest.raises(ValueError, match='办理人员'):
- update_storage_cert(
- db,
- sample_id='cell_storage:C-write',
- fields={'handlerUserId': 'disabled'},
- ctx=_ctx(),
- user_info=_user(),
- )
- def test_sample_form_options_return_only_enabled_staff_in_scoped_document_region():
- from app.services.market_sample import get_sample_form_options
- db = _session()
- _cell_sample(db, 'handler-options', region='r1')
- db.add_all(
- [
- SysUser(id='eligible', user_no='eligible', nick_name='可办理', email='', phone_no='', status='1', tenant_no='tenant-a', create_dept_id='', del_flag=0),
- SysUser(id='other-region', user_no='other-region', nick_name='其他地区', email='', phone_no='', status='1', tenant_no='tenant-a', create_dept_id='', del_flag=0),
- SysUser(id='disabled-staff', user_no='disabled-staff', nick_name='停用人员', email='', phone_no='', status='0', tenant_no='tenant-a', create_dept_id='', del_flag=0),
- SysUser(id='cross-tenant-staff', user_no='cross-tenant-staff', nick_name='跨租户', email='', phone_no='', status='1', tenant_no='tenant-b', create_dept_id='', del_flag=0),
- BizCollectionRegionStaff(id='region-eligible', region_id='r1', user_id='eligible', is_leader=0, tenant_no='tenant-a', create_dept_id='', del_flag=0),
- BizCollectionRegionStaff(id='region-other', region_id='r2', user_id='other-region', is_leader=0, tenant_no='tenant-a', create_dept_id='', del_flag=0),
- BizCollectionRegionStaff(id='region-disabled', region_id='r1', user_id='disabled-staff', is_leader=0, tenant_no='tenant-a', create_dept_id='', del_flag=0),
- BizCollectionRegionStaff(id='region-cross', region_id='r1', user_id='cross-tenant-staff', is_leader=0, tenant_no='tenant-b', create_dept_id='', del_flag=0),
- ]
- )
- db.commit()
- result = get_sample_form_options(
- db,
- sample_id='cell_storage:C-handler-options',
- ctx=_ctx(),
- user_info=_user(),
- )
- assert result == {'staff': [{'value': 'eligible', 'label': '可办理'}]}
- def test_sample_handler_must_be_enabled_staff_in_current_document_region():
- from app.services.market_sample import update_storage_cert
- db = _session()
- _cell_sample(db, 'handler-region', region='r1')
- db.add_all(
- [
- SysUser(id='wrong-region', user_no='wrong-region', nick_name='错区人员', email='', phone_no='', status='1', tenant_no='tenant-a', create_dept_id='', del_flag=0),
- BizCollectionRegionStaff(id='wrong-region-link', region_id='r2', user_id='wrong-region', is_leader=0, tenant_no='tenant-a', create_dept_id='', del_flag=0),
- ]
- )
- db.commit()
- with pytest.raises(ValueError, match='办理人员'):
- update_storage_cert(
- db,
- sample_id='cell_storage:C-handler-region',
- fields={'handleTime': '2026-10-11 09:30:00', 'handlerUserId': 'wrong-region'},
- ctx=_ctx(),
- user_info=_user(),
- )
- assert db.query(BizProtocolStorageCert).count() == 0
- def test_research_and_empty_code_samples_are_read_only_and_scope_is_rechecked():
- from app.services.market_sample import update_storage_cert
- db = _session()
- _research_sample(db, 'readonly')
- _, collection, _ = _cell_sample(db, 'moved')
- _cell_sample(db, 'blank', sample_code='')
- for sample_id in ('research_donation:R-readonly', 'cell_storage:@id:cs-blank'):
- with pytest.raises(ValueError, match='不支持'):
- update_storage_cert(
- db,
- sample_id=sample_id,
- fields={'remark': 'x'},
- ctx=_ctx(),
- user_info=_user(),
- )
- collection.sales_user_id = 'u2'
- db.commit()
- with pytest.raises(HTTPException) as exc:
- update_storage_cert(
- db,
- sample_id='cell_storage:C-moved',
- fields={'remark': 'x'},
- ctx=_ctx(),
- user_info=_user(),
- )
- assert exc.value.status_code == 404
- def test_lightweight_dtos_forbid_status_qc_production_lab_and_sample_fields():
- from app.schemas.market_mobile import (
- MarketSampleInsuranceBody,
- MarketSampleStorageCertBody,
- )
- allowed = {
- 'handleTime': '2026-10-11 09:30:00',
- 'handlerUserId': 'u1',
- 'attachments': 'a.jpg',
- 'remark': 'ok',
- }
- MarketSampleStorageCertBody.model_validate(allowed)
- MarketSampleInsuranceBody.model_validate(allowed)
- for forbidden in (
- 'sampleCode',
- 'sampleStatus',
- 'storageStatus',
- 'qcConclusion',
- 'productionResult',
- 'labStorageCode',
- 'makeStatus',
- 'todoStatus',
- 'tenantNo',
- ):
- with pytest.raises(ValidationError):
- MarketSampleStorageCertBody.model_validate({**allowed, forbidden: 'bad'})
- def test_write_rolls_back_when_flush_fails(monkeypatch):
- from app.services.market_sample import update_storage_cert
- db = _session()
- _cell_sample(db, 'rollback')
- original_flush = db.flush
- def fail_flush(*args, **kwargs):
- original_flush(*args, **kwargs)
- raise RuntimeError('injected failure')
- monkeypatch.setattr(db, 'flush', fail_flush)
- with pytest.raises(RuntimeError, match='injected failure'):
- update_storage_cert(
- db,
- sample_id='cell_storage:C-rollback',
- fields={'remark': 'must rollback'},
- ctx=_ctx(),
- user_info=_user(),
- )
- monkeypatch.setattr(db, 'flush', original_flush)
- assert db.query(BizProtocolStorageCert).count() == 0
- def test_list_batch_enrichment_has_bounded_query_count():
- from app.services.market_sample import list_samples
- db = _session()
- for index in range(8):
- source = 'cell_storage' if index % 2 == 0 else 'research_donation'
- if source == 'cell_storage':
- _cell_sample(db, f'q{index}')
- code = f'C-q{index}'
- else:
- _research_sample(db, f'q{index}')
- code = f'R-q{index}'
- _downstream(db, f'q{index}', code, source)
- statements = []
- def count_statement(*args):
- statements.append(args[2])
- event.listen(db.bind, 'before_cursor_execute', count_statement)
- try:
- items, total = list_samples(db, ctx=_ctx(), user_info=_user(), page_size=20)
- finally:
- event.remove(db.bind, 'before_cursor_execute', count_statement)
- assert total == len(items) == 8
- assert len(statements) <= 16
- def test_market_sample_routes_are_fixed_and_have_no_lab_transitions():
- from app.api.market_mobile import router
- paths = {route.path for route in router.routes}
- for kind in ('specialist', 'manager'):
- base = f'/mini/market/{kind}/samples'
- assert base in paths
- assert f'{base}/{{sample_id}}' in paths
- assert f'{base}/{{sample_id}}/storage-cert' in paths
- assert f'{base}/{{sample_id}}/insurance' in paths
- assert not any(
- path.startswith(base) and any(word in path for word in ('receive', '/qc', 'production', '/storage-in'))
- for path in paths
- )
- def test_global_identity_query_deduplicates_legacy_duplicate_child_rows():
- from app.services.market_sample import list_samples
- db = _session()
- protocol, _, _ = _cell_sample(db, 'duplicate-child')
- db.add(
- BizProtocolCollectionInfo(
- id='cc-duplicate-child-legacy',
- collection_protocol_id=protocol.id,
- region_id='r1',
- sales_user_id='u1',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- )
- )
- db.commit()
- items, total = list_samples(db, ctx=_ctx(), user_info=_user())
- assert total == len(items) == 1
- def test_partial_handle_update_preserves_omitted_lightweight_fields():
- from app.services.market_sample import update_storage_cert
- db = _session()
- _cell_sample(db, 'partial')
- db.add(
- SysUser(
- id='handler',
- user_no='handler',
- nick_name='办理人',
- email='',
- phone_no='',
- status='1',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- )
- )
- _assign_region_staff(db, 'handler', 'partial')
- db.commit()
- update_storage_cert(
- db,
- sample_id='cell_storage:C-partial',
- fields={
- 'handleTime': '2026-10-11 09:30:00',
- 'handlerUserId': 'handler',
- 'attachments': 'old.jpg',
- 'remark': '旧备注',
- },
- ctx=_ctx(),
- user_info=_user(),
- )
- update_storage_cert(
- db,
- sample_id='cell_storage:C-partial',
- fields={'remark': '新备注'},
- ctx=_ctx(),
- user_info=_user(),
- )
- row = db.query(BizProtocolStorageCert).one()
- assert _fmt(row.handle_time) == '2026-10-11 09:30:00'
- assert row.handler_user_id == 'handler'
- assert row.cert_image_urls == 'old.jpg'
- assert row.remark == '新备注'
- def _fmt(value):
- return value.strftime('%Y-%m-%d %H:%M:%S') if value else ''
- def test_materialized_collection_row_itself_must_be_inside_fixed_scope():
- from app.services.market_sample import list_samples
- db = _session()
- protocol, visible, _ = _cell_sample(db, 'mixed-collection', region='visible-region')
- db.add(
- BizProtocolCollectionInfo(
- id='cc-mixed-hidden',
- collection_protocol_id=protocol.id,
- region_id='hidden-region',
- sales_user_id='u2',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- )
- )
- db.commit()
- items, total = list_samples(db, ctx=_ctx(), user_info=_user())
- assert total == 1
- assert items[0]['regionId'] == visible.region_id
- def test_progress_uses_normalized_codes_and_explicit_semantic_tones():
- from app.services.market_sample import get_sample_detail
- db = _session()
- _cell_sample(db, 'tone')
- detail = get_sample_detail(
- db, sample_id='cell_storage:C-tone', ctx=_ctx(), user_info=_user()
- )
- assert detail['progress']['collection'] == {
- 'code': 'collected',
- 'label': '已采集',
- 'statusTone': 'success',
- 'tagType': 'success',
- }
- assert detail['progress']['reception']['statusTone'] == 'info'
- assert detail['progress']['qc']['statusTone'] == 'warning'
- def test_legacy_storage_cert_and_insurance_statuses_are_read_as_handled():
- from app.services.market_sample import get_sample_detail
- db = _session()
- protocol, _, _ = _cell_sample(db, 'legacy-handled')
- protocol_alt, _, _ = _cell_sample(db, 'legacy-handled-alt')
- db.add_all(
- [
- BizProtocolStorageCert(
- id='cert-legacy',
- collection_protocol_id=protocol.id,
- make_status='0',
- make_date='20261001',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- ),
- BizProtocolFullmoonTodo(
- id='todo-legacy',
- collection_protocol_id=protocol.id,
- todo_status='1',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- ),
- BizProtocolStorageCert(
- id='cert-legacy-status',
- collection_protocol_id=protocol_alt.id,
- make_status='1',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- ),
- BizProtocolFullmoonTodo(
- id='todo-legacy-date',
- collection_protocol_id=protocol_alt.id,
- todo_status='0',
- handle_date='20261002',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- ),
- ]
- )
- db.commit()
- detail = get_sample_detail(
- db,
- sample_id='cell_storage:C-legacy-handled',
- ctx=_ctx(),
- user_info=_user(),
- )
- section = next(item for item in detail['sections'] if item['key'] == 'market-handles')
- assert section['storageCert']['code'] == 'handled'
- assert section['storageCert']['statusTone'] == 'success'
- assert section['insurance']['code'] == 'handled'
- assert section['insurance']['statusTone'] == 'success'
- alt = get_sample_detail(
- db,
- sample_id='cell_storage:C-legacy-handled-alt',
- ctx=_ctx(),
- user_info=_user(),
- )
- alt_section = next(
- item for item in alt['sections'] if item['key'] == 'market-handles'
- )
- assert alt_section['storageCert']['code'] == 'handled'
- assert alt_section['insurance']['code'] == 'handled'
- def test_setting_handle_time_synchronizes_legacy_status_and_date_for_both_actions():
- from app.services.market_sample import update_insurance, update_storage_cert
- db = _session()
- _cell_sample(db, 'sync-set')
- db.add(
- SysUser(
- id='handler',
- user_no='handler',
- nick_name='办理人',
- email='',
- phone_no='',
- status='1',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- )
- )
- _assign_region_staff(db, 'handler', 'sync-set')
- db.commit()
- fields = {
- 'handleTime': '2026-10-11 09:30:00',
- 'handlerUserId': 'handler',
- 'attachments': 'keep.jpg',
- 'remark': '保留备注',
- }
- update_storage_cert(
- db,
- sample_id='cell_storage:C-sync-set',
- fields=fields,
- ctx=_ctx(),
- user_info=_user(),
- )
- update_insurance(
- db,
- sample_id='cell_storage:C-sync-set',
- fields={**fields, 'attachments': 'keep-insurance.jpg'},
- ctx=_ctx(),
- user_info=_user(),
- )
- cert = db.query(BizProtocolStorageCert).one()
- todo = db.query(BizProtocolFullmoonTodo).one()
- assert (cert.make_status, cert.make_date) == ('1', '20261011')
- assert (todo.todo_status, todo.handle_date) == ('1', '20261011')
- def test_clearing_handle_time_clears_handler_and_legacy_status_but_preserves_content():
- from app.services.market_sample import update_insurance, update_storage_cert
- db = _session()
- _cell_sample(db, 'sync-clear')
- db.add(
- SysUser(
- id='handler',
- user_no='handler',
- nick_name='办理人',
- email='',
- phone_no='',
- status='1',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- )
- )
- _assign_region_staff(db, 'handler', 'sync-clear')
- db.commit()
- initial = {
- 'handleTime': '2026-10-11 09:30:00',
- 'handlerUserId': 'handler',
- 'attachments': 'keep.jpg',
- 'remark': '保留备注',
- }
- for updater, attachment in (
- (update_storage_cert, 'keep.jpg'),
- (update_insurance, 'keep-insurance.jpg'),
- ):
- updater(
- db,
- sample_id='cell_storage:C-sync-clear',
- fields={**initial, 'attachments': attachment},
- ctx=_ctx(),
- user_info=_user(),
- )
- updater(
- db,
- sample_id='cell_storage:C-sync-clear',
- fields={'handleTime': None, 'handlerUserId': 'handler'},
- ctx=_ctx(),
- user_info=_user(),
- )
- cert = db.query(BizProtocolStorageCert).one()
- todo = db.query(BizProtocolFullmoonTodo).one()
- assert (cert.handle_time, cert.handler_user_id, cert.make_status, cert.make_date) == (
- None,
- None,
- '0',
- None,
- )
- assert (todo.handle_time, todo.handler_user_id, todo.todo_status, todo.handle_date) == (
- None,
- None,
- '0',
- None,
- )
- assert (cert.cert_image_urls, cert.remark) == ('keep.jpg', '保留备注')
- assert (todo.insurance_attachments, todo.handle_remark) == (
- 'keep-insurance.jpg',
- '保留备注',
- )
- def test_partial_insurance_update_preserves_omitted_lightweight_fields():
- from app.services.market_sample import update_insurance
- db = _session()
- _cell_sample(db, 'partial-insurance')
- db.add(
- SysUser(
- id='handler',
- user_no='handler',
- nick_name='办理人',
- email='',
- phone_no='',
- status='1',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- )
- )
- _assign_region_staff(db, 'handler', 'partial-insurance')
- db.commit()
- update_insurance(
- db,
- sample_id='cell_storage:C-partial-insurance',
- fields={
- 'handleTime': '2026-10-11 09:30:00',
- 'handlerUserId': 'handler',
- 'attachments': 'old.jpg',
- 'remark': '旧备注',
- },
- ctx=_ctx(),
- user_info=_user(),
- )
- update_insurance(
- db,
- sample_id='cell_storage:C-partial-insurance',
- fields={'remark': '新备注'},
- ctx=_ctx(),
- user_info=_user(),
- )
- row = db.query(BizProtocolFullmoonTodo).one()
- assert _fmt(row.handle_time) == '2026-10-11 09:30:00'
- assert row.handler_user_id == 'handler'
- assert row.insurance_attachments == 'old.jpg'
- assert row.handle_remark == '新备注'
- def test_partial_update_preserves_legacy_handled_fields_when_time_is_omitted():
- from app.services.market_sample import update_insurance, update_storage_cert
- db = _session()
- protocol, _, _ = _cell_sample(db, 'partial-legacy')
- db.add_all(
- [
- SysUser(
- id='handler',
- user_no='handler',
- nick_name='办理人',
- email='',
- phone_no='',
- status='1',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- ),
- BizProtocolStorageCert(
- id='cert-partial-legacy',
- collection_protocol_id=protocol.id,
- make_status='1',
- make_date='20261001',
- handler_user_id='handler',
- cert_image_urls='cert-old.jpg',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- ),
- BizProtocolFullmoonTodo(
- id='todo-partial-legacy',
- collection_protocol_id=protocol.id,
- todo_status='1',
- handle_date='20261002',
- handler_user_id='handler',
- insurance_attachments='insurance-old.jpg',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- ),
- ]
- )
- db.commit()
- update_storage_cert(
- db,
- sample_id='cell_storage:C-partial-legacy',
- fields={'remark': '证书新备注'},
- ctx=_ctx(),
- user_info=_user(),
- )
- update_insurance(
- db,
- sample_id='cell_storage:C-partial-legacy',
- fields={'remark': '保险新备注'},
- ctx=_ctx(),
- user_info=_user(),
- )
- cert = db.query(BizProtocolStorageCert).one()
- todo = db.query(BizProtocolFullmoonTodo).one()
- assert (cert.make_status, cert.make_date, cert.handler_user_id) == (
- '1',
- '20261001',
- 'handler',
- )
- assert cert.cert_image_urls == 'cert-old.jpg'
- assert (todo.todo_status, todo.handle_date, todo.handler_user_id) == (
- '1',
- '20261002',
- 'handler',
- )
- assert todo.insurance_attachments == 'insurance-old.jpg'
- def test_handler_only_update_changes_legacy_handled_records_without_clearing_status():
- from app.services.market_sample import update_insurance, update_storage_cert
- db = _session()
- protocol, _, _ = _cell_sample(db, 'handler-only-legacy')
- db.add_all(
- [
- SysUser(
- id='new-handler',
- user_no='new-handler',
- nick_name='新办理人',
- email='',
- phone_no='',
- status='1',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- ),
- BizProtocolStorageCert(
- id='cert-handler-only',
- collection_protocol_id=protocol.id,
- make_status='1',
- make_date='20261001',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- ),
- BizProtocolFullmoonTodo(
- id='todo-handler-only',
- collection_protocol_id=protocol.id,
- todo_status='0',
- handle_date='20261002',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- ),
- ]
- )
- _assign_region_staff(db, 'new-handler', 'handler-only-legacy')
- db.commit()
- for updater in (update_storage_cert, update_insurance):
- result = updater(
- db,
- sample_id='cell_storage:C-handler-only-legacy',
- fields={'handlerUserId': 'new-handler'},
- ctx=_ctx(),
- user_info=_user(),
- )
- assert result['handlerName'] == '新办理人'
- cert = db.query(BizProtocolStorageCert).one()
- todo = db.query(BizProtocolFullmoonTodo).one()
- assert (cert.handler_user_id, cert.make_status, cert.make_date) == (
- 'new-handler',
- '1',
- '20261001',
- )
- assert (todo.handler_user_id, todo.todo_status, todo.handle_date) == (
- 'new-handler',
- '0',
- '20261002',
- )
- def test_handler_only_update_rejects_truly_unhandled_cert_and_insurance():
- from app.services.market_sample import update_insurance, update_storage_cert
- db = _session()
- protocol, _, _ = _cell_sample(db, 'handler-only-unhandled')
- db.add_all(
- [
- SysUser(
- id='handler',
- user_no='handler',
- nick_name='办理人',
- email='',
- phone_no='',
- status='1',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- ),
- BizProtocolStorageCert(
- id='cert-unhandled',
- collection_protocol_id=protocol.id,
- make_status='0',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- ),
- BizProtocolFullmoonTodo(
- id='todo-unhandled',
- collection_protocol_id=protocol.id,
- todo_status='0',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- ),
- ]
- )
- db.commit()
- for updater in (update_storage_cert, update_insurance):
- with pytest.raises(ValueError, match='未办理'):
- updater(
- db,
- sample_id='cell_storage:C-handler-only-unhandled',
- fields={'handlerUserId': 'handler'},
- ctx=_ctx(),
- user_info=_user(),
- )
- def test_historical_disabled_handler_does_not_block_content_only_updates():
- from app.services.market_sample import update_insurance, update_storage_cert
- db = _session()
- protocol, _, _ = _cell_sample(db, 'disabled-history')
- db.add_all(
- [
- SysUser(
- id='disabled-history-handler',
- user_no='disabled-history-handler',
- nick_name='历史办理人',
- email='',
- phone_no='',
- status='0',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=1,
- ),
- BizProtocolStorageCert(
- id='cert-disabled-history',
- collection_protocol_id=protocol.id,
- handle_time=datetime(2026, 10, 1, 8, 0),
- handler_user_id='disabled-history-handler',
- make_status='1',
- make_date='20261001',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- ),
- BizProtocolFullmoonTodo(
- id='todo-disabled-history',
- collection_protocol_id=protocol.id,
- handle_time=datetime(2026, 10, 2, 8, 0),
- handler_user_id='disabled-history-handler',
- todo_status='1',
- handle_date='20261002',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- ),
- ]
- )
- db.commit()
- update_storage_cert(
- db,
- sample_id='cell_storage:C-disabled-history',
- fields={'attachments': 'cert-new.jpg', 'remark': '证书新备注'},
- ctx=_ctx(),
- user_info=_user(),
- )
- update_insurance(
- db,
- sample_id='cell_storage:C-disabled-history',
- fields={'attachments': 'insurance-new.jpg', 'remark': '保险新备注'},
- ctx=_ctx(),
- user_info=_user(),
- )
- cert = db.query(BizProtocolStorageCert).one()
- todo = db.query(BizProtocolFullmoonTodo).one()
- assert cert.handler_user_id == todo.handler_user_id == 'disabled-history-handler'
- assert (cert.cert_image_urls, cert.remark) == ('cert-new.jpg', '证书新备注')
- assert (todo.insurance_attachments, todo.handle_remark) == (
- 'insurance-new.jpg',
- '保险新备注',
- )
- @pytest.mark.parametrize('handler_state', ['disabled', 'deleted', 'out-of-region'])
- @pytest.mark.parametrize('kind', ['storage-cert', 'insurance'])
- def test_full_unchanged_historical_handler_payload_allows_content_update(
- handler_state, kind
- ):
- from app.services.market_sample import update_insurance, update_storage_cert
- db = _session()
- protocol, _, _ = _cell_sample(db, f'historical-full-{handler_state}-{kind}')
- handler_id = f'historical-{handler_state}-{kind}'
- handle_time = datetime(2026, 10, 1, 8, 0)
- user = SysUser(
- id=handler_id,
- user_no=handler_id,
- nick_name='历史办理人',
- email='',
- phone_no='',
- status='0' if handler_state == 'disabled' else '1',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=1 if handler_state == 'deleted' else 0,
- )
- staff = BizCollectionRegionStaff(
- id=f'staff-{handler_id}',
- region_id='r2' if handler_state == 'out-of-region' else 'r1',
- user_id=handler_id,
- is_leader=0,
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- )
- db.add_all([user, staff])
- if kind == 'storage-cert':
- db.add(
- BizProtocolStorageCert(
- id=f'cert-{handler_id}',
- collection_protocol_id=protocol.id,
- handle_time=handle_time,
- handler_user_id=handler_id,
- cert_image_urls='cert-old.jpg',
- make_status='1',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- )
- )
- updater = update_storage_cert
- attachments = 'cert-new.jpg'
- else:
- db.add(
- BizProtocolFullmoonTodo(
- id=f'todo-{handler_id}',
- collection_protocol_id=protocol.id,
- handle_time=handle_time,
- handler_user_id=handler_id,
- insurance_attachments='insurance-old.jpg',
- todo_status='1',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- )
- )
- updater = update_insurance
- attachments = 'insurance-new.jpg'
- db.commit()
- result = updater(
- db,
- sample_id=f'cell_storage:C-historical-full-{handler_state}-{kind}',
- fields={
- 'handleTime': '2026-10-01 08:00:00',
- 'handlerUserId': handler_id,
- 'attachments': attachments,
- 'remark': '只改内容',
- },
- ctx=_ctx(),
- user_info=_user(),
- )
- assert result['handlerUserId'] == handler_id
- assert result['handleTime'] == '2026-10-01 08:00:00'
- assert result['attachments'] == attachments
- assert result['remark'] == '只改内容'
- @pytest.mark.parametrize('kind', ['storage-cert', 'insurance'])
- def test_changed_handle_time_revalidates_historical_ineligible_handler(kind):
- from app.services.market_sample import update_insurance, update_storage_cert
- db = _session()
- protocol, _, _ = _cell_sample(db, f'historical-time-change-{kind}')
- handler_id = f'disabled-time-change-{kind}'
- db.add(
- SysUser(
- id=handler_id,
- user_no=handler_id,
- nick_name='停用历史办理人',
- email='',
- phone_no='',
- status='0',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- )
- )
- if kind == 'storage-cert':
- row = BizProtocolStorageCert(
- id=f'cert-{handler_id}',
- collection_protocol_id=protocol.id,
- handle_time=datetime(2026, 10, 1, 8, 0),
- handler_user_id=handler_id,
- make_status='1',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- )
- updater = update_storage_cert
- else:
- row = BizProtocolFullmoonTodo(
- id=f'todo-{handler_id}',
- collection_protocol_id=protocol.id,
- handle_time=datetime(2026, 10, 1, 8, 0),
- handler_user_id=handler_id,
- todo_status='1',
- tenant_no='tenant-a',
- create_dept_id='',
- del_flag=0,
- )
- updater = update_insurance
- db.add(row)
- db.commit()
- with pytest.raises(ValueError, match='办理人员'):
- updater(
- db,
- sample_id=f'cell_storage:C-historical-time-change-{kind}',
- fields={
- 'handleTime': '2026-10-02 08:00:00',
- 'handlerUserId': handler_id,
- 'remark': '不得写入',
- },
- ctx=_ctx(),
- user_info=_user(),
- )
- db.expire_all()
- assert row.handle_time == datetime(2026, 10, 1, 8, 0)
- def test_first_handle_parent_lock_compiles_to_mysql_for_update():
- from sqlalchemy.dialects import mysql
- from app.services.market_sample import _cell_parent_lock_query
- db = _session()
- query = _cell_parent_lock_query(db, 'cp-lock', _ctx())
- sql = str(
- query.statement.compile(
- dialect=mysql.dialect(), compile_kwargs={'literal_binds': True}
- )
- )
- assert 'FOR UPDATE' in sql
- assert "biz_collection_protocol.id = 'cp-lock'" in sql
- assert "biz_collection_protocol.tenant_no = 'tenant-a'" in sql
- assert 'biz_collection_protocol.del_flag = 0' in sql
- def test_repeated_first_handle_requests_reuse_single_child_row():
- from app.services.market_sample import update_insurance, update_storage_cert
- db = _session()
- _cell_sample(db, 'single-child')
- for updater in (update_storage_cert, update_insurance):
- updater(
- db,
- sample_id='cell_storage:C-single-child',
- fields={'remark': 'first'},
- ctx=_ctx(),
- user_info=_user(),
- )
- updater(
- db,
- sample_id='cell_storage:C-single-child',
- fields={'remark': 'second'},
- ctx=_ctx(),
- user_info=_user(),
- )
- assert db.query(BizProtocolStorageCert).count() == 1
- assert db.query(BizProtocolFullmoonTodo).count() == 1
- assert db.query(BizProtocolStorageCert).one().remark == 'second'
- assert db.query(BizProtocolFullmoonTodo).one().handle_remark == 'second'
- def test_voided_progress_uses_danger_tone():
- from app.services.market_sample import get_sample_detail
- db = _session()
- _, _, sample = _cell_sample(db, 'voided-tone')
- sample.sample_status = 'yzf'
- db.commit()
- detail = get_sample_detail(
- db,
- sample_id='cell_storage:C-voided-tone',
- ctx=_ctx(),
- user_info=_user(),
- )
- assert detail['progress']['collection']['code'] == 'voided'
- assert detail['progress']['collection']['statusTone'] == 'danger'
- assert detail['progress']['collection']['tagType'] == 'danger'
- def test_source_qualified_whitespace_token_returns_uniform_404():
- from app.services.market_sample import get_sample_detail
- db = _session()
- for sample_id in ('cell_storage: ', 'research_donation:\t'):
- with pytest.raises(HTTPException) as exc:
- get_sample_detail(
- db, sample_id=sample_id, ctx=_ctx(), user_info=_user()
- )
- assert exc.value.status_code == 404
- assert exc.value.detail == '数据不存在或无权访问'
- def test_duplicate_active_children_use_same_newest_fields_in_list_and_detail():
- from app.services.market_sample import get_sample_detail, list_samples
- db = _session()
- protocol, collection, _ = _cell_sample(db, 'deterministic')
- collection.create_time = datetime(2026, 10, 1, 8, 0)
- original_party = db.query(BizProtocolPartyCustomer).filter_by(
- collection_protocol_id=protocol.id
- ).one()
- original_party.create_time = datetime(2026, 10, 1, 8, 0)
- db.add_all(
- [
- BizProtocolCollectionInfo(
- id='cc-deterministic-new',
- collection_protocol_id=protocol.id,
- region_id='newest-region',
- sales_user_id='u1',
- tenant_no='tenant-a',
- create_dept_id='',
- create_time=datetime(2026, 10, 2, 8, 0),
- del_flag=0,
- ),
- BizProtocolPartyCustomer(
- id='pc-deterministic-new',
- collection_protocol_id=protocol.id,
- mother_name='最新客户',
- tenant_no='tenant-a',
- create_dept_id='',
- create_time=datetime(2026, 10, 2, 8, 0),
- del_flag=0,
- ),
- ]
- )
- db.commit()
- items, total = list_samples(db, ctx=_ctx(), user_info=_user())
- detail = get_sample_detail(
- db,
- sample_id='cell_storage:C-deterministic',
- ctx=_ctx(),
- user_info=_user(),
- )
- assert total == 1
- assert items[0]['regionId'] == detail['card']['regionId'] == 'newest-region'
- assert items[0]['customerName'] == detail['card']['customerName'] == '最新客户'
- def test_write_reasserts_current_scope_after_parent_lock(monkeypatch):
- from app.services import market_sample
- db = _session()
- _, collection, _ = _cell_sample(db, 'scope-race')
- original_find = market_sample._find_sample
- calls = []
- def move_out_of_scope_after_initial_read(*args, **kwargs):
- calls.append(bool(kwargs.get('for_update')))
- item = original_find(*args, **kwargs)
- if len(calls) == 1:
- collection.sales_user_id = 'u2'
- db.commit()
- return item
- monkeypatch.setattr(
- market_sample, '_find_sample', move_out_of_scope_after_initial_read
- )
- with pytest.raises(HTTPException) as exc:
- market_sample.update_storage_cert(
- db,
- sample_id='cell_storage:C-scope-race',
- fields={'remark': 'must not write'},
- ctx=_ctx(),
- user_info=_user(),
- )
- assert exc.value.status_code == 404
- assert calls == [False, True]
- assert not db.in_transaction()
- assert db.query(BizProtocolStorageCert).count() == 0
- def test_current_sample_and_child_reads_compile_as_mysql_locking_reads():
- from sqlalchemy.dialects import mysql
- from app.services.market_sample import (
- _cell_sample_lookup_query,
- _handle_child_current_query,
- )
- db = _session()
- sample_query = _cell_sample_lookup_query(
- db,
- token='C-locking',
- by_row_id=False,
- ctx=_ctx(),
- user_info=_user(),
- for_update=True,
- )
- cert_query = _handle_child_current_query(
- db, BizProtocolStorageCert, 'cp-locking', _ctx()
- )
- todo_query = _handle_child_current_query(
- db, BizProtocolFullmoonTodo, 'cp-locking', _ctx()
- )
- def mysql_sql(query):
- return str(
- query.statement.compile(
- dialect=mysql.dialect(), compile_kwargs={'literal_binds': True}
- )
- )
- sample_sql = mysql_sql(sample_query)
- cert_sql = mysql_sql(cert_query)
- todo_sql = mysql_sql(todo_query)
- assert 'FOR UPDATE' in sample_sql
- assert 'FOR UPDATE' in cert_sql
- assert 'FOR UPDATE' in todo_sql
- assert 'ORDER BY biz_protocol_storage_cert.update_time DESC' in cert_sql
- assert 'ORDER BY biz_protocol_fullmoon_todo.update_time DESC' in todo_sql
- def test_write_lock_order_is_parent_then_current_sample_then_child(monkeypatch):
- from app.services import market_sample
- db = _session()
- _cell_sample(db, 'lock-order')
- events = []
- original_parent_lock = market_sample._lock_cell_parent
- original_find = market_sample._find_sample
- original_child_query = market_sample._handle_child_current_query
- def record_parent(*args, **kwargs):
- events.append('parent')
- return original_parent_lock(*args, **kwargs)
- def record_find(*args, **kwargs):
- events.append('sample-current' if kwargs.get('for_update') else 'sample-initial')
- return original_find(*args, **kwargs)
- def record_child(*args, **kwargs):
- events.append('child')
- return original_child_query(*args, **kwargs)
- monkeypatch.setattr(market_sample, '_lock_cell_parent', record_parent)
- monkeypatch.setattr(market_sample, '_find_sample', record_find)
- monkeypatch.setattr(
- market_sample, '_handle_child_current_query', record_child
- )
- market_sample.update_storage_cert(
- db,
- sample_id='cell_storage:C-lock-order',
- fields={'remark': 'locked'},
- ctx=_ctx(),
- user_info=_user(),
- )
- assert events == ['sample-initial', 'parent', 'sample-current', 'child']
|