Files
Investidor-V2/outputs/investmoz-parques-integral-QA-20261018/test-integral.py
T
nala cf003c8623
tests / ci (push) Canceled after 0s
versao nrgocios 4
2026-10-18 15:57:06 +02:00

56 lines
7.2 KiB
Python

from http_common import *
from copy import deepcopy
manager=login('manager');outsider=login('outsider');anon=session();zone='0ae14c9a-1adb-4ee7-8094-d5d54d4e75f1';admin='/api/v2/investment/admin/zones/'+zone;public='/api/v2/investment/zones/'+zone
fixture=json.loads((Path('C:/laragon/www/portal-investidor/outputs/investmoz-parques-L1-QA-20261016')/'FIXTURE-L1.json').read_text('utf-8'))
record=call(manager,admin)['record'];initial_public=call(anon,public);initial_L1={g:deepcopy(record['operational_details'].get(g))for g in ['responsibilities','conditions','risks','review']}
phase,plot,occupant,observation,title,chain=[str(uuid.uuid4())for _ in range(6)]
common={'label':'SIMULAÇÃO QA integral','knowledge':'documented','disclosure':True,'source_reference':'Exemplar sintético QA; não descreve o parque real','source_date':'2026-10-18','scope':'Ensaio QA sem validade operacional','private_note':'INTEGRAL-NOTA-PRIVADA'}
data={'schema_version':1,'titles':[{**common,'id':title,'kind':'land_use','identifier':'QA-TITULO-001','description':'Referência documental sintética; não atribui DUAT','label_en':'Synthetic documentary title'}],
'phases':[{**common,'id':phase,'code':'QA-F1','status':'planned','starts_at':'2027-01-01','ends_at':None,'expansion':True}],
'plots':[{**common,'id':plot,'code':'QA-L1','phase_id':phase,'status':'available_declared','area':'12.5','area_unit':'ha','private_note':'INTEGRAL-LOTE-PRIVADO'}],
'occupants':[{**common,'id':occupant,'organization_id':fixture['organization_id'],'plot_id':plot,'phase_id':phase,'status':'construction','valid_from':'2026-01-01','valid_until':None}],
'observations':[{**common,'id':observation,'indicator':'employment','value':25,'unit':'pessoas','period_start':'2025-01-01','period_end':'2025-12-31','population':'Empresas abrangidas no ensaio','methodology':'Contagem sintética, sem extrapolação','phase_id':phase,'plot_id':plot}],
'chains':[{**common,'id':chain,'kind':'unmet_need','label':'SIMULAÇÃO QA — necessidade de fornecedor','disclosure':False,'documentary_name':'INTEGRAL-RELACAO-PRELIMINAR'}]}
def minimal(r):return {k:r[k]for k in ['version_no','code','category','name']}
def prepare(value,r=None,expected=200):
r=r or call(manager,admin)['record'];return call(manager,admin,{**minimal(r),'operational_details':{'integral':value}},method='PUT',expected=expected)
for name,mutate in [
('foreign_phase_refused',lambda x:x['plots'][0].update(phase_id=str(uuid.uuid4()))),
('foreign_plot_refused',lambda x:x['occupants'][0].update(plot_id=str(uuid.uuid4()))),
('duplicate_plot_code_refused',lambda x:x['plots'].append({**x['plots'][0],'id':str(uuid.uuid4())})),
('duplicate_occupant_refused',lambda x:x['occupants'].append({**x['occupants'][0],'id':str(uuid.uuid4())})),
('duplicate_indicator_period_refused',lambda x:x['observations'].append({**x['observations'][0],'id':str(uuid.uuid4())})),
('numeric_indicator_without_unit_refused',lambda x:x['observations'][0].update(unit=None)),
('numeric_indicator_without_period_refused',lambda x:x['observations'][0].update(period_end=None)),
('unknown_not_zero',lambda x:x['observations'][0].update(knowledge='unknown',value=0)),
('availability_without_date_refused',lambda x:x['plots'][0].update(source_date=None)),
('unknown_operational_status_refused',lambda x:x['phases'][0].update(knowledge='unknown')),
('foreign_geometry_refused',lambda x:x['plots'][0].update(geometry_id=str(uuid.uuid4()))),
('foreign_economic_link_refused',lambda x:x['chains'][0].update(zone_link_id=str(uuid.uuid4()))),
('unsupported_field_refused',lambda x:x['plots'][0].update(owner_password='not accepted')),
('backwards_period_refused',lambda x:x['observations'][0].update(period_end='2024-01-01'))]:
bad=deepcopy(data);mutate(bad);prepare(bad,record,422);check(name,True)
check('all_failed_preparations_leave_public_identical',call(anon,public)==initial_public)
saved=prepare(data,record);check('new_groups_private_preparation_same_UUID',saved['id']==zone and all(len(saved['operational_details']['integral'][g])==1 for g in data if g!='schema_version'))
check('preparation_preserves_public_snapshot',call(anon,public)==initial_public)
call(outsider,admin,expected=403);check('unauthorized_private_access_refused',True)
prepare(data,record,409);check('stale_version_conflict_preserves_data',True)
manager=login('manager');recovered=call(manager,admin)['record'];check('new_session_recovers_all_groups_and_private_values',recovered['operational_details']['integral']['plots'][0]['private_note']=='INTEGRAL-LOTE-PRIVADO')
check('L1_groups_unchanged_without_retesting_L1',all(recovered['operational_details'][g]==v for g,v in initial_L1.items()))
call(manager,admin+'/publish',{'version_no':recovered['version_no']});published=call(anon,public);visible=published['record']['operational_details_public']['integral'];raw=json.dumps(published,ensure_ascii=False)
check('express_publication_only_selected_groups',len(visible['plots'])==1 and visible['chains']==[])
check('public_whitelist_no_private_notes_support_or_entity_IDs',all(k not in raw for k in ['INTEGRAL-NOTA-PRIVADA','INTEGRAL-LOTE-PRIVADO','INTEGRAL-RELACAO-PRELIMINAR','private_ciphertext','support_document_ids',fixture['organization_id']]))
check('phase_plot_and_occupant_links_resolve_same_park',visible['plots'][0]['phase_id']==phase and visible['occupants'][0]['plot_id']==plot)
check('period_unit_base_and_value_present',visible['observations'][0]['value']==25 and visible['observations'][0]['unit']=='pessoas' and visible['observations'][0]['period_end']=='2025-12-31' and bool(visible['observations'][0]['population']))
post=call(manager,admin)['record'];check('private_recovery_after_publish_clears_draft',not post['has_private_draft'] and post['operational_details']['integral']['chains'][0]['documentary_name']=='INTEGRAL-RELACAO-PRELIMINAR')
# Existing compatible editor omits the new block: data must survive, not be replaced by empty defaults.
before=deepcopy(post['operational_details']);call(manager,admin,minimal(post),method='PUT');post=call(manager,admin)['record'];check('compatible_omission_preserves_private_integral',post['operational_details']==before)
check('compatible_omission_preserves_public_integral',call(anon,public)==published)
prepare({'remove_ids':{'phases':[phase]}},post,422);check('cannot_remove_referenced_phase',True)
prepare({'remove_ids':{'plots':[plot]}},post,422);check('cannot_remove_referenced_plot',True)
# Update only one item, preserving omitted fields, groups, and IDs; explicit removal of unreferenced item.
updated=prepare({'titles':[{**common,'id':title,'label':'SIMULAÇÃO QA — título revisto'}],'remove_ids':{'chains':[chain]}});new=updated['operational_details']['integral'];check('partial_patch_retains_fields_and_groups',new['titles'][0]['identifier']=='QA-TITULO-001' and len(new['plots'])==1 and new['chains']==[])
check('private_patch_does_not_publish',call(anon,public)==published)
(out/'STATE-QA.json').write_text(json.dumps({'zone_id':zone,'ids':{'phase':phase,'plot':plot,'occupant':occupant,'observation':observation,'title':title,'chain':chain},'private':new,'public':visible,'public_version':published['record']['version_no'],'working_version':updated['version_no'],'L1_unchanged':True},ensure_ascii=False,indent=2),'utf-8')
print(json.dumps({'checks':len(proof['checks']),'QA_only':True,'new_migration':False}))